Guest User

weight

a guest
Apr 2nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. import sys
  2. import time
  3. import json
  4. import random
  5. import paho.mqtt.client as mqtt
  6.  
  7. host = 'v3tnh2.messaging.internetofthings.ibmcloud.com'
  8. clientid='d:v3tnh2:sensor:weightSensor'
  9. username='use-token-auth'
  10. password='0BI_E7zyM&U_Xx1!gq'
  11. topic = 'iot-2/evt/weight/fmt/json'
  12.  
  13.  
  14. client = mqtt.Client(clientid)
  15. client.username_pw_set(username,password)
  16. client.connect(host, 1883, 60)
  17.  
  18. while True:
  19. try:
  20. t = random.randint(11,90)
  21. client.publish(topic, json.dumps({'weight':t}))
  22. print ("Message Published")
  23. print((str(t) + "kg"))
  24. time.sleep(1)
  25. except IOError:
  26. print("Error")
  27.  
  28. client.loop()
  29. client.disconnect()
Add Comment
Please, Sign In to add comment