class I2CSenseHatAdaptor(threading.Thread): rateInSec = DEFAULT_RATE_IN_SEC humidity = 0.0 def __init__(self): super(I2CSenseHatAdaptor, self).__init__() self.config = ConfigUtil(ConfigConst.DEFAULT_CONFIG_FILE_NAME) self.config.loadConfig() print('Configuration data...\n' + str(self.config)) self.initI2CBus() def initI2CBus(self): logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) logging.info("Initializing I2C bus and enabling I2C addresses...") # i2cBus.write_byte_data(accelAddr, 0, 0) # i2cBus.write_byte_data(magAddr, 0, 0) # i2cBus.write_byte_data(pressAddr, 0, 0) # Select average configuration register i2cBus.write_byte_data(humidAddr, 0, 0) def displayHumidityData(self): # Humidity Calibration values #Read data back from 0x30(48), 1 byte val1 = i2cBus.read_byte_data(0x5f, 0x30) H0_rH = val1 /2 # Read data back from 0x31(49), 1 byte val2 = i2cBus.read_byte_data(0x5f, 0x31) H1_rH= val2 /2 # Read data back from 0x36(54), 2 bytes val3 = i2cBus.read_byte_data(0x5f, 0x36) val4 = i2cBus.read_byte_data(0x5f, 0x37) H0_T0_OUT = ((val4 & 0xFF) * 256) + (val3 & 0xFF) # Read data back from 0x3A(58), 2 bytes val5 = i2cBus.read_byte_data(0x5F, 0x3A) val6 = i2cBus.read_byte_data(0x5F, 0x3B) H1_T0_OUT = ((val6 & 0xFF) * 256) + (val5 & 0xFF) # Read data back from 0x28,0x29: humidity msb, humidity lsb val7 = i2cBus.read_byte_data(0x5F, 0x28) val8 = i2cBus.read_byte_data(0x5F, 0x29) H_OUT = ((val8 & 0xFF) * 256) + (val7 & 0xFF) # Read data back from 0x28(40) with command register 0x80(128), 6 bytes data = i2cBus.read_i2c_block_data(0x5f, 0x28 | 0x80, 6) #Convert to human-readable data self.humidity = ((1.0 * H1_rH) - (1.0 * H0_rH)) * ((1.0 * H_OUT) - (1.0 * H0_T0_OUT)) / ((1.0 * H1_T0_OUT) - (1.0 * H0_T0_OUT)) + (1.0 * H0_rH) # logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) d1 = 'Relative Humidity: ' + str(self.humidity) d2 = 'Relative Humidity block data: ' + str(data) logging.info(d1) logging.info(d2) def run(self): while True: # NOTE: you must implement these methods # self.displayAccelerometerData() # self.displayMagnetometerData() # self.displayPressureData() self.displayHumidityData() sleep(self.rateInSec)
class SmtpClientConnector(object): ''' configure the remote server to send alert email; whenever an email is sent, log out the success message ''' def __init__(self): ''' Constructor ''' self.config = ConfigUtil( '../../../config/ConnectedDevicesConfig.props') # self.config = ConfigUtil('/Users/apple/workspace/iot-device/config/ConnectedDevicesConfig.props') self.config.loadConfig() logging.info('Configuration data...\n' + str(self.config)) def publishMessage(self, topic, data): host = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.HOST_KEY) port = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.PORT_KEY) fromAddr = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.FROM_ADDRESS_KEY) toAddr = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.TO_ADDRESS_KEY) authToken = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.USER_AUTH_TOKEN_KEY) # nominalTemp = self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, 'nominalTemp') # if(SensorData.getValue()): # actuate = ActuatorData() msg = MIMEMultipart() msg['From'] = fromAddr msg['To'] = toAddr msg['Subject'] = topic msgBody = str(data) msg.attach(MIMEText(msgBody)) msgText = msg.as_string() smtpServer = smtplib.SMTP_SSL(host, port) smtpServer.ehlo() smtpServer.login(fromAddr, authToken) smtpServer.sendmail(fromAddr, toAddr, msgText) smtpServer.close() logging.info('Sending Successful!\n\n')
class TempSensorAdaptor(object): ''' classdocs ''' rateInSec = 10.0 alertDiff = 10.0 curDegree = 0.0 nominalTemp = 10.0 sense=None ''' Initial all the variable I need ''' def __init__(self, a, r): ''' Constructor ''' self.sense = SenseHat() self.tempActuatorEmulator = TempActuatorEmulator() self.alertDiff = a self.rateInSec = r self.sensorData = SensorData self.connector = SmtpClientConnector() self.config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') self.config.loadConfig() self.nominalTemp = self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, 'nominalTemp') # self.nominalTemp = self.config.getProperty() _thread.start_new_thread(self.run()) ''' Connect to senseHat and obtain the environment temperature. ''' def readTempFromSH(self): self.sense.clear() self.curDegree = self.sense.get_temperature() ''' Store the temperature into SenseData. ''' def addTempToSensorData(self): self.sensorData.addValue(self.sensorData, self.curDegree) now = datetime.datetime.now() print(str(now) + "\t" + str(self.curDegree)) print(str(now) + "\t" + str(self.sensorData.getAvgValue(self.sensorData))) print("\n") ''' Determine whether or not the particular temperature is higher or lower than the average temperature and then alert the user by sending email. ''' def alert(self): if (abs(self.sensorData.getValue(self.sensorData) - self.sensorData.getAvgValue(self.sensorData)) >= self.alertDiff): # Start to send email logging.info('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...') print("Starting sending email...") output = self.sensorData.__str__(self.sensorData) self.connector.publishMessage("Excessive Temp", output) ''' Determine whether or not the new temperature value is higher or lower than the nominal temperature which is set in ConnectedDevicesConfig ''' def adjust(self): if self.sensorData.getValue(self.sensorData) < float(self.nominalTemp) or self.sensorData.getValue(self.sensorData) > float(self.nominalTemp): self.actuatorData = ActuatorData() self.actuatorData.updateData(1, 1, 0, "adjust", self.sensorData.getValue(self.sensorData)) self.tempActuatorEmulator.processMessage(self.actuatorData) ''' Start a new thread to do the task. ''' def run(self): while True: self.readTempFromSH() self.addTempToSensorData() self.alert() self.adjust() sleep(self.rateInSec)
class ConfigUtilTest(unittest.TestCase): """ Placeholder function for setup process. Not needed for this test. """ def setUp(self): self.configUtil = ConfigUtil() self.configUtil.loadConfig() pass """ Placeholder function for tear down process. Not needed for this test. """ def tearDown(self): pass """ Tests retrieval of a boolean property. """ def testGetBooleanProperty(self): # TODO: implement this pass """ Tests retrieval of an integer property. """ def testGetIntegerProperty(self): # TODO: implement this pass """ Tests retrieval of a string property. """ def testGetProperty(self): # TODO: implement this pass """ Tests if a property exists. """ def testHasProperty(self): # TODO: implement this pass """ Tests if a section exists. """ def testHasSection(self): # TODO: implement this pass """ Tests if the configuration is loaded. """ def testIsConfigDataLoaded(self): if self.configUtil.isConfigDataLoaded(): pass else: self.fail("Configuration data not loaded.")
''' Created on Mar 19, 2019 @author: GANESHRAM KANAKSABAI ''' from labs.module07.CoapServerConnector import CoapServerConnector # importing CoapServerConnector from labbenchstudios.common.ConfigUtil import ConfigUtil # importing ConfigUtil from labbenchstudios.common import ConfigConst # importing ConfigConst config = ConfigUtil('../data/ConnectedDevicesConfig.props') config.loadConfig() host = config.getProperty(ConfigConst.COAP_DEVICE_SECTION, ConfigConst.HOST_KEY) port = int( config.getProperty(ConfigConst.COAP_DEVICE_SECTION, ConfigConst.PORT_KEY)) server = CoapServerConnector(host, port, config) server.start() # starting the action of the server