def main(): _init_influxdb_database() client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) client.on_connect = connected client.on_disconnect = disconnected client.on_message = message client.on_subscribe = subscribe client.connect() client.loop_blocking()
def subscribe(self): def connected(client): print('Connected to Adafruit IO! Listening for {0} changes...'. format(self.feed_id)) client.subscribe(self.feed_id) def subscribe(client, userdata, mid, granted_qos): print('Subscribed to {0} with QoS {1}'.format( self.feed_id, granted_qos[0])) def disconnected(client): print('Disconnected from Adafruit IO!') sys.exit(1) def message(client, feed_id, payload): print('Feed {0} received new value: {1}'.format(feed_id, payload)) # Update device database print('Update database ...') res = json.loads(payload) device = Device(id=str(res["id"]), data=res["data"], name=res["name"], unit=res["unit"]) device.save() print('Data updated') # Device.objects.filter(id=device["id"]).update(data=device["data"], name=device["name"], unit=device["unit"])) # Update weather info # update_weatherinfo() # TODO: Handle value change event ( > 100, < 100) # Create an MQTT client instance. client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Setup the callback functions defined above. client.on_connect = connected client.on_disconnect = disconnected client.on_message = message client.on_subscribe = subscribe # Connect to the Adafruit IO server. client.connect() client.loop_background()
def _loop_process(self): username = os_getenv("ADAFRUIT_IO_USERNAME"); key = os_getenv("ADAFRUIT_IO_KEY"); print(f"AdafruitFeed::_loop_process::username, key: {username}, {key}"); assert(username); assert(key); ssl.SSLContext.verify_mode = ssl.VerifyMode.CERT_OPTIONAL client = MQTTClient(username, key, secure=False); client.on_connect = self._connect; client.on_disconnect = self._disconnect; client.on_message = self._activate; client.on_subscribe = self._null; print("Adafruit::_loop_process"); client.connect(); client.loop_blocking();
def subscribe(self): def connected(client): print('Connected to Adafruit IO! Listening for {0} changes...'. format(self.feed_id)) client.subscribe(self.feed_id) def subscribe(client, userdata, mid, granted_qos): print('Subscribed to {0} with QoS {1}'.format( self.feed_id, granted_qos[0])) def disconnected(client): print('Disconnected from Adafruit IO!') sys.exit(1) def message(client, feed_id, payload): print('Feed {0} received new value: {1}'.format(feed_id, payload)) # Update device database print('Update database ...') # Device.objects.filter(key=feed_id).update(value=payload) # Update weather info # update_weatherinfo() # TODO: Handle value change event ( > 100, < 100) # Create an MQTT client instance. client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Setup the callback functions defined above. client.on_connect = connected client.on_disconnect = disconnected client.on_message = message client.on_subscribe = subscribe # Connect to the Adafruit IO server. client.connect() client.loop_blocking()
def message(client, feed_id, payload): print('Feed {0} received new value: {1}'.format(feed_id, payload)) ser.write((payload + "\n").encode('ascii')) if __name__ == '__main__': ttys = glob.glob('/dev/ttyAC*') ser = serial.Serial(ttys[0], 9600, timeout=1) ser.flush() client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) client.on_connect = connected client.on_disconnect = disconnected client.on_message = message client.on_subscribe = subscribe client.connect() client.loop_background() while True: ser.write(b"READ\n") line = ser.readline().decode('utf-8').rstrip() print(line) datadict = None if (line != ""): try: datadict = eval(line) print(datadict) except SyntaxError: