38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import paho.mqtt.client as paho
|
|
from firebase_interface import set_firebase_channel
|
|
import json
|
|
|
|
# The callback for when the client receives a CONNACK response from the server.
|
|
def on_connect(client, userdata, flags, rc):
|
|
print("Connected with result code "+str(rc))
|
|
|
|
# Subscribing in on_connect() means that if we lose the connection and
|
|
# reconnect then subscriptions will be renewed.
|
|
client.subscribe("meshify/db/194/#")
|
|
|
|
# The callback for when a PUBLISH message is received from the server.
|
|
def on_message(client, userdata, msg):
|
|
print(msg.topic+" "+str(msg.payload))
|
|
payload = json.loads(msg.payload)
|
|
topic = msg.topic.split("/")
|
|
device_id = topic[3]
|
|
device_type = topic[4].split("_")[0]
|
|
channel_name = topic[5]
|
|
value = payload[0]['value']
|
|
timestamp = payload[0]['timestamp']
|
|
set_firebase_channel(device_id, device_type, channel_name, value, timestamp)
|
|
|
|
if __name__ == '__main__':
|
|
client = paho.Client()
|
|
client.username_pw_set("admin", "columbus")
|
|
client.on_connect = on_connect
|
|
client.on_message = on_message
|
|
|
|
client.connect("mqtt.meshify.com", 1884, 120)
|
|
|
|
# Blocking call that processes network traffic, dispatches callbacks and
|
|
# handles reconnecting.
|
|
# Other loop*() functions are available that give a threaded interface and a
|
|
# manual interface.
|
|
client.loop_forever()
|