Guest User

Untitled

a guest
Jan 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import paho.mqtt.client as mqtt
  4. import json
  5.  
  6. doc_file = "heimatlicher_status.json"
  7.  
  8. def handle_foo(doc, msg):
  9. print("PUT THE DATA FROM THE MQTT MESAGE INTO INTO JSON")
  10.  
  11. def on_connect(client, userdata, flags, rc):
  12. print("Connected with code: " + str(rc))
  13. client.subscribe("#")
  14.  
  15. def on_message(client, userdata, msg):
  16. print(msg.topic + " " + str(msg.payload))
  17. doc = {}
  18. with open(doc_file, 'r') as f:
  19. doc = json.loads(f.read())
  20. f.close()
  21.  
  22. handle_foo(doc, msg)
  23.  
  24. with open(doc_file, 'w') as f:
  25. f.write(json.dumps(doc))
  26. f.close()
  27.  
  28. client = mqtt.Client()
  29. client.on_message = on_message
  30. client.on_connect = on_connect
  31.  
  32. client.username_pw_set(username="servierer", password="")
  33. client.connect("heimat", 1883, 60)
  34. client.subscribe("sensors")
  35.  
  36. client.loop_forever()
Add Comment
Please, Sign In to add comment