johnstaveley
johnstaveley

Reputation: 1499

Connect to Azure IoT Hub using MQTT

I am trying to connect to an Azure IoT Hub directly using MQTT using this method:

https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly-as-a-device

I am using MQTT explorer for windows: https://mqtt-explorer.com/ I enter the following details. My ioTHub is called IoTHub and my device is called device, so as per the documentation:

Protocol: mqtt:// Host: iothub.azure-devices.net/?api-version=2018-06-3430 Port: 8883 Validate Cert: Off Encryption: Off Username: iothub.azure-devices.net/device/?api-version=2018-06-30 Password: SharedAccessSignature sr=iothub.azure-devices.net&sig=XXXXXXXXXXXXXXXXXXXredactedXXXXXXXXXXXXXXXXXXXX&se=1642603375&skn=iothubowner

No ports are blocked from my location. The connection fails and says disconnected from server. If I try with TLS encryption it says not authorised.

How can I access the IoTHub using MQTT?

Upvotes: 2

Views: 4287

Answers (1)

Matthijs van der Veer
Matthijs van der Veer

Reputation: 4085

You're on the right track! One small detail is missing, in the docs

Quote:

For the ClientId field, use the deviceId.

The client ID can be set using the advanced settings, as seen in the two screenshots below:

advanced settings client ID field

As for TLS Encryption, it's required. Switch it on and you should be able to connect.

Edit: As you mentioned in the comment, the connection string was also wrong. You need the Shared Access Signature for a device. An easy way of generating one is by using the az cli. When you run the command below, you provide the connection string of your device (you can get it from the portal) and it will create the Shared Access Signature for you.

az iot hub generate-sas-token --connection-string "HostName=iot-Hub.azure-devices.net;DeviceId=deviceId;SharedAccessKey=foo"

Upvotes: 6

Related Questions