Guest User

Untitled

a guest
Jun 1st, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. from paho.mqtt import client as mqtt
  2. import ssl
  3.  
  4. path_to_root_cert = "<local path to the generated testonly-rootca.pem>"
  5. device_cert = "<local path to the generated newdevice-cert.pem>"
  6. device_key = "<local path to the generated newdevice-key.pem>
  7.  
  8. HubName = "iothub.azure-devices.net"
  9. devicename = "device001"
  10.  
  11. def on_connect(client, userdata, flags, rc):
  12. print ("Connected with result code: " + str(rc))
  13. client.subscribe("devices/" + devicename + "/messages/devicebound/#")
  14.  
  15. def on_disconnect(client, userdata, rc):
  16. print ("Disconnected with result code: " + str(rc))
  17.  
  18. def on_message(client, userdata, msg):
  19. print (msg.topic+" "+str(msg.payload))
  20.  
  21. client.publish("devices/" + devicename + "/messages/events/", "{id=1}",qos=1)
  22.  
  23. def on_publish(client, userdata, mid):
  24. print ("Sent message")
  25.  
  26. client = mqtt.Client(client_id=devicename, protocol=mqtt.MQTTv311)
  27. client.on_connect = on_connect
  28.  
  29. client.on_disconnect = on_disconnect
  30. client.on_message = on_message
  31. client.on_publish = on_publish
  32. client.username_pw_set(username=HubName + "/" + devicename, password=None)
  33. client.tls_insecure_set(False)
  34.  
  35. client.tls_set(ca_certs=path_to_root_cert, certfile=device_cert, keyfile=device_key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
  36. client.connect(HubName, port=8883)
  37. client.publish("devices/" + devicename + "/messages/events/", "{id=MQTT Test}", qos=1)
  38. client.loop_forever()
  39.  
  40. SSL_Verification_failed
Add Comment
Please, Sign In to add comment