Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import paho.mqtt.client as mqtt
  4. import json
  5.  
  6. username = "43807467-bbbf-4cd0-9d90-b13d454209d2"
  7. password ="Orh4r16u+dntVFyNRpjpuAyYGE+hZCug906oRVV1VM0="
  8. controllTopic = "xi/blue/v1/93336b3c-49f4-44a5-885f-5cb31504dea7/d/43807467-bbbf-4cd0-9d90-b13d454209d2/device-log"
  9.  
  10. publishTopic = "xi/blue/v1/93336b3c-49f4-44a5-885f-5cb31504dea7/d/7d7afa69-bda6-4219-a9ae-633262a81cfb/device-log"
  11. publishPayload = "{\"default\": \"message message\"}"
  12.  
  13.  
  14. # The callback for when the client receives a CONNACK response from the server.
  15. def on_connect(client, userdata, rc):
  16. print("Connected with result code "+str(rc))
  17. # Subscribing in on_connect() means that if we lose the connection and
  18. # reconnect then subscriptions will be renewed.
  19. #client.subscribe("$SYS/#")
  20. client.subscribe(controllTopic)
  21.  
  22. # The callback for when a PUBLISH message is received from the server.
  23. def on_message(client, userdata, msg):
  24. global publishTopic
  25. global publishPayload
  26.  
  27. print(msg.payload)
  28. parsedMessage = json.loads(msg.payload)
  29.  
  30. if 'channel' in parsedMessage:
  31. publishTopic = json.dumps(parsedMessage['channel']).replace("\"", "")
  32. if 'message-by-device' in parsedMessage:
  33. publishPayload = json.dumps(parsedMessage['message-by-device'])
  34.  
  35. def on_publish(client, userdata, mid):
  36. print("message sent")
  37.  
  38. client = mqtt.Client()
  39.  
  40. client.on_connect = on_connect
  41. client.on_message = on_message
  42. client.on_publish = on_publish
  43.  
  44. client.username_pw_set(username, password)
  45. client.tls_set("gs_root.cer")
  46. client.connect("broker.demo.xively.com", 8883, 60)
  47.  
  48. client.loop_start()
  49.  
  50. # Handling button press on Raspbery PI
  51. GPIO.setmode(GPIO.BCM)
  52. GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  53.  
  54. while True:
  55. input_state = GPIO.input(21)
  56.  
  57. if input_state == False:
  58. print('Button Pressed')
  59. client.publish(publishTopic, publishPayload, qos=0)
  60. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement