Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Connect to Azure IOT hub using X509 Certificate(Connect to Azure IOT hub using X509 Certificate using MQTT Protocol)
Connect to Azure IOT hub using X509 Certificate [message #1759921] Thu, 20 April 2017 00:17 Go to next message
Saravanan Gajendran is currently offline Saravanan GajendranFriend
Messages: 2
Registered: April 2017
Junior Member
Hi,

I am trying to use Paho MQTT python for connecting to azure iot hub.

When i use SAS token i can connect successfully to Azure IOT hub.

This SAS token works fine for the devices that uses symmetric key.
For the devices that use X509 certificate, We cannot create a SAS token in Azure IOT hub.

Do you have any python paho mqtt samples that i can use to connect to azure IOT hub using x509 certificate.

Thanks
Saravanan G
Re: Connect to Azure IOT hub using X509 Certificate [message #1760318 is a reply to message #1759921] Tue, 25 April 2017 19:04 Go to previous message
Saravanan Gajendran is currently offline Saravanan GajendranFriend
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()
Previous Topic:paho mqtt-ssl client not working
Next Topic:C# MQTT Auto Reconnect
Goto Forum:
  


Current Time: Sat Apr 20 01:55:33 GMT 2024

Powered by FUDForum. Page generated in 0.04052 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top