Azure IoT Hub

The Azure IoT Hub sample shows the communication of an Internet-connected device with an Azure IoT Hub instance. This sample uses the Azure IoT Hub library to communicate with the IoT hub and the Azure FOTA library to provide firmware over-the-air (FOTA) functionality.


The sample supports the following development kits:

Hardware platforms


Board name

Build target

nRF9161 DK



nRF9160 DK




nRF7002 DK




When built for an _ns build target, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.


The sample supports the direct connection of an IoT device that is already registered to an Azure IoT Hub instance. Alternatively, it supports the provisioning of the device using Azure IoT Hub Device Provisioning Service (DPS) to an IoT hub. See the documentation on Azure IoT Hub library for more information.

The sample periodically publishes telemetry messages (events) to the connected Azure IoT Hub instance. By default, telemetry messages are sent every 20 seconds. To configure the default interval, set the device twin property desired.telemetryInterval, which will be interpreted by the sample as seconds. Here is an example of a telemetry message:

  "temperature": 25.2,
  "timestamp": 151325

In a telemetry message, temperature is a value between 25.0 and 25.9, and timestamp is the uptime of the device in milliseconds.

The sample has implemented the handling of Azure IoT Hub direct method with the name led. If the device receives a direct method invocation with the name led and payload 1 or 0, LED 1 on the device is turned on or off, depending on the payload. On Thingy:91, the LED turns red if the payload is 1.

Firmware update

The sample subscribes to incoming Azure IOT Hub device twin messages that contain the firmware update information. By default, the device twin document is requested upon connecting to the IoT hub. Thus, any existing firmware information in the desired property will be parsed. See Azure FOTA for more details on the content of the firmware information in the device twin.


This sample requires a file server instance that hosts the new firmware image. The Azure FOTA library does not require a specific host, but it has been tested using Azure Blob Storage that shares the same root certificate as the Azure IoT Hub instance.


See Configuring your application for information about how to permanently or temporarily change the configuration.


For the sample to work as intended, you must set up and configure an Azure IoT Hub instance. See Additional configuration for information on the configuration options that you can use to create an Azure IoT Hub instance. Also, for a successful TLS connection to the Azure IoT Hub instance, the device needs to have certificates provisioned. If you want to test FOTA, ensure that also the required credentials for the file server are provisioned and the CONFIG_AZURE_FOTA_SEC_TAG Kconfig option is set accordingly. See Prerequisites for connecting to Azure IoT Hub for information on provisioning the certificates.

Additional configuration

Check and configure the following library options that are used by the sample:

  • CONFIG_AZURE_IOT_HUB_DEVICE_ID - Sets the Azure IoT Hub device ID. Alternatively, the device ID can be provided at run time.

  • CONFIG_AZURE_IOT_HUB_HOSTNAME - Sets the Azure IoT Hub host name. If DPS is used, the sample assumes that the IoT hub host name is unknown, and the configuration is ignored. The configuration can also be omitted and the hostname provided at run time.

If DPS is used, use the Kconfig fragment found in the overlay-dps.conf file and change the desired configurations there. As an example, the following compiles with DPS for nRF9160DK:

west build -p -b nrf9160dk_nrf9160_ns -- -DOVERLAY_CONFIG=overlay-dps.conf

For FOTA, check and configure the following library Kconfig options:


The sample sets the option CONFIG_MQTT_KEEPALIVE to the maximum allowed value, 1767 seconds (29.45 minutes) as specified by Azure IoT Hub. This is to limit the IP traffic between the device and the Azure IoT Hub message broker for supporting a low power sample. In certain LTE networks, the NAT timeout can be considerably lower than 1767 seconds. As a recommendation, and to prevent the likelihood of getting disconnected unexpectedly, set the option CONFIG_MQTT_KEEPALIVE to the lowest timeout limit (Maximum allowed MQTT keepalive and NAT timeout).

Sending traces over UART on an nRF91 Series DK

To send modem traces over UART on an nRF91 Series DK, configuration must be added for the UART device in the devicetree and Kconfig. This is done by adding the modem trace UART snippet when building and programming.

Use the Cellular Monitor app for capturing and analyzing modem traces.

Building and running

This sample can be found under samples/net/azure_iot_hub in the nRF Connect SDK folder structure.

When built as firmware image for the _ns build target, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application page in the nRF Connect for VS Code extension documentation. See Building and programming an application for other building and programming scenarios and Testing and debugging an application for general information about testing and debugging in the nRF Connect SDK.


Microsoft has created Azure IoT Explorer to interact and test devices connected to an Azure IoT Hub instance. Optionally, follow the instructions at Azure IoT Explorer to install and configure the tool and use it as mentioned in the below instructions.

After programming the sample to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.

  2. Connect to the kit with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  3. Reset the development kit.

  4. Observe the log output and verify that it is similar to the Sample output.

  5. Use the Azure IoT Explorer, or log in to the Azure Portal.

  6. Select the connected IoT hub and then your device.

  7. Change the device twin’s desired property telemetryInterval to a new value, for instance 60, and save the updated device twin. If it does not exist, you can add the desired property.

  8. Observe that the device receives the updated telemetryInterval value, applies it, and starts sending new telemetry events every 10 seconds.

  9. Verify that the reported object in the device twin now has a telemetryInterval property with the correct value reported back from the device.

  10. In the Azure IoT Explorer or device page in Azure Portal, navigate to the Direct method tab.

  11. Enter led as the method name. In the payload field, enter the value 1 (or 0) and click Invoke method.

  12. Observe that LED 1 on the development kit lights up (or switches off if 0 is entered as the payload). If you are using Azure IoT Explorer, you can observe a notification in the top right corner stating if the direct method was successfully invoked based on the report received from the device.

  13. In the Azure IoT Explorer, navigate to the Telemetry tab and click start.

  14. Observe that the event messages from the device are displayed in the terminal within the specified telemetry interval.

To test FOTA update:

  1. Navigate to the view where you can update the device twin using the Azure IoT Explorer or Azure Portal.

  2. Create a firmware JSON object inside the desired object of the device twin document, containing information about the binary to download:

          "firmware": {
              "fwVersion": "v0.0.2-dev",
              "jobId": "ca186d4b-4171-4209-a49d-700c35567d1d",
              "fwLocation": {
                  "host": "",
                  "path": "firmware/my-app-update.bin"
              "fwFragmentSize": 1800
  3. Click Save to apply the changes to the device twin. Updating the device twin causes a message to be sent to the device, containing the updates to the desired object.

  4. In the terminal emulator, observe that the new firmware image is downloaded and installed as shown in Sample output.

  5. Observe that the sample displays the logs from the new application when the development kit reboots.

  6. Confirm that the device twin contains the updated application version in the reported.firmware.fwVersion field in Azure IoT Explorer or Azure Portal.

Sample output

When the sample runs, the device boots, and the sample displays the following output in the terminal over UART:

*** Booting Zephyr OS build v2.3.0-rc1-ncs1-1453-gf41496cd30d5  ***
<inf> azure_iot_hub_sample: Azure IoT Hub sample started
<inf> azure_iot_hub_sample: Bringing network interface up and connecting to the network
<inf> azure_iot_hub_sample: Device ID: my-device
<inf> azure_iot_hub_sample: Connecting...
<inf> azure_iot_hub_sample: Network connectivity established and IP address assigned
<inf> azure_fota: Current firmware version: 0.0.1-dev
<inf> azure_iot_hub_sample: Azure IoT Hub library initialized
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_CONNECTING
<inf> azure_iot_hub_sample: Connection request sent to IoT Hub
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_CONNECTED
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_READY
<inf> azure_iot_hub_sample: Sending event:
<inf> azure_iot_hub_sample: {"temperature":25.9,"timestamp":16849}
<inf> azure_iot_hub_sample: Event was successfully sent
<inf> azure_iot_hub_sample: Next event will be sent in 60 seconds

If a new telemetry interval is set in the device twin, the console output is like this:

<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_DESIRED_RECEIVED
<inf> azure_iot_hub_sample: New telemetry interval has been applied: 60
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_RESULT_SUCCESS, ID: 42740
<inf> azure_iot_hub_sample: Sending event:
<inf> azure_iot_hub_sample: {"temperature":25.5,"timestamp":47585}
<inf> azure_iot_hub_sample: Event was successfully sent
<inf> azure_iot_hub_sample: Next event will be sent in 60 seconds

If a new FOTA update is initiated, the console output is like this:

<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_RESULT_SUCCESS, ID: 140
<inf> azure_fota: Attempting to download firmware (version 'v0.0.2-dev') from
<inf> download_client: Downloading: firmware/app_update.bin [0]
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_FOTA_START
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_DESIRED_RECEIVED
<inf> download_client: Setting up TLS credentials, sec tag count 1
<inf> download_client: Connecting to
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_RESULT_SUCCESS, ID: 190
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_RESULT_SUCCESS, ID: 190
<inf> download_client: Downloaded 1800/674416 bytes (0%)
<inf> download_client: Downloaded 674416/674416 bytes (100%)
<inf> download_client: Download complete
<inf> dfu_target_mcuboot: MCUBoot image-0 upgrade scheduled. Reset device to apply
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_FOTA_DONE
<inf> azure_iot_hub_sample: The device will reboot in 5 seconds to apply update
<inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_TWIN_RESULT_SUCCESS, ID: 109


The sample uses the following nRF Connect SDK and Zephyr libraries:

It uses the following libraries and secure firmware component for nRF91 Series builds:

It uses the following libraries for nRF7 Series builds: