Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. server = "xxxxxxxxx.com"
  3. port = 1883
  4. username = "xx"
  5. password = "xxxxxxxxxxxxxxxxxxxx"
  6. topic = "temp/f/xxxx"
  7.  
  8. client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol="MQTTv31")
  9. client.username_pw_set(username, password)
  10. client.connect(server, port, keepalive=120, bind_address="")
  11.  
  12. def read_temperature(device_id):
  13. "Read float temperature value from 1wire device DS18B20."
  14. with open('/sys/bus/w1/devices/%s/w1_slave' % device_id) as f:
  15. text = f.read().strip()
  16. fragments = text.split()
  17. return float(fragments[-1][2:]) / 1000.
  18. time.sleep(10)
  19. while True:
  20. client.publish(topic, read_temperature('28-00000531cfff'), qos=1, retain=False)
  21. print round(read_temperature('28-00000531cfff'),1)
  22. time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement