Connecting a MQTT Client to IoT Central

Many years ago, my first computer science professor told me something that, to this day, I have not forgotten. I asked him a question (I can’t recall what it was, but it was regarding a feature in C++), and his answer was, “I don’t know, try it.” In this always learning mode that is the life of a technical specialist at Microsoft, I am continually trying things, and this week what no different. When a teammate asked me if you could connect an MQTT client to IoT Central, I thought, “I don’t know, let me try it.” It turns out you can, and here is how I did it.

IoT Central

Azure IoT Central is an IoT app platform that reduces the burden and cost associated with developing, managing, and maintaining enterprise-grade IoT solutions. For more information , check out this overview. The quick-start will help you get started.

Now that you are a bit more familiar with IoT Central, we can add a real device and connected via an MQTT client. In your Azure IoT Central application, use the Device Explorer page to add a real device. You can select any device template. Make a note of the device connection details (Scope IDDevice ID, and Primary key). You will need it in the next step.

Now that we have our credentials, you are going to need a connection string. We are going to use the dps-keygen tool to generate it. dps-keygen is a node based utility. Run npm i -g dps-keygen to install it.

To generate the connection string run dps-keygen -di:<Device ID> -dk:<Primary or Secondary Key> -si:<Scope ID> . Make a note of the connection string.

Azure IoT Central is an IoT app platform that reduces the burden and cost associated with developing, managing, and maintaining enterprise-grade IoT solutions. For more information, check out this overview. The quick-start will help you get started.

Now that you are a bit more familiar with IoT Central, we can add a real device and connected via an MQTT client. In your Azure IoT Central application, use the Device Explorer page to add a real device. You can select any device template. Make a note of the device connection details (Scope IDDevice ID, and Primary key). You will need it in the next step.

Now that we have our credentials, you are going to need a connection string. We are going to use the dps-keygen tool to generate it. DPS-keygen is a node-based utility. Run npm i -g dps-keygen to install it.

To generate the connection string run dps-keygen -di:<Device ID> -dk:<Primary or Secondary Key> -si:<Scope ID> . Make a note of the connection string.

MQTT Support In IoT Hub

IoT Central uses IoT Hub under the covers. It is with this understanding that we are going to configure our MQTT client to connect to IoT Central (IoT Hub).

IoT Hub is not a full featured MQTT broker but it does allow MQTT clients to connect without needing additional components. A good overview of how IoT Hub uses the MQTT protocol is available here.

We are going to use MQTT.fx to simulate a MQTT device. To connect

In the Connection Profile of MQTT.fx use the following values:

  • For the ClientId field, use the deviceId.
  • For the Username field, use {iothubhostname}/{device_id}/?api-version=2018-06-30, where {iothubhostname} is the full CName of the IoT hub.For example, if the name of your IoT hub is contoso.azure-devices.net and if the name of your device is MyDevice01, the full Username field should contain:contoso.azure-devices.net/MyDevice01/?api-version=2018-06-30. The CName is the HostName part of the connection string you generated earlier.
  • For the Password field, use a SAS token. The format of the SAS token is the same as for both the HTTPS and AMQP protocols:SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI}. For more information about how to generate SAS tokens, see the device section of Using IoT Hub security tokens.
  • For the host, use the full CName of the HostName in the connection string.

I used the IoT Hub Node SDK to generate mines. The device key is the SharedAccessKey part of the connection string.

var endpoint ="myhub.azure-devices.net/devices/device1";
var deviceKey ="...";

var token = generateSasToken(endpoint, deviceKey, null, 60);

In the SSL/TLS tab make sure you enable SSL/TLS. The connection will fail without it.

That is all

There you have it. You should now be able to connect your MQTT clients to IoT Central. You can go to your application an verify that data is flowing through. You can also use the IoT Central Explorer.

Leave a Reply

Your email address will not be published. Required fields are marked *