본문 바로가기
🐳Azure

Getting started with Azure IoT Hub (1)

by 주입식교육의폐해 2025. 6. 24.

 


 

 

In the last post, we introduced the IoT architecture proposed by Azure and explored each of its components. In this post, we'll use an example application to show how to send remote device data to an IoT hub. The simulator in the example is configured to send temperature and humidity information to an IoT hub via MQTT once per second.

 

 

 


 

 

1. Set up your lab environment

 

 

 

 

▶Installing Visual Studio 2019 - Enabling Desktop Development Workloads with C++
Install the latest version of Git
Open port 8883 on the firewall of the environment where you will run the application to allow MQTT protocol communication
Download the CMake build system for your operating system and add it to your environment variables

 

 

 

 

 

2. Download and build the SDK

 

 

In Git Bash, use the following command to clone the latest version of the Azure IoT C SDK GitHub repository (as of: LTS_07_2020_Ref01)

 

 


 

 

git clone -b LTS_07_2020_Ref01 https://github.com/Azure/azure-iot-sdk-c.git
cd azure-iot-sdk-c
git submodule update --init

 

 

 

 

Create a subdirectory in the root directory of your Git repository and navigate to that folder to build your version of the SDK.

 

 

 

3. Create an IoT Hub and enroll devices

 

 

 

★Create an IoT hub

 

 


▷In the Azure portal, search for IoT hub and select Create. For now, you can use the free version, which is available for testing. 

 

 

Select a tier to operate the service, using the following links as a guide
Select the IoT Hub tier - If you are using IoT Edge for device management, or if you need the ability to communicate between devices and the IoT Hub bi-directionally, you should select the standard tier (S1 to S3).

 

 

In the Bash shell of the Cloud Shell window, run the following command to create an IoT device ID.

 

 

 

 

 

→in the Bash shell of the Cloud Shell window, run the following command to create an IoT device ID

 

az extension add --name azure-iot #Azure IoT 확장 사용
az iot hub device-identity create --hub-name <Your IoT Hub name> --device-id MyCDevice

 

 

→ Check the connection string of the created IoT device.

 

 


 

 

Enrolling devices

 

 

In Git Bash, open the source files in the path below to modify the sample code.

 


 

◈azure-iot-sdk-c\iothub_client\samples\iothub_convenience_sample\iothub_convenience_sample.c◈

 

 

 


 

 

→Find the static const char* connectionString = ‘[device connection string]’; part and replace it with your connection string.

→ Navigate to the iothub_convenience_sample project directory in your CMake directory.

 

 

 


 

cd azure-iot-sdk-c/cmake/iothub_client/samples/iothub_convenience_sample

 


 

 

 

Build the simulated device application.

 



cmake --build . --target iothub_convenience_sample --config Debug

 



Run the build output.

 



Debug/iothub_convenience_sample.exe

 


 

 

 

 

 


 

 

 

 

 

4.Reading data from the hub

 

In the Bash shell of the Cloud Shell window, run the following command to connect and read the IoT Hub messages.

 


 

 

az iot hub monitor-events --hub-name <Your IoT Hub name> --output table

 

 


 

 

 

 

 

 

 

5.Routing messages to storage accounts

 

 

IoT Hub has message routing capabilities. It supports Azure Storage Container, Event Hub, Service Bus Queue, and Topic as custom endpoints. You can route messages based on a query, and if no endpoint matches the query, it sends the message to a built-in endpoint that is compatible with Event Hub. This feature allows you to split up and store messages coming from multiple IoT devices. You can also send messages to multiple endpoints to perform additional actions, such as analytics or alarms.

 




Create a container in your storage account to store messages.

From the IoT Hub menu, select Message routing and click Add.

Next to the Select endpoint window, click +Add, and select the type as Storage.

Specify the container you created earlier and specify the encoding format (AVRO and JSON are supported).

The Routing query default value is set to true. If you leave it as is, all messages coming to the hub will be routed to the storage account.

 

 

 

 

 

반응형