|
| 1 | +--- |
| 2 | +title: Connect a device using C on Linux | Microsoft Docs |
| 3 | +description: Describes how to connect a device to the Azure IoT Suite preconfigured remote monitoring solution using an application written in C running on Linux. |
| 4 | +services: '' |
| 5 | +suite: iot-suite |
| 6 | +documentationcenter: na |
| 7 | +author: dominicbetts |
| 8 | +manager: timlt |
| 9 | +editor: '' |
| 10 | + |
| 11 | +ms.assetid: 0c7c8039-0bbf-4bb5-9e79-ed8cff433629 |
| 12 | +ms.service: iot-suite |
| 13 | +ms.devlang: na |
| 14 | +ms.topic: article |
| 15 | +ms.tgt_pltfrm: na |
| 16 | +ms.workload: na |
| 17 | +ms.date: 11/02/2017 |
| 18 | +ms.author: dobett |
| 19 | + |
| 20 | +--- |
| 21 | +# Connect your device to the remote monitoring preconfigured solution (Linux) |
| 22 | +[!INCLUDE [iot-suite-v1-selector-connecting](../../includes/iot-suite-v1-selector-connecting.md)] |
| 23 | + |
| 24 | +## Build and run a sample C client Linux |
| 25 | +The following steps show you how to create a client application that communicates with the remote monitoring preconfigured solution. This application is written in C and built and run on Ubuntu Linux. |
| 26 | + |
| 27 | +To complete these steps, you need a device running Ubuntu version 15.04 or 15.10. Before proceeding, install the prerequisite packages on your Ubuntu device using the following command: |
| 28 | + |
| 29 | +``` |
| 30 | +sudo apt-get install cmake gcc g++ |
| 31 | +``` |
| 32 | + |
| 33 | +## Install the client libraries on your device |
| 34 | +The Azure IoT Hub client libraries are available as a package you can install on your Ubuntu device using the **apt-get** command. Complete the following steps to install the package that contains the IoT Hub client library and header files on your Ubuntu computer: |
| 35 | + |
| 36 | +1. In a shell, add the AzureIoT repository to your computer: |
| 37 | + |
| 38 | + ``` |
| 39 | + sudo add-apt-repository ppa:aziotsdklinux/ppa-azureiot |
| 40 | + sudo apt-get update |
| 41 | + ``` |
| 42 | +2. Install the azure-iot-sdk-c-dev package |
| 43 | + |
| 44 | + ``` |
| 45 | + sudo apt-get install -y azure-iot-sdk-c-dev |
| 46 | + ``` |
| 47 | +
|
| 48 | +## Install the Parson JSON parser |
| 49 | +The IoT Hub client libraries use the Parson JSON parser to parse message payloads. In a suitable folder on your computer, clone the Parson GitHub repository using the following command: |
| 50 | +
|
| 51 | +``` |
| 52 | +git clone https://github.com/kgabis/parson.git |
| 53 | +``` |
| 54 | +
|
| 55 | +## Prepare your project |
| 56 | +On your Ubuntu machine, create a folder called **remote\_monitoring**. In the **remote\_monitoring** folder: |
| 57 | +
|
| 58 | +- Create the four files **main.c**, **remote\_monitoring.c**, **remote\_monitoring.h**, and **CMakeLists.txt**. |
| 59 | +- Create folder called **parson**. |
| 60 | +
|
| 61 | +Copy the files **parson.c** and **parson.h** from your local copy of the Parson repository into the **remote\_monitoring/parson** folder. |
| 62 | +
|
| 63 | +In a text editor, open the **remote\_monitoring.c** file. Add the following `#include` statements: |
| 64 | + |
| 65 | +``` |
| 66 | +#include "iothubtransportmqtt.h" |
| 67 | +#include "schemalib.h" |
| 68 | +#include "iothub_client.h" |
| 69 | +#include "serializer_devicetwin.h" |
| 70 | +#include "schemaserializer.h" |
| 71 | +#include "azure_c_shared_utility/threadapi.h" |
| 72 | +#include "azure_c_shared_utility/platform.h" |
| 73 | +#include "parson.h" |
| 74 | +``` |
| 75 | +
|
| 76 | +[!INCLUDE [iot-suite-v1-connecting-code](../../includes/iot-suite-v1-connecting-code.md)] |
| 77 | +
|
| 78 | +## Call the remote\_monitoring\_run function |
| 79 | +In a text editor, open the **remote_monitoring.h** file. Add the following code: |
| 80 | +
|
| 81 | +``` |
| 82 | +void remote_monitoring_run(void); |
| 83 | +``` |
| 84 | +
|
| 85 | +In a text editor, open the **main.c** file. Add the following code: |
| 86 | +
|
| 87 | +``` |
| 88 | +#include "remote_monitoring.h" |
| 89 | + |
| 90 | +int main(void) |
| 91 | +{ |
| 92 | + remote_monitoring_run(); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | +``` |
| 97 | +
|
| 98 | +## Build and run the application |
| 99 | +The following steps describe how to use *CMake* to build your client application. |
| 100 | +
|
| 101 | +1. In a text editor, open the **CMakeLists.txt** file in the **remote_monitoring** folder. |
| 102 | +
|
| 103 | +1. Add the following instructions to define how to build your client application: |
| 104 | + |
| 105 | + ``` |
| 106 | + macro(compileAsC99) |
| 107 | + if (CMAKE_VERSION VERSION_LESS "3.1") |
| 108 | + if (CMAKE_C_COMPILER_ID STREQUAL "GNU") |
| 109 | + set (CMAKE_C_FLAGS "--std=c99 ${CMAKE_C_FLAGS}") |
| 110 | + set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}") |
| 111 | + endif() |
| 112 | + else() |
| 113 | + set (CMAKE_C_STANDARD 99) |
| 114 | + set (CMAKE_CXX_STANDARD 11) |
| 115 | + endif() |
| 116 | + endmacro(compileAsC99) |
| 117 | +
|
| 118 | + cmake_minimum_required(VERSION 2.8.11) |
| 119 | + compileAsC99() |
| 120 | +
|
| 121 | + set(AZUREIOT_INC_FOLDER "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/parson" "/usr/include/azureiot" "/usr/include/azureiot/inc") |
| 122 | +
|
| 123 | + include_directories(${AZUREIOT_INC_FOLDER}) |
| 124 | +
|
| 125 | + set(sample_application_c_files |
| 126 | + ./parson/parson.c |
| 127 | + ./remote_monitoring.c |
| 128 | + ./main.c |
| 129 | + ) |
| 130 | +
|
| 131 | + set(sample_application_h_files |
| 132 | + ./parson/parson.h |
| 133 | + ./remote_monitoring.h |
| 134 | + ) |
| 135 | +
|
| 136 | + add_executable(sample_app ${sample_application_c_files} ${sample_application_h_files}) |
| 137 | +
|
| 138 | + target_link_libraries(sample_app |
| 139 | + serializer |
| 140 | + iothub_client |
| 141 | + iothub_client_mqtt_transport |
| 142 | + aziotsharedutil |
| 143 | + umqtt |
| 144 | + pthread |
| 145 | + curl |
| 146 | + ssl |
| 147 | + crypto |
| 148 | + m |
| 149 | + ) |
| 150 | + ``` |
| 151 | +1. In the **remote_monitoring** folder, create a folder to store the *make* files that CMake generates and then run the **cmake** and **make** commands as follows: |
| 152 | + |
| 153 | + ``` |
| 154 | + mkdir cmake |
| 155 | + cd cmake |
| 156 | + cmake ../ |
| 157 | + make |
| 158 | + ``` |
| 159 | +
|
| 160 | +1. Run the client application and send telemetry to IoT Hub: |
| 161 | + |
| 162 | + ``` |
| 163 | + ./sample_app |
| 164 | + ``` |
| 165 | +
|
| 166 | +[!INCLUDE [iot-suite-v1-visualize-connecting](../../includes/iot-suite-v1-visualize-connecting.md)] |
| 167 | +
|
0 commit comments