SHARE
TWEET

ultra2

a guest Apr 2nd, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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:ultrasonicSensor2'
  9. username='use-token-auth'
  10. password='5Enhtk(tpyuZ03TgbU'
  11. topic = 'iot-2/evt/distance/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(0,40)
  21.             client.publish(topic, json.dumps({'distance':t}))
  22.             print ("Message Published")
  23.             print((str(t) + "cm"))
  24.             time.sleep(1)
  25.         except IOError:
  26.             print("Error")
  27.            
  28. client.loop()
  29. client.disconnect()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top