Guest User

Untitled

a guest
Apr 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. mqtt_url = "mqtt.googleapis.com"
  2. mqtt_port = 8883
  3. topic = "/devices/sm1/config"
  4.  
  5. def on_connect(client, userdata, flags, response_code):
  6. print("Connected with status: {0}".format(response_code))
  7. client.subscribe(topic, 1)
  8.  
  9. def on_message(client, userdata, msg):
  10. print("Topic: {0} -- Payload: {1}".format(msg.topic, msg.payload))
  11.  
  12. if __name__ == "__main__":
  13. client = mqtt.Client("projects/{}/locations/{}/registries/{}/devices/{}".format(
  14. project_id,
  15. cloud_region,
  16. registry_id,
  17. device_id))
  18.  
  19. client.username_pw_set(username='unused',
  20. password=jwt_maker.create_jwt(project_id,
  21. private_key,
  22. algorithm="RS256"))
  23.  
  24. client.tls_set(root_ca,
  25. certfile = public_crt,
  26. keyfile = private_key,
  27. cert_reqs = ssl.CERT_REQUIRED,
  28. tls_version = ssl.PROTOCOL_TLSv1_2,
  29. ciphers = None)
  30.  
  31.  
  32. client.on_connect = on_connect
  33. client.on_message = on_message
  34.  
  35. print "Connecting to Google IoT Broker..."
  36. client.connect(mqtt_url, mqtt_port, keepalive=60)
  37. client.loop_forever()
Add Comment
Please, Sign In to add comment