Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud for storage or processing. In this quickstart, you send telemetry from a simulated device application, through IoT Hub, to a back-end application for processing.
The quickstart uses a C sample application from the Azure IoT device SDK for C to send telemetry to an IoT hub. The Azure IoT device SDKs are written in ANSI C (C99) for portability and broad platform compatibility. Before running the sample code, you will create an IoT hub and register the simulated device with that hub.
If you don't have an Azure subscription, create a trial account before you begin.
Prerequisites
Install Visual Studio 2019 with the 'Desktop development with C++' workload enabled.
Install the latest version of Git.
Make sure that port 8883 is open in your firewall. The device sample in this quickstart uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see Connecting to IoT Hub (MQTT).
You can use the local Azure CLI.
If you prefer, install the Azure CLI to run CLI reference commands.
Local Azure CLI, see how to install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
Sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Note
This article uses the newest version of the Azure IoT extension, called azure-iot. The legacy version is called azure-cli-iot-ext. You should only have one version installed at a time. You can use the command az extension list to validate the currently installed extensions.
Use az extension remove --name azure-cli-iot-ext to remove the legacy version of the extension.
Use az extension add --name azure-iot to add the new version of the extension.
To see what extensions are currently installed, use az extension list.
Prepare the development environment
For this quickstart, you'll be using the Azure IoT device SDK for C.
For the following environments, you can use the SDK by installing these packages and libraries:
mbed: For developers creating device applications on the mbed platform, we've published a library and samples that will get you started in minutes witH Azure IoT Hub. For more information, see Use the mbed library.
Arduino: If you're developing on Arduino, you can leverage the Azure IoT library available in the Arduino IDE library manager. For more information, see The Azure IoT Hub library for Arduino.
iOS: The IoT Hub Device SDK is available as CocoaPods for Mac and iOS device development. For more information, see iOS Samples for Azure IoT.
However, in this quickstart, you'll prepare a development environment used to clone and build the Azure IoT C SDK from GitHub. The SDK on GitHub includes the sample code used in this quickstart.
Download the CMake build system.
It is important that the Visual Studio prerequisites (Visual Studio and the 'Desktop development with C++' workload) are installed on your machine, before starting the
CMakeinstallation. Once the prerequisites are in place, and the download is verified, install the CMake build system.Find the tag name for the latest release of the SDK.
Open a command prompt or Git Bash shell. Run the following commands to clone the latest release of the Azure IoT C SDK GitHub repository. Use the tag you found in the previous step as the value for the
-bparameter:git clone -b <release-tag> https://github.com/Azure/azure-iot-sdk-c.git cd azure-iot-sdk-c git submodule update --initYou should expect this operation to take several minutes to complete.
Create a
cmakesubdirectory in the root directory of the git repository, and navigate to that folder. Run the following commands from theazure-iot-sdk-cdirectory:mkdir cmake cd cmakeRun the following command to build a version of the SDK specific to your development client platform. A Visual Studio solution for the simulated device will be generated in the
cmakedirectory.cmake ..If
cmakedoesn't find your C++ compiler, you might get build errors while running the above command. If that happens, try running this command in the Visual Studio command prompt.Once the build succeeds, the last few output lines will look similar to the following output:
$ cmake .. -- Building for: Visual Studio 15 2017 -- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17134. -- The C compiler identification is MSVC 19.12.25835.0 -- The CXX compiler identification is MSVC 19.12.25835.0 ... -- Configuring done -- Generating done -- Build files have been written to: E:/IoT Testing/azure-iot-sdk-c/cmake
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
Sign in to the Azure portal.
On the Azure homepage, select the + Create a resource button.
From the Categories menu, select Internet of Things, and then select IoT Hub.
On the Basics tab, complete the fields as follows:
Important
Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.
Property Value Subscription Select the subscription to use for your hub. Resource group Select a resource group or create a new one. To create a new one, select Create new and fill in the name you want to use. IoT hub name Enter a name for your hub. This name must be globally unique, with a length between 3 and 50 alphanumeric characters. The name can also include the dash ( '-') character.Region Select the region, closest to you, where you want your hub to be located. Some features, such as IoT Hub device streams, are only available in specific regions. For these limited features, you must select one of the supported regions. Tier Select the tier that you want to use for your hub. Tier selection depends on how many features you want and how many messages you send through your solution per day.
The free tier is intended for testing and evaluation. The free tier allows 500 devices to be connected to the hub and up to 8,000 messages per day. Each Azure subscription can create one IoT hub in the free tier.
To compare the features available to each tier, select Compare tiers. For more information, see Choose the right IoT Hub tier and size for your solution.Daily message limit Select the maximum daily quota of messages for your hub. The available options depend on the tier you select for your hub. To see the available messaging and pricing options, select See all options and select the option that best matches the needs of your hub. For more information, see IoT Hub quotas and throttling.
Note
Prices shown are for example purposes only.
Select Next: Networking to continue creating your hub.
On the Networking tab, complete the fields as follows:
Property Value Connectivity configuration Choose the endpoints that devices can use to connect to your IoT hub. Accept the default setting, Public access, for this example. You can change this setting after the IoT hub is created. For more information, see IoT Hub endpoints. Minimum TLS Version Select the minimum TLS version supported by your IoT hub. Once the IoT hub is created, this value can't be changed. Accept the default setting, 1.0, for this example.
Select Next: Management to continue creating your hub.
On the Management tab, accept the default settings. If desired, you can modify any of the following fields:
Property Value Permission model Part of role-based access control, this property decides how you manage access to your IoT hub. Allow shared access policies or choose only role-based access control. For more information, see Control access to IoT Hub by using Microsoft Entra ID. Assign me You might need access to IoT Hub data APIs to manage elements within an instance. If you have access to role assignments, select IoT Hub Data Contributor role to grant yourself full access to the data APIs.
To assign Azure roles, you must haveMicrosoft.Authorization/roleAssignments/writepermissions, such as User Access Administrator or Owner.Device-to-cloud partitions This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs need only four partitions.
Select Next: Add-ons to continue to the next screen.
On the Add-ons tab, accept the default settings. If desired, you can modify any of the following fields:
Property Value Enable Device Update for IoT Hub Turn on Device Update for IoT Hub to enable over-the-air updates for your devices. If you select this option, you're prompted to provide information to provision a Device Update for IoT Hub account and instance. For more information, see What is Device Update for IoT Hub? Enable Defender for IoT Turn Defender for IoT on to add an extra layer of protection to IoT and your devices. This option isn't available for hubs in the free tier. For more information, see Security recommendations for IoT Hub in Microsoft Defender for IoT documentation.
Note
Prices shown are for example purposes only.
Select Next: Tags to continue to the next screen.
Tags are name/value pairs. You can assign the same tag to multiple resources and resource groups to categorize resources and consolidate billing. In this document, you don't add any tags. For more information, see Use tags to organize your Azure resources and management hierarchy.
Select Next: Review + create to review your choices.
Select Create to start the deployment of your new hub. Your deployment might progress for a few minutes while the hub is being created. Once the deployment is complete, select Go to resource to open the new hub.
Register a device
A device must be registered with your IoT hub before it can connect. In this section, you will use the Azure CLI with the IoT extension to register a simulated device.
Run the following commands to create the device identity.
YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.
MyCDevice: This is the name of the device you're registering. It's recommended to use MyCDevice as shown. If you choose a different name for your device, you'll also need to use that name throughout this article, and update the device name in the sample applications before you run them.
az iot hub device-identity create --hub-name {YourIoTHubName} --device-id MyCDeviceRun the following commands to get the device connection string for the device you just registered:
YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.
az iot hub device-identity connection-string show --hub-name {YourIoTHubName} --device-id MyCDevice --output tableMake a note of the device connection string, which looks like:
HostName={YourIoTHubName}.azure-devices.cn;DeviceId=MyCDevice;SharedAccessKey={YourSharedAccessKey}You'll use this value later in the quickstart.
Send simulated telemetry
The simulated device application connects to a device-specific endpoint on your IoT hub and sends a string as simulated telemetry.
Using a text editor, open the iothub_convenience_sample.c source file and review the sample code for sending telemetry. The file is located in the following location under the working directory where you cloned the Azure IoT C SDK:
azure-iot-sdk-c\iothub_client\samples\iothub_convenience_sample\iothub_convenience_sample.cFind the declaration of the
connectionStringconstant:/* Paste in your device connection string */ static const char* connectionString = "[device connection string]";Replace the value of the
connectionStringconstant with the device connection string you made a note of earlier. Then save your changes to iothub_convenience_sample.c.In a local terminal window, navigate to the iothub_convenience_sample project directory in the CMake directory that you created in the Azure IoT C SDK. Enter the following command from your working directory:
cd azure-iot-sdk-c/cmake/iothub_client/samples/iothub_convenience_sampleRun CMake in your local terminal window to build the sample with your updated
connectionStringvalue:cmake --build . --target iothub_convenience_sample --config DebugIn your local terminal window, run the following command to run the simulated device application:
Debug\iothub_convenience_sample.exeThe following screenshot shows the output as the simulated device application sends telemetry to the IoT hub:

Read the telemetry from your hub
In this section, you will use the Azure CLI with the IoT extension to monitor the device messages that are sent by the simulated device.
Using the Azure CLI, run the following command to connect and read messages from your IoT hub:
YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.
az iot hub monitor-events --hub-name {YourIoTHubName} --output table
Clean up resources
If you're continuing to the next recommended article, you can keep the resources you already created and reuse them.
Otherwise, you can delete the Azure resources created in this article to avoid charges.
Important
Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you don't accidentally delete the wrong resource group or resources. If you created the IoT hub inside an existing resource group that contains resources you want to keep, only delete the IoT Hub resource itself instead of deleting the resource group.
To delete a resource group by name:
Sign in to the Azure portal and select Resource groups.
In the Filter for any field textbox, type the name of the resource group containing your IoT hub.
In the result list, select the resource group containing your IoT hub.
In the working pane for the resource group, select Delete resource group from the command bar.
You're asked to confirm the deletion of the resource group. Type the name of your resource group again to confirm, and then select Delete. After a few moments, the resource group and all of its contained resources are deleted.
Next steps
In this quickstart, you set up an IoT hub, registered a device, sent simulated telemetry to the hub using a C application, and read the telemetry from the hub using the Azure CLI.
To learn more about developing with the Azure IoT Hub C SDK, continue to the following How-to guide: