def main(): configureLogger("WSO2IOT_RPiStats") UtilsThread() SubscribeToMQTTQueue( ) # connects and subscribes to an MQTT Queue that receives MQTT commands from the server mindwaveDataPointReader = MindwaveDataPointReader() mindwaveDataPointReader.start() if (mindwaveDataPointReader.isConnected()): counter = 0 signalInfo = [0] * 11 while (True): try: dataPoint = mindwaveDataPointReader.readNextDataPoint() if (not dataPoint.__class__ is RawDataPoint): if (dataPoint.__class__ is EEGPowersDataPoint): signalInfo[2] = float(str(dataPoint.delta)), signalInfo[3] = dataPoint.theta, signalInfo[4] = dataPoint.lowAlpha, signalInfo[5] = dataPoint.highAlpha, signalInfo[6] = dataPoint.lowBeta, signalInfo[7] = dataPoint.highBeta, signalInfo[8] = dataPoint.lowGamma, signalInfo[9] = dataPoint.midGamma if (dataPoint.__class__ is AttentionDataPoint): signalInfo[0] = float(str(dataPoint).split(":")[1]) # TODO send command to virtual fire alarm after checking AttentionDataPointor or signalInfo[0] try: if (dataPoint.__class__ is PoorSignalLevelDataPoint): signalInfo[1] = float(str(dataPoint).split(":")[1]) except ValueError: print "200 - NO CONTACT TO SKIN" signalInfo[1] = 200 pass if (dataPoint.__class__ is MeditationDataPoint): # TODO send command to virtual fire alarm after checking MeditationDataPoint or signalInfo[10] signalInfo[10] = float(str(dataPoint).split(":")[1]) counter = counter + 1 if (counter == 4): counter = 0 currentTime = calendar.timegm(time.gmtime()) PUSH_DATA_BRAIN_WAVE_INFO = iotUtils.BRAIN_WAVE_INFO.format( currentTime, signalInfo[0], signalInfo[1], signalInfo[10], float(signalInfo[2][0]), float(signalInfo[3][0]), float(signalInfo[4][0]), float(signalInfo[5][0]), float(signalInfo[6][0]), float(signalInfo[7][0]), float(signalInfo[8][0]), float(signalInfo[8][0])) print '~~~~~~~~~~~~~~~~~~~~~~~~ Publishing Devic-eData ~~~~~~~~~~~~~~~~~~~~~~~~~' print('PUBLISHED DATA: ' + PUSH_DATA_BRAIN_WAVE_INFO) print('PUBLISHED TOPIC: ' + mqttConnector.TOPIC_TO_PUBLISH_BRAIN_WAVE_INFO) mqttConnector.publish(PUSH_DATA_BRAIN_WAVE_INFO) except AssertionError: print('An error occurred while reading brain signal') pass else: print( textwrap.dedent("""\ Exiting because the program could not connect to the Mindwave Mobile device.""").replace("\n", " "))
def run(self): # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). while True: try: i = 0 for basic in self.basicDataStr: if(i > 10): break i = i+1 currentTime = calendar.timegm(time.gmtime()) longitude = basic["longitude"] latitude = basic["latitude"] lampId = basic["lampId"] sensorDataDict = getSensorData(currentTime, longitude, latitude, 1) sensorDataDict["lampStatus"] = mqttConnector.lampStatusDict[lampId] #print("mqttConnector.lampStatusDict[lampId]", mqttConnector.lampStatusDict[lampId]) #print("sensorDataDict lampStatus", sensorDataDict["lampStatus"]) sensorDataStr = json.dumps(sensorDataDict) strTopic = self.servername + '/' + DEVICETYPE + '/' + lampId + '/events'; #strTopic = self.servername + '/' + DEVICETYPE + '/' + '01010100010001' + '/events'; #strMsg = '{"lampStatus":false, "WindSpeed(km/h)":10,"PM10(ug/m3)":20,"PM2.5(ug/m3)":487,"CO(mg/m3)":65,"Humidity(%)":15,"NO2(ug/m3)":48,"O3(ug/m3)":37,"rainfall(mm)":0,"SO2(ug/m3)":48,"Temp(°C)":17,"lux":487,"noise":57}' #print("topic:", strTopic) #print("payload:", sensorDataStr) mqttConnector.publish(strTopic, sensorDataStr) time.sleep(1) #print 'RASPBERRY_STATS: Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity) time.sleep(5) except Exception, e: print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print "RASPBERRY_STATS: Exception in SensorDataReaderThread: Could not successfully read SensorData" print ("RASPBERRY_STATS: " + str(e)) pass time.sleep(self.interval)
def connectAndPushData(): currentTime = calendar.timegm(time.gmtime()) rPiTemperature = iotUtils.LAST_TEMP # Push the last read temperature value PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, rPiTemperature) print '~~~~~~~~~~~~~~~~~~~~~~~~ Publishing Device-Data ~~~~~~~~~~~~~~~~~~~~~~~~~' print ('PUBLISHED DATA: ' + PUSH_DATA) print ('PUBLISHED TOPIC: ' + mqttConnector.TOPIC_TO_PUBLISH) mqttConnector.publish(PUSH_DATA)
def connectAndPushData(): currentTime = calendar.timegm(time.gmtime()) rPiTemperature = iotUtils.LAST_TEMP # Push the last read temperature value PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, rPiTemperature) print '~~~~~~~~~~~~~~~~~~~~~~~~ Publishing Device-Data ~~~~~~~~~~~~~~~~~~~~~~~~~' print('PUBLISHED DATA: ' + PUSH_DATA) print('PUBLISHED TOPIC: ' + mqttConnector.TOPIC_TO_PUBLISH) mqttConnector.publish(PUSH_DATA)
def connectAndPushData(): currentTime = calendar.timegm(time.gmtime()) # rPiTemperature = iotUtils.LAST_TEMP # Push the last read temperature value # PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, rPiTemperature) agentSettings.cpuusage = float(agentSettings.cpuusage) agentSettings.batterypercentage = float(agentSettings.batterypercentage) agentSettings.batterypluggedin = 0 if(agentSettings.batterypluggedin): agentSettings.batterypluggedin=1 agentSettings.memoryusage=float(agentSettings.memoryusage) agentSettings.diskusage=float(agentSettings.diskusage) agentSettings.diskreads=float(agentSettings.diskreads) agentSettings.diskwrites=float(agentSettings.diskwrites) agentSettings.diskreadcount=float(agentSettings.diskreadcount) agentSettings.diskwritecount=float(agentSettings.diskwritecount) agentSettings.bytessent=float(agentSettings.bytessent) agentSettings.bytesrecv=float(agentSettings.bytesrecv) if(agentSettings.firstDataNotPushed): agentSettings.currentBytesSent = agentSettings.bytessent agentSettings.bytessent=0 agentSettings.currentBytesRecv = agentSettings.bytesrecv agentSettings.bytesrecv=0 agentSettings.currentDiskWrite = agentSettings.diskwrites agentSettings.diskwrites=0 agentSettings.currentDiskRead = agentSettings.diskreads agentSettings.diskreads =0 agentSettings.firstDataNotPushed = False else : agentSettings.bytessent = agentSettings.bytessent - agentSettings.currentBytesSent agentSettings.currentBytesSent = agentSettings.currentBytesSent + agentSettings.bytessent agentSettings.bytesrecv = agentSettings.bytesrecv - agentSettings.currentBytesRecv agentSettings.currentBytesRecv = agentSettings.currentBytesRecv + agentSettings.bytesrecv agentSettings.diskreads = agentSettings.diskreads - agentSettings.currentDiskRead agentSettings.currentDiskRead = agentSettings.currentDiskRead - agentSettings.diskreads agentSettings.diskwrites = agentSettings.diskwrites - agentSettings.currentDiskWrite agentSettings.currentDiskWrite = agentSettings.currentDiskWrite + agentSettings.diskwrites PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, agentSettings.cpuusage,agentSettings.batterypercentage,agentSettings.batterypluggedin,agentSettings.memoryusage,agentSettings.diskusage,agentSettings.diskreads,agentSettings.diskwrites,agentSettings.diskreadcount,agentSettings.diskwritecount,agentSettings.bytessent,agentSettings.bytesrecv) print '~~~~~~~~~~~~~~~~~~~~~~~~ Publishing Device-Data ~~~~~~~~~~~~~~~~~~~~~~~~~' print ('PUBLISHED DATA: ' + PUSH_DATA) print ('PUBLISHED TOPIC: ' + mqttConnector.TOPIC_TO_PUBLISH) # error in publishing data mqttConnector.publish(PUSH_DATA)
def connectAndPushData(): currentTime = calendar.timegm(time.gmtime()) # rPiTemperature = iotUtils.LAST_TEMP # Push the last read temperature value # PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, rPiTemperature) battery_level = float(iotUtils.BATTERY_LEVEL) battery_status = float(iotUtils.BATTERY_STATUS) cpuusage = float(iotUtils.CPU_USAGE) PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, battery_level, battery_status, cpuusage) print ('~~~~~~~~~~~~~~~~~~~~~~~~ Publishing Device-Data ~~~~~~~~~~~~~~~~~~~~~~~~~') print ('PUBLISHED DATA: ' + PUSH_DATA) print ('PUBLISHED TOPIC: ' + mqttConnector.TOPIC_TO_PUBLISH) # error in publishing data mqttConnector.publish(PUSH_DATA)
def connectAndPushData(): currentTime = calendar.timegm(time.gmtime()) rPiTemperature = iotUtils.LAST_TEMP # Push the last read temperature value PUSH_DATA = iotUtils.DEVICE_INFO.format(currentTime, rPiTemperature) mqttConnector.publish(PUSH_DATA)