- Main
- Tutorials
- Library
- Samples

How to build Azure IoT SDK for C

Tutorial to build the Azure IoT SDK for C.

# ioThinx-4530-azure-iot

> 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

1. In the left navigation pane, choose Create a resource > Internet of Things > IoT Hub.

   create_iot_hub_01

2. On the Basics tab, fill in the necessary information then choose Next: Size and scale ».

   create_iot_hub_02

3. On the Size and scale tab, fill in the necessary information then choose Review + create.

   create_iot_hub_03

4. On the Review + create tab, review the information you fill before then choose Create.

   create_iot_hub_04

5. Creating the IoT Hub will takes a few minutes.

   create_iot_hub_05

6. Finish creating the IoT Hub.

   create_iot_hub_06

### Create Device

1. In the left navigation pane, choose All resources, then search and choose the [IoT Hub you create before].

   create_device_01

2. On the Explorers item, choose IoT devices > Add.

   create_device_02

3. On the Create a device page, fill in the necessary information then choose Save.

   create_device_03

4. 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

1. In the left navigation pane, choose All resources, then search and choose the [IoT Hub you create before].

   copy_device_connection_string_01

2. On the Explorers item, choose IoT devices > [Device you create before].

   copy_device_connection_string_02

3. 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

1. On the right side of the search bar above, choose >_ to open Cloud Shell.

   view_device_messages_01
2. Add extension azure-cli-iot-ext to Azure CLI.
   1 user@Azure:~$ az extension add --name azure-cli-iot-ext
3. 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

1. Setup a network connection to allow host able to access the network.
2. Install GNU cross-toolchain provide by MOXA.
3. Install following package from package manager.
   1 cmake git rsync tree vim

### Build the SDK

1. Clone repository of MOXA cloud connectivity tool from github.
   1 user@Linux:~$ git clone https://github.com/MoxaCorp/ioThinx-4530-azure-iot.git
2. Setup dependencies and SDK to output directory.
   1 user@Linux:~$ cd ioThinx-4530-azure-iot
   1 user@Linux:~/ioThinx-4530-azure-iot$ ./setup.sh
   - For more setup.sh options.
     1 user@Linux:~/ioThinx-4530-azure-iot$ ./setup.sh --help
     2
     3 Usage: ./setup.sh [options]
     4
     5 Options:
     6 -git Git repository of SDK.
     7 Default: https://github.com/Azure/azure-iot-sdk-c.git
     8
     9 -ver Version of SDK.
     10 Default: 2018-10-03
     11
     12 --toolchain GNU cross-toolchain directory.
     13 Default: /usr/local/arm-linux-gnueabihf
     14
     15 --help Display this help and exit.
     16
     17 Examples:
     18 Default ./setup.sh
     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
3. Add the Connection String (primary key) of Device to SDK sample code such as example simplesample_amqp.c. [Copy Connection String]
   1 user@Linux:~/ioThinx-4530-azure-iot$ 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=";
4. Build the whole SDK.
   1 user@Linux:~/ioThinx-4530-azure-iot$ ./build.sh
   - All compiled SDK program can be found in the following directory, including example simplesample_amqp.
     1 user@Linux:~/ioThinx-4530-azure-iot$ 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
     7 ├── remote_monitoring
     8 │ └── remote_monitoring
     9 ├── simplesample_amqp
     10 │ └── simplesample_amqp
     11 ├── simplesample_http
     12 │ └── simplesample_http
     13 ├── simplesample_mqtt
     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. This sample code can be executed directly on the target device with module 45MR-2606 located in slot 1.
  1 user@Linux:~/ioThinx-4530-azure-iot$ tree sample
  2 sample
  3 ├── binary
  4 │ └── moxa_sample_mqtt
  5 └── source
  6 └── moxa_sample_mqtt.c
  - 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

1. Setup a network connection to allow target able to access the network.
2. Install following package from package manager.
   1 tree
3. Copy compiled SDK program from host to target.
   1 moxa@Moxa:~$ tree
   2 .
   3 └── simplesample_amqp

### Execute the SDK

1. Execute SDK program that cross-compiled by host.
   1 moxa@Moxa:~$ sudo ./simplesample_amqp
2. 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