Skip to main content

Connection test to Azure IoT Hub

Testing connection in development environment is better before directly trying to connect and send data from PLC/Motion Controller. To test connection few tools has to be installed in the development environment system.

Requirements​

Install VS Code​

Install Visual Studio Code by visiting official website.

VS Code can also be installed with Winget, Windows Package Manager

winget install -e --id Microsoft.VisualStudioCode

Install extensions​

To install required extension, click on Extension icon on Activity Bar.

VS Code Extensions on Activity Bar

Search for Azure IoT Hub and Install the Azure IoT Hub extension. This extensions depends on Azure Account extension. So, it'll be installed along with this extension.

Azure Iot Hub Vs Code Extension

Login to Azure account​

To login to azure account, Click on Azure icon on the Activity Tab and click on Sign in to Azure.

Sing in to azure

A browser window will open and prompts to login the azure account. After the authentication completed, browser windows can be closed and login process will be completed. Close the VS Code and reopen it.

VS Code Azure IoT Hub windows

Previously created IoT Hub, Devices and Module Identities should be present here. Please keep username, SAS token generated previously and others in the hand.

Testing Connection​

To test device login, connection test and publish data, MQTT client is required. One such client is MQTTX. Download it from their GitHub Release page here.

Create a new connection in MQTTX

MQTTX New connection

Fill the details from IoT Hub, and Enable SSL/TLS. Scroll down to Last WILL and Testment section and add sample json body to send.

Name: Testing # Any name
Client ID: plc-001/plc-001-temp-sensor-001 # in format {device-id}/{module-id}
Host: Si-Iot-Hub--001.azure-devices.net # <hubname>.azure-devices.net and `mqtts`
Port: 8883 # 8883 for mqtts
Username: <hubname>.azure-devices.net/{device_id}/{module_id}/?api-version=2021-04-12
Password: # SAS token
SSL/TLS: Enable
SSL Secure: Enable
Certificate: CS Signed Server
MQTT Version: 3.1.1 # Current stable version on azure is 3.1.1 & v5 is in preview

Connection session timing can be adjusted based on the user preferences.

MQTTX New connection WILL Topic

Click on connect on right side top. Once connection established, Connected message will be shown.

MQTTX Connected

If the connection the device module established, color of the modules changes from blue to green.

Azure IoT Module Connected

Monitor Messages​

To monitor the messages published by devices, Right click on the device and select Start Monitoring Built-in Event Endpoint.

Monitor Messages

Output windows with Azure IoT Hub will open and displays the data from the IoT Hub and any and all published data.

To push data, Edit the payload and click on send icon.

MQTTX publish data

Published data will be displayed in output window of the VS Code

Monitor Published Messages

Messages published by different modules will also be displayed in the Output windows.

Monitor Published Messages all modules