Tutorial to build the Azure IoT SDK for C.
AZURE-IOT-SDK-C
Table of Contents
Cloud: Create device instance for connecting by SDK program
Host: Cross-compiling the SDK
Target: Executing the SDK program
Cloud (Azure)
Create device instance for connecting by SDK program
Sign in to Cloud
- Sign in to Azure IoT Cloud. If you do not have an account, please register a new one.
Create IoT Hub
In the left navigation pane, choose Create a resource > Internet of Things > IoT Hub.
create_iot_hub_01
On the Basics tab, fill in the necessary information then choose Next: Size and scale ».
create_iot_hub_02
On the Size and scale tab, fill in the necessary information then choose Review + create.
create_iot_hub_03
On the Review + create tab, review the information you fill before then choose Create.
create_iot_hub_04
Creating the IoT Hub will takes a few minutes.
create_iot_hub_05
Finish creating the IoT Hub.
create_iot_hub_06
Create Device
In the left navigation pane, choose All resources, then search and choose the [IoT Hub you create before].
create_device_01
On the Explorers item, choose IoT devices > Add.
create_device_02
On the Create a device page, fill in the necessary information then choose Save.
create_device_03
Finish creating the Device.
create_device_04
Copy Device Connection String
Device Connection String is the key to build connection between physical and virtual device on cloud
You will need this infomation in the section Build the SDK
In the left navigation pane, choose All resources, then search and choose the [IoT Hub you create before].
copy_device_connection_string_01
On the Explorers item, choose IoT devices > [Device you create before].
copy_device_connection_string_02
Finish copying the Connection String (primary key) of Device.
copy_device_connection_string_03
View Device Messages
You can view the following device messages after section Execute the SDK
On the right side of the search bar above, choose >_ to open Cloud Shell.
view_device_messages_01
- Add extension azure-cli-iot-ext to Azure CLI.
1 user@Azure:~$ az extension add --name azure-cli-iot-ext
- Monitor device telemetry and messages that sent to IoT Hub.
1 user@Azure:~$ az iot hub monitor-events -n Example-IoT-Hub
view_device_messages_02
Host (x86_64-linux)
Cross-compiling the SDK
Setup the Environment
- Setup a network connection to allow host able to access the network.
- Install GNU cross-toolchain provide by MOXA.
- Install following package from package manager.
1 cmake git rsync tree vim
Build the SDK
- Clone repository of MOXA cloud connectivity tool from github.
1 user@Linux:~$ git clone https://github.com/MoxaCorp/ioThinx-4530-azure-iot.git
- Setup dependencies and SDK to output directory.
1 user@Linux:~/azure$ ./setup.sh
- For more setup.sh options.
1 user@Linux:~/azure$ ./setup.sh --help
3 Usage: ./setup.sh [options]
6 -git Git repository of SDK.
7 Default: https://github.com/Azure/azure-iot-sdk-c.git
12 --toolchain GNU cross-toolchain directory.
13 Default: /usr/local/arm-linux-gnueabihf
15 --help Display this help and exit.
19 Specify ./setup.sh -git https://github.com/Azure/azure-iot-sdk-c.git -ver 2018-10-03
20 ./setup.sh --toolchain /usr/local/arm-linux-gnueabihf
- Add the Connection String (primary key) of Device to SDK sample code such as example simplesample_amqp.c. [Copy Connection String]
1 user@Linux:~/azure$ vim output/sdk_azure/serializer/samples/simplesample_amqp/simplesample_amqp.c
1 /*String containing Hostname, Device Id & Device Key in the format: */
2 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
3 static const char* connectionString = "HostName=Example-IoT-Hub.azure-devices.net;DeviceId=Example-Device;SharedAccessKey=hQUkHWv6Gq+1r2yQU5vLzQ86KUjyIquow+XuuDG1DqM=";
- Build the whole SDK.
1 user@Linux:~/azure$ ./build.sh
- All compiled SDK program can be found in the following directory, including example simplesample_amqp.
1 user@Linux:~/azure$ tree output/sdk_azure/cmake/iotsdk_linux/serializer/samples
2 output/sdk_azure/cmake/iotsdk_linux/serializer/samples
3 ├── devicemethod_simplesample
4 │ └── devicemethod_simplesample
5 ├── devicetwin_simplesample
6 │ └── devicetwin_simplesample
8 │ └── remote_monitoring
10 │ └── simplesample_amqp
12 │ └── simplesample_http
14 │ └── simplesample_mqtt
15 └── temp_sensor_anomaly
16 └── temp_sensor_anomaly
- You can also reference to the MOXA sample code with ioThinx I/O library moxa_sample_mqtt.c in the following directory.
1 user@Linux:~/azure$ tree sample
- The compiled MOXA program moxa_sample_mqtt will be generated after the whole SDK is built.
- Note
1 In general, the setup.sh only needs to be executed once.
2 The build.sh needs to be executed after any code change of the SDK.
Target (arm-linux)
Executing the SDK program
Setup the Environment
- Setup a network connection to allow target able to access the network.
- Install following package from package manager.
- Copy compiled SDK program from host to target.
Execute the SDK
- Execute SDK program that cross-compiled by host.
1 moxa@Moxa:~$ sudo ./simplesample_amqp
- View device messages on cloud.
Reference
[1] https://github.com/Azure/azure-iot-sdk-c
[2] https://docs.microsoft.com/en-us/azure/iot-hub