|
Re: Connect to Azure IOT hub using X509 Certificate [message #1760318 is a reply to message #1759921] |
Tue, 25 April 2017 19:04 |
Saravanan Gajendran Messages: 2 Registered: April 2017 |
Junior Member |
|
|
Found out the way to connect using x509 certificate.
And below is the process.
1. Register a device to IOT hub using the client certificate thumbprint.
On the device side, Below is the code
Insted of passing sas token, use the certificate and key at tls_set()
from paho.mqtt import client as mqtt # be sure to install package paho-mqtt
import ssl
BaltimoreCyberTrustRootCER = "D:\\device\\samples\\certificate.pem\\d4de20d05e66fc53fe1a50882c78db2852cae474.cer" #Azure Certificate available under "azure-iot-sdk-c-master\certs" folder of IOT Hub c client
deviceCert = "D:\\device\\samples\\certificate.pem"
deviceCertKey = "D:\\device\\samples\\key.pem"
HubName = "iothub.azure-devices.net"
devicename = "device001"
def on_connect(client, userdata, flags, rc):
print ("Connected with result code: " + str(rc))
client.subscribe("devices/" + devicename + "/messages/devicebound/#")
def on_disconnect(client, userdata, rc):
print ("Disconnected with result code: " + str(rc))
def on_message(client, userdata, msg):
print (msg.topic+" "+str(msg.payload))
client.publish("devices/" + devicename + "/messages/events/", "{id=1}", qos=1)
def on_publish(client, userdata, mid):
print ("Sent message")
client = mqtt.Client(client_id=devicename, protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.on_publish = on_publish
client.username_pw_set(username=HubName + "/" + devicename, password=None)
client.tls_insecure_set(False)
client.tls_set(ca_certs=BaltimoreCyberTrustRootCER, certfile=deviceCert, keyfile=deviceCertKey, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
client.connect(HubName, port=8883)
client.publish("devices/" + devicename + "/messages/events/", "{id=SG MQTT Test}", qos=1)
client.loop_forever()
|
|
|
Powered by
FUDForum. Page generated in 0.03703 seconds