def pubSensorReadingsToGoolgeCloud(sensors, _log): # Droneponics Start _log.info("pubSensorReadingsToGoolgeCloud") parser = ConfigParser() parser.read("/home/pi/droneponics/config/Google/" + drone.gethostname() + ".ini") _log.info("parser.read") device_id = parser.get('Google', 'device_id') _log.info("device_id = {}".format(device_id)) _MQTT_TOPIC = '/devices/{}/events/sensorReading'.format(device_id) payload = "" if (sensors[1].name == "EC"): _log.info("build payload ec") # payload = '{{ "deviceTime": "{}", "deviceMAC": "{}", "temperature": "{}", "pH": "{}", "conductivity": "{}", "totalDissolvedSolids": "{}", "salinity": "{}", "specificGravity": "{}"}}'.format(int(time.time()), drone.get_mac(), buildPayloadField(sensors[0].value), buildPayloadField(sensors[2].value), buildPayloadField(sensors[1].value), buildPayloadField(sensors[1].value2), buildPayloadField(sensors[1].value3), buildPayloadField(sensors[1].value4) ) payload = '{{ "deviceTime": "{}", "deviceMAC": "{}", "temperature": "{}", "pH": "{}", "conductivity": "{}", "totalDissolvedSolids": "{}", "salinity": "{}", "specificGravity": "{}"}}'.format( int(time.time()), drone.get_mac(), sensors[0].value, sensors[2].value, sensors[1].value, sensors[1].value2, sensors[1].value3, sensors[1].value4) elif (sensors[1].name == "Dissolved Oxygen"): payload = '{{ "deviceTime": "{}", "deviceMAC": "{}", "temperature": "{}", "dissolvedOxygen": "{}", "saturation": "{}", "oxidationReductionPotential": "{}"}}'.format( int(time.time()), drone.get_mac(), buildPayloadField(sensors[0].value), buildPayloadField(sensors[1].value), buildPayloadField(sensors[1].value2), buildPayloadField(sensors[2].value)) _log.info(payload) return pubGoolgeCloud(_MQTT_TOPIC, payload)
def getWithSub(self): payloadString = '{{ "deviceTime": "{}", "deviceMAC": "{}",'.format( int(time.time()), drone.get_mac()) for pItem in self.objects: payloadString = payloadString + pItem.getSub() payloadString = payloadString[:-1] + '}' return payloadString
def buildSensorPayload(sensors, _log): _log("buildSensorPayload") _log(sensors[1].name) if (sensors[1].name == "EC"): payload = '{{ "deviceTime": "{}", "deviceMAC": "{}", "temperature": "{}", "pH": "{}", "conductivity": "{}", "totalDissolvedSolids": "{}", "salinity": "{}", "specificGravity": "{}"}}'.format( int(time.time()), drone.get_mac(), buildPayloadField(sensors[0].value), buildPayloadField(sensors[2].value), buildPayloadField(sensors[1].value), buildPayloadField(sensors[1].value2), buildPayloadField(sensors[1].value3), buildPayloadField(sensors[1].value4)) elif (sensors[1].name == "Dissolved Oxygen"): payload = '{{ "deviceTime": "{}", "deviceMAC": "{}", "temperature": "{}", "dissolvedOxygen": "{}", "saturation": "{}", "oxidationReductionPotential": "{}"}}'.format( int(time.time()), drone.get_mac(), buildPayloadField(sensors[0].value), buildPayloadField(sensors[1].value), buildPayloadField(sensors[1].value2), buildPayloadField(sensors[2].value)) return payload
def pubDeviceBootToGoolgeCloud(): # Droneponics Start parser = ConfigParser() parser.read("/home/pi/droneponics/config/Google/" + drone.gethostname() + ".ini") device_id = parser.get('Google', 'device_id') _MQTT_TOPIC = '/devices/{}/events/deviceBoot'.format(device_id) payload = '{{ "bootTime": "{}", "deviceMAC": "{}", "deviceName": "{}", "deviceIP": "{}"}}'.format( int(time.time()), drone.get_mac(), drone.gethostname(), drone.get_ip()) return pubGoolgeCloud(_MQTT_TOPIC, payload)
def on_connect(unusued_client, unused_userdata, unused_flags, rc): print('on_connect', error_str(rc)) def on_publish(unused_client, unused_userdata, unused_mid): print('on_publish') client.on_connect = on_connect client.on_publish = on_publish client.tls_set(ca_certs=root_cert_filepath) # Replace this with 3rd party cert if that was used when creating registry client.connect('mqtt.googleapis.com', 8883) client.loop_start() payload = '' if(str(device_sensor_type) == "PH"): payload = '{{ "ts": {}, "devicemac": {}, "temperature": {}, "conductivity": {}, "totaldissolvedsolids": {}, "salinity": {}, "specificgravity": {}, "pH": {}}}'.format(int(time.time()), drone.get_mac(), 25, 1250, 860, 0.81, 1.001, 5.5 ) else: payload = '{{ "ts": {}, "devicemac": {}, "temperature": {}, "dissolvedoxygen": {}, "saturation": {}, "oxidationreductionpotential": {}}}'.format(int(time.time()), drone.get_mac(), 25, 0.07, .1, 327.2) _log.info("{}\n".format(payload)) # Uncomment following line when ready to publish client.publish(_MQTT_TOPIC, payload, qos=1) # Droneponics End client.loop_stop()
def payload(payload): payload["bootTime"] = int(time.time()) payload["deviceMAC"] = drone.get_mac() payload["deviceIP"] = drone.get_ip() payload["gethostname"] = drone.gethostname()
def getBootPayload(__payload): __payload["bootTime"] = int(time.time()) __payload["deviceMAC"] = drone.get_mac() __payload["deviceIP"] = drone.get_ip() __payload["gethostname"] = drone.gethostname()
import sys import os sys.path.append('/home/pi/droneponics') import drone macAddress = drone.get_mac() print(macAddress)
parser = ConfigParser() parser.read("/home/pi/droneponics/config/Google/"+drone.gethostname()+".ini") # tune console logging _log = logging.getLogger('GoogleLog') logFormatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s") consoleHandler = logging.StreamHandler() consoleHandler.setFormatter(logFormatter) _log.addHandler(consoleHandler) _log.setLevel(parser.get('logging', 'logLevel', fallback=logging.DEBUG)) _log.critical("critical") _log.error("error") _log.warning("warning") _log.info("info") _log.debug("debug") _log.info("ConfigParser path = /home/pi/droneponics/config/Google/"+drone.gethostname()+".ini") _log.critical('{{ "ts": "{}", "deviceMAC": "{}", "deviceName": "{}", "deviceIP": "{}"}}'.format(int(time.time()), drone.get_mac(), drone.gethostname(), drone.get_ip())) payload = drone.dronePayload(_log) payload.add("deviceName", drone.gethostname()) payload.add("deviceIP", drone.get_ip()) payload.add("TestNone", None) _log.critical(payload.get())
client.on_publish = on_publish client.on_message = on_message client.tls_set( ca_certs=root_cert_filepath ) # Replace this with 3rd party cert if that was used when creating registry client.connect('mqtt.googleapis.com', 8883) client.subscribe(_MQTT_CONFIG_TOPIC, qos=1) client.subscribe(_MQTT_COMMANDS_TOPIC, qos=1) client.loop_start() payload = '' if (str(device_sensor_type) == "PH"): payload = '{{ "ts": {}, "devicemac": {}, "temperature": {}, "conductivity": {}, "totaldissolvedsolids": {}, "salinity": {}, "specificgravity": {}, "pH": {}}}'.format( int(time.time()), drone.get_mac(), 25, 1250, 860, 0.81, 1.001, 5.5) else: payload = '{{ "ts": {}, "devicemac": {}, "temperature": {}, "dissolvedoxygen": {}, "saturation": {}, "oxidationreductionpotential": {}}}'.format( int(time.time()), drone.get_mac(), 25, 0.07, .1, 327.2) _log.info("{}\n".format(payload)) # Uncomment following line when ready to publish to IoT Core client.publish(_MQTT_TELEMETRY_TOPIC, payload, qos=1) print("{}\n".format(payload)) # This is sleeping for an arbitrarily long time because it has to be connected # in order to receive the command/config messages. Well, the config messages would # come through next time the device connected, but that's not as interesting # from a starting point