Connect IGT-30 Series to Azure IoT Hub via C SDK

As an industrial IoT Gateway, IGT-30 series has pre-tested with Microsoft Azure IoT Hub and got certified. Though there have get started documents on Microsoft Azure IoT Device Catalog, I still maintain a version with full step-by-step instructions here.

Create an IoT Hub on Azure

This is definitely the first step if you’d like to connect IGT-30 series to Microsoft Azure. However, in my experience, this is the most difficult step though Microsoft already provide documentation for it. What you need at the beginning is only to create an IoT Hub as describe at the link.

Create a Device in the IoT Hub on Azure

An IoT Hub managers many devices. However, a device cannot connect to a hub unless it has an entry in the identity registry. Here’s what Microsoft said to create a device. You can have your own “Device ID”, such as “IGT30D-00001” because you might have many IGT series devices in the future, but if this is your first time and first device “myDeviceId” is also fine. The most important thing is to copy the Primary Connection String to use later.

Connect to IGT Series

There are at lease two ways to connect to IGT series with the original OS image. It’s suggested to connect to IGT series via the console port as described in the previous post. If your laptop PC has only USB without RS-232, I found a convenient USB to RS-232 cable. I personally use USB-RS232-WE-1800-BT_0.0, for you don’t need the 5V or 3.3V output from the cable. It’s available from many on-line shop, such as  mouser, digikey and rs-components.

Download the Azure IoT SDK for C

Besides C, Microsoft provides Azure IoT SDK for several programming languages, such Python, Node.js and so on. Though this article focuses on C SDK, the concept is about the same. Azure IoT SDK is available on Github. To get the SDK and build it, some additional software packages are required. Use the following command to download SDK and other necessary packages.

cd
sudo apt-get update
sudo apt-get install -y curl uuid-dev libcurl4-openssl-dev build-essential cmake git libssl1.0-dev
#(sudo apt-get install -y curl uuid-dev:armhf libcurl4-openssl-dev:armhf build-essential cmake git libssl-dev:armhf ## this is for cross compiler and for my own reference.)
git clone --recursive https://github.com/Azure/azure-iot-sdk-c.git

Upon completion, verify that you now have a copy of the source code under the directory ~/azure-iot-sdk-c.

Build the Azure IoT SDK

To successfully connect to Azure IoT Hub, you’ll at least modify the samples in the SDK with the correct Connection String obtained after “Create a Device in the IoT Hub on Azure”, but let build it first with the following command

cd
sudo ./azure-iot-sdk-c/build_all/linux/build.sh | tee LogFile.txt
#(sudo ./azure-iot-sdk-c/build_all/linux/build.sh --toolchain-file IGT-toolchain-file | tee LogFile.txt ## this is for cross compiler and for my own reference. )

You can see similar message if there’s no error during this build.

...(omitted)
[100%] Building C object samples/solutions/remote_monitoring_client/CMakeFiles/remote_monitoring_client.dir/remote_monitoring.c.o
[100%] Linking C executable remote_monitoring_client
[100%] Built target remote_monitoring_client
completed run...
Thu Dec 5 10:45:02 UTC 2019
Test project /home/neousys/azure-iot-sdk-c/cmake/iotsdk_linux
/home/neousys
neousys@igt30:~$

If you don’t see this, check the log file LogFile.txt to find out what was wrong. As prompted by the log, all the built sample locates at ~/azure-iot-sdk-c/cmake/iotsdk_linux.

Modify the Sample

The sample code locates somewhere different from the built samples. Use a text editor to open it.

nano ~/azure-iot-sdk-c/iothub_client/samples/iothub_ll_telemetry_sample/iothub_ll_telemetry_sample.c

There might be two things to modify for a correct connection to you IoT Hub. The first one is Connection String obtained after “Create a Device in the IoT Hub on Azure”, and the other is the protocol, such as MQTT, AMQP and HTTP. IGT-30 supports MQTT, AMQP and HTTP protocols, and the Azure IoT samples have already implement all these. Therefore we can ignore the protocol setting at this moment. What you need is to find the sample you’d like to test, modify the connection string and build. The placeholder of the connection string in the sample code will look like

static const char* connectionString = "[device connection string]";

Find it and replace the string between quotation marks.

Build Solely the Sample

If the whole SDK has been built successfully in the previous step, it’s not necessary to re-build the whole SDK again. Switching to the fold of the built sample and only build the sample will save you a lot of time.

cd ~/azure-iot-sdk-c/cmake/iotsdk_linux/iothub_client/samples/iothub_ll_telemetry_sample
sudo make
./iothub_ll_telemetry_sample

Soon the building process will be done. If you execute the sample, you might see something like

Creating IoTHub Device handle
Sending message 1 to IoTHub
Sending message 2 to IoTHub
Sending message 3 to IoTHub
Sending message 4 to IoTHub
Sending message 5 to IoTHub
-> 11:34:06 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: azureIGT.azure-devices.net/IGT30-00021/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.3.6%20(native%3b%20Linux%3b%20armv7l) | PWD: XXXX | CLEAN: 0
<- 11:34:06 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
The device client is connected to iothub

......(omitted)

<- 11:34:07 PUBACK | PACKET_ID: 5
Confirmation callback received for message 4 with result IOTHUB_CLIENT_CONFIRMATION_OK
<- 11:34:07 PUBACK | PACKET_ID: 6
Confirmation callback received for message 5 with result IOTHUB_CLIENT_CONFIRMATION_OK
-> 11:34:07 DISCONNECT
Press any key to continue

Congratulation. The test messages have been sent to Azure IoT Hub.

 

Comments are closed.