IOT
IOT
IOT
IoT Hub is a managed service, hosted in the cloud, that acts as a central message hub for bi-directional
communication between your IoT application and the devices it manages. You can use Azure IoT Hub to build IoT
solutions with reliable and secure communications between millions of IoT devices and a cloud-hosted solution
backend. You can connect virtually any device to IoT Hub.
IoT Hub supports communications both from the device to the cloud and from the cloud to the device. IoT Hub
supports multiple messaging patterns such as device-to-cloud telemetry, file upload from devices, and request-
reply methods to control your devices from the cloud. IoT Hub monitoring helps you maintain the health of your
solution by tracking events such as device creation, device failures, and device connections.
IoT Hub's capabilities help you build scalable, full-featured IoT solutions such as managing industrial equipment
used in manufacturing, tracking valuable assets in healthcare, and monitoring office building usage.
Next steps
To try out an end-to-end IoT solution, check out the IoT Hub quickstarts:
Quickstart: Send telemetry from a device to an IoT hub
Quickstart: Send telemetry from a device to an IoT
hub and read the telemetry from the hub with a
back-end application (Node.js)
5/29/2018 • 5 min to read • Edit Online
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 two pre-written Node.js applications, one to send the telemetry and one to read the telemetry
from the hub. Before you run these two applications, you create an IoT hub and register a device with the hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Node.js. You need Node.js v4.x.x or later
on your development machine.
You can download Node.js for multiple platforms from nodejs.org.
You can verify the current version of Node.js on your development machine using the following command:
node --version
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you
chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need a service connection string to enable the back-end application to connect to your IoT hub and
retrieve the messages. The following command retrieves the service connection string for your IoT hub:
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart. The service connection string is different from the device connection string.
npm install
node SimulatedDevice.js
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
Read the telemetry from your hub
The back-end application connects to the service-side Events endpoint on your IoT Hub. The application receives
the device-to-cloud messages sent from your simulated device. An IoT Hub back-end application typically runs in
the cloud to receive and process device-to-cloud messages.
1. In another terminal window, navigate to the root folder of the sample Node.js project. Then navigate to the
read-d2c-messages folder.
2. Open the iot-hub\Quickstarts\ReadDeviceToCloudMessages.js file in a text editor of your choice.
Replace the value of the connectionString variable with the service connection string you made a note of
previously. Then save your changes to the ReadDeviceToCloudMessages.js file.
3. In the terminal window, run the following commands to install the required libraries and run the back-end
application:
npm install
node ReadDeviceToCloudMessages.js
The following screenshot shows the output as the back-end application receives telemetry sent by the
simulated device to the hub:
Clean up resources
If you plan to complete the next quickstart, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the qs-iot-
hub-rg resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've setup an IoT hub, registered a device, sent simulated telemetry to the hub using a Node.js
application, and read the telemetry from the hub using a simple back-end application.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
Quickstart: Control a device connected to an IoT hub
Quickstart: Send telemetry from a device to an IoT
hub and read the telemetry from the hub with a
back-end application (C#)
5/29/2018 • 5 min to read • Edit Online
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 two pre-written C# applications, one to send the telemetry and one to read the telemetry from
the hub. Before you run these two applications, you create an IoT hub and register a device with the hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using C#. You need the .NET Core SDK 2.1.0 or
greater on your development machine.
You can download the .NET Core SDK for multiple platforms from .NET.
You can verify the current version of C# on your development machine using the following command:
dotnet --version
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you
chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need the Event Hubs-compatible endpoint, Event Hubs-compatible path, and iothubowner
primary key from your IoT hub to enable the back-end application to connect to your IoT hub and retrieve
the messages. The following commands retrieve these values for your IoT hub:
az iot hub policy show --name iothubowner --query primaryKey --hub-name {your IoT Hub name}
Make a note of these three values, which you use later in the quickstart.
dotnet restore
4. In the terminal window, run the following command to build and run the simulated device application:
dotnet run
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
dotnet restore
4. In the terminal window, run the following commands to build and run the back-end application:
dotnet run
The following screenshot shows the output as the back-end application receives telemetry sent by the
simulated device to the hub:
Clean up resources
If you plan to complete the next quickstart, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the qs-iot-
hub-rg resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've setup an IoT hub, registered a device, sent simulated telemetry to the hub using a C#
application, and read the telemetry from the hub using a simple back-end application.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
Quickstart: Control a device connected to an IoT hub
Quickstart: Send telemetry from a device to an IoT
hub and read the telemetry from the hub with a
back-end application (Java)
5/29/2018 • 6 min to read • Edit Online
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 two pre-written Java applications, one to send the telemetry and one to read the telemetry
from the hub. Before you run these two applications, you create an IoT hub and register a device with the hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Java. You need Java SE 8 or later on your
development machine.
You can download Java for multiple platforms from Oracle.
You can verify the current version of Java on your development machine using the following command:
java --version
To build the samples, you need to install Maven 3. You can download Maven for multiple platforms from Apache
Maven.
You can verify the current version of Maven on your development machine using the following command:
mvn --version
Download the sample Java project from https://github.com/Azure-Samples/azure-iot-samples-
java/archive/master.zip and extract the ZIP archive.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you
chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need the Event Hubs-compatible endpoint, Event Hubs-compatible path, and iothubowner
primary key from your IoT hub to enable the back-end application to connect to your IoT hub and retrieve
the messages. The following commands retrieve these values for your IoT hub:
az iot hub policy show --name iothubowner --query primaryKey --hub-name {your IoT Hub name}
Make a note of these three values, which you use later in the quickstart.
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
4. In the terminal window, run the following commands to run the back-end application:
Clean up resources
If you plan to complete the next quickstart, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the qs-iot-
hub-rg resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've setup an IoT hub, registered a device, sent simulated telemetry to the hub using a Java
application, and read the telemetry from the hub using a simple back-end application.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
Quickstart: Control a device connected to an IoT hub
Quickstart: Send telemetry from a device to an IoT
hub and read the telemetry from the hub with a
back-end application (Python)
5/29/2018 • 5 min to read • Edit Online
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 pre-written Python application to send the telemetry and a CLI utility to read the telemetry
from the hub. Before you run these two applications, you create an IoT hub and register a device with the hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Python. You need either Python 2.7.x or
3.5.x on your development machine.
You can download Python for multiple platforms from Python.org.
You can verify the current version of Python on your development machine using one of the following
commands:
python --version
python3 --version
node --version
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name
you chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample application before
you run it.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need a service connection string to enable the iothub-explorer CLI utility to connect to your IoT
hub and retrieve the messages. The following command retrieves the service connection string for your
IoT hub:
az iot hub show-connection-string --hub-name {YourIoTHubName} --output table
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart. The service connection string is different from the device connection string.
4. In the terminal window, run the following commands to run the simulated device application:
python SimulatedDevice.py
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
The following screenshot shows the output as the utility receives telemetry sent by the simulated device to the
hub:
Clean up resources
If you plan to complete the next quickstart, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the qs-iot-
hub-rg resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've setup an IoT hub, registered a device, sent simulated telemetry to the hub using a Python
application, and read the telemetry from the hub using a simple back-end application.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
Quickstart: Control a device connected to an IoT hub
Quickstart: Send telemetry from a device to an IoT
hub (iOS)
6/6/2018 • 6 min to read • Edit Online
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 article, you send telemetry from a simulated device application to IoT Hub.
Then you can view the data from a back-end application.
This article uses a pre-written Swift application to send the telemetry and a CLI utility to read the telemetry from
IoT Hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
Download the code sample from Azure samples
The latest version of XCode, running the latest version of the iOS SDK. This quickstart was tested with XCode
9.3 and iOS 11.3.
The latest version of CocoaPods.
The iothub-explorer CLI utility, which reads telemetry from IoT Hub. To install, first install Node.js v4.x.x or
higher, then run the following command:
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with a name for
your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
article.
3. You also need a service connection string to enable back-end applications to connect to your IoT hub and
retrieve device-to-cloud messages. The following command retrieves the service connection string for
your IoT hub:
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in
the article.
cd quickstart/sample-device
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in
the podfile file:
pod install
Along with installing the pods required for your project, the installation command also created an XCode
workspace file that is already configured to use the pods for dependencies.
Run the sample application
1. Open the sample workspace in XCode.
2. Expand the MQTT Client Sample project and then expand the folder of the same name.
3. Open ViewController.swift for editing in XCode.
4. Search for the connectionString variable and update the value with the device connection string that you
made a note of previously.
5. Save your changes.
6. Run the project in the device emulator with the Build and run button or the key combo command + r.
The following screenshot shows the type of telemetry that you see in your terminal window:
If you get an error when you run the iothub-explorer command, double check that you're using the service
connection string for your IoT hub, rather than the device connection string for your IoT device. Both connection
strings start with Hostname={iothubname} but the service connection string contains the
SharedAccessKeyName property while the device connection string contains DeviceID.
Clean up resources
If you plan to continue testing IoT Hub with other articles, leave the your resource group and IoT hub and reuse
them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
resource group that contains your IoT hub and click Delete.
Next steps
In this article, you set up an IoT hub, registered a device, sent simulated telemetry to the hub from an iOS device,
and read the telemetry from the hub.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
Quickstart: Control a device connected to an IoT hub
Quickstart: Control a device connected to an IoT hub
(Node.js)
6/6/2018 • 6 min to read • Edit Online
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the
cloud and manage your devices from the cloud. In this quickstart, you use a direct method to control a simulated
device connected to your IoT hub. You can use direct methods to remotely change the behavior of a device
connected to your IoT hub.
The quickstart uses two pre-written Node.js applications:
A simulated device application that responds to direct methods called from a back-end application. To receive
the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
A back-end application that calls the direct methods on the simulated device. To call a direct method on a
device, this application connects to service-side endpoint on your IoT hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Node.js. You need Node.js v4.x.x or later
on your development machine.
You can download Node.js for multiple platforms from nodejs.org.
You can verify the current version of Node.js on your development machine using the following command:
node --version
If you haven't already done so, download the sample Node.js project from https://github.com/Azure-
Samples/azure-iot-samples-node/archive/master.zip and extract the ZIP archive.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name of
your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need a service connection string to enable the back-end application to connect to your IoT hub and
retrieve the messages. The following command retrieves the service connection string for your IoT hub:
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart. The service connection string is different from the device connection string.
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
npm install
node BackEndApplication.js
The following screenshot shows the output as the application makes a direct method call to the device and
receives an acknowledgement:
After you run the back-end application, you see a message in the console window running the simulated
device, and the rate at which it sends messages changes:
Clean up resources
If you plan to move on to the tutorials, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've called a direct method on a device from a back-end application, and responded to the
direct method call in a simulated device application.
To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.
Tutorial: Route telemetry to different endpoints for processing
Quickstart: Control a device connected to an IoT hub
(.NET)
6/6/2018 • 6 min to read • Edit Online
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the
cloud and manage your devices from the cloud. In this quickstart, you use a direct method to control a simulated
device connected to your IoT hub. You can use direct methods to remotely change the behavior of a device
connected to your IoT hub.
The quickstart uses two pre-written .NET applications:
A simulated device application that responds to direct methods called from a back-end application. To receive
the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
A back-end application that calls the direct methods on the simulated device. To call a direct method on a
device, this application connects to service-side endpoint on your IoT hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using C#. You need the .NET Core SDK 2.1.0 or
greater on your development machine.
You can download the .NET Core SDK for multiple platforms from .NET.
You can verify the current version of C# on your development machine using the following command:
dotnet --version
If you haven't already done so, download the sample C# project from https://github.com/Azure-Samples/azure-
iot-samples-csharp/archive/master.zip and extract the ZIP archive.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Register a device
If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you
chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
4. In the terminal window, run the following command to build and run the simulated device application:
dotnet run
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
dotnet restore
4. In the terminal window, run the following commands to build and run the back-end application:
dotnet run
The following screenshot shows the output as the application makes a direct method call to the device and
receives an acknowledgement:
After you run the back-end application, you see a message in the console window running the simulated
device, and the rate at which it sends messages changes:
Clean up resources
If you plan to move on to the tutorials, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've called a direct method on a device from a back-end application, and responded to the
direct method call in a simulated device application.
To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.
Tutorial: Route telemetry to different endpoints for processing
Quickstart: Control a device connected to an IoT hub
(Java)
6/6/2018 • 6 min to read • Edit Online
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the
cloud and manage your devices from the cloud. In this quickstart, you use a direct method to control a simulated
device connected to your IoT hub. You can use direct methods to remotely change the behavior of a device
connected to your IoT hub.
The quickstart uses two pre-written Java applications:
A simulated device application that responds to direct methods called from a back-end application. To receive
the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
A back-end application that calls the direct methods on the simulated device. To call a direct method on a
device, this application connects to service-side endpoint on your IoT hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Java. You need Java SE 8 or later on your
development machine.
You can download Java for multiple platforms from Oracle.
You can verify the current version of Java on your development machine using the following command:
java --version
To build the samples, you need to install Maven 3. You can download Maven for multiple platforms from Apache
Maven.
You can verify the current version of Maven on your development machine using the following command:
mvn --version
If you haven't already done so, download the sample Java project from https://github.com/Azure-Samples/azure-
iot-samples-java/archive/master.zip and extract the ZIP archive.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Register a device
If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name you
chose for your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
4. In the terminal window, run the following commands to run the simulated device application:
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
Call the direct method
The back-end application connects to a service-side endpoint on your IoT Hub. The application makes direct
method calls to a device through your IoT hub and listens for acknowledgements. An IoT Hub back-end
application typically runs in the cloud.
1. In another terminal window, navigate to the root folder of the sample Java project. Then navigate to the
iot-hub\Quickstarts\back-end-application folder.
2. Open the src/main/java/com/microsoft/docs/iothub/samples/BackEndApplication.java file in a
text editor of your choice.
Replace the value of the iotHubConnectionString variable with the service connection string you made a
note of previously. Then save your changes to the BackEndApplication.java file.
3. In the terminal window, run the following commands to install the required libraries and build the back-end
application:
4. In the terminal window, run the following commands to run the back-end application:
The following screenshot shows the output as the application makes a direct method call to the device and
receives an acknowledgement:
After you run the back-end application, you see a message in the console window running the simulated
device, and the rate at which it sends messages changes:
Clean up resources
If you plan to move on to the tutorials, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've called a direct method on a device from a back-end application, and responded to the
direct method call in a simulated device application.
To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.
Tutorial: Route telemetry to different endpoints for processing
Quickstart: Control a device connected to an IoT hub
(Python)
6/6/2018 • 6 min to read • Edit Online
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the
cloud and manage your devices from the cloud. In this quickstart, you use a direct method to control a simulated
device connected to your IoT hub. You can use direct methods to remotely change the behavior of a device
connected to your IoT hub.
The quickstart uses two pre-written Python applications:
A simulated device application that responds to direct methods called from a back-end application. To receive
the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
A back-end application that calls the direct methods on the simulated device. To call a direct method on a
device, this application connects to service-side endpoint on your IoT hub.
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Python. You need either Python 2.7.x or
3.5.x on your development machine.
You can download Python for multiple platforms from Python.org.
You can verify the current version of Python on your development machine using one of the following commands:
python --version
python3 --version
If you haven't already done so, download the sample Python project from https://github.com/Azure-
Samples/azure-iot-samples-python/archive/master.zip and extract the ZIP archive.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Register a device
If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI to
register a simulated device.
1. Add the IoT Hub CLI extension and create the device identity. Replace {YourIoTHubName} with the name of
your IoT hub:
If you choose a different name for your device, update the device name in the sample applications before
you run them.
2. Run the following command to get the device connection string for the device you just registered:
Make a note of the device connection string, which looks like Hostname=...= . You use this value later in the
quickstart.
3. You also need a service connection string to enable the back-end application to connect to your IoT hub and
retrieve the messages. The following command retrieves the service connection string for your IoT hub:
az iot hub show-connection-string --hub-name {YourIoTHubName} --output table
Make a note of the service connection string, which looks like Hostname=...= . You use this value later in the
quickstart. The service connection string is different from the device connection string.
4. In the terminal window, run the following commands to run the simulated device application:
python SimulatedDevice.py
The following screenshot shows the output as the simulated device application sends telemetry to your IoT
hub:
4. In the terminal window, run the following commands to run the back-end application:
python BackEndApplication.py
The following screenshot shows the output as the application makes a direct method call to the device and
receives an acknowledgement:
After you run the back-end application, you see a message in the console window running the simulated
device, and the rate at which it sends messages changes:
Clean up resources
If you plan to move on to the tutorials, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
resource group that contains your IoT hub and click Delete.
Next steps
In this quickstart, you've called a direct method on a device from a back-end application, and responded to the
direct method call in a simulated device application.
To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.
Tutorial: Route telemetry to different endpoints for processing
Tutorial: Configure message routing with IoT Hub
5/31/2018 • 21 min to read • Edit Online
Message routing enables sending telemetry data from your IoT devices to built-in Event Hub-compatible
endpoints or custom endpoints such as blob storage, Service Bus Queue, Service Bus Topic, and Event Hubs.
While configuring message routing, you can create routing rules to customize the route that matches a certain
rule. Once set up, the incoming data is automatically routed to the endpoints by the IoT Hub.
In this tutorial, you learn how to set up and use routing rules with IoT Hub. You will route messages from an IoT
device to one of multiple services, including blob storage and a Service Bus queue. Messages to the Service Bus
queue will be picked up by a Logic App and sent via e-mail. Messages that do not have routing specifically set
up are sent to the default endpoint, and viewed in a Power BI visualization.
In this tutorial, you perform the following tasks:
Using Azure CLI or PowerShell, set up the base resources -- an IoT hub, a storage account, a Service Bus
queue, and a simulated device.
Configure endpoints and routes in IoT hub for the storage account and Service Bus queue.
Create a Logic App that is triggered and sends e-mail when a message is added to the Service Bus queue.
Download and run an app that simulates an IoT Device sending messages to the hub for the different
routing options.
Create a Power BI visualization for data sent to the default endpoint.
View the results ...
...in the Service Bus queue and e-mails.
...in the storage account.
...in the Power BI visualization.
Prerequisites
An Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
Install Visual Studio for Windows.
A Power BI account to analyze the default endpoint's stream analytics. (Try Power BI for free.)
An Office 365 account to send notification e-mails.
You need either Azure CLI or Azure PowerShell to do the setup steps for this tutorial.
To use Azure CLI, while you can install Azure CLI locally, we recommend you use the Azure Cloud Shell. Azure
Cloud Shell is a free, interactive shell that you can use to run Azure CLI scripts. Common Azure tools are
preinstalled and configured in Cloud Shell for you to use with your account, so you don't have to install them
locally.
To use PowerShell, install it locally using the instructions below.
Azure Cloud Shell
There are a few ways to open Cloud Shell:
Select Try It in the upper-right corner of a code block.
Set up resources
For this tutorial, you need an IoT hub, a storage account, and a Service Bus queue. These resources can all be
created using Azure CLI or Azure PowerShell. Use the same resource group and location for all of the
resources. Then at the end, you can remove everything in one step by deleting the resource group.
The following sections describe how to do these required steps. Follow the CLI or the PowerShell instructinos.
1. Create a resource group.
2. Create an IoT hub in the S1 tier. Add a consumer group to your IoT hub. The consumer group is used by
the Azure Stream Analytics when retrieving data.
3. Create a standard V1 storage account with Standard_LRS replication.
4. Create a Service Bus namespace and queue.
5. Create a device identity for the simulated device that sends messages to your hub. Save the key for the
testing phase.
Azure CLI instructions
The easiest way to use this script is to copy it and paste it into Cloud Shell. Assuming you are already logged in,
it will run the script one line at a time.
# Set the values for the resource names that don't have to be globally unique.
# The resources that have to have unique names are named in the script below
# with a random number concatenated to the name so you can probably just
# run this script, and it will work with no conflicts.
location=westus
resourceGroup=ContosoResources
iotHubConsumerGroup=ContosoConsumers
containerName=contosoresults
iotDeviceName=Contoso-Test-Device
# The IoT hub name must be globally unique, so add a random number to the end.
iotHubName=ContosoTestHub$RANDOM
echo "IoT hub name = " $iotHubName
# The storage account name must be globally unique, so add a random number to the end.
storageAccountName=contosostorage$RANDOM
echo "Storage account name = " $storageAccountName
# The Service Bus namespace must be globally unique, so add a random number to the end.
sbNameSpace=ContosoSBNamespace$RANDOM
echo "Service Bus namespace = " $sbNameSpace
# The Service Bus queue name must be globally unique, so add a random number to the end.
sbQueueName=ContosoSBQueue$RANDOM
echo "Service Bus queue name = " $sbQueueName
# Retrieve the information about the device identity, then copy the primary key to
# Notepad. You need this to run the device simulation during the testing phase.
az iot hub device-identity show --device-id $iotDeviceName \
--hub-name $iotHubName
PowerShell instructions
The easiest way to use this script is to open PowerShell ISE, copy the script to the clipboard, and then paste the
whole script into the script window. Then you can change the values for the resource names (if you wish), and
run the entire script.
# Set the values for the resource names that don't have to be globally unique.
# The resources that have to have unique names are named in the script below
# with a random number concatenated to the name so you can probably just
# run this script, and it will work with no conflicts.
$location = "West US"
$resourceGroup = "ContosoResources"
$iotHubConsumerGroup = "ContosoConsumers"
$containerName = "contosoresults"
$iotDeviceName = "Contoso-Test-Device"
# The IoT hub name must be globally unique, so add a random number to the end.
$iotHubName = "ContosoTestHub$(Get-Random)"
Write-Host "IoT hub name is " $iotHubName
# Add a consumer group to the IoT hub for the 'events' endpoint.
Add-AzureRmIotHubEventHubConsumerGroup -ResourceGroupName $resourceGroup `
-Name $iotHubName `
-EventHubConsumerGroupName $iotHubConsumerGroup `
-EventHubEndpointName "events"
# The storage account name must be globally unique, so add a random number to the end.
$storageAccountName = "contosostorage$(Get-Random)"
Write-Host "storage account name is " $storageAccountName
Next, create a device identity and save its key for later use. This device identity is used by the simulation
application to send messages to the IoT hub. This capability is not available in PowerShell, but you can create
the device in the Azure portal.
1. Open the Azure portal and log into your Azure account.
2. Click on Resource groups and select your resource group. This tutorial uses ContosoResources.
3. In the list of resources, click your IoT hub. This tutorial uses ContosoTestHub. Select IoT Devices from
the Hub pane.
4. Click + Add. On the Add Device pane, fill in the device ID. This tutorial uses Contoso-Test-Device.
Leave the keys empty, and check Auto Generate Keys. Make sure Connect device to IoT hub is
enabled. Click Save.
5. Now that it's been created, click on the device to see the generated keys. Click the Copy icon on the
Primary key and save it somewhere such as Notepad for the testing phase of this tutorial.
Set up message routing
You are going to route messages to different resources based on properties attached to the message by the
simulated device. Messages that are not custom routed are sent to the default endpoint (messages/events).
VALUE RESULT
Close the Routes pane, which returns you to the Resource group page.
Create a Logic App
The Service Bus queue is to be used for receiving messages designated as critical. Set up a Logic app to monitor
the Service Bus queue, and send an e-mail when a message is added to the queue.
1. In the Azure portal, click + Create a resource. Put logic app in the search box and click Enter. From the
search results displayed, select Logic App, then click Create to continue to the Create logic app pane.
Fill in the fields.
Name: This field is the name of the logic app. This tutorial uses ContosoLogicApp.
Subscription: Select your Azure subscription.
Resource group: Click Use existing and select your resource group. This tutorial uses
ContosoResources.
Location: Use your location. This tutorial uses West US.
Log Analytics: This toggle should be turned off.
Click Create.
2. Now go to the Logic App. The easiest way to get to the Logic App is to click on Resource groups, select
your resource group (this tutorial uses ContosoResources), then select the Logic App from the list of
resources. The Logic Apps Designer page appears (you might have to scroll over to the right to see the
full page). On the Logic Apps Designer page, scroll down until you see the tile that says Blank Logic
App + and click it.
3. A list of connectors is displayed. Select Service Bus.
4. A list of triggers is displayed. Select Service Bus - When a message is received in a queue (auto-
complete).
5. On the next screen, fill in the Connection Name. This tutorial uses ContosoConnection.
Click the Service Bus namespace. This tutorial uses ContosoSBNamespace. When you select the
namespace, the portal queries the Service Bus namespace to retrieve the keys. Select
RootManageSharedAccessKey and click Create.
6. On the next screen, select the name of the queue (this tutorial uses contososbqueue) from the
dropdown list. You can use the defaults for the rest of the fields.
7. Now set up the action to send an e-mail when a message is received in the queue. In the Logic Apps
Designer, click + New step to add a step, then click Add an action. In the Choose an action pane, find
and click Office 365 Outlook. On the triggers screen, select Office 365 Outlook - Send an email.
8. Next, log into your Office 365 account to set up the connection. Specify the e-mail addresses for the
recipient(s) of the e-mails. Also specify the subject, and type what message you'd like the recipient to see
in the body. For testing, fill in your own e-mail address as the recipient.
Click Add dynamic content to show the content from the message that you can include. Select
Content -- it will include the message in the e-mail.
9. Click Save. Then close the Logic App Designer.
4. Click Save.
5. Close the Query pane. This returns you to the view of the resources in the Resource Group. Click the
Stream Analytics job. This tutorial calls it contosoJob.
Run the Stream Analytics job
In the Stream Analytics job, click Start > Now > Start. Once the job successfully starts, the job status changes
from Stopped to Running.
To set up the Power BI report, you need data, so you'll set up Power BI after creating the device and running the
device simulation application.
Clean up resources
If you want to remove all of the resources you've created, delete the resource group. This action deletes all
resources contained within the group. In this case, it removes the IoT hub, the Service Bus namespace and
queue, the Logic App, the storage account, and the resource group itself.
Clean up resources in the Power BI visualization
Log into your Power BI account. Go to your workspace. This tutorial uses My Workspace. To remove the Power
BI visualization, go to DataSets and click the trash can icon to delete the dataset. This tutorial uses
contosodataset. When you remove the dataset, the report is removed as well.
Clean up resources using Azure CLI
To remove the resource group, use the az group delete command.
Next steps
In this tutorial, you learned how to use message routing to route IoT Hub messages to different destinations by
performing the following tasks.
Using Azure CLI or PowerShell, set up the base resources -- an IoT hub, a storage account, a Service Bus
queue, and a simulated device.
Configure endpoints and routes in IoT hub for the storage account and Service Bus queue.
Create a Logic App that is triggered and sends e-mail when a message is added to the Service Bus queue.
Download and run an app that simulates an IoT Device sending messages to the hub for the different
routing options.
Create a Power BI visualization for data sent to the default endpoint.
View the results ...
...in the Service Bus queue and e-mails.
...in the storage account.
...in the Power BI visualization.
Advance to the next tutorial to learn how to manage the state of an IoT device.
Configure your devices from a back-end service
Tutorial: Configure your devices from a back-end
service
5/30/2018 • 13 min to read • Edit Online
As well as receiving telemetry from your devices, you may need to configure your devices from your back-end
service. When you send a desired configuration to your devices, you may also want to receive status and
compliance updates from those devices. For example, you might set a target operational temperature range for a
device or collect firmware version information from your devices.
To synchronize state information between a device and an IoT hub, you use device twins. A device twin is a JSON
document, associated with a specific device, and stored by IoT Hub in the cloud where you can query them. A
device twin contains desired properties, reported properties, and tags. A desired property is set by a back-end
application and read by a device. A reported property is set by a device and read by a back-end application. A tag is
set by a back-end application and is never sent to a device. You use tags to organize your devices. This tutorial
shows you how to use desired and reported properties to synchronize state information:
If you don’t have an Azure subscription, create a free account before you begin.
Prerequisites
The two sample applications you run in this quickstart are written using Node.js. You need Node.js v4.x.x or later
on your development machine.
You can download Node.js for multiple platforms from nodejs.org.
You can verify the current version of Node.js on your development machine using the following command:
node --version
hubname=tutorial-iot-hub
location=centralus
# Create your free-tier IoT Hub. You can only have one free IoT Hub per subscription:
az iot hub create --name $hubname --location $location --resource-group tutorial-iot-hub-rg --sku S1
This tutorial uses a simulated device called MyTwinDevice. The following script adds this device to your identity
registry and retrieves its connection string:
# Set the name of your IoT hub:
hubname=tutorial-iot-hub
Create handlers
You can create handlers for desired property updates that respond to updates at different levels in the JSON
hierarchy. For example, this handler sees all desired property changes sent to the device from a back-end
application. The delta variable contains the desired properties sent from the solution back end:
The following handler only reacts to changes made to the fanOn desired property:
The local twin object stores a complete set of desired and reported properties. The delta sent from the back end
might update just a subset of desired properties.
Handle insert, update, and delete operations
The desired properties sent from the back end don't indicate what operation is being performed on a particular
desired property. Your code needs to infer the operation from the current set of desired properties stored locally
and the changes sent from the hub.
The following snippet shows how the simulated device handles insert, update, and delete operations on the list of
components in the desired properties. You can see how to use null values to indicate that a component should be
deleted:
// Keep track of all the components the device knows about
var componentList = {};
} else if (delta[key]) {
if (componentList[key]) {
// The delta contains a component, and the
// device has a record of it.
// Must be an update operation.
console.log(chalk.green('\nUpdating component ' + key + ':'));
console.log(JSON.stringify(delta[key]));
// Store the complete object instead of just the delta
componentList[key] = twin.properties.desired.components[key];
} else {
// The delta contains a component, and the
// device has no record of it.
// Must be an add operation.
console.log(chalk.green('\nAdding component ' + key + ':'));
console.log(JSON.stringify(delta[key]));
// Store the complete object instead of just the delta
componentList[key] = twin.properties.desired.components[key];
}
}
});
}
});
// Get the device twin and send desired property update patches at intervals.
// Print the reported properties after some of the desired property updates.
registry.getTwin(deviceId, async (err, twin) => {
if (err) {
console.error(err.message);
} else {
console.log('Got device twin');
The following snippet shows different desired property patches the back end application sends to the device:
// Turn the fan on
var twinPatchFanOn = {
properties: {
desired: {
patchId: "Switch fan on",
fanOn: "false",
}
}
};
npm install
node SimulatedDevice.js "{your device connection string}"
To run the back-end application, open another shell or command prompt window. Then navigate to the iot-
hub/Tutorials/DeviceTwins folder in the Node.js project you downloaded. Then run the following commands:
npm install
node ServiceClient.js "{your service connection string}"
The following screenshot shows the output from the simulated device application and highlights how it handles an
update to the maxTemperature desired property. You can see how both the top-level handler and the climate
component handlers run:
The following screenshot shows the output from the back-end application and highlights how it sends an update to
the maxTemperature desired property:
The simulated device uses the following function to send the patch that contains the reported properties to the
hub:
npm install
node SimulatedDevice.js "{your device connection string}"
To run the back-end application, open another shell or command prompt window. Then navigate to the iot-
hub/Tutorials/DeviceTwins folder in the Node.js project you downloaded. Then run the following commands:
npm install
node ServiceClient.js "{your service connection string}"
The following screenshot shows the output from the simulated device application and highlights how it sends a
reported property update to your hub:
The following screenshot shows the output from the back-end application and highlights how it receieves and
processes a reported property update from a device:
Clean up resources
If you plan to complete the next tutorial, leave the resource group and IoT hub and reuse them later.
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
tutorial-iot-hub-rg resource group that contains your IoT hub and click Delete.
Alternatively, use the CLI:
# Delete your resource group and its contents
az group delete --name tutorial-iot-hub-rg
Next steps
In this tutorial, you learned how to synchronize state information between your devices and your IoT hub by
performing the following tasks:
Create an IoT hub and add a test device to the identity registry.
Use desired properties to send state information to your simulated device.
Use reported properties to receive state information from your simulated device.
Advance to the next tutorial to learn how to use device twins to implement a firmware update process.
Use a simulated device to test connectivity with your IoT hub
Tutorial: Use a simulated device to test connectivity
with your IoT hub
5/30/2018 • 8 min to read • Edit Online
In this tutorial, you use Azure IoT Hub portal tools and Azure CLI commands to test device connectivity. This
tutorial also uses a simple device simulator that you run on your desktop machine.
If you don't have an Azure subscription, create a free account before you begin.
In this tutorial, you learn how to:
Check your device authentication
Check device-to-cloud connectivity
Check cloud-to-device connectivity
Check device twin synchronization
Prerequisites
The CLI scripts you run in this tutorial use the Microsoft Azure IoT Extension for Azure CLI 2.0. To install this
extension, run the following CLI command:
The device simulator application you run in this tutorial is written using Node.js. You need Node.js v4.x.x or later on
your development machine.
You can download Node.js for multiple platforms from nodejs.org.
You can verify the current version of Node.js on your development machine using the following command:
node --version
Download the sample device simulator Node.js project from https://github.com/Azure-Samples/iot-hub-tutorials-
node/archive/master.zip and extract the ZIP archive.
3. To create your free-tier IoT hub, use the values in the following tables:
SETTING VALUE
Resource group Create new. This tutorial uses the name tutorials-iot-hub-
rg.
Region This tutorial uses West US. You can choose the region
closest to you.
Pricing and scale tier F1 Free. You can only have one free tier hub in a
subscription.
4. Click Create. It can take several minutes for the hub to be created.
5. Make a note of the IoT hub name you chose. You use this value later in the tutorial.
To register a new device, click + Add, set Device ID to MyTestDevice, and click Save:
To retrieve the connection string for MyTestDevice, click on it in the list of devices and then copy the Connection
string-primary key value. The connection string includes the shared access key for the device.
To simulate MyTestDevice sending telemetry to your IoT hub, run the Node.js simulated device application you
downloaded previously.
In a terminal window on your development machine, navigate to the root folder of the sample Node.js project you
downloaded. Then navigate to the iot-hub\Tutorials\ConnectivityTests\simulated-device folder.
In the terminal window, run the following commands to install the required libraries and run the simulated device
application. Use the device connectin string you made a note of when you added the device in the portal.
npm install
node SimulatedDevice-1.js "{your device connection string}"
In the terminal window on your development machine, run the simulated device application again:
npm install
node SimulatedDevice-1.js "{your device connection string}"
This time you see an authentication error when the application tries to connect:
Generate shared access signature (SAS ) token
If your device uses one of the IoT Hub device SDKs, the SDK library code generates the SAS token used to
authenticate with the hub. A SAS token is generated from the name of your hub, the name of your device, and the
device key.
In some scenarios, such as in a cloud protocol gateway or as part of a custom authentication scheme, you may
need to generate the SAS token yourself. To troubleshoot issues with your SAS generation code, it's useful to be
able to generate a known-good SAS token to use during testing.
To generate a known-good SAS token using the CLI, run the following command:
Make a note of the full text of the generated SAS token. A SAS token looks like the following:
'SharedAccessSignature sr=tutorials-iot-hub.azure-devices.net%2Fdevices%2FMyTestDevice&sig=....&se=1524155307'
In a terminal window on your development machine, navigate to the root folder of the sample Node.js project you
downloaded. Then navigate to the iot-hub\Tutorials\ConnectivityTests\simulated-device folder.
In the terminal window, run the following commands to install the required libraries and run the simulated device
application:
npm install
node SimulatedDevice-2.js "{Your SAS token}"
The terminal window displays information as it tries to connect to your hub using the SAS token:
You've now successfully authenticated from a device using a test SAS token generated by a CLI command. The
SimulatedDevice-2.js file includes sample code that shows you how to generate a SAS token in code.
Protocols
A device can use any of the following protocols to connect to your IoT hub:
MQTT 8883
AMQP 5671
HTTPS 443
npm install
node SimulatedDevice-3.js "{your device connection string}"
Select your IoT hub in the Resource drop-down, select Telemetry messages sent as the metric, and set the time
range to Past hour. The chart shows the aggregate count of messages sent by the simulated device:
It takes a few minutes for the metrics to become available after you start the simulated device.
The simulated device prints a message to the console when it receives a direct method call:
When the simulated device successfully receives the direct method call, it sends an acknowledgement back to the
hub:
To verify that the hub received the reported properties from the device, use the following CLI command:
In the output from the command, you can see the devicelaststarted property in the reported properties section.
This property shows the date and time you last started the simulated device.
To verify that the hub can send desired property values to the device, use the following CLI command:
The simulated device prints a message when it receives a desired property update from the hub:
In addition to receiving desired property changes as they're made, the simulated device automatically checks for
desired properties when it starts up.
Clean up resources
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the
tutorials-iot-hub-rg resource group that contains your IoT hub and click Delete.
Next steps
In this tutorial, you've seen how to check your device keys, check device-to-cloud connectivity, check cloud-to-
device connectivity, and check device twin synchronization. To learn more about how to monitor your IoT hub, visit
the how -to article for IoT Hub monitoring.
Monitor with diagnostics
Send messages from the cloud to your device with
IoT Hub (.NET)
5/29/2018 • 6 min to read • Edit Online
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create an
IoT hub, provision a device identity in it, and code a device app that sends device-to-cloud messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device from
IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two .NET console apps:
SimulatedDevice, a modified version of the app created in Get started with IoT Hub, which connects to your
IoT hub and receives cloud-to-device messages.
SendCloudToDevice, which sends a cloud-to-device message to the device app through IoT Hub, and then
receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the IoT Hub developer guide.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received message: {0}",
Encoding.ASCII.GetString(receivedMessage.GetBytes()));
Console.ResetColor();
await deviceClient.CompleteAsync(receivedMessage);
}
}
The ReceiveAsync method asynchronously returns the received message at the time that it is received by the
device. It returns null after a specifiable timeout period (in this case, the default of one minute is used). When
the app receives a null, it should continue to wait for new messages. This requirement is the reason for the
if (receivedMessage == null) continue line.
The call to CompleteAsync() notifies IoT Hub that the message has been successfully processed. The
message can be safely removed from the device queue. If something happened that prevented the device
app from completing the processing of the message, IoT Hub delivers it again. It is then important that
message processing logic in the device app is idempotent, so that receiving the same message multiple
times produces the same result. An application can also temporarily abandon a message, which results in IoT
hub retaining the message in the queue for future consumption. Or, the application can reject a message,
which permanently removes the message from the queue. For more information about the cloud-to-device
message lifecycle, see the IoT Hub developer guide.
NOTE
When using HTTPS instead of MQTT or AMQP as a transport, the ReceiveAsync method returns immediately. The
supported pattern for cloud-to-device messages with HTTPS is intermittently connected devices that check for
messages infrequently (less than every 25 minutes). Issuing more HTTPS receives results in IoT Hub throttling the
requests. For more information about the differences between MQTT, AMQP and HTTPS support, and IoT Hub
throttling, see the IoT Hub developer guide.
2. Add the following method in the Main method, right before the Console.ReadLine() line:
ReceiveC2dAsync();
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Substitute the placeholder value with the IoT hub connection
string from Get started with IoT Hub:
This method sends a new cloud-to-device message to the device with the ID, myFirstDevice . Change this
parameter only if you modified it from the one used in Get started with IoT Hub.
7. Finally, add the following lines to the Main method:
Console.WriteLine("Send Cloud-to-Device message\n");
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
8. From within Visual Studio, right-click your solution, and select Set StartUp projects.... Select Multiple startup
projects, then select the Start action for ReadDeviceToCloudMessages, SimulatedDevice, and
SendCloudToDevice.
9. Press F5. All three applications should start. Select the SendCloudToDevice windows, and press Enter.
You should see the message being received by the device app.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received feedback: {0}", string.Join(", ", feedbackBatch.Records.Select(f =>
f.StatusCode)));
Console.ResetColor();
await feedbackReceiver.CompleteAsync(feedbackBatch);
}
}
Note this receive pattern is the same one used to receive cloud-to-device messages from the device app.
2. Add the following method in the Main method, right after the
serviceClient = ServiceClient.CreateFromConnectionString(connectionString) line:
ReceiveFeedbackAsync();
3. To request feedback for the delivery of your cloud-to-device message, you have to specify a property in the
SendCloudToDeviceMessageAsync method. Add the following line, right after the
var commandMessage = new Message(...); line:
commandMessage.Ack = DeliveryAcknowledgement.Full;
4. Run the apps by pressing F5. You should see all three applications start. Select the SendCloudToDevice
windows, and press Enter. You should see the message being received by the device app, and after a few
seconds, the feedback message being received by your SendCloudToDevice application.
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Send cloud-to-device messages with IoT Hub (Java)
6/6/2018 • 5 min to read • Edit Online
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create an
IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device from
IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Java console apps:
simulated-device, a modified version of the app created in Get started with IoT Hub, which connects to your
IoT hub and receives cloud-to-device messages.
send-c2d-messages, which sends a cloud-to-device message to the simulated device app through IoT Hub, and
then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the Azure IoT Developer Center.
return IotHubMessageResult.COMPLETE;
}
}
3. Modify the main method to create an AppMessageCallback instance and call the setMessageCallback
method before it opens the client as follows:
NOTE
If you use HTTPS instead of MQTT or AMQP as the transport, the DeviceClient instance checks for messages from
IoT Hub infrequently (less than every 25 minutes). For more information about the differences between MQTT, AMQP
and HTTPS support, and IoT Hub throttling, see the IoT Hub developer guide.
4. To build the simulated-device app using Maven, execute the following command at the command prompt
in the simulated-device folder:
NOTE
You can check for the latest version of iot-service-client using Maven search.
import com.microsoft.azure.sdk.iot.service.*;
import java.io.IOException;
import java.net.URISyntaxException;
7. Add the following class-level variables to the App class, replacing {yourhubconnectionstring} and
{yourdeviceid} with the values your noted earlier:
8. Replace the main method with the following code. This code connects to your IoT hub, sends a message to
your device, and then waits for an acknowledgment that the device received and processed the message:
if (serviceClient != null) {
serviceClient.open();
FeedbackReceiver feedbackReceiver = serviceClient
.getFeedbackReceiver();
if (feedbackReceiver != null) feedbackReceiver.open();
serviceClient.send(deviceId, messageToSend);
System.out.println("Message sent to device");
9. To build the simulated-device app using Maven, execute the following command at the command prompt
in the simulated-device folder:
2. At a command prompt in the send-c2d-messages folder, run the following command to send a cloud-to-
device message and wait for a feedback acknowledgment:
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create an
IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device from
IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Node.js console apps:
SimulatedDevice, a modified version of the app created in Get started with IoT Hub, which connects to your
IoT hub and receives cloud-to-device messages.
SendCloudToDeviceMessage, which sends a cloud-to-device message to the simulated device app through
IoT Hub, and then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the Azure IoT Developer Center.
NOTE
If you use HTTPS instead of MQTT or AMQP as the transport, the DeviceClient instance checks for messages from
IoT Hub infrequently (less than every 25 minutes). For more information about the differences between MQTT, AMQP
and HTTPS support, and IoT Hub throttling, see the IoT Hub developer guide.
npm init
2. At your command prompt in the sendcloudtodevicemessage folder, run the following command to install
the azure-iothub package:
'use strict';
5. Add the following code to SendCloudToDeviceMessage.js file. Replace the "{iot hub connection string}"
placeholder value with the IoT Hub connection string for the hub you created in the Get started with IoT
Hub tutorial. Replace the "{device id}" placeholder with the device ID of the device you added in the Get
started with IoT Hub tutorial:
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.constructor.name);
};
}
7. Add the following function to print delivery feedback messages to the console:
8. Add the following code to send a message to your device and handle the feedback message when the device
acknowledges the cloud-to-device message:
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
serviceClient.getFeedbackReceiver(receiveFeedback);
var message = new Message('Cloud to device message.');
message.ack = 'full';
message.messageId = "My Message ID";
console.log('Sending message: ' + message.getData());
serviceClient.send(targetDevice, message, printResultFor('send'));
}
});
node SimulatedDevice.js
2. At a command prompt in the sendcloudtodevicemessage folder, run the following command to send a
cloud-to-device message and wait for the acknowledgment feedback:
node SendCloudToDeviceMessage.js
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Send cloud-to-device messages with IoT Hub
(Python)
5/29/2018 • 6 min to read • Edit Online
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create an
IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device from
IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Python console apps:
SimulatedDevice.py, a modified version of the app created in Get started with IoT Hub, which connects to
your IoT hub and receives cloud-to-device messages.
SendCloudToDeviceMessage.py, which sends a cloud-to-device message to the simulated device app
through IoT Hub, and then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the Azure IoT Developer Center.
import time
import sys
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError
RECEIVE_CONTEXT = 0
WAIT_COUNT = 10
RECEIVED_COUNT = 0
RECEIVE_CALLBACKS = 0
3. Add the following code to SimulatedDevice.py file. Replace the "{deviceConnectionString}" placeholder
value with the device connection string for the device you created in the Get started with IoT Hub tutorial:
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
client.set_message_callback(receive_message_callback, RECEIVE_CONTEXT)
return client
def print_last_message_time(client):
try:
last_message = client.get_last_message_receive_time()
print ( "Last Message: %s" % time.asctime(time.localtime(last_message)) )
print ( "Actual time : %s" % time.asctime() )
except IoTHubClientError as iothub_client_error:
if iothub_client_error.args[0].result == IoTHubClientResult.INDEFINITE_TIME:
print ( "No message received" )
else:
print ( iothub_client_error )
5. Add the following code to initialize the client and wait to recieve the cloud-to-device message:
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
client.set_message_callback(receive_message_callback, RECEIVE_CONTEXT)
return client
def iothub_client_sample_run():
try:
client = iothub_client_init()
while True:
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
status = client.get_send_status()
print ( "Send status: %s" % status )
time.sleep(10)
status_counter += 1
print_last_message_time(client)
iothub_client_sample_run()
import random
import sys
import iothub_service_client
from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
OPEN_CONTEXT = 0
FEEDBACK_CONTEXT = 1
MESSAGE_COUNT = 1
AVG_WIND_SPEED = 10.0
MSG_TXT = "{\"service client sent a message\": %.2f}"
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
def open_complete_callback(context):
print ( 'open_complete_callback called with context: {0}'.format(context) )
5. Add the following code to send a message to your device and handle the feedback message when the device
acknowledges the cloud-to-device message:
def iothub_messaging_sample_run():
try:
iothub_messaging = IoTHubMessaging(CONNECTION_STRING)
iothub_messaging.open(open_complete_callback, OPEN_CONTEXT)
try:
# Try Python 2.xx first
raw_input("Press Enter to continue...\n")
except:
pass
# Use Python 3.xx in the case of exception
input("Press Enter to continue...\n")
iothub_messaging.close()
if __name__ == '__main__':
print ( "Starting the IoT Hub Service Client Messaging Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_messaging_sample_run()
2. At the command prompt, run the following command to listen for cloud-to-device messages:
python SimulatedDevice.py
3. Open a new command prompt and install the Azure IoT Hub Service SDK for Python.
4. At a command prompt, run the following command to send a cloud-to-device message and wait for the
message feedback:
python SendCloudToDeviceMessage.py
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Get started with device management (Node)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app can
work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a Node.js console app that calls the reboot direct method in the simulated device app through your IoT
hub.
At the end of this tutorial, you have two Node.js console apps:
dmpatterns_getstarted_device.js, which connects to your IoT hub with the device identity created earlier, receives
a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
dmpatterns_getstarted_service.js, which calls a direct method in the simulated device app, displays the response,
and displays the updated reported properties.
To complete this tutorial, you need the following:
Node.js version 4.0.x or later,
Prepare your development environment describes how to install Node.js for this tutorial on either Windows or
Linux.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the azure-
iot-device Device SDK package and azure-iot-device-mqtt package:
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the connection string with
your device connection string.
6. Add the following function to implement the direct method on the device
var onReboot = function(request, response) {
7. Open the connection to your IoT hub and start the direct method listener:
client.open(function(err) {
if (err) {
console.error('Could not open IotHub client');
} else {
console.log('Client opened. Waiting for reboot method.');
client.onDeviceMethod('reboot', onReboot);
}
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
npm init
2. At your command prompt in the triggerrebootondevice folder, run the following command to install the
azure-iothub Device SDK package and azure-iot-device-mqtt package:
'use strict';
5. Add the following variable declarations and replace the placeholder values:
6. Add the following function to invoke the device method to reboot the target device:
var methodParams = {
methodName: methodName,
payload: null,
timeoutInSeconds: 30
};
7. Add the following function to query for the device and get the last reboot time:
var queryTwinLastReboot = function() {
if (twin.properties.reported.iothubDM != null)
{
if (err) {
console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
} else {
var lastRebootTime = twin.properties.reported.iothubDM.reboot.lastReboot;
console.log('Last reboot time: ' + JSON.stringify(lastRebootTime, null, 2));
}
} else
console.log('Waiting for device to report last reboot time.');
});
};
8. Add the following code to call the functions that trigger the reboot direct method and query for the last
reboot time:
startRebootDevice();
setInterval(queryTwinLastReboot, 2000);
node dmpatterns_getstarted_device.js
2. At the command prompt in the triggerrebootondevice folder, run the following command to trigger the
remote reboot and query for the device twin to find the last reboot time.
node dmpatterns_getstarted_service.js
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported properties
to report the last reboot time from the device, and queried the device twin to discover the last reboot time of the
device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (.NET/Node)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app can
work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a .NET console app that calls the reboot direct method in the simulated device app through your IoT hub.
At the end of this tutorial, you have a Node.js console device app and a .NET (C#) console back-end app:
dmpatterns_getstarted_device.js, which connects to your IoT hub with the device identity created earlier, receives
a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
TriggerReboot, which calls a direct method in the simulated device app, displays the response, and displays the
updated reported properties.
To complete this tutorial, you need the following:
Visual Studio 2015 or Visual Studio 2017.
Node.js version 4.0.x or later,
Prepare your development environment describes how to install Node.js for this tutorial on either Windows or
Linux.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
2. In Solution Explorer, right-click the TriggerReboot project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the section "Create an IoT hub."
6. Add the following method to the Program class. This code gets the device twin for the rebooting device and
outputs the reported properties.
7. Add the following method to the Program class. This code initiates the reboot on the device using a direct
method.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartReboot().Wait();
QueryTwinRebootReported().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the azure-
iot-device Device SDK package and azure-iot-device-mqtt package:
3. Using a text editor, create a new dmpatterns_getstarted_device.js file in the manageddevice folder.
4. Add the following 'require' statements at the start of the dmpatterns_getstarted_device.js file:
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the connection string with
your device connection string.
6. Add the following function to implement the direct method on the device
var onReboot = function(request, response) {
7. Add the following code to open the connection to your IoT hub and start the direct method listener:
client.open(function(err) {
if (err) {
console.error('Could not open IotHub client');
} else {
console.log('Client opened. Waiting for reboot method.');
client.onDeviceMethod('reboot', onReboot);
}
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_getstarted_device.js
2. Run the C# console app TriggerReboot. Right-click the TriggerReboot project, select Debug, and then
select Start new instance.
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported properties
to report the last reboot time from the device, and queried the device twin to discover the last reboot time of the
device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (.NET/.NET)
5/29/2018 • 10 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app can
work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a .NET console app that calls the reboot direct method in the simulated device app through your IoT hub.
At the end of this tutorial, you have two .NET console apps:
SimulateManagedDevice, which connects to your IoT hub with the device identity created earlier, receives a
reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
TriggerReboot, which calls a direct method in the simulated device app, displays the response, and displays the
updated reported properties.
To complete this tutorial, you need the following:
Visual Studio 2015 or Visual Studio 2017.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
2. In Solution Explorer, right-click the TriggerReboot project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the section "Create an IoT hub."
6. Add the following method to the Program class. This code gets the device twin for the rebooting device and
outputs the reported properties.
7. Add the following method to the Program class. This code initiates the reboot on the device using a direct
method.
NOTE
This tutorial performs only a single query for the device's reported properties. In production code, we recommend polling to
detect changes in the reported properties.
2. In Solution Explorer, right-click the SimulateManagedDevice project, and then click Manage NuGet
Packages....
3. In the NuGet Package Manager window, select Browse and search for microsoft.azure.devices.client.
Select Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use. This
procedure downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the previous section.
7. Finally, add the following code to the Main method to open the connection to your IoT hub and initialize the
method listener:
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Exiting...");
8. In the Visual Studio Solution Explorer, right-click your solution, and then click Set StartUp Projects.... Select
Single startup project, and then select the SimulateManagedDevice project in the dropdown menu.
Build the solution.
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported properties
to report the last reboot time from the device, and queried the device twin to discover the last reboot time of the
device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (Java)
5/29/2018 • 12 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app can
work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that implements a direct method to reboot the device. Direct methods are
invoked from the cloud.
Create an app that invokes the reboot direct method in the simulated device app through your IoT hub. This app
then monitors the reported properties from the device to see when the reboot operation is complete.
At the end of this tutorial, you have two Java console apps:
simulated-device. This app:
Connects to your IoT hub with the device identity created earlier.
Receives a reboot direct method call.
Simulates a physical reboot.
Reports the time of the last reboot through a reported property.
trigger-reboot. This app:
Calls a direct method in the simulated device app.
Displays the response to the direct method call sent by the simulated device
Displays the updated reported properties.
NOTE
For information about the SDKs that you can use to build applications to run on devices and your solution back end, see
Azure IoT SDKs.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod;
import com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your IoT
hub connection string you noted in the Create an IoT Hub section:
10. To implement a thread that reads the reported properties from the device twin every 10 seconds, add the
following nested class to the App class:
12. To invoke the reboot direct method on the simulated device, add the following code to the main method:
System.out.println("Starting sample...");
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
try
{
System.out.println("Invoke reboot direct method");
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout,
null);
if(result == null)
{
throw new IOException("Invoke direct method reboot returns null");
}
System.out.println("Invoked reboot on device");
System.out.println("Status for device: " + result.getStatus());
System.out.println("Message from device: " + result.getPayload());
}
catch (IotHubException e)
{
System.out.println(e.getMessage());
}
13. To start the thread to poll the reported properties from the simulated device, add the following code to the
main method:
14. To enable you to stop the app, add the following code to the main method:
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.32</version>
</dependency>
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
8. Add the following class-level variables to the App class. Replace {yourdeviceconnectionstring} with the
device connection string you noted in the Create a device identity section:
private static final int METHOD_SUCCESS = 200;
private static final int METHOD_NOT_DEFINED = 404;
9. To implement a callback handler for direct method status events, add the following nested class to the App
class:
10. To implement a callback handler for device twin status events, add the following nested class to the App
class:
11. To implement a callback handler for property events, add the following nested class to the App class:
12. To implement a thread to simulate the device reboot, add the following nested class to the App class. The
thread sleeps for five seconds and then sets the lastReboot reported property:
protected static class RebootDeviceThread implements Runnable {
public void run() {
try {
System.out.println("Rebooting...");
Thread.sleep(5000);
Property property = new Property("lastReboot", LocalDateTime.now());
Set<Property> properties = new HashSet<Property>();
properties.add(property);
client.sendReportedProperties(properties);
System.out.println("Rebooted");
}
catch (Exception ex) {
System.out.println("Exception in reboot thread: " + ex.getMessage());
}
}
}
13. To implement the direct method on the device, add the following nested class to the App class. When the
simulated app receives a call to the reboot direct method, it returns an acknowledgement to the caller and
then starts a thread to process the reboot:
14. Modify the signature of the main method to throw the following exceptions:
15. To instantiate a DeviceClient, add the following code to the main method:
16. To start listening for direct method calls, add the following code to the main method:
try
{
client.open();
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(),
null);
client.startDeviceTwin(new DeviceTwinStatusCallback(), null, new PropertyCallback(), null);
System.out.println("Subscribed to direct methods and polling for reported properties. Waiting...");
}
catch (Exception e)
{
System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \n" +
e.getMessage());
client.close();
System.out.println("Shutting down...");
}
17. To shut down the device simulator, add the following code to the main method:
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported properties
to report the last reboot time from the device, and queried the device twin to discover the last reboot time of the
device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (Python)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app can
work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a Python console app that calls the reboot direct method in the simulated device app through your IoT
hub.
At the end of this tutorial, you have two Python console apps:
dmpatterns_getstarted_device.py, which connects to your IoT hub with the device identity created earlier,
receives a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
dmpatterns_getstarted_service.py, which calls a direct method in the simulated device app, displays the
response, and displays the updated reported properties.
To complete this tutorial, you need the following:
Python 2.x or 3.x. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted
during the installation, make sure to add Python to your platform-specific environment variable. If you are using
Python 2.x, you may need to install or upgrade pip, the Python package management system.
Install the azure-iothub-device-client package, using the command
pip install azure-iothub-device-client
Install the azure-iothub-service-client package, using the command
pip install azure-iothub-service-client
If you are using Windows OS, then Visual C++ redistributable package to allow the use of native DLLs from
Python.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
Create an IoT hub
Create an IoT hub for your simulated device app to connect to. The following steps show you how to complete this
task by using the Azure portal.
1. Sign in to the Azure portal.
2. Select Create a resource > Internet of Things > IoT Hub.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
import random
import time, datetime
import sys
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult,
IoTHubError, DeviceMethodReturnValue
3. Add variables including a CONNECTION_STRING variable and the client intialization. Replace the
connection string with your device connection string.
CONNECTION_STRING = "{deviceConnectionString}"
PROTOCOL = IoTHubTransportProvider.MQTT
WAIT_COUNT = 5
SEND_REPORTED_STATE_CONTEXT = 0
METHOD_CONTEXT = 0
SEND_REPORTED_STATE_CALLBACKS = 0
METHOD_CALLBACKS = 0
4. Add the following function callbacks to implement the direct method on the device.
if method_name == "rebootDevice":
print ( "Rebooting device..." )
time.sleep(20)
current_time = str(datetime.datetime.now())
reported_state = "{\"rebootTime\":\"" + current_time + "\"}"
CLIENT.send_reported_state(reported_state, len(reported_state), send_reported_state_callback,
SEND_REPORTED_STATE_CONTEXT)
device_method_return_value = DeviceMethodReturnValue()
device_method_return_value.response = "{ \"Response\": \"This is the response from the device\" }"
device_method_return_value.status = 200
return device_method_return_value
def iothub_client_init():
if CLIENT.protocol == IoTHubTransportProvider.MQTT or client.protocol ==
IoTHubTransportProvider.MQTT_WS:
CLIENT.set_device_method_callback(device_method_callback, METHOD_CONTEXT)
def iothub_client_sample_run():
try:
iothub_client_init()
while True:
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
time.sleep(10)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Python sample..." )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_client_sample_run()
3. Add the following variable declarations. Only replace placeholder values for IoTHubConnectionString and
deviceId.
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
METHOD_NAME = "rebootDevice"
METHOD_PAYLOAD = "{\"method_number\":\"42\"}"
TIMEOUT = 60
WAIT_COUNT = 10
4. Add the following function to invoke the device method to reboot the target device, then query for the
device twins and get the last reboot time.
def iothub_devicemethod_sample_run():
try:
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
print ( "" )
print ( "Invoking device to reboot..." )
print ( "" )
print ( "Successfully invoked the device to reboot." )
print ( "" )
print ( response.payload )
while True:
print ( "" )
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
twin_info = iothub_twin_method.get_twin(DEVICE_ID)
if twin_info.find("rebootTime") != -1:
print ( "Last reboot time: " +
twin_info[twin_info.find("rebootTime")+11:twin_info.find("rebootTime")+37])
else:
print ("Waiting for device to report last reboot time...")
time.sleep(5)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Service Client DeviceManagement Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_devicemethod_sample_run()
python dmpatterns_getstarted_device.py
2. At another command prompt, run the following command to trigger the remote reboot and query for the
device twin to find the last reboot time.
python dmpatterns_getstarted_service.py
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported properties
to report the last reboot time from the device, and queried the device twin to discover the last reboot time of the
device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Use device management to initiate a device firmware
update (Node/Node)
5/29/2018 • 10 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and provides guidance and
shows you how to do an end-to-end simulated firmware update. This pattern is used in the firmware update
implementation for the Intel Edison device sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the triggerfwupdateondevice folder, run the following command to install
the azure-iot-hub package:
'use strict';
5. Add the following variable declarations and replace the placeholder values:
6. Add the following function to find and display the value of the firmwareUpdate reported property.
var queryTwinFWUpdateReported = function() {
registry.getTwin(deviceToUpdate, function(err, twin){
if (err) {
console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
} else {
console.log((JSON.stringify(twin.properties.reported.iothubDM.firmwareUpdate)) + "\n");
}
});
};
7. Add the following function to invoke the firmwareUpdate method to reboot the target device:
var methodParams = {
methodName: methodName,
payload: payloadData,
timeoutInSeconds: 30
};
8. Finally, Add the following function to code to start the firmware update sequence and start periodically
showing the reported properties:
startFirmwareUpdateDevice();
setInterval(queryTwinFWUpdateReported, 500);
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the azure-
iot-device and azure-iot-device-mqtt Device SDK packages:
npm install azure-iot-device azure-iot-device-mqtt --save
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the
{yourdeviceconnectionstring} placeholder with the connection string you previously made a note of in the
"Create a device identity" section previously:
twin.properties.reported.update(patch, function(err) {
if (err) throw err;
console.log('twin state reported: ' + firmwareUpdateValue.status);
});
};
7. Add the following functions that simulate downloading and applying the firmware image:
callback(error, image);
}
if (!imageData) {
error = {message: 'Apply image failed because of missing image data.'};
}
callback(error);
}
8. Add the following function that updates the firmware update status through the reported properties to
waiting. Typically, devices are informed of an available update and an administrator defined policy causes
the device to start downloading and applying the update. This function is where the logic to enable that
policy should run. For simplicity, the sample waits for four seconds before proceeding to download the
firmware image:
reportFWUpdateThroughTwin(twin, {
fwPackageUri: fwPackageUriVal,
status: 'waiting',
error : null,
startedWaitingTime : now.toISOString()
});
setTimeout(callback, 4000);
};
9. Add the following function that updates the firmware update status through the reported properties to
downloading. The function then simulates a firmware download and finally updates the firmware update
status to either downloadFailed or downloadComplete:
reportFWUpdateThroughTwin(twin, {
status: 'downloading',
});
setTimeout(function() {
// Simulate download
simulateDownloadImage(fwPackageUriVal, function(err, image) {
if (err)
{
reportFWUpdateThroughTwin(twin, {
status: 'downloadfailed',
error: {
code: error_code,
message: error_message,
}
});
}
else {
reportFWUpdateThroughTwin(twin, {
status: 'downloadComplete',
downloadCompleteTime: now.toISOString(),
});
}, 4000);
}
10. Add the following function that updates the firmware update status through the reported properties to
applying. The function then simulates applying the firmware image and finally updates the firmware update
status to either applyFailed or applyComplete:
var applyImage = function(twin, imageData, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
status: 'applying',
startedApplyingImage : now.toISOString()
});
setTimeout(function() {
}
});
setTimeout(callback, 4000);
}, 4000);
}
11. Add the following function that handles the firmwareUpdate direct method and initiates the multi-stage
firmware update process:
var onFirmwareUpdate = function(request, response) {
}
});
}
12. Finally, add the following code that connects to your IoT hub:
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client');
} else {
console.log('Client connected to IoT Hub. Waiting for firmwareUpdate direct method.');
}
client.onDeviceMethod('firmwareUpdate', onFirmwareUpdate);
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_fwupdate_device.js
2. At the command prompt in the triggerfwupdateondevice folder, run the following command to trigger the
remote reboot and query for the device twin to find the last reboot time.
node dmpatterns_fwupdate_service.js
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device firmware
update (.NET/Node)
5/29/2018 • 10 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do an
end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
2. In Solution Explorer, right-click the TriggerFWUpdate project, and then click Manage NuGet Packages....
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the multiple placeholder values with the IoT Hub
connection string for the hub that you created in the previous section and the Id of your device.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartFirmwareUpdate().Wait();
QueryTwinFWUpdateReported().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
9. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for TriggerFWUpdate
project is Start.
10. Build the solution.
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the azure-
iot-device and azure-iot-device-mqtt Device SDK packages:
'use strict';
twin.properties.reported.update(patch, function(err) {
if (err) throw err;
console.log('twin state reported: ' + firmwareUpdateValue.status);
});
};
7. Add the following functions that simulate downloading and applying the firmware image:
callback(error, image);
}
if (!imageData) {
error = {message: 'Apply image failed because of missing image data.'};
}
callback(error);
}
8. Add the following function that updates the firmware update status through the reported properties to
waiting. Typically, devices are informed of an available update and an administrator defined policy causes
the device to start downloading and applying the update. This function is where the logic to enable that
policy should run. For simplicity, the sample waits for four seconds before proceeding to download the
firmware image:
var waitToDownload = function(twin, fwPackageUriVal, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
fwPackageUri: fwPackageUriVal,
status: 'waiting',
error : null,
startedWaitingTime : now.toISOString()
});
setTimeout(callback, 4000);
};
9. Add the following function that updates the firmware update status through the reported properties to
downloading. The function then simulates a firmware download and finally updates the firmware update
status to either downloadFailed or downloadComplete:
reportFWUpdateThroughTwin(twin, {
status: 'downloading',
});
setTimeout(function() {
// Simulate download
simulateDownloadImage(fwPackageUriVal, function(err, image) {
if (err)
{
reportFWUpdateThroughTwin(twin, {
status: 'downloadfailed',
error: {
code: error_code,
message: error_message,
}
});
}
else {
reportFWUpdateThroughTwin(twin, {
status: 'downloadComplete',
downloadCompleteTime: now.toISOString(),
});
}, 4000);
}
10. Add the following function that updates the firmware update status through the reported properties to
applying. The function then simulates applying the firmware image and finally updates the firmware update
status to either applyFailed or applyComplete:
var applyImage = function(twin, imageData, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
status: 'applying',
startedApplyingImage : now.toISOString()
});
setTimeout(function() {
}
});
setTimeout(callback, 4000);
}, 4000);
}
11. Add the following function that handles the firmwareUpdate direct method and initiates the multi-stage
firmware update process:
var onFirmwareUpdate = function(request, response) {
}
});
}
12. Finally, add the following code that connects to your IoT hub:
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client');
} else {
console.log('Client connected to IoT Hub. Waiting for firmwareUpdate direct method.');
}
client.onDeviceMethod('firmwareUpdate', onFirmwareUpdate);
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_fwupdate_device.js
2. In Visual Studio, right-click on the TriggerFWUpdate project, select Debug and Start new instance.
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device firmware
update (.NET/.NET)
6/4/2018 • 12 min to read • Edit Online
Introduction
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do an
end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
2. In Solution Explorer, right-click the TriggerFWUpdate project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the multiple placeholder values with the IoT Hub
connection string for the hub that you created in the previous section and the ID of your device.
6. Add the following method to the Program class. This method polls the device twin for updated status every
500 milliseconds. It writes to the console only when status has actually changed. For this sample, to prevent
consuming extra IoT Hub messages in your subscription, polling stops when the device reports a status of
applyComplete or an error.
public static async Task QueryTwinFWUpdateReported(DateTime startTime)
{
DateTime lastUpdated = startTime;
while (true)
{
Twin twin = await registryManager.GetTwinAsync(targetDevice);
8. Finally, add the following lines to the Main method. This creates a registry manager to read the device twin
with, starts the polling task on a worker thread, and then triggers the firmware update.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartFirmwareUpdate().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
2. In Solution Explorer, right-click the SimulateDeviceFWUpdate project, and then click Manage NuGet
Packages.
3. In the NuGet Package Manager window, select Browse and search for microsoft.azure.devices.client.
Select Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use. This
procedure downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Newtonsoft.Json.Linq;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the Create a device identity section.
static string DeviceConnectionString = "HostName=<yourIotHubName>.azure-devices.net;DeviceId=
<yourIotDeviceName>;SharedAccessKey=<yourIotDeviceAccessKey>";
static DeviceClient Client = null;
6. Add the following method to report status back to the cloud through the device twin:
iothubDM["firmwareUpdate"] = fwUpdateValue;
patch["iothubDM"] = iothubDM;
await Client.UpdateReportedPropertiesAsync(patch);
Console.WriteLine("Twin state reported: {0}", fwUpdateValue["status"]);
}
catch
{
Console.WriteLine("Error updating device twin");
throw;
}
}
await Task.Delay(4000);
return Encoding.ASCII.GetBytes(image);
8. Add the following method to simulate applying the firmware image to the device:
await Task.Delay(4000);
9. Add the following method to simulate waiting to download the firmware image. Update status to waiting
and clear other firmware update properties on the twin. These properties are cleared to remove any existing
values from prior firmware updates. This is necessary because reported properties are sent as a PATCH
operation (a delta) and not a PUT operation (a complete set of properties that replaces all of the previous
values). Typically, devices are informed of an available update and an administrator defined policy causes the
device to start downloading and applying the update. This function is where the logic to enable that policy
should run.
await Task.Delay(2000);
}
10. Add the following method to perform the download. It updates the status to downloading through the
device twin, calls the simulate download method, and reports a status of downloadComplete or
downloadFailed through the twin depending on the results of the download operation.
11. Add the following method to apply the image. It updates the status to applying through the device twin,
calls the simulate apply image method, and updates status to applyComplete or applyFailed through the
twin depending on the results of the apply operation.
static async Task applyImage(Twin twin, byte[] imageData)
{
try
{
TwinCollection statusUpdate = new TwinCollection();
statusUpdate["status"] = "applying";
statusUpdate["startedApplyingImage"] = DateTime.Now;
await reportFwUpdateThroughTwin(twin, statusUpdate);
await simulateApplyImage(imageData);
12. Add the following method to sequence the firmware update operation from waiting to download the image
through applying the image to the device:
13. Add the following method to handle the updateFirmware direct method from the cloud. It extracts the URL
to the firmware update from the message payload and passes it to the doUpdate task, which it starts on
another threadpool thread. It then immediately returns the method response to the cloud.
14. Finally, add the following code to the Main method to open the connection to your IoT hub and initialize the
method listener:
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Exiting...");
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do an
end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.5.22</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod;
import com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your
IoT hub connection string you noted in the Create an IoT Hub section:
10. To implement a method that reads the reported properties from the device twin, add the following to the
App class:
public static void ShowReportedProperties()
{
try
{
DeviceTwin deviceTwins = DeviceTwin.createFromConnectionString(iotHubConnectionString);
DeviceTwinDevice twinDevice = new DeviceTwinDevice(deviceId);
while (!firmwareUpdated)
{
if (timeoutCycle > 5)
{
System.out.println("Operation timed out");
break;
}
Thread.sleep(1000);
deviceTwins.getTwin(twinDevice);
if(reportedProperties.contains("status=waiting"))
{
System.out.println("Waiting on device...");
}
else if(reportedProperties.contains("status=downloadComplete"))
{
System.out.println("Download complete, applying firmware...");
}
else if (reportedProperties.contains("status=applyComplete"))
{
System.out.println("Firmware applied");
System.out.println("Get reported properties from device twin");
System.out.println(twinDevice.reportedPropertiesToString());
firmwareUpdated = true;
}
else
{
timeoutCycle++;
}
}
} catch (Exception ex) {
System.out.println("Exception reading reported properties: " + ex.getMessage());
}
}
11. Modify the signature of the main method to throw the following exceptions:
12. To invoke the firmwareUpdate direct method on the simulated device, add the following code to the main
method:
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
try
{
String payload = "https://someurl";
if(result == null)
{
throw new IOException("Invoke direct method reboot returns null");
}
13. To poll the reported properties from the simulated device, add the following code to the main method:
ShowReportedProperties();
14. To enable you to stop the app, add the following code to the main method:
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;
8. Add the following class-level variables to the App class. Replace {yourdeviceconnectionstring} with your
device connection string you noted in the Create a Device Identity section:
9. To implement direct method functionality, provide callbacks by adding the following nested classes to the
App class:
10. To implement device twin functionality, provide callbacks by adding the following nested classes to the App
class:
11. To implement the firmware update, add the following nested class to the App class:
protected static class FirmwareUpdateThread implements Runnable {
public void run() {
try {
HashMap initialUpdate = new HashMap();
Property sentProperty = new Property("firmwareUpdate", initialUpdate);
Set<Property> sentPackage = new HashSet<Property>();
initialUpdate.put("status", "waiting");
initialUpdate.put("fwPackageUri", downloadURL);
initialUpdate.put("startedWaitingTime", LocalDateTime.now().toString());
sentPackage.add(sentProperty);
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
System.out.println("Download complete");
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
System.out.println("Apply complete");
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
client.sendReportedProperties(sentPackage);
}
catch (Exception ex) {
System.out.println("Exception in reboot thread: " + ex.getMessage());
}
}
}
12. Modify the signature of the main method to throw the following exceptions:
13. To initiate the direct methods and device twins routine, add the following code to the main method:
client = new DeviceClient(connString, protocol);
try
{
client.open();
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(),
null);
client.startDeviceTwin(new DeviceTwinStatusCallback(), null, new PropertyCallback(), null);
System.out.println("Client connected to IoT Hub. Waiting for firmwareUpdate direct method.");
}
catch (Exception e)
{
System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \n" +
e.getMessage());
client.close();
System.out.println("Shutting down...");
}
14. To enable you to stop the app, add the following code to the end of the main method:
2. At a command prompt in the firmware-update folder, run the following command to invoke the firmware
update and query the device twins on your simulated device from your IoT hub:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
3. You can see the simulated device responding to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device firmware
update (Python/Python)
5/29/2018 • 9 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and provides guidance and
shows you how to do an end-to-end simulated firmware update. This pattern is used in the firmware update
implementation for the Intel Edison device sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
import sys
import time
import iothub_service_client
from iothub_service_client import IoTHubDeviceTwin, IoTHubDeviceMethod, IoTHubError
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
METHOD_NAME = "firmwareUpdate"
METHOD_PAYLOAD = "{\"fwPackageUri\":\"test.com\"}"
TIMEOUT = 60
MESSAGE_COUNT = 5
4. Add the following function to call the direct method and display the value of the firmwareUpdate reported
property. Also add the main routine:
def iothub_firmware_sample_run():
try:
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
print ( "" )
print ( "Direct Method called." )
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
print ( "" )
print ( "Device Twin queried, press Ctrl-C to exit" )
while True:
twin_info = iothub_twin_method.get_twin(DEVICE_ID)
if "\"firmwareStatus\":\"standBy\"" in twin_info:
print ( "Waiting on device..." )
elif "\"firmwareStatus\":\"downloading\"" in twin_info:
print ( "Downloading firmware..." )
elif "\"firmwareStatus\":\"applying\"" in twin_info:
print ( "Download complete, applying firmware..." )
elif "\"firmwareStatus\":\"completed\"" in twin_info:
print ( "Firmware applied" )
print ( "" )
print ( "Get reported properties from device twin:" )
print ( twin_info )
break
else:
print ( "Unknown status" )
status_counter = 0
while status_counter <= MESSAGE_COUNT:
time.sleep(1)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub firmware update Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_firmware_sample_run()
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubError, DeviceMethodReturnValue
SEND_REPORTED_STATE_CONTEXT = 0
METHOD_CONTEXT = 0
MESSAGE_COUNT = 10
PROTOCOL = IoTHubTransportProvider.MQTT
CONNECTION_STRING = "{deviceConnectionString}"
CLIENT = IoTHubClient(CONNECTION_STRING, PROTOCOL)
4. Add the following functions that are used to provide reported properties updates and implement the direct
method:
device_method_return_value = DeviceMethodReturnValue()
device_method_return_value.response = "{ \"Response\": \"Firmware update started\" }"
device_method_return_value.status = 200
return device_method_return_value
5. Add the following functions that simulate downloading and applying the firmware image:
def simulate_download_image(image_url):
time.sleep(15)
print ( "Downloading image from: " + image_url )
simulate_apply_image(image_url)
def simulate_apply_image(image_url):
print ( "Applying image from: " + image_url )
simulate_complete_image()
def simulate_complete_image():
print ( "Image applied." )
6. Add the following function that initializes the device twin's reported properties and wait for the direct
method to be called. Also add the main routine:
def iothub_firmware_sample_run():
try:
CLIENT.set_device_method_callback(device_method_callback, METHOD_CONTEXT)
while True:
status_counter = 0
while status_counter <= MESSAGE_COUNT:
time.sleep(10)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Python firmware update sample..." )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_firmware_sample_run()
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
python dmpatterns_fwupdate_device.py
2. At another command prompt, run the following command to trigger the remote reboot and query for the
device twin to find the last reboot time.
python dmpatterns_fwupdate_service.py
3. You see the device response to the direct method in the console. Then note the change in reported
properties throughout the firmware update.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Manage your IoT Hub device identities in bulk
5/29/2018 • 11 min to read • Edit Online
Each IoT hub has an identity registry you can use to create per-device resources in the service. The identity
registry also enables you to control access to the device-facing endpoints. This article describes how to import
and export device identities in bulk to and from an identity registry.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Import and export operations take place in the context of Jobs that enable you to execute bulk service operations
against an IoT hub.
The RegistryManager class includes the ExportDevicesAsync and ImportDevicesAsync methods that use the
Job framework. These methods enable you to export, import, and synchronize the entirety of an IoT hub identity
registry.
This topic discusses using the RegistryManager class and Job system to perform bulk imports and exports of
devices to and from an IoT hub’s identity registry. You can also use the Azure IoT Hub Device Provisioning
Service to enable zero-touch, just-in-time provisioning to one or more IoT hubs without requiring human
intervention. To learn more, see the provisioning service documentation.
NOTE
To use the RegistryManager class in your C# code, add the Microsoft.Azure.Devices NuGet package to your project. The
RegistryManager class is in the Microsoft.Azure.Devices namespace.
You can use the RegistryManager class to query the state of the Job using the returned JobProperties
metadata. To create an instance of the RegistryManager class, use the CreateFromConnectionString method:
await Task.Delay(TimeSpan.FromSeconds(5));
}
Export devices
Use the ExportDevicesAsync method to export the entirety of an IoT hub identity registry to an Azure Storage
blob container using a Shared Access Signature.
This method enables you to create reliable backups of your device information in a blob container that you
control.
The ExportDevicesAsync method requires two parameters:
A string that contains a URI of a blob container. This URI must contain a SAS token that grants write access
to the container. The job creates a block blob in this container to store the serialized export device data. The
SAS token must include these permissions:
SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read |
SharedAccessBlobPermissions.Delete
A boolean that indicates if you want to exclude authentication keys from your export data. If false,
authentication keys are included in export output. Otherwise, keys are exported as null.
The following C# code snippet shows how to initiate an export job that includes device authentication keys in the
export data and then poll for completion:
// Call an export job on the IoT Hub to retrieve all devices
JobProperties exportJob = await registryManager.ExportDevicesAsync(containerSasUri, false);
await Task.Delay(TimeSpan.FromSeconds(5));
}
The job stores its output in the provided blob container as a block blob with the name devices.txt. The output
data consists of JSON serialized device data, with one device per line.
The following example shows the output data:
{"id":"Device1","eTag":"MA==","status":"enabled","authentication":{"symmetricKey":
{"primaryKey":"abc=","secondaryKey":"def="}}}
{"id":"Device2","eTag":"MA==","status":"enabled","authentication":{"symmetricKey":
{"primaryKey":"abc=","secondaryKey":"def="}}}
{"id":"Device3","eTag":"MA==","status":"disabled","authentication":{"symmetricKey":
{"primaryKey":"abc=","secondaryKey":"def="}}}
{"id":"Device4","eTag":"MA==","status":"disabled","authentication":{"symmetricKey":
{"primaryKey":"abc=","secondaryKey":"def="}}}
{"id":"Device5","eTag":"MA==","status":"enabled","authentication":{"symmetricKey":
{"primaryKey":"abc=","secondaryKey":"def="}}}
If a device has twin data, then the twin data are also exported together with the device data. The following
example shows this format. All data from the "twinETag" line until the end are twin data.
{
"id":"export-6d84f075-0",
"eTag":"MQ==",
"status":"enabled",
"statusReason":"firstUpdate",
"authentication":null,
"twinETag":"AAAAAAAAAAI=",
"tags":{
"Location":"LivingRoom"
},
"properties":{
"desired":{
"Thermostat":{
"Temperature":75.1,
"Unit":"F"
},
"$metadata":{
"$lastUpdated":"2017-03-09T18:30:52.3167248Z",
"$lastUpdatedVersion":2,
"Thermostat":{
"$lastUpdated":"2017-03-09T18:30:52.3167248Z",
"$lastUpdatedVersion":2,
"Temperature":{
"$lastUpdated":"2017-03-09T18:30:52.3167248Z",
"$lastUpdatedVersion":2
},
"Unit":{
"$lastUpdated":"2017-03-09T18:30:52.3167248Z",
"$lastUpdatedVersion":2
}
}
},
"$version":2
},
"reported":{
"$metadata":{
"$lastUpdated":"2017-03-09T18:30:51.1309437Z"
},
"$version":1
}
}
}
If you need access to this data in code, you can easily deserialize this data using the ExportImportDevice class.
The following C# code snippet shows how to read device information that was previously exported to a block
blob:
Import devices
The ImportDevicesAsync method in the RegistryManager class enables you to perform bulk import and
synchronization operations in an IoT hub identity registry. Like the ExportDevicesAsync method, the
ImportDevicesAsync method uses the Job framework.
Take care using the ImportDevicesAsync method because in addition to provisioning new devices in your
identity registry, it can also update and delete existing devices.
WARNING
An import operation cannot be undone. Always back up your existing data using the ExportDevicesAsync method to
another blob container before you make bulk changes to your identity registry.
SharedAccessBlobPermissions.Read
A string that contains a URI of an Azure Storage blob container to use as output from the job. The job
creates a block blob in this container to store any error information from the completed import Job. The
SAS token must include these permissions:
SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read |
SharedAccessBlobPermissions.Delete
NOTE
The two parameters can point to the same blob container. The separate parameters simply enable more control over your
data as the output container requires additional permissions.
This method can also be used to import the data for the device twin. The format for the data input is the same as
the format shown in the ExportDevicesAsync section. In this way, you can reimport the exported data. The
$metadata is optional.
Import behavior
You can use the ImportDevicesAsync method to perform the following bulk operations in your identity registry:
Bulk registration of new devices
Bulk deletions of existing devices
Bulk status changes (enable or disable devices)
Bulk assignment of new device authentication keys
Bulk auto-regeneration of device authentication keys
Bulk update of twin data
You can perform any combination of the preceding operations within a single ImportDevicesAsync call. For
example, you can register new devices and delete or update existing devices at the same time. When used along
with the ExportDevicesAsync method, you can completely migrate all your devices from one IoT hub to another.
If the import file includes twin metadata, then this metadata overwrites the existing twin metadata. If the import
file does not include twin metadata, then only the lastUpdateTime metadata is updated using the current time.
Use the optional importMode property in the import serialization data for each device to control the import
process per-device. The importMode property has the following options:
IMPORTMODE DESCRIPTION
createOrUpdate If a device does not exist with the specified id, it is newly
registered.
If the device already exists, existing information is overwritten
with the provided input data without regard to the ETag
value.
The user can optionally specify twin data along with the
device data. The twin’s etag, if specified, is processed
independently from the device’s etag. If there is a mismatch
with the existing twin’s etag, an error is written to the log file.
create If a device does not exist with the specified id, it is newly
registered.
If the device already exists, an error is written to the log file.
The user can optionally specify twin data along with the
device data. The twin’s etag, if specified, is processed
independently from the device’s etag. If there is a mismatch
with the existing twin’s etag, an error is written to the log file.
createOrUpdateIfMatchETag If a device does not exist with the specified id, it is newly
registered.
If the device already exists, existing information is overwritten
with the provided input data only if there is an ETag match.
If there is an ETag mismatch, an error is written to the log file.
The user can optionally specify twin data along with the
device data. The twin’s etag, if specified, is processed
independently from the device’s etag. If there is a mismatch
with the existing twin’s etag, an error is written to the log file.
deleteIfMatchETag If a device already exists with the specified id, it is deleted only
if there is an ETag match. If the device does not exist, an error
is written to the log file.
If there is an ETag mismatch, an error is written to the log file.
NOTE
If the serialization data does not explicitly define an importMode flag for a device, it defaults to createOrUpdate during
the import operation.
await Task.Delay(TimeSpan.FromSeconds(5));
}
// Update property
device.ImportMode = ImportMode.Delete;
// Re-serialize
sb.AppendLine(JsonConvert.SerializeObject(device));
});
// Step 2: Write the new import data back to the block blob
await blob.DeleteIfExistsAsync();
using (CloudBlobStream stream = await blob.OpenWriteAsync())
{
byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
for (var i = 0; i < bytes.Length; i += 500)
{
int length = Math.Min(bytes.Length - i, 500);
await stream.WriteAsync(bytes, i, length);
}
}
// Step 3: Call import using the same blob to delete all devices
importJob = await registryManager.ImportDevicesAsync(containerSasUri, containerSasUri);
await Task.Delay(TimeSpan.FromSeconds(5));
}
Next steps
In this article, you learned how to perform bulk operations against the identity registry in an IoT hub. Follow these
links to learn more about managing Azure IoT Hub:
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
To explore using the IoT Hub Device Provisioning Service to enable zero-touch, just-in-time provisioning, see:
Azure IoT Hub Device Provisioning Service
Overview of device management with IoT Hub
5/29/2018 • 5 min to read • Edit Online
Azure IoT Hub provides the features and an extensibility model that enable device and back-end developers to
build robust device management solutions. Devices range from constrained sensors and single purpose
microcontrollers, to powerful gateways that route communications for groups of devices. In addition, the use cases
and requirements for IoT operators vary significantly across industries. Despite this variation, device management
with IoT Hub provides the capabilities, patterns, and code libraries to cater to a diverse set of devices and end
users.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
A crucial part of creating a successful enterprise IoT solution is to provide a strategy for how operators handle the
ongoing management of their collection of devices. IoT operators require simple and reliable tools and
applications that enable them to focus on the more strategic aspects of their jobs. This article provides:
A brief overview of Azure IoT Hub approach to device management.
A description of common device management principles.
A description of the device lifecycle.
An overview of common device management patterns.
Scale and automation: IoT solutions require simple tools that can automate routine tasks and enable a
relatively small operations staff to manage millions of devices. Day-to-day, operators expect to handle device
operations remotely, in bulk, and to only be alerted when issues arise that require their direct attention.
Openness and compatibility: The device ecosystem is extraordinarily diverse. Management tools must be
tailored to accommodate a multitude of device classes, platforms, and protocols. Operators must be able to
support many types of devices, from the most constrained embedded single-process chips, to powerful and
fully functional computers.
Context awareness: IoT environments are dynamic and ever-changing. Service reliability is paramount. Device
management operations must take into account the following factors to ensure that maintenance downtime
doesn't affect critical business operations or create dangerous conditions:
SL A maintenance windows
Network and power states
In-use conditions
Device geolocation
Service many roles: Support for the unique workflows and processes of IoT operations roles is crucial. The
operations staff must work harmoniously with the given constraints of internal IT departments. They must also
find sustainable ways to surface realtime device operations information to supervisors and other business
managerial roles.
Device lifecycle
There is a set of general device management stages that are common to all enterprise IoT projects. In Azure IoT,
there are five stages within the device lifecycle:
Within each of these five stages, there are several device operator requirements that should be fulfilled to provide
a complete solution:
Plan: Enable operators to create a device metadata scheme that enables them to easily and accurately query
for, and target a group of devices for bulk management operations. You can use the device twin to store this
device metadata in the form of tags and properties.
Further reading: Get started with device twins, Understand device twins, How to use device twin properties.
Provision: Securely provision new devices to IoT Hub and enable operators to immediately discover device
capabilities. Use the IoT Hub identity registry to create flexible device identities and credentials, and perform
this operation in bulk by using a job. Build devices to report their capabilities and conditions through device
properties in the device twin.
Further reading: Manage device identities, Bulk management of device identities, How to use device twin
properties, Azure IoT Hub Device Provisioning Service.
Configure: Facilitate bulk configuration changes and firmware updates to devices while maintaining both
health and security. Perform these device management operations in bulk by using desired properties or
with direct methods and broadcast jobs.
Further reading: Use direct methods, Invoke a direct method on a device, How to use device twin properties,
Schedule and broadcast jobs, Schedule jobs on multiple devices.
Monitor: Monitor overall device collection health, the status of ongoing operations, and alert operators to
issues that might require their attention. Apply the device twin to allow devices to report realtime operating
conditions and status of update operations. Build powerful dashboard reports that surface the most
immediate issues by using device twin queries.
Further reading: How to use device twin properties, IoT Hub query language for device twins, jobs, and
message routing.
Retire: Replace or decommission devices after a failure, upgrade cycle, or at the end of the service lifetime.
Use the device twin to maintain device info if the physical device is being replaced, or archived if being
retired. Use the IoT Hub identity registry for securely revoking device identities and credentials.
Further reading: How to use device twin properties, Manage device identities.
Factory Reset - The back-end app informs the device through a direct method that it has initiated a factory
reset. The device uses the reported properties to update the factory reset status of the device.
Configuration - The back-end app uses the desired properties to configure software running on the device.
The device uses the reported properties to update configuration status of the device.
Firmware Update - The back-end app informs the device through a direct method that it has initiated a
firmware update. The device initiates a multistep process to download the firmware image, apply the
firmware image, and finally reconnect to the IoT Hub service. Throughout the multistep process, the device
uses the reported properties to update the progress and status of the device.
Reporting progress and status - The solution back end runs device twin queries, across a set of devices, to
report on the status and progress of actions running on the devices.
Next Steps
The capabilities, patterns, and code libraries that IoT Hub provides for device management, enable you to create
IoT applications that fulfill enterprise IoT operator requirements within each device lifecycle stage.
To continue learning about the device management features in IoT Hub, see the Get started with device
management tutorial.
Connecting IoT Devices to Azure: IoT Hub and Event
Hubs
5/29/2018 • 2 min to read • Edit Online
Azure provides services specifically developed for diverse types of connectivity and communication to help you
connect your data to the power of the cloud. Both Azure IoT Hub and Azure Event Hubs are cloud services that can
ingest large amounts of data and process or store that data for business insights. The two services are similar in
that they both support ingestion of data with low latency and high reliability, but they are designed for different
purposes. IoT Hub was developed specifically to address the unique requirements of connecting IoT devices, at-
scale, to the Azure Cloud while Event Hubs was designed for big data streaming. This is why Microsoft
recommends using Azure IoT Hub to connect IoT devices to Azure
Azure IoT Hub is the cloud gateway that connects IoT devices to gather data to drive business insights and
automation. In addition, IoT Hub includes features that enrich the relationship between your devices and your
backend systems. Bi-directional communication capabilities mean that while you receive data from devices you can
also send commands and policies back to devices, for example, to update properties or invoke device management
actions. This cloud-to-device connectivity also powers the important capability of delivering cloud intelligence to
your edge devices with Azure IoT Edge. The unique device-level identity provided by IoT Hub helps better secure
your IoT solution from potential attacks.
Azure Event Hubs is the big data streaming service of Azure. It is designed for high throughput data streaming
scenarios where customers may send billions of requests per day. Event Hubs uses a partitioned consumer model
to scale out your stream and is integrated into the big data and analytics services of Azure including Databricks,
Stream Analytics, ADLS, and HDInsight. With features like Event Hubs Capture and Auto-Inflate, this service is
designed to support your big data apps and solutions. Additionally, IoT Hub leverages Event Hubs for its telemetry
flow path, so your IoT solution also benefits from the tremendous power of Event Hubs.
To summarize, while both solutions are designed for data ingestion at a massive scale, only IoT Hub provides the
rich IoT-specific capabilities that are designed for you to maximize the business value of connecting your IoT
devices to the Azure cloud. If your IoT journey is just beginning, starting with IoT Hub to support your data
ingestion scenarios will assure that you have instant access to the full-featured IoT capabilities once your business
and technical needs require them.
The following table provides details about how the two tiers of IoT Hub compare to Event Hubs when you're
evaluating them for IoT capabilities. For more information about the standard and basic tiers of IoT Hub, see How
to choose the right IoT Hub tier.
IOT CAPABILITY IOT HUB STANDARD TIER IOT HUB BASIC TIER EVENT HUBS
Device-to-cloud messaging
Per-device identity
IOT CAPABILITY IOT HUB STANDARD TIER IOT HUB BASIC TIER EVENT HUBS
Cloud-to-device messaging
IoT Edge
Even if the only use case is device-to-cloud data ingestion, we highly recommend using IoT Hub as it provides a
service that is designed for IoT device connectivity.
Next steps
To further explore the capabilities of IoT Hub, see the IoT Hub developer guide
Choose the right IoT Hub tier for your solution
5/29/2018 • 4 min to read • Edit Online
Every IoT solution is different, so Azure IoT Hub offers several options based on pricing and scale. This article is
meant to help you evaluate your IoT Hub needs. For pricing information about IoT Hub tiers refer to IoT Hub
pricing.
To decide which IoT Hub tier is right for your solution, ask yourself two questions:
What features do I plan to use? Azure IoT Hub offers two tiers, basic and standard, that differ in the number of
features they support. If your IoT solution is based around collecting data from devices and analyzing it centrally
then the basic tier is probably right for you. If you want to use more advanced configurations to control IoT devices
remotely or distribute some of your workloads onto the devices themselves then you should consider the standard
tier. For a detailed breakdown of which features are included in each tier continue to Basic and standard tiers.
How much data do I plan to move daily? Each IoT Hub tier is available in three sizes, based around how much
data throughput they can handle in any given day. These sizes are numerically identified as 1, 2, and 3. For example,
each unit of a level 1 IoT hub can handle 400 thousand messages a day, while a level 3 unit can handle 300 million.
For more details about the data guidelines, continue to Message throughput.
Bulk device operation Yes, except for IoT Edge capabilites Yes
Message throughput
The best way to size an IoT Hub solution is to evaluate the traffic on a per-unit basis. In particular, consider the
required peak throughput for the following categories of operations:
Device-to-cloud messages
Cloud-to-device messages
Identity registry operations
Traffic is measured on a per-unit basis, not per hub. A level 1 or 2 IoT Hub instance can have as many as 200 units
associated with it. A level 3 IoT Hub instance can have up to 10 units. Once you create your IoT hub you can change
the number of units or move between the 1, 2, and 3 sizes within a specific tier without interrupting your existing
operations. For more information, see How to upgrade your IoT Hub.
As an example of each tier's traffic capabilities, device-to-cloud messages follow these sustained throughput
guidelines:
In addition to this throughput information, see IoT Hub quotas and throttles and design your solution accordingly.
Identity registry operation throughput
IoT Hub identity registry operations are not supposed to be run-time operations, as they are mostly related to
device provisioning.
For specific burst performance numbers, see IoT Hub quotas and throttles.
Sharding
While a single IoT hub can scale to millions of devices, sometimes your solution requires specific performance
characteristics that a single IoT hub cannot guarantee. In that case you can partition your devices across multiple
IoT hubs. Multiple IoT hubs smooth traffic bursts and obtain the required throughput or operation rates that are
required.
Next steps
For additional information about IoT Hub capabilities and performance details, see [IoT Hub pricing][link-
pricing] or IoT Hub quotas and throttles.
To change your IoT Hub tier, follow the steps in Upgrade your IoT hub.
IoT Hub high availability and disaster recovery
5/29/2018 • 3 min to read • Edit Online
As an Azure service, IoT Hub provides high availability (HA) using redundancies at the Azure region level, without
any additional work required by the solution. The Microsoft Azure platform also includes features to help you build
solutions with disaster recovery (DR ) capabilities or cross-region availability. If you want to provide global, cross-
region high availability for devices or users, take advantage of these Azure DR features. The article Azure Business
Continuity Technical Guidance describes the built-in features in Azure for business continuity and DR. The Disaster
recovery and high availability for Azure applications paper provides architecture guidance on strategies for Azure
applications to achieve HA and DR.
FUNCTIONALITY RPO
Service availability for registry and communication operations Possible CName loss
Next steps
Follow these links to learn more about Azure IoT Hub:
Get started with IoT Hubs (Tutorial)
What is Azure IoT Hub?
Support additional protocols for IoT Hub
5/29/2018 • 1 min to read • Edit Online
Azure IoT Hub natively supports communication over the MQTT, AMQP, and HTTPS protocols. In some cases,
devices or field gateways might not be able to use one of these standard protocols and require protocol adaptation.
In such cases, you can use a custom gateway. A custom gateway enables protocol adaptation for IoT Hub endpoints
by bridging the traffic to and from IoT Hub. You can use the Azure IoT protocol gateway as a custom gateway to
enable protocol adaptation for IoT Hub.
Next steps
To learn more about the Azure IoT protocol gateway and how to use and deploy it as part of your IoT solution, see:
Azure IoT protocol gateway repository on GitHub
Azure IoT protocol gateway developer guide
To learn more about planning your IoT Hub deployment, see:
Compare with Event Hubs
Scaling, high availability, and disaster recovery
IoT Hub developer guide
Compare message routing and Event Grid for IoT
Hub
5/29/2018 • 4 min to read • Edit Online
Azure IoT Hub provides the capability to stream data from your connected devices, and integrate that data into
your business applications. IoT Hub offers two methods for integrating IoT events into other Azure services or
business applications. This article discusses the two features that provide this capability, so that you can choose
which option is best for your scenario.
IoT Hub message routing: This IoT Hub feature enables users to route device-to-cloud messages to service
endpoints like Azure Storage containers, Event Hubs, Service Bus queues, and Service Bus topics. Routing rules
provide the flexibility to perform query-based routes. They also enable critical alerts that trigger actions through
queries, and can be based on the message headers and body.
IoT Hub integration with Event Grid: Azure Event Grid is a fully managed event routing service that uses a
publish-subscribe model. IoT Hub and Event Grid work together to integrate IoT Hub events into Azure and
non-Azure services, in near-real time.
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Device messages Yes, message routing can be used for No, Event Grid can only be used for
telemetry data. non-telemetry IoT Hub events.
Event type Yes, message routing can report twin Yes, Event Grid can report when devices
changes and device lifecycle events. are registered to an IoT Hub, and when
devices are deleted.
Ordering Yes, ordering of events is maintained. No, order of events is not guaranteed.
Filtering Rich filtering through SQL-like language Filtering based on suffix/prefix of device
supports filtering on message headers IDs, which works well for hierarchical
and bodies. For examples, see IoT Hub services like storage.
query language.
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Cost There is no separate charge for message There is no charge from IoT Hub. Event
routing. Only ingress of telemetry into Grid offers the first 100,000 operations
IoT Hub is charged. For example, if you per month for free, and then $0.60 per
have a message routed to three million operations after that.
different endpoints, you are billed for
only one message.
IoT Hub message routing and Event Grid have similarities too, some of which are detailed in the following table:
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Reliability High: Delivers each message to the High: Delivers each message to the
endpoint at least once for each route. webhook at least once for each
Expires all messages that are not subscription. Expires all events that are
delivered within one hour. not delivered within 24 hours.
Send to multiple endpoints Yes, send a single message to multiple Yes, send a single message to multiple
endpoints. endpoints.
Security Iot Hub provides per-device identity and Event Grid provides validation at three
revocable access control. For more points: event subscriptions, event
information, see the IoT Hub access publishing, and webhook event delivery.
control. For more information, see Event Grid
security and authentication.
How to choose
IoT Hub message routing and the IoT Hub integration with Event Grid perform different actions to achieve similar
results. They both take information from your IoT Hub solution and pass it on so that other services can react. So
how do you decide which one to use? In addition to the data from the previous section, use the following questions
to help guide your decision:
What kind of data are you sending to the endpoints?
Use IoT Hub message routing when you have to send telemetry data to other services. Message routing also
enables querying message headers and message bodies.
The IoT Hub integration with Event Grid works with events that occur in the IoT Hub service. These IoT Hub
events include device creation and deletion.
What endpoints need to receive this information?
IoT Hub message routing supports limited endpoints, but you can build connectors to reroute the data and
events to additional endpoints. For a complete list of supported endpoints, see the table in the previous
section.
The IoT Hub integration with Event Grid supports more endpoints. It also works with webhooks to extend
routing outside of the Azure service ecosystem and into third-party business applications.
Does it matter if your data arrives in order?
IoT Hub message routing maintains the order in which messages are sent, so that they arrive in the same
way.
Event Grid does not guarantee that endpoints will receive events in the same order that they occurred.
However, the event schema does include a timestamp that can be used to identify the order after the events
arrive at the endpoint.
Next steps
Learn more about IoT Hub message routing and the IoT Hub endpoints.
Learn more about Azure Event Grid
Try out the Event Grid integration by Sending email notifications about Azure IoT Hub events using Logic
Apps
Azure IoT Hub developer guide
5/29/2018 • 3 min to read • Edit Online
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
When sending information from the device app to the solution back end, IoT Hub exposes three options:
Device-to-cloud messages for time series telemetry and alerts.
Device twin's reported properties for reporting device state information such as available capabilities,
conditions, or the state of long-running workflows. For example, configuration and software updates.
File uploads for media files and large telemetry batches uploaded by intermittently connected devices or
compressed to save bandwidth.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
Scenario Telemetry time series and Available capabilities and Media files. Large (typically
alerts. For example, 256-KB conditions. For example, the compressed) telemetry
sensor data batches sent current device connectivity batches.
every 5 minutes. mode such as cellular or
WiFi. Synchronizing long-
running workflows, such as
configuration and software
updates.
Storage and retrieval Temporarily stored by IoT Stored by IoT Hub in the Stored in user-provided
Hub, up to 7 days. Only device twin. Retrievable using Azure Storage account.
sequential reading. the IoT Hub query language.
Frequency High. For more information, Medium. For more Low. For more information,
see IoT Hub limits. information, see IoT Hub see IoT Hub limits.
limits.
Protocol Available on all protocols. Available using MQTT or Available when using any
AMQP. protocol, but requires HTTPS
on the device.
An application may need to send information both as a telemetry time series or alert and make it available in the
device twin. In this scenario, you can choose one of the following options:
The device app sends a device-to-cloud message and reports a property change.
The solution back end can store the information in the device twin's tags when it receives the message.
Since device-to-cloud messages enable a much higher throughput than device twin updates, it is sometimes
desirable to avoid updating the device twin for every device-to-cloud message.
Cloud-to-device communications guidance
5/29/2018 • 1 min to read • Edit Online
IoT Hub provides three options for device apps to expose functionality to a back-end app:
Direct methods for communications that require immediate confirmation of the result. Direct methods are often
used for interactive control of devices such as turning on a fan.
Twin's desired properties for long-running commands intended to put the device into a certain desired state. For
example, set the telemetry send interval to 30 minutes.
Cloud-to-device messages for one-way notifications to the device app.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Data flow Two-way. The device app can One-way. The device app One-way. The device app
respond to the method right receives a notification with receives the message
away. The solution back end the property change.
receives the outcome
contextually to the request.
Durability Disconnected devices are not Property values are Messages can be retained by
contacted. The solution back preserved in the device twin. IoT Hub for up to 48 hours.
end is notified that the Device will read it at next
device is not connected. reconnection. Property
values are retrievable with
the IoT Hub query language.
Targets Single device using Single device using Single device by deviceId.
deviceId, or multiple devices deviceId, or multiple devices
using jobs. using jobs.
Frequency High. For more information, Medium. For more Low. For more information,
see IoT Hub limits. information, see IoT Hub see IoT Hub limits.
limits.
DIRECT METHODS TWIN'S DESIRED PROPERTIES CLOUD-TO-DEVICE MESSAGES
Protocol Available using MQTT or Available using MQTT or Available on all protocols.
AMQP. AMQP. Device must poll when using
HTTPS.
Learn how to use direct methods, desired properties, and cloud-to-device messages in the following tutorials:
Use direct methods, for direct methods;
Use desired properties to configure devices, for device twin's desired properties;
Send cloud-to-device messages, for cloud-to-device messages.
Device-to-cloud and cloud-to-device messaging with
IoT Hub
5/29/2018 • 1 min to read • Edit Online
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
Core properties of IoT Hub messaging functionality are the reliability and durability of messages. These properties
enable resilience to intermittent connectivity on the device side, and to load spikes in event processing on the cloud
side. IoT Hub implements at least once delivery guarantees for both device-to-cloud and cloud-to-device
messaging.
For an introduction to the capabilities of IoT Hub, see the Overview of the Azure IoT Hub service.
Next steps
Learn about IoT Hub device-to-cloud messaging.
Learn about IoT Hub cloud-to-device messaging.
Send device-to-cloud messages to IoT Hub
6/6/2018 • 3 min to read • Edit Online
To send time-series telemetry and alerts from your devices to your solution back end, send device-to-cloud
messages from your device to your IoT hub. For a discussion of other device-to-cloud options supported by IoT
Hub, see Device-to-cloud communications guidance.
You send device-to-cloud messages through a device-facing endpoint (/devices/{deviceId}/messages/events).
Routing rules then route your messages to one of the service-facing endpoints on your IoT hub. Routing rules use
the headers and body of the device-to-cloud messages to determine where to route them. By default, messages are
routed to the built-in service-facing endpoint (messages/events), that is compatible with Event Hubs. Therefore,
you can use standard Event Hubs integration and SDKs to receive device-to-cloud messages in your solution back
end.
IoT Hub implements device-to-cloud messaging using a streaming messaging pattern. IoT Hub's device-to-cloud
messages are more like Event Hubs events than Service Bus messages in that there is a high volume of events
passing through the service that can be read by multiple readers.
Device-to-cloud messaging with IoT Hub has the following characteristics:
Device-to-cloud messages are durable and retained in an IoT hub's default messages/events endpoint for up
to seven days.
Device-to-cloud messages can be at most 256 KB, and can be grouped in batches to optimize sends. Batches can
be at most 256 KB.
As explained in the Control access to IoT Hub section, IoT Hub enables per-device authentication and access
control.
IoT Hub allows you to create up to 10 custom endpoints. Messages are delivered to the endpoints based on
routes configured on your IoT hub. For more information, see Routing rules.
IoT Hub enables millions of simultaneously connected devices (see Quotas and throttling).
IoT Hub does not allow arbitrary partitioning. Device-to-cloud messages are partitioned based on their
originating deviceId.
For more information about the differences between IoT Hub and Event Hubs, see Comparison of Azure IoT Hub
and Azure Event Hubs.
Anti-spoofing properties
To avoid device spoofing in device-to-cloud messages, IoT Hub stamps all messages with the following properties:
ConnectionDeviceId
ConnectionDeviceGenerationId
ConnectionAuthMethod
The first two contain the deviceId and generationId of the originating device, as per Device identity properties.
The ConnectionAuthMethod property contains a JSON serialized object, with the following properties:
{
"scope": "{ hub | device }",
"type": "{ symkey | sas | x509 }",
"issuer": "iothub"
}
Next steps
For information about the SDKs you can use to send device-to-cloud messages, see Azure IoT SDKs.
The Get Started tutorials show you how to send device-to-cloud messages from both simulated and physical
devices. For more detail, see the Process IoT Hub device-to-cloud messages using routes tutorial.
Read device-to-cloud messages from the built-in
endpoint
6/6/2018 • 2 min to read • Edit Online
By default, messages are routed to the built-in service-facing endpoint (messages/events) that is compatible with
Event Hubs. This endpoint is currently only exposed using the AMQP protocol on port 5671. An IoT hub exposes
the following properties to enable you to control the built-in Event Hub-compatible messaging endpoint
messages/events.
PROPERTY DESCRIPTION
Retention time This property specifies how long in days messages are retained
by IoT Hub. The default is one day, but it can be increased to
seven days.
IoT Hub also enables you to manage consumer groups on the built-in device-to-cloud receive endpoint.
By default, all messages that do not explicitly match a message routing rule are written to the built-in endpoint. If
you disable this fallback route, messages that do not explicitly match any message routing rules are dropped.
You can modify the retention time, either programmatically using the IoT Hub resource provider REST APIs, or
with the Azure portal.
IoT Hub exposes the messages/events built-in endpoint for your back-end services to read the device-to-cloud
messages received by your hub. This endpoint is Event Hub-compatible, which enables you to use any of the
mechanisms the Event Hubs service supports for reading messages.
The SDKs and integrations that you can use with Event Hub-compatible endpoints that IoT Hub exposes includes
the items in the following list:
Java Event Hubs client.
Apache Storm spout. You can view the spout source on GitHub.
Apache Spark integration.
Next steps
For more information about IoT Hub endpoints, see IoT Hub endpoints.
The Get Started tutorials show you how to send device-to-cloud messages from simulated devices and read the
messages from the built-in endpoint. For more detail, see the Process IoT Hub device-to-cloud messages using
routes tutorial.
If you want to route your device-to-cloud messages to custom endpoints, see Use message routes and custom
endpoints for device-to-cloud messages.
React to IoT Hub events by using Event Grid to
trigger actions - Preview
5/29/2018 • 2 min to read • Edit Online
Azure IoT Hub integrates with Azure Event Grid so that you can send event notifications to other services and
trigger downstream processes. Configure your business applications to listen for IoT Hub events so that you can
react to critical events in a reliable, scalable, and secure manner. For example, build an application to perform
multiple actions like updating a database, creating a ticket, and delivering an email notification every time a new IoT
device is registered to your IoT hub.
Azure Event Grid is a fully managed event routing service that uses a publish-subscribe model. Event Grid has
built-in support for Azure services like Azure Functions and Azure Logic Apps, and can deliver event alerts to non-
Azure services using webhooks. For a complete list of the event handlers that Event Grid supports, see An
introduction to Azure Event Grid.
Regional availability
The Event Grid integration is available for IoT hubs located in the regions where Event Grid is supported. For the
latest list of regions, see An introduction to Azure Event Grid.
Event types
IoT Hub publishes the following event types:
Event schema
IoT Hub events contain all the information you need to respond to changes in your device lifecycle. You can identify
an IoT Hub event by checking that the eventType property starts with Microsoft.Devices. For more information
about how to use Event Grid event properties, see the Event Grid event schema.
Device created schema
The following example shows the schema of a device created event:
[{
"id": "56afc886-767b-d359-d59e-0da7877166b2",
"topic": "/SUBSCRIPTIONS/<subscription ID>/RESOURCEGROUPS/<resource group
name>/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/<hub name>",
"subject": "devices/LogicAppTestDevice",
"eventType": "Microsoft.Devices.DeviceCreated",
"eventTime": "2018-01-02T19:17:44.4383997Z",
"data": {
"twin": {
"deviceId": "LogicAppTestDevice",
"etag": "AAAAAAAAAAE=",
"status": "enabled",
"statusUpdateTime": "0001-01-01T00:00:00",
"connectionState": "Disconnected",
"lastActivityTime": "0001-01-01T00:00:00",
"cloudToDeviceMessageCount": 0,
"authenticationType": "sas",
"x509Thumbprint": {
"primaryThumbprint": null,
"secondaryThumbprint": null
},
"version": 2,
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2018-01-02T19:17:44.4383997Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2018-01-02T19:17:44.4383997Z"
},
"$version": 1
}
}
},
"hubName": "egtesthub1",
"deviceId": "LogicAppTestDevice",
"operationTimestamp": "2018-01-02T19:17:44.4383997Z",
"opType": "DeviceCreated"
},
"dataVersion": "",
"metadataVersion": "1"
}]
For a detailed description of each property, see Azure Event Grid event schema for IoT Hub
Filter events
IoT Hub event subscriptions can filter events based on event type and device name. Subject filters in Event Grid
work based on prefix and suffix matches. The filter uses an AND operator, so events with a subject that match both
the prefix and suffix are delivered to the subscriber.
The subject of IoT Events uses the format:
devices/{deviceId}
Next steps
Try the IoT Hub events tutorial
Learn more about Event Grid
Compare the differences between routing IoT Hub events and messages
Use message routes and custom endpoints for
device-to-cloud messages
6/6/2018 • 2 min to read • Edit Online
IoT Hub enables you to route device-to-cloud messages to IoT Hub service-facing endpoints based on message
properties. Routing rules give you the flexibility to send messages where they need to go without the need for
additional services or custom code. Each routing rule you configure has the following properties:
PROPERTY DESCRIPTION
Source The origin of the data stream to be acted upon. For example,
device telemetry.
Condition The query expression for the routing rule that is run against
the message's headers and body and determines if it is a
match for the endpoint. For more information about
constructing a route condition, see the Reference - query
language for device twins and jobs.
Endpoint The name of the endpoint where IoT Hub sends messages that
match the condition. Endpoints should be in the same region
as the IoT hub, otherwise you may be charged for cross-
region writes.
A single message may match the condition on multiple routing rules, in which case IoT Hub delivers the message
to the endpoint associated with each matched rule. IoT Hub also automatically deduplicates message delivery, so if
a message matches multiple rules that have the same destination, it is only written once to that destination.
NOTE
IoT Hub only supports writing data to Azure Storage containers as blobs.
WARNING
Service Bus queues and topics with Sessions or Duplicate Detection enabled are not supported as custom endpoints.
For more information about creating custom endpoints in IoT Hub, see IoT Hub endpoints.
For more information about reading from custom endpoints, see:
Reading from Azure Storage containers.
Reading from Event Hubs.
Reading from Service Bus queues.
Reading from Service Bus topics.
Latency
When you route device-to-cloud telemetry messages using built-in endpoints, there is a slight increase in the end-
to-end latency after the creation of the first route.
In most cases, the average increase in latency is less than one second. You can monitor the latency using
d2c.endpoints.latency.builtIn.events IoT Hub metric. Creating or deleting any route after the first one does not
impact the end-to-end latency.
Next steps
For more information about IoT Hub endpoints, see IoT Hub endpoints.
For more information about the query language you use to define routing rules, see IoT Hub query language for
device twins, jobs, and message routing.
The Process IoT Hub device-to-cloud messages using routes tutorial shows you how to use routing rules and
custom endpoints.
Send cloud-to-device messages from IoT Hub
5/29/2018 • 5 min to read • Edit Online
To send one-way notifications to the device app from your solution back end, send cloud-to-devices messages from
your IoT hub to your device. For a discussion of other cloud-to-devices options supported by IoT Hub, see Cloud-
to-device communications guidance.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
You send cloud-to-device messages through a service-facing endpoint (/messages/devicebound). A device then
receives the messages through a device-specific endpoint (/devices/{deviceId}/messages/devicebound).
To target each cloud-to-device message at a single device, IoT Hub sets the to property to
/devices/{deviceId}/messages/devicebound.
Each device queue holds at most 50 cloud-to-device messages. Trying to send more messages to the same device
results in an error.
When the IoT Hub service sends a message to a device, the service sets the message state to Enqueued. When a
device wants to receive a message, IoT Hub locks the message (by setting the state to Invisible), which allows
other threads on the device to start receiving other messages. When a device thread completes the processing of a
message, it notifies IoT Hub by completing the message. IoT Hub then sets the state to Completed.
A device can also choose to:
Reject the message, which causes IoT Hub to set it to the Dead lettered state. Devices that connect over the
MQTT protocol cannot reject cloud-to-device messages.
Abandon the message, which causes IoT Hub to put the message back in the queue, with the state set to
Enqueued. Devices that connect over the MQTT protocol cannot abandon cloud-to-device messages.
A thread could fail to process a message without notifying IoT Hub. In this case, messages automatically transition
from the Invisible state back to the Enqueued state after a visibility (or lock) timeout. The default value of this
timeout is one minute.
The max delivery count property on IoT Hub determines the maximum number of times a message can
transition between the Enqueued and Invisible states. After that number of transitions, IoT Hub sets the state of
the message to Dead lettered. Similarly, IoT Hub sets the state of a message to Dead lettered after its expiration
time (see Time to live).
The How to send cloud-to-device messages with IoT Hub shows you how to send cloud-to-device messages from
the cloud and receive them on a device.
Typically, a device completes a cloud-to-device message when the loss of the message does not affect the
application logic. For example, when the device has persisted the message content locally or has successfully
executed an operation. The message could also carry transient information, whose loss would not impact the
functionality of the application. Sometimes, for long-running tasks, you can:
Complete the cloud-to-device message after persisting the task description in local storage.
Notify the solution back end with one or more device-to-cloud messages at various stages of progress of the
task.
Message feedback
When you send a cloud-to-device message, the service can request the delivery of per-message feedback regarding
the final state of that message.
PROPERTY DESCRIPTION
ContentType application/vnd.microsoft.iothub.feedback.json
The body is a JSON -serialized array of records, each with the following properties:
PROPERTY DESCRIPTION
The service must specify a MessageId for the cloud-to-device message to be able to correlate its feedback with the
original message.
The following example shows the body of a feedback message.
[
{
"OriginalMessageId": "0987654321",
"EnqueuedTimeUtc": "2015-07-28T16:24:48.789Z",
"StatusCode": 0,
"Description": "Success",
"DeviceId": "123",
"DeviceGenerationId": "abcdefghijklmnopqrstuvwxyz"
},
{
...
},
...
]
For more information about how to set these configuration options, see Create IoT hubs.
Next steps
For information about the SDKs you can use to receive cloud-to-device messages, see Azure IoT SDKs.
To try out receiving cloud-to-device messages, see the Send cloud-to-device tutorial.
Create and read IoT Hub messages
5/29/2018 • 3 min to read • Edit Online
To support seamless interoperability across protocols, IoT Hub defines a common message format for all device-
facing protocols. This message format is used for both device-to-cloud and cloud-to-device messages.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
PROPERTY DESCRIPTION
CreationTimeUtc Date and time the message was created on a device. A device
must set this value explicitly.
Message size
IoT Hub measures message size in a protocol-agnostic way, considering only the actual payload. The size in bytes is
calculated as the sum of the following:
The body size in bytes.
The size in bytes of all the values of the message system properties.
The size in bytes of all user property names and values.
Property names and values are limited to ASCII characters, so the length of the strings equals the size in bytes.
Next steps
For information about message size limits in IoT Hub, see IoT Hub quotas and throttling.
To learn how to create and read IoT Hub messages in various programming languages, see the Get started
tutorials.
Reference - choose a communication protocol
5/29/2018 • 2 min to read • Edit Online
IoT Hub allows devices to use the following protocols for device-side communications:
MQTT
MQTT over WebSockets
AMQP
AMQP over WebSockets
HTTPS
For information about how these protocols support specific IoT Hub features, see Device-to-cloud communications
guidance and Cloud-to-device communications guidance.
The following table provides the high-level recommendations for your choice of protocol:
Consider the following points when you choose your protocol for device-side communications:
Cloud-to-device pattern. HTTPS does not have an efficient way to implement server push. As such, when you
are using HTTPS, devices poll IoT Hub for cloud-to-device messages. This approach is inefficient for both the
device and IoT Hub. Under current HTTPS guidelines, each device should poll for messages every 25 minutes or
more. MQTT and AMQP support server push when receiving cloud-to-device messages. They enable
immediate pushes of messages from IoT Hub to the device. If delivery latency is a concern, MQTT or AMQP are
the best protocols to use. For rarely connected devices, HTTPS works as well.
Field gateways. When using MQTT and HTTPS, you cannot connect multiple devices (each with its own per-
device credentials) using the same TLS connection. For Field gateway scenarios that require one TLS connection
between the field gateway and IoT Hub for each connected device, these protocols are suboptimal.
Low resource devices. The MQTT and HTTPS libraries have a smaller footprint than the AMQP libraries. As
such, if the device has limited resources (for example, less than 1-MB RAM ), these protocols might be the only
protocol implementation available.
Network traversal. The standard AMQP protocol uses port 5671, and MQTT listens on port 8883. USe of
these ports could cause problems in networks that are closed to non-HTTPS protocols. Use MQTT over
WebSockets, AMQP over WebSockets, or HTTPS in this scenario.
Payload size. MQTT and AMQP are binary protocols, which result in more compact payloads than HTTPS.
WARNING
When using HTTPS, each device should poll for cloud-to-device messages every 25 minutes or more. However, during
development, it is acceptable to poll more frequently than every 25 minutes.
Port numbers
Devices can communicate with IoT Hub in Azure using various protocols. Typically, the choice of protocol is driven
by the specific requirements of the solution. The following table lists the outbound ports that must be open for a
device to be able to use a specific protocol:
PROTOCOL PORT
MQTT 8883
AMQP 5671
HTTPS 443
Once you have created an IoT hub in an Azure region, the IoT hub keeps the same IP address for the lifetime of
that IoT hub. However, if Microsoft moves the IoT hub to a different scale unit to maintain quality of service, then it
is assigned a new IP address.
Next steps
To learn more about how IoT Hub implements the MQTT protocol, see Communicate with your IoT hub using the
MQTT protocol.
Upload files with IoT Hub
5/29/2018 • 5 min to read • Edit Online
As detailed in the IoT Hub endpoints article, a device can initiate a file upload by sending a notification through a
device-facing endpoint (/devices/{deviceId}/files). When a device notifies IoT Hub that an upload is complete,
IoT Hub sends a file upload notification message through the /messages/servicebound/filenotifications
service-facing endpoint.
Instead of brokering messages through IoT Hub itself, IoT Hub instead acts as a dispatcher to an associated Azure
Storage account. A device requests a storage token from IoT Hub that is specific to the file the device wishes to
upload. The device uses the SAS URI to upload the file to storage, and when the upload is complete the device
sends a notification of completion to IoT Hub. IoT Hub checks the file upload is complete and then adds a file
upload notification message to the service-facing file notification endpoint.
Before you upload a file to IoT Hub from a device, you must configure your hub by associating an Azure Storage
account to it.
Your device can then initialize an upload and then notify IoT hub when the upload completes. Optionally, when a
device notifies IoT Hub that the upload is complete, the service can generate a notification message.
When to use
Use file upload to send media files and large telemetry batches uploaded by intermittently connected devices or
compressed to save bandwidth.
Refer to Device-to-cloud communication guidance if in doubt between using reported properties, device-to-cloud
messages, or file upload.
NOTE
The Azure IoT SDKs automatically handle retrieving the SAS URI, uploading the file, and notifying IoT Hub of a completed
upload.
{
"blobName": "{name of the file for which a SAS URI will be generated}"
}
IoT Hub returns the following data, which the device uses to upload the file:
{
"correlationId": "somecorrelationid",
"hostName": "contoso.azure-devices.net",
"containerName": "testcontainer",
"blobName": "test-device1/image.jpg",
"sasToken": "1234asdfSAStoken"
}
NOTE
This section describes deprecated functionality for how to receive a SAS URI from IoT Hub. Use the POST method described
previously.
IoT Hub has two REST endpoints to support file upload, one to get the SAS URI for storage and the other to notify
the IoT hub of a completed upload. The device initiates the file upload process by sending a GET to the IoT hub at
{iot hub}.azure-devices.net/devices/{deviceId}/files/{filename} . The IoT hub returns:
{
"correlationId": "{correlation ID received from the initial request}",
"isSuccess": bool,
"statusCode": XXX,
"statusDescription": "Description of status"
}
The value of isSuccess is a Boolean representing whether the file was uploaded successfully. The status code for
statusCode is the status for the upload of the file to storage, and the statusDescription corresponds to the
statusCode .
Reference topics:
The following reference topics provide you with more information about uploading files from a device.
Example. This example shows the body of a file upload notification message.
{
"deviceId":"mydevice",
"blobUri":"https://{storage account}.blob.core.windows.net/{container name}/mydevice/myfile.jpg",
"blobName":"mydevice/myfile.jpg",
"lastUpdatedTime":"2016-06-01T21:22:41+00:00",
"blobSizeInBytes":1234,
"enqueuedTimeUtc":"2016-06-01T21:22:43.7996883Z"
}
fileNotifications.ttlAsIso8601 Default TTL for file upload notifications. ISO_8601 interval up to 48H (minimum
1 minute). Default: 1 hour.
fileNotifications.lockDuration Lock duration for the file upload 5 to 300 seconds (minimum 5 seconds).
notifications queue. Default: 60 seconds.
fileNotifications.maxDeliveryCount Maximum delivery count for the file 1 to 100. Default: 100.
upload notification queue.
Next steps
Now you have learned how to upload files from devices using IoT Hub, you may be interested in the following IoT
Hub developer guide topics:
Manage device identities in IoT Hub
Control access to IoT Hub
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
How to upload files from devices to the cloud with IoT Hub
Understand the identity registry in your IoT hub
5/29/2018 • 10 min to read • Edit Online
Every IoT hub has an identity registry that stores information about the devices and modules permitted to connect
to the IoT hub. Before a device or module can connect to an IoT hub, there must be an entry for that device or
module in the IoT hub's identity registry. A device or module must also authenticate with the IoT hub based on
credentials stored in the identity registry.
The device or module ID stored in the identity registry is case-sensitive.
At a high level, the identity registry is a REST-capable collection of device or module identity resources. When you
add an entry in the identity registry, IoT Hub creates a set of per-device resources such as the queue that contains
in-flight cloud-to-device messages.
Use the identity registry when you need to:
Provision devices or modules that connect to your IoT hub.
Control per-device/per-module access to your hub's device or module-facing endpoints.
NOTE
The identity registry does not contain any application-specific metadata.
IMPORTANT
The only way to retrieve all identities in an IoT hub's identity registry is to use the Export functionality.
IMPORTANT
Only use the identity registry for device management and provisioning operations. High throughput operations at run time
should not depend on performing operations in the identity registry. For example, checking the connection state of a device
before sending a command is not a supported pattern. Make sure to check the throttling rates for the identity registry, and
the device heartbeat pattern.
Disable devices
You can disable devices by updating the status property of an identity in the identity registry. Typically, you use this
property in two scenarios:
During a provisioning orchestration process. For more information, see Device Provisioning.
If, for any reason, you think a device is compromised or has become unauthorized.
This feature is not availble for modules.
Device provisioning
The device data that a given IoT solution stores depends on the specific requirements of that solution. But, as a
minimum, a solution must store device identities and authentication keys. Azure IoT Hub includes an identity
registry that can store values for each device such as IDs, authentication keys, and status codes. A solution can use
other Azure services such as table storage, blob storage, or Cosmos DB to store any additional device data.
Device provisioning is the process of adding the initial device data to the stores in your solution. To enable a new
device to connect to your hub, you must add a device ID and keys to the IoT Hub identity registry. As part of the
provisioning process, you might need to initialize device-specific data in other solution stores. You can also use the
Azure IoT Hub Device Provisioning Service to enable zero-touch, just-in-time provisioning to one or more IoT hubs
without requiring human intervention. To learn more, see the provisioning service documentation.
Device heartbeat
The IoT Hub identity registry contains a field called connectionState. Only use the connectionState field during
development and debugging. IoT solutions should not query the field at run time. For example, do not query the
connectionState field to check if a device is connected before you send a cloud-to-device message or an SMS.
If your IoT solution needs to know if a device is connected, you should implement the heartbeat pattern.
In the heartbeat pattern, the device sends device-to-cloud messages at least once every fixed amount of time (for
example, at least once every hour). Therefore, even if a device does not have any data to send, it still sends an
empty device-to-cloud message (usually with a property that identifies it as a heartbeat). On the service side, the
solution maintains a map with the last heartbeat received for each device. If the solution does not receive a
heartbeat message within the expected time from the device, it assumes that there is a problem with the device.
A more complex implementation could include the information from operations monitoring to identify devices that
are trying to connect or communicate but failing. When you implement the heartbeat pattern, make sure to check
IoT Hub Quotas and Throttles.
NOTE
If an IoT solution uses the connection state solely to determine whether to send cloud-to-device messages, and messages are
not broadcast to large sets of devices, consider using the simpler short expiry time pattern. This pattern achieves the same
result as maintaining a device connection state registry using the heartbeat pattern, while being more efficient. If you request
message acknowledgements, IoT Hub can notify you about which devices are able to receive messages and which are not.
NAME VALUE
$content-type application/json
$iothub-message-source deviceLifecycleEvents
$content-encoding utf-8
iothub-message-schema deviceLifecycleNotification
Body: This section is in JSON format and represents the twin of the created device identity. For example,
{
"deviceId":"11576-ailn-test-0-67333793211",
"etag":"AAAAAAAAAAE=",
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
NAME VALUE
$content-type application/json
$iothub-message-source moduleLifecycleEvents
$content-encoding utf-8
iothub-message-schema moduleLifecycleNotification
Body: This section is in JSON format and represents the twin of the created module identity. For example,
{
"deviceId":"11576-ailn-test-0-67333793211",
"moduleId":"tempSensor",
"etag":"AAAAAAAAAAE=",
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
NOTE
Connection state can only represent the IoT Hub view of the status of the connection. Updates to this state may be delayed,
depending on network conditions and configurations.
Next steps
Now that you have learned how to use the IoT Hub identity registry, you may be interested in the following IoT
Hub developer guide topics:
Control access to IoT Hub
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
Get started with Azure IoT Hub
To explore using the IoT Hub Device Provisioning Service to enable zero-touch, just-in-time provisioning, see:
Azure IoT Hub Device Provisioning Service
Control access to IoT Hub
6/6/2018 • 16 min to read • Edit Online
This article describes the options for securing your IoT hub. IoT Hub uses permissions to grant access to each IoT
hub endpoint. Permissions limit the access to an IoT hub based on functionality.
This article introduces:
The different permissions that you can grant to a device or back-end app to access your IoT hub.
The authentication process and the tokens it uses to verify permissions.
How to scope credentials to limit access to specific resources.
IoT Hub support for X.509 certificates.
Custom device authentication mechanisms that use existing device identity registries or authentication schemes.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
You must have appropriate permissions to access any of the IoT Hub endpoints. For example, a device must include
a token containing security credentials along with every message it sends to IoT Hub.
Authentication
Azure IoT Hub grants access to endpoints by verifying a token against the shared access policies and identity
registry security credentials.
Security credentials, such as symmetric keys, are never sent over the wire.
NOTE
The Azure IoT Hub resource provider is secured through your Azure subscription, as are all providers in the Azure Resource
Manager.
For more information about how to construct and use security tokens, see IoT Hub security tokens.
Protocol specifics
Each supported protocol, such as MQTT, AMQP, and HTTPS, transports tokens in different ways.
When using MQTT, the CONNECT packet has the deviceId as the ClientId, {iothubhostname}/{deviceId} in the
Username field, and a SAS token in the Password field. {iothubhostname} should be the full CName of the IoT hub
(for example, contoso.azure-devices.net).
When using AMQP, IoT Hub supports SASL PL AIN and AMQP Claims-Based-Security.
If you use AMQP claims-based-security, the standard specifies how to transmit these tokens.
For SASL PL AIN, the username can be:
{policyName}@sas.root.{iothubName} if using IoT hub-level tokens.
{deviceId}@sas.{iothubname} if using device-scoped tokens.
In both cases, the password field contains the token, as described in IoT Hub security tokens.
HTTPS implements authentication by including a valid token in the Authorization request header.
Example
Username (DeviceId is case-sensitive): iothubname.azure-devices.net/DeviceId
NOTE
The Azure IoT SDKs automatically generate tokens when connecting to the service. In some cases, the Azure IoT SDKs do not
support all the protocols or all the authentication methods.
Security tokens
IoT Hub uses security tokens to authenticate devices and services to avoid sending keys on the wire. Additionally,
security tokens are limited in time validity and scope. Azure IoT SDKs automatically generate tokens without
requiring any special configuration. Some scenarios do require you to generate and use security tokens directly.
Such scenarios include:
The direct use of the MQTT, AMQP, or HTTPS surfaces.
The implementation of the token service pattern, as explained in Custom device authentication.
IoT Hub also allows devices to authenticate with IoT Hub using X.509 certificates.
Security token structure
You use security tokens to grant time-bounded access to devices and services to specific functionality in IoT Hub. To
get authorization to connect to IoT Hub, devices and services must send security tokens signed with either a shared
access or symmetric key. These keys are stored with a device identity in the identity registry.
A token signed with a shared access key grants access to all the functionality associated with the shared access
policy permissions. A token signed with a device identity's symmetric key only grants the DeviceConnect
permission for the associated device identity.
The security token has the following format:
SharedAccessSignature sig={signature-string}&se={expiry}&skn={policyName}&sr={URL-encoded-resourceURI}
VALUE DESCRIPTION
{resourceURI} URI prefix (by segment) of the endpoints that can be accessed
with this token, starting with host name of the IoT hub (no
protocol). For example,
myHub.azure-devices.net/devices/device1
VALUE DESCRIPTION
{expiry} UTF8 strings for number of seconds since the epoch 00:00:00
UTC on 1 January 1970.
{policyName} The name of the shared access policy to which this token
refers. Absent if the token refers to device-registry credentials.
Note on prefix: The URI prefix is computed by segment and not by character. For example /a/b is a prefix for
/a/b/c but not for /a/bc .
The following Node.js snippet shows a function called generateSasToken that computes the token from the inputs
resourceUri, signingKey, policyName, expiresInMins . The next sections detail how to initialize the different inputs
for the different token use cases.
// Use crypto
var hmac = crypto.createHmac('sha256', new Buffer(signingKey, 'base64'));
hmac.update(toSign);
var base64UriEncoded = encodeURIComponent(hmac.digest('base64'));
rawtoken = {
'sr' : uri,
'sig': signature,
'se' : str(int(ttl))
}
using System;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
public static string generateSasToken(string resourceUri, string key, string policyName, int expiryInSeconds =
3600)
{
TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + expiryInSeconds);
if (!String.IsNullOrEmpty(policyName))
{
token += "&skn=" + policyName;
}
return token;
}
NOTE
Since the time validity of the token is validated on IoT Hub machines, the drift on the clock of the machine that generates the
token must be minimal.
IMPORTANT
The only way that IoT Hub authenticates a specific device is using the device identity symmetric key. In cases when a shared
access policy is used to access device functionality, the solution must consider the component issuing the security token as a
trusted subcomponent.
ENDPOINT FUNCTIONALITY
The result, which grants access to all functionality for device1, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices%2fdevice1&sig=13y8ejUk2z7PLmvtwR5RqlGBOVwiq7rQR3WZ5xZX3N4%3D&se=1456971697
NOTE
It is possible to generate a SAS token using the .NET device explorer tool or the cross-platform, Python-based The IoT
extension for Azure CLI 2.0 command-line utility.
The result, which grants access to all functionality for device1, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices%2fdevice1&sig=13y8ejUk2z7PLmvtwR5RqlGBOVwiq7rQR3WZ5xZX3N4%3D&se=1456971697&skn=device
A protocol gateway could use the same token for all devices simply setting the resource URI to
myhub.azure-devices.net/devices .
ENDPOINT FUNCTIONALITY
{iot hub host name}/devices Create, update, retrieve, and delete device identities.
As an example, a service generating using the pre-created shared access policy called registryRead would create a
token with the following parameters:
resource URI: {IoT hub name}.azure-devices.net/devices ,
signing key: one of the keys of the registryRead policy,
policy name: registryRead ,
any expiration time.
var endpoint ="myhub.azure-devices.net/devices";
var policyName = 'device';
var policyKey = '...';
The result, which would grant access to read all device identities, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices&sig=JdyscqTpXdEJs49elIUCcohw2DlFDR3zfH5KqGJo4r4%3D&se=1456973447&skn=registryRead
NOTE
You can use the .NET class SharedAccessSignatureBuilder or the Java class IotHubServiceSasToken to create a token in your
token service.
The token service can set the token expiration as desired. When the token expires, the IoT hub severs the
device/module connection. Then, the device/module must request a new token from the token service. A short
expiry time increases the load on both the device/module and the token service.
For a device/module to connect to your hub, you must still add it to the IoT Hub identity registry — even though
the it is using a token and not a key to connect. Therefore, you can continue to use per-device/per-module access
control by enabling or disabling device/module identities in the identity registry. This approach mitigates the risks
of using tokens with long expiry times.
Comparison with a custom gateway
The token service pattern is the recommended way to implement a custom identity registry/authentication scheme
with IoT Hub. This pattern is recommended because IoT Hub continues to handle most of the solution traffic.
However, if the custom authentication scheme is so intertwined with the protocol, you may require a custom
gateway to process all the traffic. An example of such a scenario is usingTransport Layer Security (TLS ) and pre-
shared keys (PSKs). For more information, see the protocol gateway article.
Reference topics:
The following reference topics provide you with more information about controlling access to your IoT hub.
PERMISSION NOTES
RegistryReadWrite Grants read and write access to the identity registry. For more
information, see Identity registry.
This permission is used by back-end cloud services.
Next steps
Now that you have learned how to control access IoT Hub, you may be interested in the following IoT Hub
developer guide topics:
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
If you would like to try out some of the concepts described in this article, see the following IoT Hub tutorials:
Get started with Azure IoT Hub
How to send cloud-to-device messages with IoT Hub
How to process IoT Hub device-to-cloud messages
Understand and use device twins in IoT Hub
5/29/2018 • 11 min to read • Edit Online
Device twins are JSON documents that store device state information including metadata, configurations, and
conditions. Azure IoT Hub maintains a device twin for each device that you connect to IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Device twins
Device twins store device-related information that:
Device and back ends can use to synchronize device conditions and configuration.
The solution back end can use to query and target long-running operations.
The lifecycle of a device twin is linked to the corresponding device identity. Device twins are implicitly created and
deleted when a device identity is created or deleted in IoT Hub.
A device twin is a JSON document that includes:
Tags. A section of the JSON document that the solution back end can read from and write to. Tags are not
visible to device apps.
Desired properties. Used along with reported properties to synchronize device configuration or conditions.
The solution back end can set desired properties, and the device app can read them. The device app can also
receive notifications of changes in the desired properties.
Reported properties. Used along with desired properties to synchronize device configuration or conditions.
The device app can set reported properties, and the solution back end can read and query them.
Device identity properties. The root of the device twin JSON document contains the read-only properties
from the corresponding device identity stored in the identity registry.
The following example shows a device twin JSON document:
{
"deviceId": "devA",
"etag": "AAAAAAAAAAc=",
"status": "enabled",
"statusReason": "provisioned",
"statusUpdateTime": "0001-01-01T00:00:00",
"connectionState": "connected",
"lastActivityTime": "2015-02-30T16:24:48.789Z",
"cloudToDeviceMessageCount": 0,
"authenticationType": "sas",
"x509Thumbprint": {
"primaryThumbprint": null,
"secondaryThumbprint": null
},
"version": 2,
"tags": {
"$etag": "123",
"deploymentLocation": {
"building": "43",
"floor": "1"
}
},
"properties": {
"desired": {
"telemetryConfig": {
"sendFrequency": "5m"
},
"$metadata" : {...},
"$version": 1
},
"reported": {
"telemetryConfig": {
"sendFrequency": "5m",
"status": "success"
}
"batteryLevel": 55,
"$metadata" : {...},
"$version": 4
}
}
}
In the root object are the device identity properties, and container objects for tags and both reported and
desired properties. The properties container contains some read-only elements ( $metadata , $etag , and
$version ) described in the Device twin metadata and Optimistic concurrency sections.
NOTE
Reported properties simplify scenarios where the solution back end is interested in the last known value of a property. Use
device-to-cloud messages if the solution back end needs to process device telemetry in the form of sequences of
timestamped events, such as time series.
...
"desired": {
"telemetryConfig": {
"sendFrequency": "5m"
},
...
},
...
2. The device app is notified of the change immediately if connected, or at the first reconnect. The device app
then reports the updated configuration (or an error condition using the status property). Here is the
portion of the reported properties:
...
"reported": {
"telemetryConfig": {
"sendFrequency": "5m",
"status": "success"
}
...
}
...
3. The solution back end can track the results of the configuration operation across many devices, by querying
device twins.
NOTE
The preceding snippets are examples, optimized for readability, of one way to encode a device configuration and its status. IoT
Hub does not impose a specific schema for the device twin desired and reported properties in the device twins.
You can use twins to synchronize long-running operations such as firmware updates. For more information on how
to use properties to synchronize and track a long running operation across devices, see Use desired properties to
configure devices.
Back-end operations
The solution back end operates on the device twin using the following atomic operations, exposed through HTTPS:
Retrieve device twin by ID. This operation returns the device twin document, including tags and desired and
reported system properties.
Partially update device twin. This operation enables the solution back end to partially update the tags or
desired properties in a device twin. The partial update is expressed as a JSON document that adds or
updates any property. Properties set to null are removed. The following example creates a new desired
property with value {"newProperty": "newValue"} , overwrites the existing value of existingProperty with
"otherNewValue" , and removes otherOldProperty . No other changes are made to existing desired properties
or tags:
{
"properties": {
"desired": {
"newProperty": {
"nestedProperty": "newValue"
},
"existingProperty": "otherNewValue",
"otherOldProperty": null
}
}
}
Replace desired properties. This operation enables the solution back end to completely overwrite all
existing desired properties and substitute a new JSON document for properties/desired .
Replace tags. This operation enables the solution back end to completely overwrite all existing tags and
substitute a new JSON document for tags .
Receive twin notifications. This operation allows the solution back end to be notified when the twin is
modified. To do so, your IoT solution needs to create a route and to set the Data Source equal to
twinChangeEvents. By default, no twin notifications are sent, that is, no such routes pre-exist. If the rate of
change is too high, or for other reasons such as internal failures, the IoT Hub might send only one
notification that contains all changes. Therefore, if your application needs reliable auditing and logging of all
intermediate states, you should use device-to-cloud messages. The twin notification message includes
properties and body.
Properties
NAME VALUE
$content-type application/json
$iothub-message-source twinChangeEvents
$content-encoding utf-8
iothub-message-schema deviceLifecycleNotification
{
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
All the preceding operations support Optimistic concurrency and require the ServiceConnect permission, as
defined in the Security article.
In addition to these operations, the solution back end can:
Query the device twins using the SQL -like IoT Hub query language.
Perform operations on large sets of device twins using jobs.
Device operations
The device app operates on the device twin using the following atomic operations:
Retrieve device twin. This operation returns the device twin document (including tags and desired and
reported system properties) for the currently connected device.
Partially update reported properties. This operation enables the partial update of the reported properties of
the currently connected device. This operation uses the same JSON update format that the solution back end
uses for a partial update of desired properties.
Observe desired properties. The currently connected device can choose to be notified of updates to the
desired properties when they happen. The device receives the same form of update (partial or full replacement)
executed by the solution back end.
All the preceding operations require the DeviceConnect permission, as defined in the Security article.
The Azure IoT device SDKs make it easy to use the preceding operations from many languages and platforms. For
more information on the details of IoT Hub primitives for desired properties synchronization, see Device
reconnection flow.
{
...
"tags": {
"one": {
"two": {
"three": {
"four": {
"five": {
"property": "value"
}
}
}
}
}
},
...
}
This information is kept at every level (not just the leaves of the JSON structure) to preserve updates that remove
object keys.
Optimistic concurrency
Tags, desired, and reported properties all support optimistic concurrency. Tags have an ETag, as per RFC7232, that
represents the tag's JSON representation. You can use ETags in conditional update operations from the solution
back end to ensure consistency.
Device twin desired and reported properties do not have ETags, but have a $version value that is guaranteed to be
incremental. Similarly to an ETag, the version can be used by the updating party to enforce consistency of updates.
For example, a device app for a reported property or the solution back end for a desired property.
Versions are also useful when an observing agent (such as the device app observing the desired properties) must
reconcile races between the result of a retrieve operation and an update notification. The section Device
reconnection flow provides more information.
NOTE
This logic is already implemented in the Azure IoT device SDKs. This description is useful only if the device app cannot use any
of Azure IoT device SDKs and must program the MQTT interface directly.
Next steps
Now you have learned about device twins, you may be interested in the following IoT Hub developer guide topics:
Understand and use module twins in IoT Hub
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorials:
How to use the device twin
How to use device twin properties
Understand and invoke direct methods from IoT Hub
6/4/2018 • 5 min to read • Edit Online
IoT Hub gives you the ability to invoke direct methods on devices from the cloud. Direct methods represent a
request-reply interaction with a device similar to an HTTP call in that they succeed or fail immediately (after a user-
specified timeout). This approach is useful for scenarios where the course of immediate action is different
depending on whether the device was able to respond.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Each device method targets a single device. Jobs provide a way to invoke direct methods on multiple devices, and
schedule method invocation for disconnected devices.
Anyone with service connect permissions on IoT Hub may invoke a method on a device.
Direct methods follow a request-response pattern and are meant for communications that require immediate
confirmation of their result. For example, interactive control of the device, such as turning on a fan.
Refer to Cloud-to-device communication guidance if in doubt between using desired properties, direct methods, or
cloud-to-device messages.
Method lifecycle
Direct methods are implemented on the device and may require zero or more inputs in the method payload to
correctly instantiate. You invoke a direct method through a service-facing URI (
{iot hub}/twins/{device id}/methods/ ). A device receives direct methods through a device-specific MQTT topic (
$iothub/methods/POST/{method name}/ ) or through AMQP links ( IoThub-methodname and IoThub-status application
properties).
NOTE
When you invoke a direct method on a device, property names and values can only contain US-ASCII printable alphanumeric,
except any in the following set:
{'$', '(', ')', '<', '>', '@', ',', ';', ':', '\', '"', '/', '[', ']', '?', '=', '{', '}', SP, HT} .
Direct methods are synchronous and either succeed or fail after the timeout period (default: 30 seconds, settable up
to 3600 seconds). Direct methods are useful in interactive scenarios where you want a device to act if and only if
the device is online and receiving commands. For example, turning on a light from a phone. In these scenarios, you
want to see an immediate success or failure so the cloud service can act on the result as soon as possible. The
device may return some message body as a result of the method, but it isn't required for the method to do so.
There is no guarantee on ordering or any concurrency semantics on method calls.
Direct methods are HTTPS -only from the cloud side, and MQTT or AMQP from the device side.
The payload for method requests and responses is a JSON document up to 128 KB.
{
"methodName": "reboot",
"responseTimeoutInSeconds": 200,
"payload": {
"input1": "someInput",
"input2": "anotherInput"
}
}
{
"status" : 201,
"payload" : {...}
}
Both status and body are provided by the device and used to respond with the device's own status code
and/or description.
Method invocation for IoT Edge modules
Invoking direct methods using a module ID is supported in the C# preview SDK (available here).
For this purpose, use the ServiceClient.InvokeDeviceMethodAsync() method and pass in the deviceId and
moduleId as parameters.
{
"input1": "someInput",
"input2": "anotherInput"
}
The AMQP message arrives on the receive link that represents the method request. It contains the following:
The correlation ID property, which contains a request ID that should be passed back with the corresponding
method response
An application property named IoThub-methodname , which contains the name of the method being invoked
The AMQP message body containing the method payload as JSON
Response
The device creates a sending link to return the method response on address
amqps://{hostname}:5671/devices/{deviceId}/methods/deviceBound
The method’s response is returned on the sending link and is structured as follows:
The correlation ID property, which contains the request ID passed in the method’s request message
An application property named IoThub-status , which contains the user supplied method status
The AMQP message body containing the method response as JSON
Next steps
Now you have learned how to use direct methods, you may be interested in the following IoT Hub developer guide
article:
Schedule jobs on multiple devices
If you would like to try out some of the concepts described in this article, you may be interested in the following IoT
Hub tutorial:
Use direct methods
Schedule jobs on multiple devices
5/29/2018 • 4 min to read • Edit Online
Azure IoT Hub enables a number of building blocks like device twin properties and tags and direct methods.
Typically, back-end apps enable device administrators and operators to update and interact with IoT devices in bulk
and at a scheduled time. Jobs execute device twin updates and direct methods against a set of devices at a
scheduled time. For example, an operator would use a back-end app that initiates and tracks a job to reboot a set of
devices in building 43 and floor 3 at a time that would not be disruptive to the operations of the building.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Consider using jobs when you need to schedule and track progress any of the following activities on a set of
devices:
Update desired properties
Update tags
Invoke direct methods
Job lifecycle
Jobs are initiated by the solution back end and maintained by IoT Hub. You can initiate a job through a service-
facing URI ( {iot hub}/jobs/v2/{device id}/methods/<jobID>?api-version=2016-11-14 ) and query for progress on an
executing job through a service-facing URI ( {iot hub}/jobs/v2/<jobId>?api-version=2016-11-14 ). To refresh the
status of running jobs once a job is initiated, run a job query.
NOTE
When you initiate a job, property names and values can only contain US-ASCII printable alphanumeric, except any in the
following set: $ ( ) < > @ , ; : \ " / [ ] ? = { } SP HT .
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
{
jobId: '<jobId>',
type: 'scheduleDirectRequest',
cloudToDeviceMethod: {
methodName: '<methodName>',
payload: <payload>,
responseTimeoutInSeconds: methodTimeoutInSeconds
},
queryCondition: '<queryOrDevices>', // query condition
startTime: <jobStartTime>, // as an ISO-8601 date string
maxExecutionTimeInSeconds: <maxExecutionTimeInSeconds>
}
The query condition can also be on a single device ID or on a list of device IDs as shown in the following examples:
IoT Hub Query Language covers IoT Hub query language in additional detail.
PUT /jobs/v2/<jobId>?api-version=2016-11-14
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
{
jobId: '<jobId>',
type: 'scheduleTwinUpdate',
updateTwin: <patch> // Valid JSON object
queryCondition: '<queryOrDevices>', // query condition
startTime: <jobStartTime>, // as an ISO-8601 date string
maxExecutionTimeInSeconds: <maxExecutionTimeInSeconds> // format TBD
}
GET /jobs/v2/query?api-version=2016-11-14[&jobType=<jobType>][&jobStatus=<jobStatus>][&pageSize=<pageSize>]
[&continuationToken=<continuationToken>]
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
The continuationToken is provided from the response.
Jobs Properties
The following list shows the properties and corresponding descriptions, which can be used when querying for jobs
or job results.
PROPERTY DESCRIPTION
endTime IoT Hub provided date (ISO-8601) for when the job
completed. Valid only after the job reaches the 'completed'
state.
deviceJobStatistics properties:
Next steps
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
Schedule and broadcast jobs
Reference - IoT Hub endpoints
5/29/2018 • 5 min to read • Edit Online
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
Custom endpoints
You can link existing Azure services in your subscription to your IoT hub to act as endpoints for message routing.
These endpoints act as service endpoints and are used as sinks for message routes. Devices cannot write directly to
the additional endpoints. To learn more about message routes, see the developer guide entry on sending and
receiving messages with IoT hub.
IoT Hub currently supports the following Azure services as additional endpoints:
Azure Storage containers
Event Hubs
Service Bus Queues
Service Bus Topics
IoT Hub needs write access to these service endpoints for message routing to work. If you configure your
endpoints through the Azure portal, the necessary permissions are added for you. Make sure you configure your
services to support the expected throughput. When you first configure your IoT solution, you may need to monitor
your additional endpoints and make any necessary adjustments for the actual load.
If a message matches multiple routes that all point to the same endpoint, IoT Hub delivers message to that
endpoint only once. Therefore, you do not need to configure deduplication on your Service Bus queue or topic. In
partitioned queues, partition affinity guarantees message ordering.
For the limits on the number of endpoints you can add, see Quotas and throttling.
When using Azure Storage containers
IoT Hub only supports writing data to Azure Storage containers as blobs in the Apache Avro format. IoT Hub
batches messages and writes data to a blob whenever:
The batch reaches a certain size.
Or a certain amount of time has elapsed.
IoT Hub will write to an empty blob if there is no data to write.
IoT Hub defaults to the following file naming convention:
{iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}
You may use whatever file naming convention you wish, however you must use all listed tokens.
When using Service Bus queues and topics
Service Bus queues and topics used as IoT Hub endpoints must not have Sessions or Duplicate Detection
enabled. If either of those options are enabled, the endpoint appears as Unreachable in the Azure portal.
Field gateways
In an IoT solution, a field gateway sits between your devices and your IoT Hub endpoints. It is typically located
close to your devices. Your devices communicate directly with the field gateway by using a protocol supported by
the devices. The field gateway connects to an IoT Hub endpoint using a protocol that is supported by IoT Hub. A
field gateway might be a dedicated hardware device or a low -power computer running custom gateway software.
You can use Azure IoT Edge to implement a field gateway. IoT Edge offers functionality such as multiplexing
communications from multiple devices onto the same IoT Hub connection.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub query language for device twins, jobs, and message routing
Quotas and throttling
IoT Hub MQTT support
IoT Hub query language for device and module
twins, jobs, and message routing
5/29/2018 • 15 min to read • Edit Online
IoT Hub provides a powerful SQL -like language to retrieve information regarding device twins and jobs, and
message routing. This article presents:
An introduction to the major features of the IoT Hub query language, and
The detailed description of the language.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
NOTE
Azure IoT SDKs support paging of large results.
IoT Hub allows you to retrieve device twins filtering with arbitrary conditions. For instance, to receive device twins
where the location.region tag is set to US use the following query:
SELECT * FROM devices
WHERE tags.location.region = 'US'
Boolean operators and arithmetic comparisons are supported as well. For example, to retrieve device twins located
in the US and configured to send telemetry less than every minute use the following query:
As a convenience, it is also possible to use array constants with the IN and NIN (not in) operators. For instance, to
retrieve device twins that report WiFi or wired connectivity use the following query:
It is often necessary to identify all device twins that contain a specific property. IoT Hub supports the function
is_defined() for this purpose. For instance, to retrieve device twins that define the connectivity property use the
following query:
Refer to the WHERE clause section for the full reference of the filtering capabilities.
Grouping and aggregations are also supported. For instance, to find the count of devices in each telemetry
configuration status use the following query:
This grouping query would return a result similar to the following example:
[
{
"numberOfDevices": 3,
"status": "Success"
},
{
"numberOfDevices": 2,
"status": "Pending"
},
{
"numberOfDevices": 1,
"status": "Error"
}
]
In this example, three devices reported successful configuration, two are still applying the configuration, and one
reported an error.
Projection queries allow developers to return only the properties they care about. For example, to retrieve the last
activity time of all disconnected devices use the following query:
SELECT LastActivityTime FROM devices WHERE status = 'enabled'
We don't allow join between the devices and devices.modules collections. If you want to query module twins across
devices, you do do it based on tags. This query will return all module twins across all devices with the scanning
status:
This query will return all module twins with the scanning status, but only on the specified subset of devices.
C# example
The query functionality is exposed by the C# service SDK in the RegistryManager class. Here is an example of a
simple query:
The query object is instantiated with a page size (up to 100). Then multiple pages are retrieved by calling the
GetNextAsTwinAsync methods multiple times.
The query object exposes multiple Next values, depending on the deserialization option required by the query. For
example, device twin or job objects, or plain JSON when using projections.
Node.js example
The query functionality is exposed by the Azure IoT service SDK for Node.js in the Registry object. Here is an
example of a simple query:
var query = registry.createQuery('SELECT * FROM devices', 100);
var onResults = function(err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function(twin) {
console.log(twin.deviceId);
});
if (query.hasMoreResults) {
query.nextAsTwin(onResults);
}
}
};
query.nextAsTwin(onResults);
The query object is instantiated with a page size (up to 100). Then multiple pages are retrieved by calling the
nextAsTwin method multiple times.
The query object exposes multiple Next values, depending on the deserialization option required by the query. For
example, device twin or job objects, or plain JSON when using projections.
Limitations
IMPORTANT
Query results can have a few minutes of delay with respect to the latest values in device twins. If querying individual device
twins by ID, use the retrieve device twin API. This API always contains the latest values and has higher throttling limits.
Currently, comparisons are supported only between primitive types (no objects), for instance
... WHERE properties.desired.config = properties.reported.config is supported only if those properties have
primitive values.
Currently, this collection is queryable as devices.jobs in the IoT Hub query language.
IMPORTANT
Currently, the jobs property is never returned when querying device twins. That is, queries that contain 'FROM devices'. The
jobs property can only be accessed directly with queries using FROM devices.jobs .
For instance, to get all jobs (past and scheduled) that affect a single device, you can use the following query:
Note how this query provides the device-specific status (and possibly the direct method response) of each job
returned. It is also possible to filter with arbitrary Boolean conditions on all object properties in the devices.jobs
collection. For instance, to retrieve all completed device twin update jobs that were created after September 2016
for a specific device, use the following query:
Limitations
Currently, queries on devices.jobs do not support:
Projections, therefore only SELECT * is possible.
Conditions that refer to the device twin in addition to job properties (see the preceding section).
Performing aggregations, such as count, avg, group by.
{
"message": {
"systemProperties": {
"contentType": "application/json",
"contentEncoding": "utf-8",
"iothub-message-source": "deviceMessages",
"iothub-enqueuedtime": "2017-05-08T18:55:31.8514657Z"
},
"appProperties": {
"processingPath": "<optional>",
"verbose": "<optional>",
"severity": "<optional>",
"testDevice": "<optional>"
},
"body": "{\"Weather\":{\"Temperature\":50}}"
}
}
Message system properties are prefixed with the '$' symbol. User properties are always accessed with their
name. If a user property name coincides with a system property (such as $contentType ), the user property is
retrieved with the $contentType expression. You can always access the system property using brackets {} : for
instance, you can use the expression {$contentType} to access the system property contentType . Bracketed
property names always retrieve the corresponding system property.
Remember that property names are case insensitive.
NOTE
All message properties are strings. System properties, as described in the developer guide, are currently not available to use
in queries.
For example, if you use a messageType property, you might want to route all telemetry to one endpoint, and all
alerts to another endpoint. You can write the following expression to route the telemetry:
messageType = 'telemetry'
Boolean expressions and functions are also supported. This feature enables you to distinguish between severity
level, for example:
Refer to the Expression and conditions section for the full list of supported operators and functions.
Routing on message bodies
IoT Hub can only route based on message body contents if the message body is properly formed JSON encoded in
UTF -8, UTF -16, or UTF -32. Set the content type of the message to application/json . Set the content encoding to
one of the supported UTF encodings in the message headers. If either of the headers is not specified, IoT Hub does
not attempt to evaluate any query expression involving the body against the message. If your message is not a
JSON message, or if the message does not specify the content type and content encoding, you can still use
message routing to route the message based on the message headers.
The following example shows how to create a message with a properly formed and encoded JSON body:
string messageBody = @"{
""Weather"":{
""Temperature"":50,
""Time"":""2017-03-09T00:00:00.000Z"",
""PrevTemperatures"":[
20,
30,
40
],
""IsEnabled"":true,
""Location"":{
""Street"":""One Microsoft Way"",
""City"":""Redmond"",
""State"":""WA""
},
""HistoricalData"":[
{
""Month"":""Feb"",
""Temperature"":40
},
{
""Month"":""Jan"",
""Temperature"":30
}
]
}
}";
await deviceClient.SendEventAsync(message);
}
You can use $body in the query expression to route the message. You can use a simple body reference, body array
reference, or multiple body references in the query expression. Your query expression can also combine a body
reference with a message header reference. For example, the following are all valid query expressions:
$body.Weather.HistoricalData[0].Month = 'Feb'
$body.Weather.Temperature = 50 AND $body.Weather.IsEnabled
length($body.Weather.Location.State) = 2
$body.Weather.Temperature = 50 AND Status = 'Active'
FROM clause
The FROM <from_specification> clause can assume only two values: FROM devices to query device twins, or
FROM devices.jobs to query job per-device details.
WHERE clause
The WHERE <filter_condition> clause is optional. It specifies one or more conditions that the JSON documents
in the FROM collection must satisfy to be included as part of the result. Any JSON document must evaluate the
specified conditions to "true" to be included in the result.
The allowed conditions are described in section Expressions and conditions.
SELECT clause
The SELECT <select_list> is mandatory and specifies what values are retrieved from the query. It specifies the
JSON values to be used to generate new JSON objects. For each element of the filtered (and optionally grouped)
subset of the FROM collection, the projection phase generates a new JSON object. This object is constructed with
the values specified in the SELECT clause.
Following is the grammar of the SELECT clause:
<projection_list> ::=
'*'
| <projection_element> AS alias [, <projection_element> AS alias]+
<projection_element> :==
attribute_name
| <projection_element> '.' attribute_name
| <aggregate>
<aggregate> :==
count()
| avg(<projection_element>)
| sum(<projection_element>)
| min(<projection_element>)
| max(<projection_element>)
Attribute_name refers to any property of the JSON document in the FROM collection. Some examples of
SELECT clauses can be found in the Getting started with device twin queries section.
Currently, selection clauses different than SELECT* are only supported in aggregate queries on device twins.
GROUP BY clause
The GROUP BY <group_specification> clause is an optional step that executes after the filter specified in the
WHERE clause, and before the projection specified in the SELECT. It groups documents based on the value of an
attribute. These groups are used to generate aggregated values as specified in the SELECT clause.
An example of a query using GROUP BY is:
SELECT properties.reported.telemetryConfig.status AS status,
COUNT() AS numberOfDevices
FROM devices
GROUP BY properties.reported.telemetryConfig.status
GROUP BY <group_by_element>
<group_by_element> :==
attribute_name
| < group_by_element > '.' attribute_name
Attribute_name refers to any property of the JSON document in the FROM collection.
Currently, the GROUP BY clause is only supported when querying device twins.
<expression> ::=
<constant> |
attribute_name |
<function_call> |
<expression> binary_operator <expression> |
<create_array_expression> |
'(' <expression> ')'
<function_call> ::=
<function_name> '(' expression ')'
<constant> ::=
<undefined_constant>
| <null_constant>
| <number_constant>
| <string_constant>
| <array_constant>
To understand what each symbol in the expressions syntax stands for, refer to the following table:
SYMBOL DEFINITION
Operators
The following operators are supported:
FAMILY OPERATORS
Arithmetic +, -, *, /, %
Functions
When querying twins and jobs the only supported function is:
FUNCTION DESCRIPTION
FUNCTION DESCRIPTION
CEILING(x) Returns the smallest integer value greater than, or equal to,
the specified numeric expression.
FUNCTION DESCRIPTION
FLOOR(x) Returns the largest integer less than or equal to the specified
numeric expression.
SIGN(x) Returns the positive (+1), zero (0), or negative (-1) sign of the
specified numeric expression.
In routes conditions, the following type checking and casting functions are supported:
FUNCTION DESCRIPTION
FUNCTION DESCRIPTION
SUBSTRING(string, start [, length]) Returns part of a string expression starting at the specified
character zero-based position and continues to the specified
length, or to the end of the string.
INDEX_OF(string, fragment) Returns the starting position of the first occurrence of the
second string expression within the first specified string
expression, or -1 if the string is not found.
Next steps
Learn how to execute queries in your apps using Azure IoT SDKs.
Reference - IoT Hub quotas and throttling
5/29/2018 • 4 min to read • Edit Online
Operation throttles
Operation throttles are rate limitations that are applied in minute ranges, and are intended to prevent abuse. IoT
Hub tries to avoid returning errors whenever possible, but starts returning exceptions if the throttle is violated for
too long.
At any given time, you can increase quotas or throttle limits by increasing the number of provisioned units in an IoT
hub.
The following table shows the enforced throttles. Values refer to an individual hub.
New device connections (this Higher of 100/sec or 120 new 6000 new
limit applies to the rate at 12/sec/unit connections/sec/unit connections/sec/unit
which new connections are For example, two S1 units
established, not the total are 2*12 = 24 new
number of connections) connections/sec, but you
have at least 100 new
connections/sec across your
units. With nine S1 units,
you have 108 new
connections/sec (9*12)
across your units.
File upload 1.67 file upload 1.67 file upload 83.33 file upload
notifications/sec/unit notifications/sec/unit notifications/sec/unit
(100/min/unit) (100/min/unit) (5000/min/unit)
1This feature is not available in the basic tier of IoT Hub. For more information, see How to choose the right IoT
Hub.
2Throttling meter size is 8 KB.
The device connections throttle governs the rate at which new device connections can be established with an IoT
hub. The device connections throttle does not govern the maximum number of simultaneously connected devices.
The throttle depends on the number of units that are provisioned for the IoT hub.
For example, if you buy a single S1 unit, you get a throttle of 100 connections per second. Therefore, to connect
100,000 devices, it takes at least 1000 seconds (approximately 16 minutes). However, you can have as many
simultaneously connected devices as you have devices registered in your identity registry.
For an in-depth discussion of IoT Hub throttling behavior, see the blog post IoT Hub throttling and you.
IMPORTANT
Identity registry operations are intended for run-time use in device management and provisioning scenarios. Reading or
updating a large number of device identities is supported through import and export jobs.
Other limits
IoT Hub enforces other operational limits:
OPERATION LIMIT
File upload URIs 10000 SAS URIs can be out for a storage account at one time.
10 SAS URIs/device can be out at one time.
OPERATION LIMIT
Additional endpoints Paid SKU hubs may have 10 additional endpoints. Free SKU
hubs may have one additional endpoint.
Message routing rules Paid SKU hubs may have 100 routing rules. Free SKU hubs
may have five routing rules.
1This feature is not available in the basic tier of IoT Hub. For more information, see How to choose the right IoT
Hub.
NOTE
Currently, the maximum number of devices you can connect to a single IoT hub is 500,000. If you want to increase this limit,
contact Microsoft Support.
Latency
IoT Hub strives to provide low latency for all operations. However, due to network conditions and other
unpredictable factors it cannot guarantee a maximum latency. When designing your solution, you should:
Avoid making any assumptions about the maximum latency of any IoT Hub operation.
Provision your IoT hub in the Azure region closest to your devices.
Consider using Azure IoT Edge to perform latency-sensitive operations on the device or on a gateway close to
the device.
Multiple IoT Hub units affect throttling as described previously, but do not provide any additional latency benefits
or guarantees. If you see unexpected increases in operation latency, contact Microsoft Support.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub endpoints
IoT Hub query language for device twins, jobs, and message routing
IoT Hub MQTT support
Azure IoT Hub pricing information
5/29/2018 • 3 min to read • Edit Online
Azure IoT Hub pricing provides the general information on different SKUs and pricing for IoT Hub. This article
contains additional details on how the various IoT Hub functionalities are metered as messages by IoT Hub.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
Cloud-to-device messages Successfully sent messages are charged in 4-KB chunks, for
example a 6-KB message is charged 2 messages.
File uploads File transfer to Azure Storage is not metered by IoT Hub. File
transfer initiation and completion messages are charged as
messaged metered in 4-KB increments. For example,
transferring a 10-MB file is charged two messages in addition
to the Azure Storage cost.
Device and module twin reads Twin reads from the device or module and from the solution
back end are charged as messages in 512-byte chunks. For
example, reading a 6-KB twin is charged as 12 messages.
Device and module twin updates (tags and properties) Twin updates from the device or module and from the solution
back end are charged as messages in 512-byte chunks. For
example, reading a 6-KB twin is charged as 12 messages.
Device and module twin queries Queries are charged as messages depending on the result size
in 512-byte chunks.
OPERATION BILLING INFORMATION
Jobs per-device operations Jobs operations (such as twin updates, and methods) are
charged as normal. For example, a job resulting in 1000
method calls with 1-KB requests and empty-body responses is
charged 1000 messages.
NOTE
All sizes are computed considering the payload size in bytes (protocol framing is ignored). For messages, which have
properties and body, the size is computed in a protocol-agnostic way. For more information, see IoT Hub messaging
developer's guide.
Example #1
A device sends one 1-KB device-to-cloud message per minute to IoT Hub, which is then read by Azure Stream
Analytics. The solution back end invokes a method (with 512-byte payload) on the device every 10 minutes to
trigger a specific action. The device responds to the method with a result of 200 bytes.
The device consumes:
One message * 60 minutes * 24 hours = 1440 messages per day for the device-to-cloud messages.
Two request plus response * 6 times per hour * 24 hours = 288 messages for the methods.
This calculation gives a total of 1728 messages per day.
Example #2
A device sends one 100-KB device-to-cloud message every hour. It also updates its device twin with 1-KB payloads
every four hours. The solution back end, once per day, reads the 14-KB device twin and updates it with 512-byte
payloads to change configurations.
The device consumes:
25 (100 KB / 4 KB ) messages * 24 hours for device-to-cloud messages.
Two messages (1 KB / 0.5 KB ) * six times per day for device twin updates.
This calculation gives a total of 612 messages per day.
The solution back end consumes 28 messages (14 KB / 0.5 KB ) to read the device twin, plus one message to update
it, for a total of 29 messages.
In total, the device and the solution back end consume 641 messages per day.
Understand and use Azure IoT Hub SDKs
6/6/2018 • 2 min to read • Edit Online
There are two categories of software development kits (SDKs) for working with IoT Hub:
Device SDKs enable you to build apps that run on your IoT devices. These apps send telemetry to your IoT
hub, and optionally receive messages, job, method, or twin updates from your IoT hub.
Service SDKs enable you to manage your IoT hub, and optionally send messages, schedule jobs, invoke
direct methods, or send desired property updates to your IoT devices.
Learn about the benefits of developing using Azure IoT SDKs here.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
NOTE
See the readme files in the GitHub repositories for information about using language and platform-specific package managers
to install binaries and dependencies on your development machine.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub endpoints
IoT Hub query language for device twins, jobs, and message routing
Quotas and throttling
IoT Hub MQTT support
IoT Hub REST API reference
Develop for constrained devices using Azure IoT
SDKs
5/25/2018 • 3 min to read • Edit Online
Next steps
To learn more about Azure IoT C SDK architecture:
Azure IoT C SDK source code
Azure IoT device SDK for C introduction
Develop for mobile devices using Azure IoT SDKs
5/29/2018 • 2 min to read • Edit Online
Things in the Internet of Things may refer to a wide range of devices with varying capability: sensors,
microcontrollers, smart devices, industrial gateways, and even mobile devices. A mobile device can be an IoT
device, where it is sending device-to-cloud telemetry and managed by the cloud. It can also be the device running a
back-end service application, which manages other IoT devices. In both cases, Azure IoT Hub SDKs can be used to
develop applications that work for mobile devices.
Next steps
IoT Hub REST API reference
Azure IoT C SDK source code
Communicate with your IoT hub using the MQTT
protocol
5/29/2018 • 11 min to read • Edit Online
IoT Hub enables devices to communicate with the IoT Hub device endpoints using:
MQTT v3.1.1 on port 8883
MQTT v3.1.1 over WebSocket on port 443.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
All device communication with IoT Hub must be secured using TLS/SSL. Therefore, IoT Hub doesn’t support non-
secure connections over port 1883.
Node.js azure-iot-device-mqtt
Java IotHubClientProtocol.MQTT
C MQTT_Protocol
C# TransportType.Mqtt
Python IoTHubTransportProvider.MQTT
Migrating a device app from AMQP to MQTT
If you are using the device SDKs, switching from using AMQP to MQTT requires changing the protocol parameter
in the client initialization as stated previously.
When doing so, make sure to check the following items:
AMQP returns errors for many conditions, while MQTT terminates the connection. As a result your exception
handling logic might require some changes.
MQTT does not support the reject operations when receiving cloud-to-device messages. If your back-end app
needs to receive a response from the device app, consider using direct methods.
For the Password field, use a SAS token. The format of the SAS token is the same as for both the HTTPS
and AMQP protocols:
SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI}
NOTE
If you use X.509 certificate authentication, SAS token passwords are not required. For more information, see Set up
X.509 security in your Azure IoT Hub
For more information about how to generate SAS tokens, see the device section of Using IoT Hub security
tokens.
When testing, you can also use the device explorer tool to quickly generate a SAS token that you can copy
and paste into your own code:
1. Go to the Management tab in Device Explorer.
2. Click SAS Token (top right).
3. On SASTokenForm, select your device in the DeviceID drop down. Set your TTL.
4. Click Generate to create your token.
The SAS token that's generated has the following structure:
HostName={your hub name}.azure-
devices.net;DeviceId=javadevice;SharedAccessSignature=SharedAccessSignature sr={your hub
name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-version%3D2016-11-
14&sig=vSgHBMUG.....Ntg%3d&se=1456481802
The part of this token to use as the Password field to connect using MQTT is:
SharedAccessSignature sr={your hub name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-
version%3D2016-11-14&sig=vSgHBMUG.....Ntg%3d&se=1456481802
For MQTT connect and disconnect packets, IoT Hub issues an event on the Operations Monitoring channel. This
event has additional information that can help you to troubleshoot connectivity issues.
The device app can specify a Will message in the CONNECT packet. The device app should use
devices/{device_id}/messages/events/{property_bag} or devices/{device_id}/messages/events/{property_bag} as the
Will topic name to define Will messages to be forwarded as a telemetry message. In this case, if the network
connection is closed, but a DISCONNECT packet was not previously received from the device, then IoT Hub sends
the Will message supplied in the CONNECT packet to the telemetry channel. The telemetry channel can be either
the default Events endpoint or a custom endpoint defined by IoT Hub routing. The message has the iothub-
MessageType property with a value of Will assigned to it.
TLS/SSL configuration
To use the MQTT protocol directly, your client must connect over TLS/SSL. Attempts to skip this step fail with
connection errors.
In order to establish a TLS connection, you may need to download and reference the DigiCert Baltimore Root
Certificate. This certificate is the one that Azure uses to secure the connection. You can find this certificate in the
Azure-iot-sdk-c repository. More information about these certificates can be found on Digicert's website.
An example of how to implement this using the Python version of the Paho MQTT library by the Eclipse
Foundation might look like the following.
First, install the Paho library from your command-line environment:
Then, implement the client in a Python script. Replace the placeholders as follows:
<local path to digicert.cer> is the path to a local file that contains the DigiCert Baltimore Root certificate. You
can create this file by copying the certificate information from certs.c in the Azure IoT SDK for C. Include the
lines -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- , remove the " marks at the beginning and
end of every line, and remove the \r\n characters at the end of every line.
<device id from device registry> is the ID of a device you added to your IoT hub.
<generated SAS token> is a SAS token for the device created as described previously in this article.
<iot hub name> the name of your IoT hub.
from paho.mqtt import client as mqtt
import ssl
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.connect(iot_hub_name+".azure-devices.net", port=8883)
NOTE
This {property_bag} element uses the same encoding as for query strings in the HTTPS protocol.
{
"properties": {
"desired": {
"telemetrySendFrequency": "5m",
"$version": 12
},
"reported": {
"telemetrySendFrequency": "5m",
"batteryLevel": 55,
"$version": 123
}
}
}
STATUS DESCRIPTION
200 Success
3. The service then sends a response message that contains the new ETag value for the reported properties
collection on topic $iothub/twin/res/{status}/?$rid={request id} . This response message uses the same
request ID as the request.
The request message body contains a JSON document, that contains new values for reported properties. Each
member in the JSON document updates or add the corresponding member in the device twin’s document. A
member set to null , deletes the member from the containing object. For example:
{
"telemetrySendFrequency": "35m",
"batteryLevel": 60
}
STATUS DESCRIPTION
200 Success
{
"telemetrySendFrequency": "5m",
"route": null
}
As for property updates, null values means that the JSON object member is being deleted.
IMPORTANT
IoT Hub generates change notifications only when devices are connected. Make sure to implement the device reconnection
flow to keep the desired properties synchronized between IoT Hub and the device app.
Next steps
To learn more about the MQTT protocol, see the MQTT documentation.
To learn more about planning your IoT Hub deployment, see:
Azure Certified for IoT device catalog
Support additional protocols
Compare with Event Hubs
Scaling, HA, and DR
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Glossary of IoT Hub terms
5/29/2018 • 19 min to read • Edit Online
This article lists some of the common terms used in the IoT Hub articles.
Azure CLI
The Azure CLI is a cross-platform, open-source, shell-based, command tool for creating and managing resources in
Microsoft Azure. This version of the CLI is implemented using Node.js.
Azure portal
The Microsoft Azure portal is a central place where you can provision and manage your Azure resources. It
organizes its content using blades.
Azure PowerShell
Azure PowerShell is a collection of cmdlets you can use to manage Azure with Windows PowerShell. You can use
the cmdlets to create, test, deploy, and manage solutions and services delivered through the Azure platform.
Azure Storage
Azure Storage is a cloud storage solution. It includes the Blob Storage service that you can use to store
unstructured object data. Some IoT Hub tutorials use blob storage.
Back-end app
In the context of IoT Hub, a back-end app is an app that connects to one of the service-facing endpoints on an IoT
hub. For example, a back-end app might retrieve device-to-cloudmessages or manage the identity registry.
Typically, a back-end app runs in the cloud, but in many of the tutorials the back-end apps are console apps running
on your local development machine.
Built-in endpoints
Every IoT hub includes a built-in endpoint that is Event Hub-compatible. You can use any mechanism that works
with Event Hubs to read device-to-cloud messages from this endpoint.
Cloud gateway
A cloud gateway enables connectivity for devices that cannot connect directly to IoT Hub. A cloud gateway is
hosted in the cloud in contrast to a field gateway that runs local to your devices. A typical use case for a cloud
gateway is to implement protocol translation for your devices.
Cloud-to-device
Refers to messages sent from an IoT hub to a connected device. Often, these messages are commands that instruct
the device to take an action. For more information, see Send and receive messages with IoT Hub.
Configuration
In the context of automatic device configuration, a configuration within IoT Hub defines the desired configuration
for a set of devices twins and provides a set of metrics to report status and progress.
Connection string
You use connection strings in your app code to encapsulate the information required to connect to an endpoint. A
connection string typically includes the address of the endpoint and security information, but connection string
formats vary across services. There are two types of connection string associated with the IoT Hub service:
Device connection strings enable devices to connect to the device-facing endpoints on an IoT hub.
IoT Hub connection strings enable back-end apps to connect to the service-facing endpoints on an IoT hub.
Custom endpoints
You can create custom endpoints on an IoT hub to deliver messages dispatched by a routing rule. Custom
endpoints connect directly to an Event hub, a Service Bus queue, or a Service Bus topic.
Custom gateway
A gateway enables connectivity for devices that cannot connect directly to IoT Hub. You can use Azure IoT Edge to
build custom gateways that implement custom logic to handle messages, custom protocol conversions, and other
processing on the edge.
Data-point message
A data-point message is a device-to-cloud message that contains telemetry data such as wind speed or
temperature.
Desired configuration
In the context of a device twin, desired configuration refers to the complete set of properties and metadata in the
device twin that should be synchronized with the device.
Desired properties
In the context of a device twin, desired properties is a subsection of the device twin that is used with reported
properties to synchronize device configuration or condition. Desired properties can only be set by a back-end app
and are observed by the device app.
Device-to-cloud
Refers to messages sent from a connected device to IoT Hub. These messages may be data-point or interactive
messages. For more information, see Send and receive messages with IoT Hub.
Device
In the context of IoT, a device is typically a small-scale, standalone computing device that may collect data or control
other devices. For example, a device might be an environmental monitoring device, or a controller for the watering
and ventilation systems in a greenhouse. The device catalog provides a list of hardware devices certified to work
with IoT Hub.
Device app
A device app runs on your device and handles the communication with your IoT hub. Typically, you use one of the
Azure IoT device SDKs when you implement a device app. In many of the IoT tutorials, you use a simulated device
for convenience.
Device condition
Refers to device state information, such as the connectivity method currently in use, as reported by a device app.
Device apps can also report their capabilities. You can query for condition and capability information using device
twins.
Device data
Device data refers to the per-device data stored in the IoT Hub identity registry. It is possible to import and export
this data.
Device explorer
The device explorer is a tool that runs on Windows and enables you to manage your devices in the identity
registry.The tool can also send and receive messages to your devices.
Device identity
The device identity is the unique identifier assigned to every device registered in the identity registry.
Module identity
The module identity is the unique identifier assigned to every module that belong to a device. Module identity is
also registered in the identity registry.
Device management
Device management encompasses the full lifecycle associated with managing the devices in your IoT solution
including planning, provisioning, configuring, monitoring, and retiring.
Device provisioning
Device provisioning is the process of adding the initial device data to the stores in your solution. To enable a new
device to connect to your hub, you must add a device ID and keys to the IoT Hub identity registry. As part of the
provisioning process, you might need to initialize device-specific data in other solution stores.
Device twin
A device twin is JSON document that stores device state information such as metadata, configurations, and
conditions. IoT Hub persists a device twin for each device that you provision in your IoT hub. Device twins enable
you to synchronize device conditions and configurations between the device and the solution back end. You can
query device twins to locate specific devices and query the status of long-running operations.
Module twin
Similar to device twin, a module twin is JSON document that stores module state information such as metadata,
configurations, and conditions. IoT Hub persists a module twin for each module identity that you provision under a
device identity in your IoT hub. Module twins enable you to synchronize module conditions and configurations
between the module and the solution back end. You can query module twins to locate specific modules and query
the status of long-running operations.
Twin queries
Device and module twin queries use the SQL -like IoT Hub query language to retrieve information from your
device twins or module twins. You can use the same IoT Hub query language to retrieve information about jobs
running in your IoT hub.
Twin synchronization
Twin synchronization uses the desired properties in your device twins or module twins to configure your devices or
modules and retrieve reported properties from them to store in the twin.
Direct method
A direct method is a way for you to trigger a method to execute on a device by invoking an API on your IoT hub.
Endpoint
An IoT hub exposes multiple endpoints that enable your apps to connect to the IoT hub. There are device-facing
endpoints that enable devices to perform operations such as sending device-to-cloud messages and receiving
cloud-to-device messages. There are service-facing management endpoints that enable back-end apps to perform
operations such as device identity management and device twin management. There are service-facing built-in
endpoints for reading device-to-cloud messages. You can create custom endpoints to receive device-to-cloud
messages dispatched by a routing rule.
Field gateway
A field gateway enables connectivity for devices that cannot connect directly to IoT Hub and is typically deployed
locally with your devices. For more information, see What is Azure IoT Hub?
Free account
You can create a free Azure account to complete the IoT Hub tutorials and experiment with the IoT Hub service
(and other Azure services).
Gateway
A gateway enables connectivity for devices that cannot connect directly to IoT Hub. See also Field Gateway, Cloud
Gateway, and Custom Gateway.
Identity registry
The identity registry is the built-in component of an IoT hub that stores information about the individual devices
permitted to connect to an IoT hub.
Interactive message
An interactive message is a cloud-to-device message that triggers an immediate action in the solution back end. For
example, a device might send an alarm about a failure that should be automatically logged in to a CRM system.
IoT Edge
Azure IoT Edge enables cloud-driven deployment of Azure services and solution-specific code to on-premises
devices. IoT Edge devices can aggregate data from other devices to perform computing and analytics before the
data is sent to the cloud. For more information please see Azure IoT Edge.
IoT Hub
IoT Hub is a fully managed Azure service that enables reliable and secure bidirectional communications between
millions of devices and a solution back end. For more information, see What is Azure IoT Hub? Using your Azure
subscription, you can create IoT hubs to handle your IoT messaging workloads.
Job
Your solution back end can use jobs to schedule and track activities on a set of devices registered with your IoT hub.
Activities include updating device twin desired properties, updating device twin tags, and invoking direct methods.
IoT Hub also uses jobs to import to and export from the identity registry.
MQTT
MQTT is one of the messaging protocols that IoT Hub supports for communicating with devices. For more
information about the messaging protocols that IoT Hub supports, see Send and receive messages with IoT Hub.
Operations monitoring
IoT Hub operations monitoring enables you to monitor the status of operations on your IoT hub in real time. IoT
Hub tracks events across several categories of operations. You can opt into sending events from one or more
categories to an IoT Hub endpoint for processing. You can monitor the data for errors or set up more complex
processing based on data patterns.
Physical device
A physical device is a real device such as a Raspberry Pi that connects to an IoT hub. For convenience, many of the
IoT Hub tutorials use simulated devices to enable you to run samples on your local machine.
Protocol gateway
A protocol gateway is typically deployed in the cloud and provides protocol translation services for devices
connecting to IoT Hub. For more information, see What is Azure IoT Hub?
Reported configuration
In the context of a device twin, reported configuration refers to the complete set of properties and metadata in the
device twin that should be reported to the solution back end.
Reported properties
In the context of a device twin, reported properties is a subsection of the device twin used with desired properties
to synchronize device configuration or condition. Reported properties can only be set by the device app and can be
read and queried by a back-end app.
Resource group
Azure Resource Manager uses resource groups to group related resources together. You can use a resource group
to perform operations on all the resources on the group simultaneously.
Retry policy
You use a retry policy to handle transient errors when you connect to a cloud service.
Routing rules
You configure routing rules in your IoT hub to route device-to-cloud messages to a built-in endpoint or to custom
endpoints for processing by your solution back end.
SASL PLAIN
SASL PL AIN is a protocol that the AMQP protocol uses to transfer security tokens.
Shared access signature
Shared Access Signatures (SAS ) are an authentication mechanism based on SHA-256 secure hashes or URIs. SAS
authentication has two components: a Shared Access Policy and a Shared Access Signature (often called a token). A
device uses SAS to authenticate with an IoT hub. Back-end apps also use SAS to authenticate with the service-
facing endpoints on an IoT hub. Typically, you include the SAS token in the connection string that an app uses to
establish a connection to an IoT hub.
Simulated device
For convenience, many of the IoT Hub tutorials use simulated devices to enable you to run samples on your local
machine. In contrast, a physical device is a real device such as a Raspberry Pi that connects to an IoT hub.
Solution
A solution can refer to a Visual Studio solution that includes one or more projects. A solution might also refer to an
IoT solution that includes elements such as devices, device apps, an IoT hub, other Azure services, and back-end
apps.
Subscription
An Azure subscription is where billing takes place. Each Azure resource you create or Azure service you use is
associated with a single subscription. Many quotas also apply at the level of a subscription.
System properties
In the context of a device twin, system properties are read-only and include information regarding the device usage
such as last activity time and connection state.
Tags
In the context of a device twin, tags are device metadata stored and retrieved by the solution back end in the form
of a JSON document. Tags are not visible to apps on a device.
Telemetry
Devices collect telemetry data, such as wind speed or temperature, and use data-point messages to send the
telemetry to an IoT hub.
Token service
You can use a token service to implement an authentication mechanism for your devices. It uses an IoT Hub shared
access policy with DeviceConnect permissions to create device-scoped tokens. These tokens enable a device to
connect to your IoT hub. A device uses a custom authentication mechanism to authenticate with the token service.
IF the device authenticates successfully, the token service issues a SAS token for the device to use to access your
IoT hub.
The Azure IoT device SDK is a set of libraries designed to simplify the process of sending messages to and
receiving messages from the Azure IoT Hub service. There are different variations of the SDK, each targeting a
specific platform, but this article describes the Azure IoT device SDK for C.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
The Azure IoT device SDK for C is written in ANSI C (C99) to maximize portability. This feature makes the libraries
well-suited to operate on multiple platforms and devices, especially where minimizing disk and memory footprint is
a priority.
There are a broad range of platforms on which the SDK has been tested (see the Azure Certified for IoT device
catalog for details). Although this article includes walkthroughs of sample code running on the Windows platform,
the code described in this article is identical across the range of supported platforms.
The following video presents an overview of the Azure IoT SDK for C:
This article introduces you to the architecture of the Azure IoT device SDK for C. It demonstrates how to initialize
the device library, send data to IoT Hub, and receive messages from it. The information in this article should be
enough to get started using the SDK, but also provides pointers to additional information about the libraries.
SDK architecture
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
The latest version of the libraries can be found in the master branch of the repository:
The core implementation of the SDK is in the iothub_client folder that contains the implementation of the
lowest API layer in the SDK: the IoTHubClient library. The IoTHubClient library contains APIs implementing
raw messaging for sending messages to IoT Hub and receiving messages from IoT Hub. When using this
library, you are responsible for implementing message serialization, but other details of communicating with IoT
Hub are handled for you.
The serializer folder contains helper functions and samples that show you how to serialize data before sending
to Azure IoT Hub using the client library. The use of the serializer is not mandatory and is provided as a
convenience. To use the serializer library, you define a model that specifies the data to send to IoT Hub and the
messages you expect to receive from it. Once the model is defined, the SDK provides you with an API surface
that enables you to easily work with device-to-cloud and cloud-to-device messages without worrying about the
serialization details. The library depends on other open source libraries that implement transport using
protocols such as MQTT and AMQP.
The IoTHubClient library depends on other open source libraries:
The Azure C shared utility library, which provides common functionality for basic tasks (such as strings,
list manipulation, and IO ) needed across several Azure-related C SDKs.
The Azure uAMQP library, which is a client-side implementation of AMQP optimized for resource
constrained devices.
The Azure uMQTT library, which is a general-purpose library implementing the MQTT protocol and
optimized for resource constrained devices.
Use of these libraries is easier to understand by looking at example code. The following sections walk you through
several of the sample applications that are included in the SDK. This walkthrough should give you a good feel for
the various capabilities of the architectural layers of the SDK and an introduction to how the APIs work.
Before you run the samples
Before you can run the samples in the Azure IoT device SDK for C, you must create an instance of the IoT Hub
service in your Azure subscription. Then complete the following tasks:
Prepare your development environment
Obtain device credentials.
Prepare your development environment
Packages are provided for common platforms (such as NuGet for Windows or apt_get for Debian and Ubuntu) and
the samples use these packages when available. In some cases, you need to compile the SDK for or on your device.
If you need to compile the SDK, see Prepare your development environment in the GitHub repository.
To obtain the sample application code, download a copy of the SDK from GitHub. Get your copy of the source from
the master branch of the GitHub repository.
Obtain the device credentials
Now that you have the sample source code, the next thing to do is to get a set of device credentials. For a device to
be able to access an IoT hub, you must first add the device to the IoT Hub identity registry. When you add your
device, you get a set of device credentials that you need for the device to be able to connect to the IoT hub. The
sample applications discussed in the next section expect these credentials in the form of a device connection
string.
There are several open source tools to help you manage your IoT hub.
A Windows application called device explorer.
A cross-platform Python CLI tool called the IoT extension for Azure CLI 2.0.
This tutorial uses the graphical device explorer tool. You can also use the the IoT extension for Azure CLI 2.0 tool if
you prefer to use a CLI tool.
The device explorer tool uses the Azure IoT service libraries to perform various functions on IoT Hub, including
adding devices. If you use the device explorer tool to add a device, you get a connection string for your device. You
need this connection string to run the sample applications.
If you're not familiar with the device explorer tool, the following procedure describes how to use it to add a device
and obtain a device connection string.
To install the device explorer tool, see How to use the Device Explorer for IoT Hub devices.
When you run the program, you see this interface:
Enter your IoT Hub Connection String in the first field and click Update. This step configures the tool so that it
can communicate with IoT Hub.
When the IoT Hub connection string is configured, click the Management tab:
This tab is where you manage the devices registered in your IoT hub.
You create a device by clicking the Create button. A dialog displays with a set of pre-populated keys (primary and
secondary). Enter a Device ID and then click Create.
When the device is created, the Devices list updates with all the registered devices, including the one you just
created. If you right-click your new device, you see this menu:
If you choose Copy connection string for selected device, the device connection string is copied to the
clipboard. Keep a copy of the device connection string. You need it when running the sample applications described
in the following sections.
When you've completed the steps above, you're ready to start running some code. Both samples have a constant at
the top of the main source file that enables you to enter a connection string. For example, the corresponding line
from the iothub_client_sample_mqtt application appears as follows.
This solution contains a single project. There are four NuGet packages installed in this solution:
Microsoft.Azure.C.SharedUtility
Microsoft.Azure.IoTHub.MqttTransport
Microsoft.Azure.IoTHub.IoTHubClient
Microsoft.Azure.umqtt
You always need the Microsoft.Azure.C.SharedUtility package when you are working with the SDK. This sample
uses the MQTT protocol, therefore you must include the Microsoft.Azure.umqtt and
Microsoft.Azure.IoTHub.MqttTransport packages (there are equivalent packages for AMQP and HTTPS ).
Because the sample uses the IoTHubClient library, you must also include the
Microsoft.Azure.IoTHub.IoTHubClient package in your solution.
You can find the implementation for the sample application in the iothub_client_sample_mqtt.c source file.
The following steps use this sample application to walk you through what's required to use the IoTHubClient
library.
Initialize the library
NOTE
Before you start working with the libraries, you may need to perform some platform-specific initialization. For example, if you
plan to use AMQP on Linux you must initialize the OpenSSL library. The samples in the GitHub repository call the utility
function platform_init when the client starts and call the platform_deinit function before exiting. These functions are
declared in the platform.h header file. Examine the definitions of these functions for your target platform in the repository to
determine whether you need to include any platform-specific initialization code in your client.
To start working with the libraries, first allocate an IoT Hub client handle:
if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
{
(void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
}
else
{
...
You pass a copy of the device connection string you obtained from the device explorer tool to this function. You also
designate the communications protocol to use. This example uses MQTT, but AMQP and HTTPS are also options.
When you have a valid IOTHUB_CLIENT_HANDLE, you can start calling the APIs to send and receive messages
to and from IoT Hub.
Send messages
The sample application sets up a loop to send messages to your IoT hub. The following snippet:
Creates a message.
Adds a property to the message.
Sends a message.
First, create a message:
size_t iterator = 0;
do
{
if (iterator < MESSAGE_COUNT)
{
sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed
+ (rand() % 4 + 2));
if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned
char*)msgText, strlen(msgText))) == NULL)
{
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
}
else
{
messages[iterator].messageTrackingId = iterator;
MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
(void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
{
(void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
}
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle,
SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT
Hub.\r\n", (int)iterator);
}
}
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1);
iterator++;
} while (g_continueRunning);
Every time you send a message, you specify a reference to a callback function that's invoked when the data is sent.
In this example, the callback function is called SendConfirmationCallback. The following snippet shows this
callback function:
Note the call to the IoTHubMessage_Destroy function when you're done with the message. This function frees
the resources allocated when you created the message.
Receive messages
Receiving a message is an asynchronous operation. First, you register the callback to invoke when the device
receives a message:
The last parameter is a void pointer to whatever you want. In the sample, it's a pointer to an integer but it could be a
pointer to a more complex data structure. This parameter enables the callback function to operate on shared state
with the caller of this function.
When the device receives a message, the registered callback function is invoked. This callback function retrieves:
The message id and correlation id from the message.
The message content.
Any custom properties from the message.
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
int* counter = (int*)userContextCallback;
const char* buffer;
size_t size;
MAP_HANDLE mapProperties;
const char* messageId;
const char* correlationId;
// Message properties
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
{
messageId = "<null>";
}
// Message content
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
{
(void)printf("unable to retrieve the message data\r\n");
}
else
{
(void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> &
Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
// If we receive the work 'quit' then we stop running
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
{
g_continueRunning = false;
}
}
Use the IoTHubMessage_GetByteArray function to retrieve the message, which in this example is a string.
Uninitialize the library
When you're done sending events and receiving messages, you can uninitialize the IoT library. To do so, issue the
following function call:
IoTHubClient_LL_Destroy(iotHubClientHandle);
NOTE
If you open this project in Visual Studio 2017, accept the prompts to retarget the project to the latest version.
As with the previous sample, this one includes several NuGet packages:
Microsoft.Azure.C.SharedUtility
Microsoft.Azure.IoTHub.MqttTransport
Microsoft.Azure.IoTHub.IoTHubClient
Microsoft.Azure.IoTHub.Serializer
Microsoft.Azure.umqtt
You've seen most of these packages in the previous sample, but Microsoft.Azure.IoTHub.Serializer is new. This
package is required when you use the serializer library.
You can find the implementation of the sample application in the simplesample_mqtt.c file.
The following sections walk you through the key parts of this sample.
Initialize the library
To start working with the serializer library, call the initialization APIs:
if (serializer_init(NULL) != SERIALIZER_OK)
{
(void)printf("Failed on serializer_init\r\n");
}
else
{
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString,
MQTT_Protocol);
srand((unsigned int)time(NULL));
int avgWindSpeed = 10;
if (iotHubClientHandle == NULL)
{
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
}
else
{
ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
if (myWeather == NULL)
{
(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
}
else
{
...
The call to the serializer_init function is a one-time call and initializes the underlying library. Then, you call the
IoTHubClient_LL_CreateFromConnectionString function, which is the same API as in the IoTHubClient
sample. This call sets your device connection string (this call is also where you choose the protocol you want to
use). This sample uses MQTT as the transport, but could use AMQP or HTTPS.
Finally, call the CREATE_MODEL_INSTANCE function. WeatherStation is the namespace of the model and
ContosoAnemometer is the name of the model. Once the model instance is created, you can use it to start
sending and receiving messages. However, it's important to understand what a model is.
Define the model
A model in the serializer library defines the messages that your device can send to IoT Hub and the messages,
called actions in the modeling language, which it can receive. You define a model using a set of C macros as in the
simplesample_mqtt sample application:
BEGIN_NAMESPACE(WeatherStation);
DECLARE_MODEL(ContosoAnemometer,
WITH_DATA(ascii_char_ptr, DeviceId),
WITH_DATA(int, WindSpeed),
WITH_ACTION(TurnFanOn),
WITH_ACTION(TurnFanOff),
WITH_ACTION(SetAirResistance, int, Position)
);
END_NAMESPACE(WeatherStation);
The BEGIN_NAMESPACE and END_NAMESPACE macros both take the namespace of the model as an
argument. It's expected that anything between these macros is the definition of your model or models, and the data
structures that the models use.
In this example, there is a single model called ContosoAnemometer. This model defines two pieces of data that
your device can send to IoT Hub: DeviceId and WindSpeed. It also defines three actions (messages) that your
device can receive: TurnFanOn, TurnFanOff, and SetAirResistance. Each data element has a type, and each action
has a name (and optionally a set of parameters).
The data and actions defined in the model define an API surface that you can use to send messages to IoT Hub, and
respond to messages sent to the device. Use of this model is best understood through an example.
Send messages
The model defines the data you can send to IoT Hub. In this example, that means one of the two data items defined
using the WITH_DATA macro. There are several steps required to send DeviceId and WindSpeed values to an
IoT hub. The first is to set the data you want to send:
myWeather->DeviceId = "myFirstDevice";
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
The model you defined earlier enables you to set the values by setting members of a struct. Next, serialize the
message you want to send:
This code serializes the device-to-cloud to a buffer (referenced by destination). The code then invokes the
sendMessage function to send the message to IoT Hub:
static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
{
static unsigned int messageTrackingId;
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
}
else
{
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)
(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
}
IoTHubMessage_Destroy(messageHandle);
}
messageTrackingId++;
}
The second parameter is a pointer to user context; the same pointer passed to
IoTHubClient_LL_SendEventAsync. In this case, the context is a simple counter, but it can be anything you want.
That's all there is to sending device-to-cloud messages. The only thing left to cover is how to receive messages.
Receive messages
Receiving a message works similarly to the way messages work in the IoTHubClient library. First, you register a
message callback function:
Then, you write the callback function that's invoked when a message is received:
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
/*buffer is not zero terminated*/
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
(void)memcpy(temp, buffer, size);
temp[size] = '\0';
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
result =
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
IOTHUBMESSAGE_REJECTED;
free(temp);
}
}
return result;
}
This code is boilerplate -- it's the same for any solution. This function receives the message and takes care of
routing it to the appropriate function through the call to EXECUTE_COMMAND. The function called at this point
depends on the definition of the actions in your model.
When you define an action in your model, you're required to implement a function that's called when your device
receives the corresponding message. For example, if your model defines this action:
Note how the name of the function matches the name of the action in the model and that the parameters of the
function match the parameters specified for the action. The first parameter is always required and contains a
pointer to the instance of your model.
When the device receives a message that matches this signature, the corresponding function is called. Therefore,
aside from having to include the boilerplate code from IoTHubMessage, receiving messages is just a matter of
defining a simple function for each action defined in your model.
Uninitialize the library
When you're done sending data and receiving messages, you can uninitialize the IoT library:
...
DESTROY_MODEL_INSTANCE(myWeather);
}
IoTHubClient_LL_Destroy(iotHubClientHandle);
}
serializer_deinit();
Each of these three functions aligns with the three initialization functions described previously. Calling these APIs
ensures that you free previously allocated resources.
Next Steps
This article covered the basics of using the libraries in the Azure IoT device SDK for C. It provided you with
enough information to understand what's included in the SDK, its architecture, and how to get started working with
the Windows samples. The next article continues the description of the SDK by explaining more about the
IoTHubClient library.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Azure IoT device SDK for C – more about
IoTHubClient
5/29/2018 • 13 min to read • Edit Online
The first article in this series introduced the Azure IoT device SDK for C. That article explained that there are two
architectural layers in SDK. At the base is the IoTHubClient library which directly manages communication with
IoT Hub. There's also the serializer library that builds on top of that to provide serialization services. In this article
we'll provide additional detail on the IoTHubClient library.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
The previous article described how to use the IoTHubClient library to send events to IoT Hub and receive
messages. This article extends that discussion by explaining how to more precisely manage when you send and
receive data, introducing you to the lower-level APIs. We'll also explain how to attach properties to events (and
retrieve them from messages) using the property handling features in the IoTHubClient library. Finally, we'll
provide additional explanation of different ways to handle messages received from IoT Hub.
The article concludes by covering a couple of miscellaneous topics, including more about device credentials and
how to change the behavior of the IoTHubClient through configuration options.
We'll use the IoTHubClient SDK samples to explain these topics. If you want to follow along, see the
iothub_client_sample_http and iothub_client_sample_amqp applications that are included in the Azure IoT
device SDK for C. Everything described in the following sections is demonstrated in these samples.
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
IOTHUB_CLIENT_HANDLE iotHubClientHandle;
iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
The article also described how to receive messages by registering a callback function.
int receiveContext = 0;
IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext);
The article also showed how to free resources using code such as the following.
IoTHubClient_Destroy(iotHubClientHandle);
EVENT_INSTANCE message;
sprintf_s(msgText, sizeof(msgText), "Message_%d_From_IoTHubClient_LL_Over_HTTP", i);
message.messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText));
The first three lines create the message, and the last line sends the event. However, as mentioned previously,
"sending" the event means that the data is simply placed in a buffer. Nothing is transmitted on the network when
we call IoTHubClient_LL_SendEventAsync. In order to actually ingress the data to IoT Hub, you must call
IoTHubClient_LL_DoWork, as in this example:
while (1)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1000);
}
This code (from the iothub_client_sample_http application) repeatedly calls IoTHubClient_LL_DoWork. Each
time IoTHubClient_LL_DoWork is called, it sends some events from the buffer to IoT Hub and it retrieves a
queued message being sent to the device. The latter case means that if we registered a callback function for
messages, then the callback is invoked (assuming any messages are queued up). We would have registered such a
callback function with code such as the following:
The reason that IoTHubClient_LL_DoWork is often called in a loop is that each time it’s called, it sends some
buffered events to IoT Hub and retrieves the next message queued up for the device. Each call isn’t guaranteed to
send all buffered events or to retrieve all queued messages. If you want to send all events in the buffer and then
continue on with other processing you can replace this loop with code such as the following:
IOTHUB_CLIENT_STATUS status;
This code calls IoTHubClient_LL_DoWork until all events in the buffer have been sent to IoT Hub. Note this does
not also imply that all queued messages have been received. Part of the reason for this is that checking for "all"
messages isn’t as deterministic an action. What happens if you retrieve "all" of the messages, but then another one
is sent to the device immediately after? A better way to deal with that is with a programmed timeout. For example,
the message callback function could reset a timer every time it’s invoked. You can then write logic to continue
processing if, for example, no messages have been received in the last X seconds.
When you’re finished ingressing events and receiving messages, be sure to call the corresponding function to clean
up resources.
IoTHubClient_LL_Destroy(iotHubClientHandle);
Basically there’s only one set of APIs to send and receive data with a background thread and another set of APIs
that does the same thing without the background thread. A lot of developers may prefer the non-LL APIs, but the
lower-level APIs are useful when the developer wants explicit control over network transmissions. For example,
some devices collect data over time and only ingress events at specified intervals (for example, once an hour or
once a day). The lower-level APIs give you the ability to explicitly control when you send and receive data from IoT
Hub. Others will simply prefer the simplicity that the lower-level APIs provide. Everything happens on the main
thread rather than some work happening in the background.
Whichever model you choose, be sure to be consistent in which APIs you use. If you start by calling
IoTHubClient_LL_CreateFromConnectionString, be sure you only use the corresponding lower-level APIs for
any follow -up work:
IoTHubClient_LL_SendEventAsync
IoTHubClient_LL_SetMessageCallback
IoTHubClient_LL_Destroy
IoTHubClient_LL_DoWork
The opposite is true as well. If you start with IoTHubClient_CreateFromConnectionString, then use the non-LL
APIs for any additional processing.
In the Azure IoT device SDK for C, see the iothub_client_sample_http application for a complete example of the
lower-level APIs. The iothub_client_sample_amqp application can be referenced for a full example of the non-LL
APIs.
Property handling
So far when we've described sending data, we've been referring to the body of the message. For example, consider
this code:
EVENT_INSTANCE message;
sprintf_s(msgText, sizeof(msgText), "Hello World");
message.messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText));
IoTHubClient_LL_SendEventAsync(iotHubClientHandle, message.messageHandle, SendConfirmationCallback, &message)
This example sends a message to IoT Hub with the text "Hello World." However, IoT Hub also allows properties to
be attached to each message. Properties are name/value pairs that can be attached to the message. For example,
we can modify the previous code to attach a property to the message:
We start by calling IoTHubMessage_Properties and passing it the handle of our message. What we get back is a
MAP_HANDLE reference that enables us to start adding properties. The latter is accomplished by calling
Map_AddOrUpdate, which takes a reference to a MAP_HANDLE, the property name, and the property value.
With this API we can add as many properties as we like.
When the event is read from Event Hubs, the receiver can enumerate the properties and retrieve their
corresponding values. For example, in .NET this would be accomplished by accessing the Properties collection on
the EventData object.
In the previous example, we’re attaching properties to an event that we send to IoT Hub. Properties can also be
attached to messages received from IoT Hub. If we want to retrieve properties from a message, we can use code
such as the following in our message callback function:
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
. . .
. . .
}
The call to IoTHubMessage_Properties returns the MAP_HANDLE reference. We then pass that reference to
Map_GetInternals to obtain a reference to an array of the name/value pairs (as well as a count of the properties).
At that point it's a simple matter of enumerating the properties to get to the values we want.
You don't have to use properties in your application. However, if you need to set them on events or retrieve them
from messages, the IoTHubClient library makes it easy.
Message handling
As stated previously, when messages arrive from IoT Hub the IoTHubClient library responds by invoking a
registered callback function. There is a return parameter of this function that deserves some additional explanation.
Here’s an excerpt of the callback function in the iothub_client_sample_http sample application:
Note that the return type is IOTHUBMESSAGE_DISPOSITION_RESULT and in this particular case we return
IOTHUBMESSAGE_ACCEPTED. There are other values we can return from this function that change how the
IoTHubClient library reacts to the message callback. Here are the options.
IOTHUBMESSAGE_ACCEPTED – The message has been processed successfully. The IoTHubClient library
will not invoke the callback function again with the same message.
IOTHUBMESSAGE_REJECTED – The message was not processed and there is no desire to do so in the
future. The IoTHubClient library should not invoke the callback function again with the same message.
IOTHUBMESSAGE_ABANDONED – The message was not processed successfully, but the IoTHubClient
library should invoke the callback function again with the same message.
For the first two return codes, the IoTHubClient library sends a message to IoT Hub indicating that the message
should be deleted from the device queue and not delivered again. The net effect is the same (the message is deleted
from the device queue), but whether the message was accepted or rejected is still recorded. Recording this
distinction is useful to senders of the message who can listen for feedback and find out if a device has accepted or
rejected a particular message.
In the last case a message is also sent to IoT Hub, but it indicates that the message should be redelivered. Typically
you’ll abandon a message if you encounter some error but want to try to process the message again. In contrast,
rejecting a message is appropriate when you encounter an unrecoverable error (or if you simply decide you don’t
want to process the message).
In any case, be aware of the different return codes so that you can elicit the behavior you want from the
IoTHubClient library.
IOTHUB_CLIENT_HANDLE iotHubClientHandle;
iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
HostName=IOTHUBNAME.IOTHUBSUFFIX;DeviceId=DEVICEID;SharedAccessKey=SHAREDACCESSKEY
There are four pieces of information in this string: IoT Hub name, IoT Hub suffix, device ID, and shared access key.
You obtain the fully qualified domain name (FQDN ) of an IoT hub when you create your IoT hub instance in the
Azure portal — this gives you the IoT hub name (the first part of the FQDN ) and the IoT hub suffix (the rest of the
FQDN ). You get the device ID and the shared access key when you register your device with IoT Hub (as described
in the previous article).
IoTHubClient_CreateFromConnectionString gives you one way to initialize the library. If you prefer, you can
create a new IOTHUB_CLIENT_HANDLE by using these individual parameters rather than the device connection
string. This is achieved with the following code:
IOTHUB_CLIENT_CONFIG iotHubClientConfig;
iotHubClientConfig.iotHubName = "";
iotHubClientConfig.deviceId = "";
iotHubClientConfig.deviceKey = "";
iotHubClientConfig.iotHubSuffix = "";
iotHubClientConfig.protocol = HTTP_Protocol;
IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_LL_Create(&iotHubClientConfig);
Next steps
This article describes in detail the behavior of the IoTHubClient library found in the Azure IoT device SDK for
C. With this information, you should have a good understanding of the capabilities of the IoTHubClient library.
The next article provides similar detail on the serializer library.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Azure IoT device SDK for C – more about serializer
5/29/2018 • 21 min to read • Edit Online
The first article in this series introduced the Azure IoT device SDK for C. The next article provided a more
detailed description of the IoTHubClient. This article completes coverage of the SDK by providing a more detailed
description of the remaining component: the serializer library.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only
available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to choose
the right IoT Hub tier.
The introductory article described how to use the serializer library to send events to and receive messages from
IoT Hub. In this article, we extend that discussion by providing a more complete explanation of how to model your
data with the serializer macro language. The article also includes more detail about how the library serializes
messages (and in some cases how you can control the serialization behavior). We'll also describe some parameters
you can modify that determine the size of the models you create.
Finally, the article revisits some topics covered in previous articles such as message and property handling. As we'll
find out, those features work in the same way using the serializer library as they do with the IoTHubClient
library.
Everything described in this article is based on the serializer SDK samples. If you want to follow along, see the
simplesample_amqp and simplesample_http applications included in the Azure IoT device SDK for C.
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
BEGIN_NAMESPACE(WeatherStation);
DECLARE_MODEL(ContosoAnemometer,
WITH_DATA(ascii_char_ptr, DeviceId),
WITH_DATA(double, WindSpeed),
WITH_ACTION(TurnFanOn),
WITH_ACTION(TurnFanOff),
WITH_ACTION(SetAirResistance, int, Position)
);
END_NAMESPACE(WeatherStation);
As you can see, the modeling language is based on C macros. You always begin your definition with
BEGIN_NAMESPACE and always end with END_NAMESPACE. It's common to name the namespace for your
company or, as in this example, the project that you're working on.
What goes inside the namespace are model definitions. In this case, there is a single model for an anemometer.
Once again, the model can be named anything, but typically this is named for the device or type of data you want to
exchange with IoT Hub.
Models contain a definition of the events you can ingress to IoT Hub (the data) as well as the messages you can
receive from IoT Hub (the actions). As you can see from the example, events have a type and a name; actions have a
name and optional parameters (each with a type).
What’s not demonstrated in this sample are additional data types that are supported by the SDK. We'll cover that
next.
NOTE
IoT Hub refers to the data a device sends to it as events, while the modeling language refers to it as data (defined using
WITH_DATA). Likewise, IoT Hub refers to the data you send to devices as messages, while the modeling language refers to it
as actions (defined using WITH_ACTION). Be aware that these terms may be used interchangeably in this article.
TYPE DESCRIPTION
bool boolean
EDM_GUID GUID
EDM_BINARY binary
Let’s start with the last data type. The DECLARE_STRUCT allows you to define complex data types, which are
groupings of the other primitive types. These groupings allow us to define a model that looks like this:
DECLARE_STRUCT(TestType,
double, aDouble,
int, aInt,
float, aFloat,
long, aLong,
int8_t, aInt8,
uint8_t, auInt8,
int16_t, aInt16,
int32_t, aInt32,
int64_t, aInt64,
bool, aBool,
ascii_char_ptr, aAsciiCharPtr,
EDM_DATE_TIME_OFFSET, aDateTimeOffset,
EDM_GUID, aGuid,
EDM_BINARY, aBinary
);
DECLARE_MODEL(TestModel,
WITH_DATA(TestType, Test)
);
Our model contains a single data event of type TestType. TestType is a complex type that includes several
members, which collectively demonstrate the primitive types supported by the serializer modeling language.
With a model like this, we can write code to send data to IoT Hub that appears as follows:
testModel->Test.aDouble = 1.1;
testModel->Test.aInt = 2;
testModel->Test.aFloat = 3.0f;
testModel->Test.aLong = 4;
testModel->Test.aInt8 = 5;
testModel->Test.auInt8 = 6;
testModel->Test.aInt16 = 7;
testModel->Test.aInt32 = 8;
testModel->Test.aInt64 = 9;
testModel->Test.aBool = true;
testModel->Test.aAsciiCharPtr = "ascii string 1";
time_t now;
time(&now);
testModel->Test.aDateTimeOffset = GetDateTimeOffset(now);
EDM_GUID guid = { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F } };
testModel->Test.aGuid = guid;
Basically, we’re assigning a value to every member of the Test structure and then calling SendAsync to send the
Test data event to the cloud. SendAsync is a helper function that sends a single data event to IoT Hub:
void SendAsync(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const void *dataEvent)
{
unsigned char* destination;
size_t destinationSize;
if (SERIALIZE(&destination, &destinationSize, *(const unsigned char*)dataEvent) ==
{
// null terminate the string
char* destinationAsString = (char*)malloc(destinationSize + 1);
if (destinationAsString != NULL)
{
memcpy(destinationAsString, destination, destinationSize);
destinationAsString[destinationSize] = '\0';
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromString(destinationAsString);
if (messageHandle != NULL)
{
IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)0);
IoTHubMessage_Destroy(messageHandle);
}
free(destinationAsString);
}
free(destination);
}
}
This function serializes the given data event and sends it to IoT Hub using IoTHubClient_SendEventAsync. This
is the same code discussed in previous articles (SendAsync encapsulates the logic into a convenient function).
One other helper function used in the previous code is GetDateTimeOffset. This function transforms the given
time into a value of type EDM_DATE_TIME_OFFSET:
If you run this code, the following message is sent to IoT Hub:
Note that the serialization is JSON, which is the format generated by the serializer library. Also note that each
member of the serialized JSON object matches the members of the TestType that we defined in our model. The
values also exactly match those used in the code. However, note that the binary data is base64-encoded: "AQID" is
the base64 encoding of {0x01, 0x02, 0x03}.
This example demonstrates the advantage of using the serializer library -- it enables us to send JSON to the cloud,
without having to explicitly deal with serialization in our application. All we have to worry about is setting the
values of the data events in our model and then calling simple APIs to send those events to the cloud.
With this information, we can define models that include the range of supported data types, including complex
types (we could even include complex types within other complex types). However, he serialized JSON generated
by the example above brings up an important point. How we send data with the serializer library determines
exactly how the JSON is formed. That particular point is what we'll cover next.
BEGIN_NAMESPACE(Contoso);
DECLARE_STRUCT(TemperatureEvent,
int, Temperature,
EDM_DATE_TIME_OFFSET, Time);
DECLARE_STRUCT(HumidityEvent,
int, Humidity,
EDM_DATE_TIME_OFFSET, Time);
DECLARE_MODEL(Thermostat,
WITH_DATA(TemperatureEvent, Temperature),
WITH_DATA(HumidityEvent, Humidity)
);
END_NAMESPACE(Contoso);
Note that the model includes two data events: Temperature and Humidity. Unlike previous examples, the type of
each event is a structure defined using DECLARE_STRUCT. TemperatureEvent includes a temperature
measurement and a timestamp; HumidityEvent contains a humidity measurement and a timestamp. This model
gives us a natural way to model the data for the scenario described above. When we send an event to the cloud,
we'll either send a temperature/timestamp or a humidity/timestamp pair.
We can send a temperature event to the cloud using code such as the following:
time_t now;
time(&now);
thermostat->Temperature.Temperature = 75;
thermostat->Temperature.Time = GetDateTimeOffset(now);
We'll use hard-coded values for temperature and humidity in the sample code, but imagine that we’re actually
retrieving these values by sampling the corresponding sensors on the thermostat.
The code above uses the GetDateTimeOffset helper that was introduced previously. For reasons that will become
clear later, this code explicitly separates the task of serializing and sending the event. The previous code serializes
the temperature event into a buffer. Then, sendMessage is a helper function (included in simplesample_amqp)
that sends the event to IoT Hub:
static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
{
static unsigned int messageTrackingId;
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle != NULL)
{
IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)
(uintptr_t)messageTrackingId);
IoTHubMessage_Destroy(messageHandle);
}
free((void*)buffer);
}
This code is a subset of the SendAsync helper described in the previous section, so we won’t go over it again here.
When we run the previous code to send the Temperature event, this serialized form of the event is sent to IoT Hub:
{"Temperature":75, "Time":"2015-09-17T18:45:56Z"}
We're sending a temperature which is of type TemperatureEvent and that struct contains a Temperature and
Time member. This is directly reflected in the serialized data.
Similarly, we can send a humidity event with this code:
thermostat->Humidity.Humidity = 45;
thermostat->Humidity.Time = GetDateTimeOffset(now);
if (SERIALIZE(&destination, &destinationSize, thermostat->Humidity) == IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
{"Humidity":45, "Time":"2015-09-17T18:45:56Z"}
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
In this case we've eliminated the DECLARE_STRUCT macros and are simply defining the data items from our
scenario using simple types from the modeling language.
Just for the moment let’s ignore the Time event. With that aside, here’s the code to ingress Temperature:
time_t now;
time(&now);
thermostat->Temperature = 75;
{"Temperature":75}
And the code for sending the Humidity event appears as follows:
thermostat->Humidity = 45;
if (SERIALIZE(&destination, &destinationSize, thermostat->Humidity) == IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
{"Humidity":45}
So far there are still no surprises. Now let's change how we use the SERIALIZE macro.
The SERIALIZE macro can take multiple data events as arguments. This enables us to serialize the Temperature
and Humidity event together and send them to IoT Hub in one call:
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
More to the point, we didn’t model these events where Temperature and Humidity are in the same structure:
DECLARE_STRUCT(TemperatureAndHumidityEvent,
int, Temperature,
int, Humidity,
);
DECLARE_MODEL(Thermostat,
WITH_DATA(TemperatureAndHumidityEvent, TemperatureAndHumidity),
);
If we used this model, it would be easier to understand how Temperature and Humidity would be sent in the
same serialized message. However it may not be clear why it works that way when you pass both data events to
SERIALIZE using model 2.
This behavior is easier to understand if you know the assumptions that the serializer library is making. To make
sense of this let’s go back to our model:
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
Think of this model in object-oriented terms. In this case we’re modeling a physical device (a thermostat) and that
device includes attributes like Temperature and Humidity.
We can send the entire state of our model with code such as the following:
Sometimes you may only want to send some properties of the model to the cloud (this is especially true if your
model contains a large number of data events). It’s useful to send only a subset of data events, such as in our earlier
example:
{"Temperature":75, "Time":"2015-09-17T18:45:56Z"}
This generates exactly the same serialized event as if we had defined a TemperatureEvent with a Temperature
and Time member, just as we did with model 1. In this case we were able to generate exactly the same serialized
event by using a different model (model 2) because we called SERIALIZE in a different way.
The important point is that if you pass multiple data events to SERIALIZE, then it assumes each event is a
property in a single JSON object.
The best approach depends on you and how you think about your model. If you’re sending "events" to the cloud
and each event contains a defined set of properties, then the first approach makes a lot of sense. In that case you
would use DECLARE_STRUCT to define the structure of each event and then include them in your model with the
WITH_DATA macro. Then you send each event as we did in the first example above. In this approach you would
only pass a single data event to SERIALIZER.
If you think about your model in an object-oriented fashion, then the second approach may suit you. In this case,
the elements defined using WITH_DATA are the "properties" of your object. You pass whatever subset of events to
SERIALIZE that you like, depending on how much of your "object’s" state you want to send to the cloud.
Nether approach is right or wrong. Just be aware of how the serializer library works, and pick the modeling
approach that best fits your needs.
Message handling
So far this article has only discussed sending events to IoT Hub, and hasn't addressed receiving messages. The
reason for this is that what we need to know about receiving messages has largely been covered in an earlier
article. Recall from that article that you process messages by registering a message callback function:
You then write the callback function that’s invoked when a message is received:
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
{
/*buffer is not zero terminated*/
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
{
memcpy(temp, buffer, size);
temp[size] = '\0';
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
result =
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
IOTHUBMESSAGE_REJECTED;
free(temp);
}
}
return result;
}
This implementation of IoTHubMessage calls the specific function for each action in your model. For example, if
your model defines this action:
You're sending a serialized JSON object with two properties: Name is the name of the action (message) and
Parameters contains the parameters of that action.
For example, to invoke SetAirResistance you can send this message to a device:
The action name must exactly match an action defined in your model. The parameter names must match as well.
Also note case sensitivity. Name and Parameters are always uppercase. Make sure to match the case of your
action name and parameters in your model. In this example, the action name is "SetAirResistance" and not
"setairresistance".
The two other actions TurnFanOn and TurnFanOff can be invoked by sending these messages to a device:
This section described everything you need to know when sending events and receiving messages with the
serializer library. Before moving on, let's cover some parameters you can configure that control how large your
model is.
Macro configuration
If you’re using the Serializer library an important part of the SDK to be aware of is found in the azure-c-shared-
utility library. If you have cloned the Azure-iot-sdk-c repository from GitHub using the --recursive option, then you
will find this shared utility library here:
.\\c-utility
If you have not cloned the library, you can find it here.
Within the shared utility library, you will find the following folder:
azure-c-shared-utility\\macro\_utils\_h\_generator.
The program in this solution generates the macro_utils.h file. There’s a default macro_utils.h file included with the
SDK. This solution allows you to modify some parameters and then recreate the header file based on these
parameters.
The two key parameters to be concerned with are nArithmetic and nMacroParameters which are defined in
these two lines found in macro_utils.tt:
<#int nArithmetic=1024;#>
<#int nMacroParameters=124;/*127 parameters in one macro definition in C99 in chapter 5.2.4.1 Translation
limits*/#>
These values are the default parameters included with the SDK. Each parameter has the following meaning:
nMacroParameters – Controls how many parameters you can have in one DECL ARE_MODEL macro definition.
nArithmetic – Controls the total number of members allowed in a model.
The reason these parameters are important is because they control how large your model can be. For example,
consider this model definition:
DECLARE_MODEL(MyModel,
WITH_DATA(int, MyData)
);
As mentioned previously, DECLARE_MODEL is just a C macro. The names of the model and the WITH_DATA
statement (yet another macro) are parameters of DECLARE_MODEL. nMacroParameters defines how many
parameters can be included in DECLARE_MODEL. Effectively, this defines how many data event and action
declarations you can have. As such, with the default limit of 124 this means that you can define a model with a
combination of about 60 actions and data events. If you try to exceed this limit, you'll receive compiler errors that
look similar to this:
The nArithmetic parameter is more about the internal workings of the macro language than your application. It
controls the total number of members you can have in your model, including DECLARE_STRUCT macros. If you
start seeing compiler errors such as this, then you should try increasing nArithmetic:
If you want to change these parameters, modify the values in the macro_utils.tt file, recompile the
macro_utils_h_generator.sln solution, and run the compiled program. When you do so, a new macro_utils.h file is
generated and placed in the .\common\inc directory.
In order to use the new version of macro_utils.h, remove the serializer NuGet package from your solution and in
its place include the serializer Visual Studio project. This enables your code to compile against the source code of
the serializer library. This includes the updated macro_utils.h. If you want to do this for simplesample_amqp, start
by removing the NuGet package for the serializer library from the solution:
Then add this project to your Visual Studio solution:
.\c\serializer\build\windows\serializer.vcxproj
Now when you compile your solution, the updated macro_utils.h is included in your binary.
Note that increasing these values high enough can exceed compiler limits. To this point, the nMacroParameters is
the main parameter with which to be concerned. The C99 spec specifies that a minimum of 127 parameters are
allowed in a macro definition. The Microsoft compiler follows the spec exactly (and has a limit of 127), so you won't
be able to increase nMacroParameters beyond the default. Other compilers might allow you to do so (for
example, the GNU compiler supports a higher limit).
So far we've covered just about everything you need to know about how to write code with the serializer library.
Before concluding, let's revisit some topics from previous articles that you may be wondering about.
Additional topics
A few other topics worth mentioning again are property handling, using alternate device credentials, and
configuration options. These are all topics covered in a previous article. The main point is that all of these features
work in the same way with the serializer library as they do with the IoTHubClient library. For example, if you
want to attach properties to an event from your model, you use IoTHubMessage_Properties and
Map_AddorUpdate, the same way as described previously:
Whether the event was generated from the serializer library or created manually using the IoTHubClient library
does not matter.
For the alternate device credentials, using IoTHubClient_LL_Create works just as well as
IoTHubClient_CreateFromConnectionString for allocating an IOTHUB_CLIENT_HANDLE.
Finally, if you're using the serializer library, you can set configuration options with IoTHubClient_LL_SetOption
just as you did when using the IoTHubClient library.
A feature that is unique to the serializer library are the initialization APIs. Before you can start working with the
library, you must call serializer_init:
serializer_init(NULL);
This is done just before you call IoTHubClient_CreateFromConnectionString.
Similarly, when you're done working with the library, the last call you’ll make is to serializer_deinit:
serializer_deinit();
Otherwise, all of the other features listed above work the same in the serializer library as they do in the
IoTHubClient library. For more information about any of these topics, see the previous article in this series.
Next steps
This article describes in detail the unique aspects of the serializer library contained in the Azure IoT device SDK
for C. With the information provided you should have a good understanding of how to use models to send events
and receive messages from IoT Hub.
This also concludes the three-part series on how to develop applications with the Azure IoT device SDK for C.
This should be enough information to not only get you started but give you a thorough understanding of how the
APIs work. For additional information, there are a few samples in the SDK not covered here. Otherwise, the SDK
documentation is a good resource for additional information.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Internet of Things security from the ground up
5/29/2018 • 11 min to read • Edit Online
The Internet of Things (IoT) poses unique security, privacy, and compliance challenges to businesses worldwide.
Unlike traditional cyber technology where these issues revolve around software and how it is implemented, IoT
concerns what happens when the cyber and the physical worlds converge. Protecting IoT solutions requires
ensuring secure provisioning of devices, secure connectivity between these devices and the cloud, and secure data
protection in the cloud during processing and storage. Working against such functionality, however, are resource-
constrained devices, geographic distribution of deployments, and a large number of devices within a solution.
This article explores how the IoT solution accelerators provide a secure and private Internet of Things cloud
solution. The solution accelerators deliver a complete end-to-end solution, with security built into every stage from
the ground up. At Microsoft, developing secure software is part of the software engineering practice, rooted in
Microsoft's decades long experience of developing secure software. To ensure this, Security Development Lifecycle
(SDL ) is the foundational development methodology, coupled with a host of infrastructure-level security services
such as Operational Security Assurance (OSA) and the Microsoft Digital Crimes Unit, Microsoft Security
Response Center, and Microsoft Malware Protection Center.
The solution accelerators offer unique features that make provisioning, connecting to, and storing data from IoT
devices easy and transparent and, most of all, secure. This article examines the Azure IoT solution accelerators
security features and deployment strategies to ensure security, privacy, and compliance challenges are addressed.
Introduction
The Internet of Things (IoT) is the wave of the future, offering businesses immediate and real-world opportunities
to reduce costs, increase revenue, and transform their business. Many businesses, however, are hesitant to deploy
IoT in their organizations due to concerns about security, privacy, and compliance. A major point of concern comes
from the uniqueness of the IoT infrastructure, which merges the cyber and physical worlds together, compounding
individual risks inherent in these two worlds. Security of IoT pertains to ensuring the integrity of code running on
devices, providing device and user authentication, defining clear ownership of devices (as well as data generated
by those devices), and being resilient to cyber and physical attacks.
Then, there’s the issue of privacy. Companies want transparency concerning data collection, as in what’s being
collected and why, who can see it, who controls access, and so on. Finally, there are general safety issues of the
equipment along with the people operating them, and issues of maintaining industry standards of compliance.
Given the security, privacy, transparency, and compliance concerns, choosing the right IoT solution provider
remains a challenge. Stitching together individual pieces of IoT software and services provided by a variety of
vendors introduces gaps in security, privacy, transparency, and compliance, which may be hard to detect, let alone
fix. The choice of the right IoT software and service provider is based on finding providers that have extensive
experience running services, which span across verticals and geographies, but are also able to scale in a secure and
transparent fashion. Similarly, it helps for the selected provider to have decades of experience with developing
secure software running on billions of machines worldwide, and have the ability to appreciate the threat landscape
posed by this new world of the Internet of Things.
Conclusion
The Internet of Things starts with your things—the things that matter most to businesses. IoT can deliver amazing
value to a business by reducing costs, increasing revenue, and transforming business. Success of this
transformation largely depends on choosing the right IoT software and service provider. That means finding a
provider that not only catalyzes this transformation by understanding business needs and requirements, but also
provides services and software built with security, privacy, transparency, and compliance as major design
considerations. Microsoft has extensive experience with developing and deploying secure software and services
and continues to be a leader in this new age of Internet of Things.
The solution accelerators build in security measures by design, enabling secure monitoring of assets to improve
efficiencies, drive operational performance to enable innovation, and employ advanced data analytics to transform
businesses. With its layered approach towards security, multiple security features, and design patterns, the solution
accelerators help deploy an infrastructure that can be trusted to transform any business.
Additional information
Each solution accelerator creates instances of Azure services, such as:
Azure IoT Hub: Your gateway that connects the cloud to devices. You can scale to millions of connections per
hub and process massive volumes of data with per-device authentication support helping you secure your
solution.
Azure Cosmos DB: A scalable, fully-indexed database service for semi-structured data that manages metadata
for the devices you provision, such as attributes, configuration, and security properties. Azure Cosmos DB
offers high-performance and high-throughput processing, schema-agnostic indexing of data, and a rich SQL
query interface.
Azure Stream Analytics: Real-time stream processing in the cloud that enables you to rapidly develop and
deploy a low -cost analytics solution to uncover real-time insights from devices, sensors, infrastructure, and
applications. The data from this fully-managed service can scale to any volume while still achieving high
throughput, low latency, and resiliency.
Azure App Services: A cloud platform to build powerful web and mobile apps that connect to data anywhere;
in the cloud or on-premises. Build engaging mobile apps for iOS, Android, and Windows. Integrate with your
Software as a Service (SaaS ) and enterprise applications with out-of-the-box connectivity to dozens of cloud-
based services and enterprise applications. Code in your favorite language and IDE —.NET, Node.js, PHP,
Python, or Java—to build web apps and APIs faster than ever.
Logic Apps: The Logic Apps feature of Azure App Service helps integrate your IoT solution to your existing
line-of-business systems and automate workflow processes. Logic Apps enables developers to design
workflows that start from a trigger and then execute a series of steps—rules and actions that use powerful
connectors to integrate with your business processes. Logic Apps offers out-of-the-box connectivity to a vast
ecosystem of SaaS, cloud-based, and on-premises applications.
Azure blob storage: Reliable, economical cloud storage for the data that your devices send to the cloud.
See also
To learn more about securing your IoT solution, see:
IoT Security Best Practices
IoT Security Architecture
Secure your IoT deployment
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Internet of Things security best practices
5/29/2018 • 6 min to read • Edit Online
Securing an Internet of Things (IoT) infrastructure requires a rigorous security-in-depth strategy. This strategy
requires you to secure data in the cloud, protect data integrity while in transit over the public internet, and securely
provision devices. Each layer builds greater security assurance in the overall infrastructure.
See also
To learn more about securing your IoT solution, see:
IoT Security Architecture
Secure your IoT deployment
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Internet of Things security architecture
5/29/2018 • 24 min to read • Edit Online
When designing a system, it is important to understand the potential threats to that system, and add appropriate
defenses accordingly, as the system is designed and architected. It is important to design the product from the
start with security in mind because understanding how an attacker might be able to compromise a system helps
make sure appropriate mitigations are in place from the beginning.
Security in IoT
Connected special-purpose devices have a significant number of potential interaction surface areas and interaction
patterns, all of which must be considered to provide a framework for securing digital access to those devices. The
term “digital access” is used here to distinguish from any operations that are carried out through direct device
interaction where access security is provided through physical access control. For example, putting the device into
a room with a lock on the door. While physical access cannot be denied using software and hardware, measures
can be taken to prevent physical access from leading to system interference.
As you explore the interaction patterns, look at “device control” and “device data” with the same level of attention.
“Device control” can be classified as any information that is provided to a device by any party with the goal of
changing or influencing its behavior towards its state or the state of its environment. “Device data” can be
classified as any information that a device emits to any other party about its state and the observed state of its
environment.
In order to optimize security best practices, it is recommended that a typical IoT architecture is divided into several
component/zones as part of the threat modeling exercise. These zones are described fully throughout this section
and include:
Device,
Field Gateway,
Cloud gateways, and
Services.
Zones are broad way to segment a solution; each zone often has its own data and authentication and authorization
requirements. Zones can also be used to isolation damage and restrict the impact of low trust zones on higher
trust zones.
Each zone is separated by a Trust Boundary, which is noted as the dotted red line in the following diagram. It
represents a transition of data/information from one source to another. During this transition, the
data/information could be subject to Spoofing, Tampering, Repudiation, Information Disclosure, Denial of Service
and Elevation of Privilege (STRIDE ).
The components depicted within each boundary are also subjected to STRIDE, enabling a full 360 threat modeling
view of the solution. The following sections elaborate on each of the components and specific security concerns
and solutions that should be put into place.
The following sections discuss standard components typically found in these zones.
The Device Zone
The device environment is the immediate physical space around the device where physical access and/or “local
network” peer-to-peer digital access to the device is feasible. A “local network” is assumed to be a network that is
distinct and insulated from – but potentially bridged to – the public Internet, and includes any short-range wireless
radio technology that permits peer-to-peer communication of devices. It does not include any network
virtualization technology creating the illusion of such a local network and it does also not include public operator
networks that require any two devices to communicate across public network space if they were to enter a peer-to-
peer communication relationship.
The Field Gateway Zone
Field gateway is a device/appliance or some general-purpose server computer software that acts as
communication enabler and, potentially, as a device control system and device data processing hub. The field
gateway zone includes the field gateway itself and all devices that are attached to it. As the name implies, field
gateways act outside dedicated data processing facilities, are usually location bound, are potentially subject to
physical intrusion, and has limited operational redundancy. All to say that a field gateway is commonly a thing one
can touch and sabotage while knowing what its function is.
A field gateway is different from a mere traffic router in that it has had an active role in managing access and
information flow, meaning it is an application addressed entity and network connection or session terminal. An
NAT device or firewall, in contrast, does not qualify as field gateways since they are not explicit connection or
session terminals, but rather a route (or block) connections or sessions made through them. The field gateway has
two distinct surface areas. One faces the devices that are attached to it and represents the inside of the zone, and
the other faces all external parties and is the edge of the zone.
The cloud gateway zone
Cloud gateway is a system that enables remote communication from and to devices or field gateways from several
different sites across public network space, typically towards a cloud-based control and data analysis system, a
federation of such systems. In some cases, a cloud gateway may immediately facilitate access to special-purpose
devices from terminals such as tablets or phones. In the context discussed here, “cloud” is meant to refer to a
dedicated data processing system that is not bound to the same site as the attached devices or field gateways. Also
in a Cloud Zone, operational measures prevent targeted physical access and are not necessarily exposed to a
“public cloud” infrastructure.
A cloud gateway may potentially be mapped into a network virtualization overlay to insulate the cloud gateway
and all of its attached devices or field gateways from any other network traffic. The cloud gateway itself is not a
device control system or a processing or storage facility for device data; those facilities interface with the cloud
gateway. The cloud gateway zone includes the cloud gateway itself along with all field gateways and devices
directly or indirectly attached to it. The edge of the zone is a distinct surface area where all external parties
communicate through.
The services zone
A “service” is defined for this context as any software component or module that is interfacing with devices
through a field- or cloud gateway for data collection and analysis, as well as for command and control. Services
are mediators. They act under their identity towards gateways and other subsystems, store and analyze data,
autonomously issue commands to devices based on data insights or schedules and expose information and
control capabilities to authorized end users.
Information-devices versus special-purpose devices
PCs, phones, and tablets are primarily interactive information devices. Phones and tablets are explicitly optimized
around maximizing battery lifetime. They preferably turn off partially when not immediately interacting with a
person, or when not providing services like playing music or guiding their owner to a particular location. From a
systems perspective, these information technology devices are mainly acting as proxies towards people. They are
“people actuators” suggesting actions and “people sensors” collecting input.
Special-purpose devices, from simple temperature sensors to complex factory production lines with thousands of
components inside them, are different. These devices are much more scoped in purpose and even if they provide
some user interface, they are largely scoped to interfacing with or be integrated into assets in the physical world.
They measure and report environmental circumstances, turn valves, control servos, sound alarms, switch lights,
and do many other tasks. They help to do work for which an information device is either too generic, too
expensive, too large, or too brittle. The concrete purpose immediately dictates their technical design as well the
available monetary budget for their production and scheduled lifetime operation. The combination of these two
key factors constrains the available operational energy budget, physical footprint, and thus available storage,
compute, and security capabilities.
If something “goes wrong” with automated or remote controllable devices, for example, physical defects or control
logic defects to willful unauthorized intrusion and manipulation. The production lots may be destroyed, buildings
may be looted or burned down, and people may be injured or even die. This is a whole different class of damage
than someone maxing out a stolen credit card's limit. The security bar for devices that make things move, and also
for sensor data that eventually results in commands that cause things to move, must be higher than in any e-
commerce or banking scenario.
Device control and device data interactions
Connected special-purpose devices have a significant number of potential interaction surface areas and interaction
patterns, all of which must be considered to provide a framework for securing digital access to those devices. The
term “digital access” is used here to distinguish from any operations that are carried out through direct device
interaction where access security is provided through physical access control. For example, putting the device into
a room with a lock on the door. While physical access cannot be denied using software and hardware, measures
can be taken to prevent physical access from leading to system interference.
As you explore the interaction patterns, look at “device control” and “device data” with the same level of attention
while threat modeling. “Device control” can be classified as any information that is provided to a device by any
party with the goal of changing or influencing its behavior towards its state or the state of its environment. “Device
data” can be classified as any information that a device emits to any other party about its state and the observed
state of its environment.
The following diagram provides a simplified view of Microsoft’s IoT Architecture using a Data Flow Diagram
model that is used by the Microsoft Threat Modeling Tool:
It is important to note that the architecture separates the device and gateway capabilities. This approach enables
the user to leverage gateway devices that are more secure: they are capable of communicating with the cloud
gateway using secure protocols, which typically requires greater processing overhead that a native device - such as
a thermostat - could provide on its own. In the Azure services zone, assume that the Cloud Gateway is represented
by the Azure IoT Hub service.
Device and data sources/data transport
This section explores the architecture outlined previously through the lens of threat modeling and gives an
overview of how to address some of the inherent concerns. This example focuses on the core elements of a threat
model:
Processes (both under your control and external items)
Communication (also called data flows)
Storage (also called data stores)
Processes
In each of the categories outlined in the Azure IoT architecture, this example tries to mitigate a number of different
threats across the different stages data/information exists in: process, communication, and storage. Following is an
overview of the most common ones for the “process” category, followed by an overview of how these threats
could be best mitigated:
Spoofing (S ): An attacker may extract cryptographic key material from a device, either at the software or
hardware level, and subsequently access the system with a different physical or virtual device under the identity of
the device the key material has been taken from. A good illustration is remote controls that can turn any TV and
that are popular prankster tools.
Denial of Service (D ): A device can be rendered incapable of functioning or communicating by interfering with
radio frequencies or cutting wires. For example, a surveillance camera that had its power or network connection
intentionally knocked out cannot report data, at all.
Tampering (T): An attacker may partially or wholly replace the software running on the device, potentially
allowing the replaced software to leverage the genuine identity of the device if the key material or the
cryptographic facilities holding key materials were available to the illicit program. For example, an attacker may
leverage extracted key material to intercept and suppress data from the device on the communication path and
replace it with false data that is authenticated with the stolen key material.
Information Disclosure (I ): If the device is running manipulated software, such manipulated software could
potentially leak data to unauthorized parties. For example, an attacker may leverage extracted key material to
inject itself into the communication path between the device and a controller or field gateway or cloud gateway to
siphon off information.
Elevation of Privilege (E ): A device that does specific function can be forced to do something else. For example,
a valve that is programmed to open half way can be tricked to open all the way.
Field Gateway S Authenticating the If someone can spoof TLS RSA/PSK, IPSec,
Field gateway to Field Gateway, then it RFC 4279. All the
Cloud Gateway (such can present itself as same key storage and
as cert based, PSK, or any device. attestation concerns
Claim based.) of devices in general –
best case is use TPM.
6LowPAN extension
for IPSec to support
Wireless Sensor
Networks (WSN).
COMPONENT THREAT MITIGATION RISK IMPLEMENTATION
E Access control
mechanism for Field
Gateway
External Entity Device TID Strong pairing of the Eavesdropping the Securely pairing the
external entity to the connection to the external entity to the
device device. Interfering the device NFC/Bluetooth
communication with LE. Controlling the
the device operational panel of
the device (Physical)
Device storage TRID Storage encryption, Reading data from the Encryption, message
signing the logs storage (PII data), authentication code
tampering with (MAC), or digital
telemetry data. signature. Where
Tampering with possible, strong
queued or cached access control
command control through resource
data. Tampering with access control lists
configuration or (ACLs) or permissions.
firmware update
packages while
cached or queued
locally can lead to OS
and/or system
components being
compromised
Field Gateway storage TRID Storage encryption, Reading data from the BitLocker
(queuing the data) signing the logs storage (PII data),
tampering with
telemetry data,
tampering with
queued or cached
command control
data. Tampering with
configuration or
firmware update
packages (destined
for devices or field
gateway) while cached
or queued locally can
lead to OS and/or
system components
being compromised
Additional resources
For more information, see the following articles:
SDL Threat Modeling Tool
Microsoft Azure IoT reference architecture
See also
To learn more about securing your IoT solution see, Secure your IoT deployment
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Secure your IoT deployment
5/29/2018 • 7 min to read • Edit Online
This article provides the next level of detail for securing the Azure IoT-based Internet of Things (IoT)
infrastructure. It links to implementation level details for configuring and deploying each component. It also
provides comparisons and choices between various competing methods.
Securing the Azure IoT deployment can be divided into the following three security areas:
Device Security: Securing the IoT device while it is deployed in the wild.
Connection Security: Ensuring all data transmitted between the IoT device and IoT Hub is confidential and
tamper-proof.
Cloud Security: Providing a means to secure data while it moves through, and is stored in the cloud.
Conclusion
This article provides overview of implementation level details for designing and deploying an IoT infrastructure
using Azure IoT. Configuring each component to be secure is key in securing the overall IoT infrastructure. The
design choices available in Azure IoT provide some level of flexibility and choice; however, each choice may have
security implications. It is recommended that each of these choices be evaluated through a risk/cost assessment.
See also
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Device Authentication using X.509 CA Certificates
5/29/2018 • 4 min to read • Edit Online
This article describes how to use X.509 Certificate Authority (CA) certificates to authenticate devices connecting
IoT Hub. In this article you will learn:
How to get an X.509 CA certificate
How to register the X.509 CA certificate to IoT Hub
How to sign devices using X.509 CA certificates
How devices signed with X.509 CA are authenticated
Overview
The X.509 CA feature enables device authentication to IoT Hub using a Certificate Authority (CA). It greatly
simplifies initial device enrollment process, and supply chain logistics during device manufacturing. Learn more in
this scenario article about the value of using X.509 CA certificates for device authentication. We encourage you to
read this scenario article before proceeding as it explains why the steps that follow exist.
Prerequisite
Using the X.509 CA feature requires that you have an IoT Hub account. Learn how to create an IoT Hub instance if
you don't already have one.
Next Steps
Learn about the value of X.509 CA authentication in IoT.
Get started with IoT Hub Device Provisioning Service.
Conceptual understanding of X.509 CA certificates in
the IoT industry
5/29/2018 • 11 min to read • Edit Online
This article describes the value of using X.509 certificate authority (CA) certificates in IoT device manufacturing
and authentication to IoT Hub. It includes information about supply chain setup and highlight advantages.
This article describes:
What X.509 CA certificates are and how to get them
How to register your X.509 CA certificate to IoT Hub
How to set up a manufacturing supply chain for X.509 CA-based authentication
How devices signed with X.509 CA connect to IoT Hub
Overview
X.509 Certificate Authority (CA) authentication is an approach for authenticating devices to IoT Hub using a
method that dramatically simplifies device identity creation and life-cycle management in the supply chain.
A distinguishing attribute of the X.509 CA authentication is a one-to-many relationship a CA certificate has with
its downstream devices. This relationship enables registration of any number of devices into IoT Hub by
registering an X.509 CA certificate once, otherwise device unique certificates must be pre-registered for every
device before a device can connect. This one-to-many relationship also simplifies device certificates life-cycle
management operations.
Another important attribute of the X.509 CA authentication is simplification of supply chain logistics. Secure
authentication of devices requires that each device holds a unique secret like a key as basis for trust. In certificates-
based authentication, this secret is a private key. A typical device manufacturing flow involves multiple steps and
custodians. Securely managing device private keys across multiple custodians and maintaining trust is difficult and
expensive. Using certificate authorities solves this problem by signing each custodian into a cryptographic chain of
trust rather than entrusting them with device private keys. Each custodian in turn signs devices at their respective
process step of the manufacturing flow. The overall result is an optimal supply chain with built-in accountability
through use of the cryptographic chain of trust. It is worth noting that this process yields the most security when
devices protect their unique private keys. To this end, we urge the use of Hardware Secure Modules (HSM )
capable of internally generating private keys that will never see the light of day.
This article offers an end-to-end view of using the X.509 CA authentication, from supply chain setup to device
connection, while making use of a real world example to solidify understanding.
Introduction
The X.509 CA certificate is a digital certificate whose holder can sign other certificates. This digital certificate is
X.509 because it conforms to a certificate formatting standard prescribed by IETF's RFC 5280 standard, and is a
certificate authority (CA) because its holder can sign other certificates.
The use of X.509 CA is best understood in relation to a concrete example. Consider Company-X, a maker of
Smart-X-Widgets designed for professional installation. Company-X outsources both manufacturing and
installation. It contracts manufacturer Factory-Y to manufacture the Smart-X-Widgets, and service provider
Technician-Z to install. Company-X desires that Smart-X-Widget directly ships from Factory-Y to Technician-Z for
installation and that it connects directly to Company-X's instance of IoT Hub after installation without further
intervention from Company-X. To make this happen, Company-X need to complete a few one-time setup
operations to prime Smart-X-Widget for automatic connection. With the end-to-end scenario in mind, the rest of
this article is structured as follows:
Acquire the X.509 CA certificate
Register X.509 CA certificate to IoT Hub
Sign devices into a certificate chain of trust
Device connection
Details on how to accomplish these steps differ with various service providers.
Purchasing an X.509 CA certificate
Purchasing a CA certificate has the benefit of having a well-known root CA act as a trusted third party to vouch
for the legitimacy of IoT devices when the devices connect. Company-X would choose this option if they intend
Smart-X-Widget to interact with third party products or services after initial connection to IoT Hub.
To purchase an X.509 CA certificate, Company-X would choose a root certificates services provider. An internet
search for the phrase 'Root CA' will yield good leads. The root CA will guide Company-X on how to create the
public/private key pair and how to generate a Certificate Signing Request (CSR ) for their services. A CSR is the
formal process of applying for a certificate from a certificate authority. The outcome of this purchase is a certificate
for use as an authority certificate. Given the ubiquity of X.509 certificates, the certificate is likely to have been
properly formatted to IETF's RFC 5280 standard.
Creating a Self-Signed X.509 CA certificate
The process to create a Self-Signed X.509 CA certificate is similar to purchasing with the exception of involving a
third party signer like the root certificate authority. In our example, Company-X will sign its authority certificate
instead of a root certificate authority. Company-X may choose this option for testing until they're ready to
purchase an authority certificate. Company-X may also use a self-signed X.509 CA certificate in production, if
Smart-X-Widget is not intended to connect to any third party services outside of the IoT Hub.
Above cascade of certificates in the chain presents the logical hand-off of authority. Many supply chains follow this
logical hand-off whereby each intermediate CA gets signed into the chain while receiving all upstream CA
certificates, and the last intermediate CA finally signs each device and inject all the authority certificates from the
chain into the device. This is common when the contract manufacturing company with a hierarchy of factories
commissions a particular factory to do the manufacturing. While the hierarchy may be several levels deep (for
example, by geography/product type/manufacturing line), only the factory at the end gets to interact with the
device but the chain is maintained from the top of the hierarchy.
Alternate chains may have different intermediate CA interact with the device in which case the CA interacting with
the device injects certificate chain content at that point. Hybrid models are also possible where only some of the
CA has physical interaction with the device.
In our example, both Factory-Y and Technician-Z interact with the Smart-X-Widget. While Company-X owns
Smart-X-Widget, it actually does not physically interact with it in the entire supply chain. The certificate chain of
trust for Smart-X-Widget therefore comprise Company-X signing Factory-Y which in turn signs Technician-Z that
will then provide final signature to Smart-X-Widget. The manufacture and installation of Smart-X-Widget
comprise Factory-Y and Technician-Z using their respective intermediate CA certificates to sign each and every
Smart-X-Widgets. The end result of this entire process is Smart-X-Widgets with unique device certificates and
certificate chain of trust going up to Company-X CA certificate.
This is a good point to review the value of the X.509 CA method. Instead of pre-generating and handing off
certificates for every Smart-X-Widget into the supply chain, Company-X only hand to sign Factory-Y once. Instead
of having to track every device throughout the devices life-cycle, Company-X may not track and manage devices
through groups that naturally emerge from the supply chain process, for example, devices installed by Technician-
Z after July of some year.
Last but not least, the CA method of authentication infuses secure accountability into the device manufacturing
supply chain. Because of the certificate chain process, the actions of every member in the chain is
cryptographically recorded and verifiable.
This process relies on certain assumptions that must be surfaced for completeness. It requires independent
creation of device unique public/private key pair and that the private key be protected within the device.
Fortunately, secure silicon chips in the form of Hardware Secure Modules (HSM ) capable of internally generating
keys and protecting private keys exist. Company-X only need to add one of such chips into Smart-X-Widget's
component bill of materials.
Device Connection
Previous sections above have been building up to device connection. By simply registering an X.509 CA certificate
to IoT Hub one time, how do potentially millions of devices connect and get authenticated from the first time?
Simple; through the same certificate upload and proof-of-possession flow we earlier encountered with registering
the X.509 CA certificate.
Devices manufactured for X.509 CA authentication are equipped with device unique certificates and a certificate
chain from their respective manufacturing supply chain. Device connection, even for the very first time, happens in
a two-step process: certificate chain upload and proof-of-possession.
During the certificate chain upload, the device uploads its device unique certificate together with the certificate
chain installed within it to IoT Hub. Using the pre-registered X.509 CA certificate, IoT Hub can cryptographically
validate a couple of things, that the uploaded certificate chain is internally consistent, and that the chain was
originated by the valid owner of the X.509 CA certificate. Just was with the X.509 CA registration process, IoT Hub
would initiate a proof-of-possession challenge-response process to ascertain that the chain and hence device
certificate actually belongs to the device uploading it. It does so by generating a random challenge to be signed by
the device using its private key for validation by IoT Hub. A successful response triggers IoT Hub to accept the
device as authentic and grant it connection.
In our example, each Smart-X-Widget would upload its device unique certificate together with Factory-Y and
Technician-Z X.509 CA certificates and then respond to the proof-of-possession challenge from IoT Hub.
Notice that the foundation of trust rests in protecting private keys including device private keys. We therefore
cannot stress enough the importance of secure silicon chips in the form of Hardware Secure Modules (HSM ) for
protecting device private keys, and the overall best practice of never sharing any private keys, like one factory
entrusting another with its private key.
Set up X.509 security in your Azure IoT hub
5/29/2018 • 6 min to read • Edit Online
This tutorial simulates the steps you need to secure your Azure IoT hub using the X.509 Certificate Authentication.
For the purpose of illustration, we will show how to use the open source tool OpenSSL to create certificates locally
on your Windows machine. We recommend that you use this tutorial for test purposes only. For production
environment, you should purchase the certificates from a root certificate authority (CA ).
Prerequisites
This tutorial requires that you have the following resources ready:
You have created an IoT hub with your Azure subscription. See Create an IoT hub through portal for detailed
steps.
You have Visual Studio 2015 or Visual Studio 2017 installed on your machine.
2. In Solution Explorer, right-click the SimulateX509Device project, and then click Manage NuGet
Packages.... In the NuGet Package Manager window, select Browse and search for
microsoft.azure.devices.client. Select Install to install the Microsoft.Azure.Devices.Client package,
and accept the terms of use. This procedure downloads, installs, and adds a reference to the Azure IoT
device SDK NuGet package and its dependencies.
3. Add the following lines of code at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System.Security.Cryptography.X509Certificates;
Use the friendly device name you used in the preceding section in place of <your_device_id> placeholder.
5. Add the following function to create random numbers for temperature and humidity and send these values
to the hub:
static async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
Console.WriteLine("Device sending {0} messages to IoTHub...\n", MESSAGE_COUNT);
await deviceClient.SendEventAsync(eventMessage);
}
}
6. Finally, add the following lines of code to the Main function, replacing the placeholders device-id, your-iot-
hub -name and absolute-path-to -your-device-pfx-file as required by your setup.
try
{
var cert = new X509Certificate2(@"<absolute-path-to-your-device-pfx-file>", "123");
var auth = new DeviceAuthenticationWithX509Certificate("<device-id>", cert);
var deviceClient = DeviceClient.Create("<your-iot-hub-name>.azure-devices.net", auth,
TransportType.Amqp_Tcp_Only);
if (deviceClient == null)
{
Console.WriteLine("Failed to create DeviceClient!");
}
else
{
Console.WriteLine("Successfully created DeviceClient!");
SendEvent(deviceClient).Wait();
}
Console.WriteLine("Exiting...\n");
}
catch (Exception ex)
{
Console.WriteLine("Error in sample: {0}", ex.Message);
}
This code connects to your IoT hub by creating the connection string for your X.509 device. Once
successfully connected, it then sends temperature and humidity events to the hub, and waits for its
response.
7. Since this application accesses a .pfx file, you need to execute this in Admin mode. Build the Visual Studio
solution. Open a new command window as an Administrator, and navigate to the folder containing this
solution. Navigate to the bin/Debug path within the solution folder. Run the application
SimulateX509Device.exe from the Admin command window. You should see your device successfully
connecting to the hub and sending the events.
See also
To learn more about securing your IoT solution, see:
IoT Security Best Practices
IoT Security Architecture
Secure your IoT deployment
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
PowerShell scripts to manage CA-signed X.509
certificates
5/29/2018 • 6 min to read • Edit Online
The X.509 certificate-based security in the IoT Hub requires you to start with an X.509 certificate chain, which
includes the root certificate as well as any intermediate certificates up until the leaf certificate. This How to guide
walks you through sample PowerShell scripts that use OpenSSL to create and sign X.509 certificates. We
recommend you to use this guide for experimentation only, since many of these steps will happen during
manufacturing process in the real world. You can use these certificates to simulate security in your Azure IoT hub
using the X.509 Certificate Authentication. The steps in this guide create certificates locally on your Windows
machine.
Prerequisites
This tutorial assumes that you have acquired the OpenSSL binaries. You may either
download the OpenSSL source code and build the binaries on your machine, or
download and install any third-party OpenSSL binaries, for example, from this project on SourceForge.
$openSSLBinSource = "<full_path_to_the_binaries>\OpenSSL\bin"
$errorActionPreference = "stop"
$rootCACerFileName = "./RootCA.cer"
$rootCAPemFileName = "./RootCA.pem"
$intermediate1CAPemFileName = "./Intermediate1.pem"
$intermediate2CAPemFileName = "./Intermediate2.pem"
$intermediate3CAPemFileName = "./Intermediate3.pem"
3. Run the following script that copies the OpenSSL binaries to your working directory and sets the
environment variables:
function Initialize-CAOpenSSL()
{
Write-Host ("Beginning copy of openssl binaries to {0} (and setting up env variables...)" -f
$openSSLBinDir)
if (-not (Test-Path $openSSLBinDir))
{
mkdir $openSSLBinDir | Out-Null
}
Write-Host "Success"
}
Initialize-CAOpenSSL
4. Next, run the following script that searches whether a certificate by the specified Subject Name is already
installed, and whether OpenSSL is configured correctly on your machine:
function Get-CACertBySubjectName([string]$subjectName)
{
$certificates = gci -Recurse Cert:\LocalMachine\ |? { $_.gettype().name -eq "X509Certificate2" }
$cert = $certificates |? { $_.subject -eq $subjectName -and $_.PSParentPath -eq
"Microsoft.PowerShell.Security\Certificate::LocalMachine\My" }
if ($NULL -eq $cert)
{
throw ("Unable to find certificate with subjectName {0}" -f $subjectName)
}
write $cert
}
function Test-CAPrerequisites()
{
$certInstalled = $null
try
{
$certInstalled = Get-CACertBySubjectName $_rootCertSubject
}
catch {}
2. The following PowerShell function creates intermediate X.509 certificates using the preceding function as
well as the OpenSSL binaries.
function New-CAIntermediateCert([string]$commonName,
[Microsoft.CertificateServices.Commands.Certificate]$signingCert, [string]$pemFileName)
{
$certFileName = ($commonName + ".cer")
$newCert = New-CASelfsignedCertificate $commonName $signingCert
Export-Certificate -Cert $newCert -FilePath $certFileName -Type CERT | Out-Null
Import-Certificate -CertStoreLocation "cert:\LocalMachine\CA" -FilePath $certFileName | Out-Null
del $certFileName
write $newCert
}
3. The following PowerShell function creates the X.509 certificate chain. Read Certificate chains for more
information.
function New-CACertChain()
{
Write-Host "Beginning to install certificate chain to your LocalMachine\My store"
$rootCACert = New-CASelfsignedCertificate $_rootCertSubject $null
function New-CAVerificationCert([string]$requestedSubjectName)
{
$verifyRequestedFileName = ".\verifyCert4.cer"
$rootCACert = Get-CACertBySubjectName $_rootCertSubject
Write-Host "Using Signing Cert:::"
Write-Host $rootCACert
Write-Host ("Certificate with subject {0} has been output to {1}" -f $requestedSubjectName, (Join-Path
(get-location).path $verifyRequestedFileName))
}
New-CAVerificationCert "<your verification code>"
This code creates a certificate with the given subject name, signed by the CA, as a file named VerifyCert4.cer in
your working directory. This certificate file will help validate with your IoT hub that you have the signing
permission (that is, the private key) of this CA.
# Export the PFX of the cert we've just created. The PFX is a format that contains both public and
private keys.
Export-PFXCertificate -cert $newDeviceCertPfx -filePath $newDevicePfxFileName -password
$certSecureStringPwd
if (-not (Test-Path $newDevicePfxFileName))
{
throw ("Error: CERT file {0} doesn't exist" -f $newDevicePfxFileName)
}
# Begin the massaging. First, turn the PFX into a PEM file which contains public key, private key, and
other attributes.
Write-Host ("When prompted for password by openssl, enter the password as {0}" -f $_privateKeyPassword)
openssl pkcs12 -in $newDevicePfxFileName -out $newDevicePemAllFileName -nodes
Write-Host ("Certificate with subject {0} has been output to {1}" -f $cnNewDeviceSubjectName, (Join-Path
(get-location).path $newDevicePemPublicFileName))
}
Then run New-CADevice "<yourTestDevice>" in your PowerShell window, using the friendly name that you used to
create your device. When prompted for the password for the CA's private key, enter "123". This creates a .pfx file in
your working directory.
Clean up certificates
In your start bar or Settings app, search for and select Manage computer certificates. Remove any certificates
issued by Azure IoT CA TestOnly*. These certificates should exist in the following three locations:
Certificates - Local Computer > Personal > Certificates
Certificates - Local Computer > Trusted Root Certification Authorities > Certificates
Certificates - Local Computer > Intermediate Certificate Authorities > Certificates
Use iothub-explorer to send and receive messages
between your device and IoT Hub
5/29/2018 • 2 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
iothub-explorer has a handful of commands that makes IoT Hub management easier. This tutorial focuses on
how to use iothub-explorer to send and receive messages between your device and your IoT hub.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
NOTE
Get <device-id> and <IoTHubConnectionString> from your IoT hub. Make sure you've finished the previous
tutorial. Or you can try to use
iothub-explorer monitor-events <device-id> --login "HostName=<my-hub>.azure-
devices.net;SharedAccessKeyName=<my-policy>;SharedAccessKey=<my-policy-key>"
if you have HostName , SharedAccessKeyName and SharedAccessKey .
The command blinks the LED that is connected to your device and sends the message to your device.
NOTE
There is no need for the device to send a separate ack command back to your IoT hub upon receiving the message.
Next steps
You’ve learned how to monitor device-to-cloud messages and send cloud-to-device messages between your IoT
device and Azure IoT Hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Save IoT hub messages that contain sensor data to
your Azure blob storage
5/29/2018 • 3 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
What you do
Create an Azure storage account.
Prepare your IoT hub to route messages to storage.
NOTE
You can skip this step if you are not doing additional processing on your telemetry messages.
Click Add from the Routes pane and enter a name for the route. Select Device Messages as the data source
and events as the endpoint. Enter true as the query string, then click Save.
Verify your message in your storage container
1. Run the sample application on your device to send messages to your IoT hub.
2. Download and install Azure Storage Explorer.
3. Open Storage Explorer, click Add an Azure Account > Sign in, and then sign in to your Azure account.
4. Click your Azure subscription > Storage Accounts > your storage account > Blob Containers > your
container.
You should see messages sent from your device to your IoT hub logged in the blob container.
Next steps
You’ve successfully created your Azure storage account and routed messages from IoT Hub to a blob container
in that storage account.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Visualize real-time sensor data from Azure IoT Hub
using Power BI
5/29/2018 • 4 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
What you do
Get your IoT hub ready for data access by adding a consumer group.
Create, configure and run a Stream Analytics job for data transfer from your IoT hub to your Power BI
account.
Create and publish a Power BI report to visualize the data.
3. Click Create.
Add an input to the Stream Analytics job
1. Open the Stream Analytics job.
2. Under Job Topology, click Inputs.
3. In the Inputs pane, click Add, and then enter the following information:
Input alias: The unique alias for the input.
Source: Select IoT hub.
Consumer group: Select the consumer group you just created.
4. Click Create.
7. Create another line chart to show real-time humidity over time. To do this, follow the same steps above
and place EventEnqueuedUtcTime on the x-axis and humidity on the y-axis.
Next steps
You’ve successfully used Power BI to visualize real-time sensor data from your Azure IoT hub. There is an
alternate way to visualize data from Azure IoT Hub. See Use Azure Web Apps to visualize real-time sensor data
from Azure IoT Hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Visualize real-time sensor data from your Azure IoT
hub by using the Web Apps feature of Azure App
Service
5/29/2018 • 4 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
What you do
Create a web app in the Azure portal.
Get your IoT hub ready for data access by adding a consumer group.
Configure the web app to read sensor data from your IoT hub.
Upload a web application to be hosted by the web app.
Open the web app to see real-time temperature and humidity data from your IoT hub.
Configure the web app to read data from your IoT hub
1. Open the web app you’ve just provisioned.
2. Click Application settings, and then, under App settings, add the following key/value pairs:
KEY VALUE
Azure.IoT.IoTHub.ConsumerGroup The name of the consumer group that you add to your
IoT hub
3. Click Application settings, under General settings, toggle the Web sockets option, and then click
Save.
NOTE
<Git clone URL> is the URL of the Git repository found on the Overview page of the web app.
Open the web app to see real-time temperature and humidity data
from your IoT hub
On the Overview page of your web app, click the URL to open the web app.
You should see the real-time temperature and humidity data from your IoT hub.
NOTE
Ensure the sample application is running on your device. If not, you will get a blank chart, you can refer to the tutorials
under Setup your device.
Next steps
You've successfully used your web app to visualize real-time sensor data from your IoT hub.
For an alternative way to visualize data from Azure IoT Hub, see Use Power BI to visualize real-time sensor data
from your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Weather forecast using the sensor data from your
IoT hub in Azure Machine Learning
5/29/2018 • 5 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
Machine learning is a technique of data science that helps computers learn from existing data to forecast future
behaviors, outcomes, and trends. Azure Machine Learning is a cloud predictive analytics service that makes it
possible to quickly create and deploy predictive models as analytics solutions.
What you do
Deploy the weather prediction model as a web service.
Get your IoT hub ready for data access by adding a consumer group.
Create a Stream Analytics job and configure the job to:
Read temperature and humidity data from your IoT hub.
Call the web service to get the rain chance.
Save the result to an Azure blob storage.
Use Microsoft Azure Storage Explorer to view the weather forecast.
3. Click Run to validate the steps in the model. This step might take 2 minutes to complete.
NOTE
Ensure that you download the Excel 2010 or earlier workbook even if you are running a later version of Excel
on your computer.
10. Open the Excel workbook, make a note of the WEB SERVICE URL and ACCESS KEY.
3. Click Create.
Add an input to the Stream Analytics job
1. Open the Stream Analytics job.
2. Under Job Topology, click Inputs.
3. In the Inputs pane, click Add, and then enter the following information:
Input alias: The unique alias for the input.
Source: Select IoT hub.
Consumer group: Select the consumer group you created.
4. Click Create.
Add an output to the Stream Analytics job
1. Under Job Topology, click Outputs.
2. In the Outputs pane, click Add, and then enter the following information:
Output alias: The unique alias for the output.
Sink: Select Blob Storage.
Storage account: The storage account for your blob storage. You can create a storage account or use an
existing one.
Container: The container where the blob is saved. You can create a container or use an existing one.
Event serialization format: Select CSV.
3. Click Create.
Add a function to the Stream Analytics job to call the web service you deployed
1. Under Job Topology, click Functions > Add.
2. Enter the following information:
Function Alias: Enter machinelearning .
Function Type: Select Azure ML.
Import option: Select Import from a different subscription.
URL: Enter the WEB SERVICE URL that you noted down from the Excel workbook.
Key: Enter the ACCESS KEY that you noted down from the Excel workbook.
3. Click Create.
Configure the query of the Stream Analytics job
1. Under Job Topology, click Query.
2. Replace the existing code with the following code:
WITH machinelearning AS (
SELECT EventEnqueuedUtcTime, temperature, humidity, machinelearning(temperature, humidity) as
result from [YourInputAlias]
)
Select System.Timestamp time, CAST (result.[temperature] AS FLOAT) AS temperature, CAST (result.
[humidity] AS FLOAT) AS humidity, CAST (result.[Scored Probabilities] AS FLOAT ) AS 'probabalities of
rain'
Into [YourOutputAlias]
From machinelearning
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you deploy
a sample application to run on your device. The application sends collected sensor data to your IoT hub.
iothub-explorer is a CLI tool that you run on a host computer to manage device identities in your IoT hub registry.
It comes with management options that you can use to perform various tasks.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Twin desired properties Put a device into certain states, such as setting an LED to
green or setting the telemetry send interval to 30 minutes.
Twin reported properties Get the reported state of a device. For example, the device
reports the LED is blinking now.
Twin tags Store device-specific metadata in the cloud. For example, the
deployment location of a vending machine.
Cloud-to-device messages Send notifications to a device. For example, "It is very likely to
rain today. Don't forget to bring an umbrella."
Device twin queries Query all device twins to retrieve those with arbitrary
conditions, such as identifying the devices that are available for
use.
For more detailed explanation on the differences and guidance on using these options, see Device-to-cloud
communication guidance and Cloud-to-device communication guidance.
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it. For more information about device twins, see Get
started with device twins.
What you do
Run iothub-explorer with various management options.
Invoke the stop method in the device app to stop sending messages to your IoT hub by running the following
command:
One of the properties is $metadata.$lastUpdated which shows the last time this device sends or receives a
message.
Add a field role = temperature&humidity to the device by running the following command:
See Use iothub-explorer to send and receive messages between your device and IoT Hub for a real scenario of
using this command.
Query all devices except those with a tag of role = 'temperature&humidity' by running the following command:
Next steps
You've learned how to use iothub-explorer with various management options.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Use the IoT extension for Azure CLI 2.0 for Azure
IoT Hub device management
5/29/2018 • 4 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
The IoT extension for Azure CLI 2.0 is a new open source IoT extension that adds to the capabilities of Azure
CLI 2.0. Azure CLI 2.0 includes commands for interacting with Azure resource manager and management
endpoints. For example, you can use Azure CLI 2.0 to create an Azure VM or an IoT hub. A CLI extension
enables an Azure service to augment the Azure CLI giving you access to additional service-specific capabilities.
The IoT extension gives IoT developers command line access to all IoT Hub, IoT Edge, and IoT Hub Device
Provisioning Service capabilities.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Twin desired properties Put a device into certain states, such as setting an LED to
green or setting the telemetry send interval to 30 minutes.
Twin reported properties Get the reported state of a device. For example, the device
reports the LED is blinking now.
Twin tags Store device-specific metadata in the cloud. For example, the
deployment location of a vending machine.
Device twin queries Query all device twins to retrieve those with arbitrary
conditions, such as identifying the devices that are available
for use.
For more detailed explanation on the differences and guidance on using these options, see Device-to-cloud
communication guidance and Cloud-to-device communication guidance.
Device twins are JSON documents that store device state information (metadata, configurations, and
conditions). IoT Hub persists a device twin for each device that connects to it. For more information about
device twins, see Get started with device twins.
What you do
Run Azure CLI 2.0 and the IoT extension for Azure CLI 2.0 with various management options.
az login
Direct methods
az iot hub invoke-device-method --device-id <your device id> --hub-name <your hub name> --method-name <the
method name> --method-payload <the method payload>
az iot hub device-twin update -n <your hub name> -d <your device id> --set properties.desired.interval =
3000
az iot hub device-twin show -n <your hub name> -d <your device id>
One of the twin reported properties is $metadata.$lastUpdated which shows the last time the device app
updated its reported property set.
az iot hub device-twin show --hub-name <your hub name> --device-id <your device id>
Add a field role = temperature&humidity to the device by running the following command:
az iot hub device-twin update --hub-name <your hub name> --device-id <your device id> --set tags =
'{"role":"temperature&humidity"}}'
az iot hub query --hub-name <your hub name> --query-command "SELECT * FROM devices WHERE tags.role =
'temperature&humidity'"
Query all devices except those with a tag of role = 'temperature&humidity' by running the following command:
az iot hub query --hub-name <your hub name> --query-command "SELECT * FROM devices WHERE tags.role !=
'temperature&humidity'"
Next steps
You’ve learned how to monitor device-to-cloud messages and send cloud-to-device messages between your IoT
device and Azure IoT Hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
IoT remote monitoring and notifications with Azure
Logic Apps connecting your IoT hub and mailbox
5/29/2018 • 4 min to read • Edit Online
NOTE
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you
deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
Azure Logic Apps provides a way to automate processes as a series of steps. A logic app can connect across
various services and protocols. It begins with a trigger such as 'When an account is added', and followed by a
combination of actions, one like 'sending a push notification'. This feature makes Logic Apps a perfect IoT
solution for IoT monitoring, such as staying alert for anomalies, among other usage scenarios.
What you do
Create a service bus namespace and add a queue to it.
Add an endpoint and a routing rule to your IoT hub.
Create, configure, and test a logic app.
4. Click Service Bus – When one or more messages arrive in a queue (auto-complete).
5. Create a service bus connection.
a. Enter a connection name.
b. Click the service bus namespace > the service bus policy > Create.
c. Click Continue after the service bus connection is created.
d. Select the queue that you created and enter 175 for Maximum message count
NOTE
Your email service provider may need to verify the sender identity to make sure it is you who sends the email.
Next steps
You have successfully created a logic app that connects your IoT hub and your mailbox for temperature
monitoring and notifications.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Use IoT DevKit AZ3166 with Azure Function and
Cognitive Services to make a language translator
5/29/2018 • 3 min to read • Edit Online
In this article, you learn how to make IoT DevKit as a language translator by using Azure Cognitive Services. It
records your voice and translates it to English text shown on the DevKit screen.
The MXChip IoT DevKit is an all-in-one Arduino compatible board with rich peripherals and sensors. You can
develop for it using Visual Studio Code extension for Arduino. And it comes with a growing projects catalog to
guide you prototype Internet of Things (IoT) solutions that take advantage of Microsoft Azure services.
After Azure Function deploys successfully, fill in the azure_config.h file with function app name. You can navigate to
Azure portal to find it:
NOTE
If the Azure Function does not work properly, check this FAQs section to resolve it.
4. After task config-device-connection finished, click F1 to load VS Code commands and select
Arduino: Upload , then VS Code starts verifying and uploading the Arduino sketch:
How it works
The Arduino sketch records your voice then posts an HTTP request to trigger Azure Functions. Azure Functions
calls the cognitive service speech translator API to do the translation. After Azure Functions gets the translation
text, it sends a C2D message to the device. Then the translation is displayed on the screen.
Change device ID
The default device ID registered in Azure IoT Hub is AZ3166. If you want to modify it, follow instructions here.
Next Steps
Now you make the IoT DevKit as a translator by using Azure Function and Cognitive Services. In this tutorial, you
learned how to:
Use Visual Studio Code task to automate cloud provisions
Configure Azure IoT device connection string
Deploy Azure Function
Test the voice message translation
Advance to the other tutorials to learn:
Connect IoT DevKit AZ3166 to Azure IoT Remote Monitoring solution accelerator
Shake, Shake for a Tweet -- Retrieve a Twitter
message with Azure Functions!
5/29/2018 • 5 min to read • Edit Online
In this project, you learn how to use the motion sensor to trigger an event using Azure Functions. The app retrieves
a random tweet with a #hashtag you configure in your Arduino sketch. The tweet displays on the DevKit screen.
NOTE
When launching VS Code, you may receive an error message that the Arduino IDE or related board package can't be found. If
this error occurs, close VS Code and launch the Arduino IDE again. VS Code should now locate the Arduino IDE path correctly.
Replace the string iot within the curly braces with your preferred hashtag. DevKit later retrieves a random tweet
that includes the hashtag you specify in this step.
NOTE
Occasionally, the Azure Function may not work properly. To resolve this issue when it occurs, check this FAQ step.
NOTE
You may get an "Error: AZ3166: Unknown package" error message. This error occurs when the board package index isn't
refreshed correctly. To resolve this issue, check this FAQ step.
macOS
1. Put the DevKit into configuration mode: Hold down button A, then push and release the reset button. The
screen displays 'Configuration'.
2. Use Cmd+P to run task device-upload to set the connection string that is retrieved from the
task cloud-provision step.
3. VS Code then starts verifying and uploading the Arduino sketch to your DevKit:
4. The DevKit reboots and starts running the code.
NOTE
You may get an "Error: AZ3166: Unknown package" error message. This error occurs when the board package index isn't
refreshed correctly. To resolve this issue, check this FAQ step.
Ready to shake...
Processing...
Press B to read...
Display a random tweet...
How it works
The Arduino sketch sends an event to the Azure IoT Hub. This event triggers the Azure Functions app. Azure
Functions app contains the logic to connect to Twitter's API and retrieve a tweet. It then wraps the tweet text into a
C2D (Cloud-to-device) message and sends it back to the device.
5. Update the code for run.csx within Functions > shakeshake-cs with your own token:
...
string authHeader = "Bearer " + "[your own token]";
...
Feedback
If you experience other problems, refer to FAQs or contact us from the following channels:
Gitter.im
Stackoverflow
Next steps
Now that you have learned how to connect a DevKit device to your Azure IoT Remote Monitoring solution
accelerator and retrieve a tweet, here are the suggested next steps:
Azure IoT Remote Monitoring solution accelerator overview
Send messages to an MQTT server
5/29/2018 • 2 min to read • Edit Online
Internet of Things (IoT) systems often deal with intermittent, poor quality, or slow internet connections. MQTT is a
machine-to-machine (M2M ) connectivity protocol, which was developed with such challenges in mind.
The MQTT client library used here is part of the Eclipse Paho project, which provides APIs for using MQTT over
multiple means of transport.
NOTE
You can also open example from command palette. Use Ctrl+Shift+P (macOS: Cmd+Shift+P ) to open the command
palette, type Arduino, and then find and select Arduino: Examples.
2. Click the power plug icon on the status bar to open the Serial Monitor:
3. On the status bar, click the number that represents the Baud Rate and set it to 115200 :
The Serial Monitor displays all the messages sent by the sample sketch. The sketch connects the DevKit to Wi-Fi.
Once the Wi-Fi connection is successful, the sketch sends a message to the MQTT broker. After that, the sample
repeatedly sends two "iot.eclipse.org" messages using QoS 0 and QoS 1, respectively.
Next steps
Now that you have learned how to configure your MXChip Iot DevKit as an MQTT client and use the MQTT Client
library to send messages to an MQTT broker, here are the suggested next steps:
Azure IoT Remote Monitoring solution accelerator overview
Connect an MXChip IoT DevKit device to your Azure IoT Central application
Door Monitor
5/29/2018 • 5 min to read • Edit Online
The MXChip IoT DevKit contains a built-in magnetic sensor. In this project, you detect the presence or absence of a
nearby strong magnetic field--in this case, coming from a small. permanent magnet.
NOTE
For a practical use of this project:
Mount a magnet to the edge of a door.
Mount the DevKit on the door jamb close to the magnet. Opening or closing the door will trigger the sensor, resulting in
your receiving an email notification of the event.
NOTE
If you have already deployed a SendGrid service, you may proceed directly to Deploy IoT Hub in Azure.
SendGrid Deployment
To provision Azure services, use the Deploy to Azure button. This button enables quick and easy deployment of
your open-source projects to Microsoft Azure.
Click the Deploy to Azure button, below.
NOTE
Check the Pin to dashboard option to make this application easier to find in the future.
NOTE
If you don't see the following page, you may need to first sign in to your Azure account.
NOTE
Check the Pin to dashboard option to make this application easier to find in the future.
Build and upload the code
Start VS Code
Make sure your DevKit is not connected to your computer.
Start VS Code.
Connect the DevKit to your computer.
NOTE
When you launch VS Code, you may receive an error message stating that it cannot find the Arduino IDE or related board
package. If you receive this error, close VS Code, launch the Arduino IDE again, and VS Code should locate the Arduino IDE
path correctly.
In the VS Code terminal, an interactive command line guides you through provisioning the required Azure services.
Select all of the same items from the prompted list that you previously provisioned in Deploy IoT Hub in Azure.
NOTE
If the page hangs in the loading status when trying to sign in to Azure, refer to FAQ to resolve this issue.
NOTE
Occasionally, you may receive an "Error: AZ3166: Unknown package" error message. This error occurs when the board
package index is not refreshed correctly. To resolve this error, refer to this FAQ.
macOS
1. Put the DevKit into configuration mode: Hold down button A, then push and release the reset button. The
screen displays 'Configuration'.
2. Use Cmd+P to run task device-upload .
This procedure sets the connection string that is retrieved from the Provision Azure services step.
VS Code then starts verifying and uploading the Arduino sketch to the DevKit:
The DevKit reboots and starts running the code.
NOTE
Occasionally, you may receive an "Error: AZ3166: Unknown package" error message. This error occurs when the board
package index is not refreshed correctly. To resolve this error, refer to this FAQ.
Azure provides services specifically developed for diverse types of connectivity and communication to help you
connect your data to the power of the cloud. Both Azure IoT Hub and Azure Event Hubs are cloud services that
can ingest large amounts of data and process or store that data for business insights. The two services are similar
in that they both support ingestion of data with low latency and high reliability, but they are designed for
different purposes. IoT Hub was developed specifically to address the unique requirements of connecting IoT
devices, at-scale, to the Azure Cloud while Event Hubs was designed for big data streaming. This is why Microsoft
recommends using Azure IoT Hub to connect IoT devices to Azure
Azure IoT Hub is the cloud gateway that connects IoT devices to gather data to drive business insights and
automation. In addition, IoT Hub includes features that enrich the relationship between your devices and your
backend systems. Bi-directional communication capabilities mean that while you receive data from devices you
can also send commands and policies back to devices, for example, to update properties or invoke device
management actions. This cloud-to-device connectivity also powers the important capability of delivering cloud
intelligence to your edge devices with Azure IoT Edge. The unique device-level identity provided by IoT Hub
helps better secure your IoT solution from potential attacks.
Azure Event Hubs is the big data streaming service of Azure. It is designed for high throughput data streaming
scenarios where customers may send billions of requests per day. Event Hubs uses a partitioned consumer
model to scale out your stream and is integrated into the big data and analytics services of Azure including
Databricks, Stream Analytics, ADLS, and HDInsight. With features like Event Hubs Capture and Auto-Inflate, this
service is designed to support your big data apps and solutions. Additionally, IoT Hub leverages Event Hubs for
its telemetry flow path, so your IoT solution also benefits from the tremendous power of Event Hubs.
To summarize, while both solutions are designed for data ingestion at a massive scale, only IoT Hub provides the
rich IoT-specific capabilities that are designed for you to maximize the business value of connecting your IoT
devices to the Azure cloud. If your IoT journey is just beginning, starting with IoT Hub to support your data
ingestion scenarios will assure that you have instant access to the full-featured IoT capabilities once your
business and technical needs require them.
The following table provides details about how the two tiers of IoT Hub compare to Event Hubs when you're
evaluating them for IoT capabilities. For more information about the standard and basic tiers of IoT Hub, see
How to choose the right IoT Hub tier.
IOT CAPABILITY IOT HUB STANDARD TIER IOT HUB BASIC TIER EVENT HUBS
Device-to-cloud messaging
Per-device identity
IOT CAPABILITY IOT HUB STANDARD TIER IOT HUB BASIC TIER EVENT HUBS
Cloud-to-device messaging
IoT Edge
Even if the only use case is device-to-cloud data ingestion, we highly recommend using IoT Hub as it provides a
service that is designed for IoT device connectivity.
Next steps
To further explore the capabilities of IoT Hub, see the IoT Hub developer guide
Choose the right IoT Hub tier for your
solution
5/29/2018 • 4 min to read • Edit Online
Every IoT solution is different, so Azure IoT Hub offers several options based on pricing and
scale. This article is meant to help you evaluate your IoT Hub needs. For pricing information
about IoT Hub tiers refer to IoT Hub pricing.
To decide which IoT Hub tier is right for your solution, ask yourself two questions:
What features do I plan to use? Azure IoT Hub offers two tiers, basic and standard, that differ
in the number of features they support. If your IoT solution is based around collecting data from
devices and analyzing it centrally then the basic tier is probably right for you. If you want to use
more advanced configurations to control IoT devices remotely or distribute some of your
workloads onto the devices themselves then you should consider the standard tier. For a
detailed breakdown of which features are included in each tier continue to Basic and standard
tiers.
How much data do I plan to move daily? Each IoT Hub tier is available in three sizes, based
around how much data throughput they can handle in any given day. These sizes are
numerically identified as 1, 2, and 3. For example, each unit of a level 1 IoT hub can handle 400
thousand messages a day, while a level 3 unit can handle 300 million. For more details about
the data guidelines, continue to Message throughput.
IoT Hub also offers a free tier that is meant for testing and evaluation. It has all the capabilities
of the standard tier, but limited messaging allowances. You cannot upgrade from the free tier to
either basic or standard.
IoT Hub REST APIs
The difference in supported capabilities between the basic and standard tiers of IoT Hub means
that some API calls do not work with basic tier hubs. The following table shows which APIs are
available:
Message throughput
The best way to size an IoT Hub solution is to evaluate the traffic on a per-unit basis. In
particular, consider the required peak throughput for the following categories of operations:
Device-to-cloud messages
Cloud-to-device messages
Identity registry operations
Traffic is measured on a per-unit basis, not per hub. A level 1 or 2 IoT Hub instance can have as
many as 200 units associated with it. A level 3 IoT Hub instance can have up to 10 units. Once
you create your IoT hub you can change the number of units or move between the 1, 2, and 3
sizes within a specific tier without interrupting your existing operations. For more information,
see How to upgrade your IoT Hub.
As an example of each tier's traffic capabilities, device-to-cloud messages follow these sustained
throughput guidelines:
In addition to this throughput information, see IoT Hub quotas and throttles and design your
solution accordingly.
Identity registry operation throughput
IoT Hub identity registry operations are not supposed to be run-time operations, as they are
mostly related to device provisioning.
For specific burst performance numbers, see IoT Hub quotas and throttles.
Sharding
While a single IoT hub can scale to millions of devices, sometimes your solution requires
specific performance characteristics that a single IoT hub cannot guarantee. In that case you can
partition your devices across multiple IoT hubs. Multiple IoT hubs smooth traffic bursts and
obtain the required throughput or operation rates that are required.
Next steps
For additional information about IoT Hub capabilities and performance details, see [IoT Hub
pricing][link-pricing] or IoT Hub quotas and throttles.
To change your IoT Hub tier, follow the steps in Upgrade your IoT hub.
IoT Hub high availability and disaster recovery
5/29/2018 • 3 min to read • Edit Online
As an Azure service, IoT Hub provides high availability (HA) using redundancies at the Azure region level, without
any additional work required by the solution. The Microsoft Azure platform also includes features to help you build
solutions with disaster recovery (DR ) capabilities or cross-region availability. If you want to provide global, cross-
region high availability for devices or users, take advantage of these Azure DR features. The article Azure Business
Continuity Technical Guidance describes the built-in features in Azure for business continuity and DR. The Disaster
recovery and high availability for Azure applications paper provides architecture guidance on strategies for Azure
applications to achieve HA and DR.
FUNCTIONALITY RPO
Service availability for registry and communication operations Possible CName loss
Next steps
Follow these links to learn more about Azure IoT Hub:
Get started with IoT Hubs (Tutorial)
What is Azure IoT Hub?
Support additional protocols for IoT Hub
5/29/2018 • 1 min to read • Edit Online
Azure IoT Hub natively supports communication over the MQTT, AMQP, and HTTPS protocols. In some cases,
devices or field gateways might not be able to use one of these standard protocols and require protocol
adaptation. In such cases, you can use a custom gateway. A custom gateway enables protocol adaptation for IoT
Hub endpoints by bridging the traffic to and from IoT Hub. You can use the Azure IoT protocol gateway as a
custom gateway to enable protocol adaptation for IoT Hub.
Next steps
To learn more about the Azure IoT protocol gateway and how to use and deploy it as part of your IoT solution,
see:
Azure IoT protocol gateway repository on GitHub
Azure IoT protocol gateway developer guide
To learn more about planning your IoT Hub deployment, see:
Compare with Event Hubs
Scaling, high availability, and disaster recovery
IoT Hub developer guide
Compare message routing and Event Grid for IoT
Hub
5/29/2018 • 4 min to read • Edit Online
Azure IoT Hub provides the capability to stream data from your connected devices, and integrate that data into
your business applications. IoT Hub offers two methods for integrating IoT events into other Azure services or
business applications. This article discusses the two features that provide this capability, so that you can choose
which option is best for your scenario.
IoT Hub message routing: This IoT Hub feature enables users to route device-to-cloud messages to service
endpoints like Azure Storage containers, Event Hubs, Service Bus queues, and Service Bus topics. Routing rules
provide the flexibility to perform query-based routes. They also enable critical alerts that trigger actions
through queries, and can be based on the message headers and body.
IoT Hub integration with Event Grid: Azure Event Grid is a fully managed event routing service that uses a
publish-subscribe model. IoT Hub and Event Grid work together to integrate IoT Hub events into Azure and
non-Azure services, in near-real time.
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Device messages Yes, message routing can be used for No, Event Grid can only be used for
telemetry data. non-telemetry IoT Hub events.
Event type Yes, message routing can report twin Yes, Event Grid can report when devices
changes and device lifecycle events. are registered to an IoT Hub, and when
devices are deleted.
Ordering Yes, ordering of events is maintained. No, order of events is not guaranteed.
Filtering Rich filtering through SQL-like language Filtering based on suffix/prefix of device
supports filtering on message headers IDs, which works well for hierarchical
and bodies. For examples, see IoT Hub services like storage.
query language.
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Cost There is no separate charge for There is no charge from IoT Hub. Event
message routing. Only ingress of Grid offers the first 100,000 operations
telemetry into IoT Hub is charged. For per month for free, and then $0.60 per
example, if you have a message routed million operations after that.
to three different endpoints, you are
billed for only one message.
IoT Hub message routing and Event Grid have similarities too, some of which are detailed in the following table:
FEATURE IOT HUB MESSAGE ROUTING IOT HUB INTEGRATION WITH EVENT GRID
Reliability High: Delivers each message to the High: Delivers each message to the
endpoint at least once for each route. webhook at least once for each
Expires all messages that are not subscription. Expires all events that are
delivered within one hour. not delivered within 24 hours.
Send to multiple endpoints Yes, send a single message to multiple Yes, send a single message to multiple
endpoints. endpoints.
Security Iot Hub provides per-device identity Event Grid provides validation at three
and revocable access control. For more points: event subscriptions, event
information, see the IoT Hub access publishing, and webhook event delivery.
control. For more information, see Event Grid
security and authentication.
How to choose
IoT Hub message routing and the IoT Hub integration with Event Grid perform different actions to achieve similar
results. They both take information from your IoT Hub solution and pass it on so that other services can react. So
how do you decide which one to use? In addition to the data from the previous section, use the following
questions to help guide your decision:
What kind of data are you sending to the endpoints?
Use IoT Hub message routing when you have to send telemetry data to other services. Message routing
also enables querying message headers and message bodies.
The IoT Hub integration with Event Grid works with events that occur in the IoT Hub service. These IoT
Hub events include device creation and deletion.
What endpoints need to receive this information?
IoT Hub message routing supports limited endpoints, but you can build connectors to reroute the data and
events to additional endpoints. For a complete list of supported endpoints, see the table in the previous
section.
The IoT Hub integration with Event Grid supports more endpoints. It also works with webhooks to extend
routing outside of the Azure service ecosystem and into third-party business applications.
Does it matter if your data arrives in order?
IoT Hub message routing maintains the order in which messages are sent, so that they arrive in the same
way.
Event Grid does not guarantee that endpoints will receive events in the same order that they occurred.
However, the event schema does include a timestamp that can be used to identify the order after the events
arrive at the endpoint.
Next steps
Learn more about IoT Hub message routing and the IoT Hub endpoints.
Learn more about Azure Event Grid
Try out the Event Grid integration by Sending email notifications about Azure IoT Hub events using Logic
Apps
1 min to read •
Edit Online
Azure IoT Hub developer guide
5/29/2018 • 3 min to read • Edit Online
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management,
are only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see
How to choose the right IoT Hub tier.
When sending information from the device app to the solution back end, IoT Hub exposes three options:
Device-to-cloud messages for time series telemetry and alerts.
Device twin's reported properties for reporting device state information such as available capabilities,
conditions, or the state of long-running workflows. For example, configuration and software updates.
File uploads for media files and large telemetry batches uploaded by intermittently connected devices or
compressed to save bandwidth.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How
to choose the right IoT Hub tier.
Scenario Telemetry time series and Available capabilities and Media files. Large (typically
alerts. For example, 256-KB conditions. For example, the compressed) telemetry
sensor data batches sent current device connectivity batches.
every 5 minutes. mode such as cellular or
WiFi. Synchronizing long-
running workflows, such as
configuration and software
updates.
Storage and retrieval Temporarily stored by IoT Stored by IoT Hub in the Stored in user-provided
Hub, up to 7 days. Only device twin. Retrievable Azure Storage account.
sequential reading. using the IoT Hub query
language.
Frequency High. For more information, Medium. For more Low. For more information,
see IoT Hub limits. information, see IoT Hub see IoT Hub limits.
limits.
Protocol Available on all protocols. Available using MQTT or Available when using any
AMQP. protocol, but requires
HTTPS on the device.
An application may need to send information both as a telemetry time series or alert and make it available in
the device twin. In this scenario, you can choose one of the following options:
The device app sends a device-to-cloud message and reports a property change.
The solution back end can store the information in the device twin's tags when it receives the message.
Since device-to-cloud messages enable a much higher throughput than device twin updates, it is sometimes
desirable to avoid updating the device twin for every device-to-cloud message.
Cloud-to-device communications guidance
5/29/2018 • 1 min to read • Edit Online
IoT Hub provides three options for device apps to expose functionality to a back-end app:
Direct methods for communications that require immediate confirmation of the result. Direct methods are
often used for interactive control of devices such as turning on a fan.
Twin's desired properties for long-running commands intended to put the device into a certain desired state.
For example, set the telemetry send interval to 30 minutes.
Cloud-to-device messages for one-way notifications to the device app.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Data flow Two-way. The device app One-way. The device app One-way. The device app
can respond to the method receives a notification with receives the message
right away. The solution the property change.
back end receives the
outcome contextually to
the request.
Durability Disconnected devices are Property values are Messages can be retained
not contacted. The solution preserved in the device by IoT Hub for up to 48
back end is notified that the twin. Device will read it at hours.
device is not connected. next reconnection. Property
values are retrievable with
the IoT Hub query
language.
Targets Single device using Single device using Single device by deviceId.
deviceId, or multiple deviceId, or multiple
devices using jobs. devices using jobs.
Frequency High. For more information, Medium. For more Low. For more information,
see IoT Hub limits. information, see IoT Hub see IoT Hub limits.
limits.
DIRECT METHODS TWIN'S DESIRED PROPERTIES CLOUD-TO-DEVICE MESSAGES
Protocol Available using MQTT or Available using MQTT or Available on all protocols.
AMQP. AMQP. Device must poll when
using HTTPS.
Learn how to use direct methods, desired properties, and cloud-to-device messages in the following tutorials:
Use direct methods, for direct methods;
Use desired properties to configure devices, for device twin's desired properties;
Send cloud-to-device messages, for cloud-to-device messages.
Device-to-cloud and cloud-to-device messaging
with IoT Hub
5/29/2018 • 1 min to read • Edit Online
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device
management, are only available in the standard tier of IoT hub. For more information about the basic and standard
IoT Hub tiers, see How to choose the right IoT Hub tier.
Core properties of IoT Hub messaging functionality are the reliability and durability of messages. These
properties enable resilience to intermittent connectivity on the device side, and to load spikes in event
processing on the cloud side. IoT Hub implements at least once delivery guarantees for both device-to-
cloud and cloud-to-device messaging.
For an introduction to the capabilities of IoT Hub, see the Overview of the Azure IoT Hub service.
Next steps
Learn about IoT Hub device-to-cloud messaging.
Learn about IoT Hub cloud-to-device messaging.
Send device-to-cloud messages to IoT Hub
6/6/2018 • 3 min to read • Edit Online
To send time-series telemetry and alerts from your devices to your solution back end, send device-to-cloud
messages from your device to your IoT hub. For a discussion of other device-to-cloud options supported by
IoT Hub, see Device-to-cloud communications guidance.
You send device-to-cloud messages through a device-facing endpoint
(/devices/{deviceId}/messages/events). Routing rules then route your messages to one of the service-
facing endpoints on your IoT hub. Routing rules use the headers and body of the device-to-cloud messages to
determine where to route them. By default, messages are routed to the built-in service-facing endpoint
(messages/events), that is compatible with Event Hubs. Therefore, you can use standard Event Hubs
integration and SDKs to receive device-to-cloud messages in your solution back end.
IoT Hub implements device-to-cloud messaging using a streaming messaging pattern. IoT Hub's device-to-
cloud messages are more like Event Hubs events than Service Bus messages in that there is a high volume of
events passing through the service that can be read by multiple readers.
Device-to-cloud messaging with IoT Hub has the following characteristics:
Device-to-cloud messages are durable and retained in an IoT hub's default messages/events endpoint for
up to seven days.
Device-to-cloud messages can be at most 256 KB, and can be grouped in batches to optimize sends.
Batches can be at most 256 KB.
As explained in the Control access to IoT Hub section, IoT Hub enables per-device authentication and
access control.
IoT Hub allows you to create up to 10 custom endpoints. Messages are delivered to the endpoints based
on routes configured on your IoT hub. For more information, see Routing rules.
IoT Hub enables millions of simultaneously connected devices (see Quotas and throttling).
IoT Hub does not allow arbitrary partitioning. Device-to-cloud messages are partitioned based on their
originating deviceId.
For more information about the differences between IoT Hub and Event Hubs, see Comparison of Azure IoT
Hub and Azure Event Hubs.
Anti-spoofing properties
To avoid device spoofing in device-to-cloud messages, IoT Hub stamps all messages with the following
properties:
ConnectionDeviceId
ConnectionDeviceGenerationId
ConnectionAuthMethod
The first two contain the deviceId and generationId of the originating device, as per Device identity
properties.
The ConnectionAuthMethod property contains a JSON serialized object, with the following properties:
{
"scope": "{ hub | device }",
"type": "{ symkey | sas | x509 }",
"issuer": "iothub"
}
Next steps
For information about the SDKs you can use to send device-to-cloud messages, see Azure IoT SDKs.
The Get Started tutorials show you how to send device-to-cloud messages from both simulated and physical
devices. For more detail, see the Process IoT Hub device-to-cloud messages using routes tutorial.
Read device-to-cloud messages from the built-in
endpoint
6/6/2018 • 2 min to read • Edit Online
By default, messages are routed to the built-in service-facing endpoint (messages/events) that is compatible
with Event Hubs. This endpoint is currently only exposed using the AMQP protocol on port 5671. An IoT hub
exposes the following properties to enable you to control the built-in Event Hub-compatible messaging endpoint
messages/events.
PROPERTY DESCRIPTION
Retention time This property specifies how long in days messages are
retained by IoT Hub. The default is one day, but it can be
increased to seven days.
IoT Hub also enables you to manage consumer groups on the built-in device-to-cloud receive endpoint.
By default, all messages that do not explicitly match a message routing rule are written to the built-in endpoint. If
you disable this fallback route, messages that do not explicitly match any message routing rules are dropped.
You can modify the retention time, either programmatically using the IoT Hub resource provider REST APIs, or
with the Azure portal.
IoT Hub exposes the messages/events built-in endpoint for your back-end services to read the device-to-cloud
messages received by your hub. This endpoint is Event Hub-compatible, which enables you to use any of the
mechanisms the Event Hubs service supports for reading messages.
The SDKs and integrations that you can use with Event Hub-compatible endpoints that IoT Hub exposes
includes the items in the following list:
Java Event Hubs client.
Apache Storm spout. You can view the spout source on GitHub.
Apache Spark integration.
Next steps
For more information about IoT Hub endpoints, see IoT Hub endpoints.
The Get Started tutorials show you how to send device-to-cloud messages from simulated devices and read the
messages from the built-in endpoint. For more detail, see the Process IoT Hub device-to-cloud messages using
routes tutorial.
If you want to route your device-to-cloud messages to custom endpoints, see Use message routes and custom
endpoints for device-to-cloud messages.
React to IoT Hub events by using Event Grid to
trigger actions - Preview
5/29/2018 • 2 min to read • Edit Online
Azure IoT Hub integrates with Azure Event Grid so that you can send event notifications to other services and
trigger downstream processes. Configure your business applications to listen for IoT Hub events so that you can
react to critical events in a reliable, scalable, and secure manner. For example, build an application to perform
multiple actions like updating a database, creating a ticket, and delivering an email notification every time a new
IoT device is registered to your IoT hub.
Azure Event Grid is a fully managed event routing service that uses a publish-subscribe model. Event Grid has
built-in support for Azure services like Azure Functions and Azure Logic Apps, and can deliver event alerts to non-
Azure services using webhooks. For a complete list of the event handlers that Event Grid supports, see An
introduction to Azure Event Grid.
Regional availability
The Event Grid integration is available for IoT hubs located in the regions where Event Grid is supported. For the
latest list of regions, see An introduction to Azure Event Grid.
Event types
IoT Hub publishes the following event types:
Event schema
IoT Hub events contain all the information you need to respond to changes in your device lifecycle. You can
identify an IoT Hub event by checking that the eventType property starts with Microsoft.Devices. For more
information about how to use Event Grid event properties, see the Event Grid event schema.
Device created schema
The following example shows the schema of a device created event:
[{
"id": "56afc886-767b-d359-d59e-0da7877166b2",
"topic": "/SUBSCRIPTIONS/<subscription ID>/RESOURCEGROUPS/<resource group
name>/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/<hub name>",
"subject": "devices/LogicAppTestDevice",
"eventType": "Microsoft.Devices.DeviceCreated",
"eventTime": "2018-01-02T19:17:44.4383997Z",
"data": {
"twin": {
"deviceId": "LogicAppTestDevice",
"etag": "AAAAAAAAAAE=",
"status": "enabled",
"statusUpdateTime": "0001-01-01T00:00:00",
"connectionState": "Disconnected",
"lastActivityTime": "0001-01-01T00:00:00",
"cloudToDeviceMessageCount": 0,
"authenticationType": "sas",
"x509Thumbprint": {
"primaryThumbprint": null,
"secondaryThumbprint": null
},
"version": 2,
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2018-01-02T19:17:44.4383997Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2018-01-02T19:17:44.4383997Z"
},
"$version": 1
}
}
},
"hubName": "egtesthub1",
"deviceId": "LogicAppTestDevice",
"operationTimestamp": "2018-01-02T19:17:44.4383997Z",
"opType": "DeviceCreated"
},
"dataVersion": "",
"metadataVersion": "1"
}]
For a detailed description of each property, see Azure Event Grid event schema for IoT Hub
Filter events
IoT Hub event subscriptions can filter events based on event type and device name. Subject filters in Event Grid
work based on prefix and suffix matches. The filter uses an AND operator, so events with a subject that match
both the prefix and suffix are delivered to the subscriber.
The subject of IoT Events uses the format:
devices/{deviceId}
Next steps
Try the IoT Hub events tutorial
Learn more about Event Grid
Compare the differences between routing IoT Hub events and messages
Use message routes and custom endpoints for
device-to-cloud messages
6/6/2018 • 2 min to read • Edit Online
IoT Hub enables you to route device-to-cloud messages to IoT Hub service-facing endpoints based on
message properties. Routing rules give you the flexibility to send messages where they need to go without the
need for additional services or custom code. Each routing rule you configure has the following properties:
PROPERTY DESCRIPTION
Condition The query expression for the routing rule that is run against
the message's headers and body and determines if it is a
match for the endpoint. For more information about
constructing a route condition, see the Reference - query
language for device twins and jobs.
Endpoint The name of the endpoint where IoT Hub sends messages
that match the condition. Endpoints should be in the same
region as the IoT hub, otherwise you may be charged for
cross-region writes.
A single message may match the condition on multiple routing rules, in which case IoT Hub delivers the
message to the endpoint associated with each matched rule. IoT Hub also automatically deduplicates message
delivery, so if a message matches multiple rules that have the same destination, it is only written once to that
destination.
NOTE
IoT Hub only supports writing data to Azure Storage containers as blobs.
WARNING
Service Bus queues and topics with Sessions or Duplicate Detection enabled are not supported as custom endpoints.
For more information about creating custom endpoints in IoT Hub, see IoT Hub endpoints.
For more information about reading from custom endpoints, see:
Reading from Azure Storage containers.
Reading from Event Hubs.
Reading from Service Bus queues.
Reading from Service Bus topics.
Latency
When you route device-to-cloud telemetry messages using built-in endpoints, there is a slight increase in the
end-to-end latency after the creation of the first route.
In most cases, the average increase in latency is less than one second. You can monitor the latency using
d2c.endpoints.latency.builtIn.events IoT Hub metric. Creating or deleting any route after the first one does
not impact the end-to-end latency.
Next steps
For more information about IoT Hub endpoints, see IoT Hub endpoints.
For more information about the query language you use to define routing rules, see IoT Hub query language
for device twins, jobs, and message routing.
The Process IoT Hub device-to-cloud messages using routes tutorial shows you how to use routing rules and
custom endpoints.
Send cloud-to-device messages from IoT Hub
5/29/2018 • 5 min to read • Edit Online
To send one-way notifications to the device app from your solution back end, send cloud-to-devices messages
from your IoT hub to your device. For a discussion of other cloud-to-devices options supported by IoT Hub,
see Cloud-to-device communications guidance.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
When the IoT Hub service sends a message to a device, the service sets the message state to Enqueued.
When a device wants to receive a message, IoT Hub locks the message (by setting the state to Invisible), which
allows other threads on the device to start receiving other messages. When a device thread completes the
processing of a message, it notifies IoT Hub by completing the message. IoT Hub then sets the state to
Completed.
A device can also choose to:
Reject the message, which causes IoT Hub to set it to the Dead lettered state. Devices that connect over the
MQTT protocol cannot reject cloud-to-device messages.
Abandon the message, which causes IoT Hub to put the message back in the queue, with the state set to
Enqueued. Devices that connect over the MQTT protocol cannot abandon cloud-to-device messages.
A thread could fail to process a message without notifying IoT Hub. In this case, messages automatically
transition from the Invisible state back to the Enqueued state after a visibility (or lock) timeout. The default
value of this timeout is one minute.
The max delivery count property on IoT Hub determines the maximum number of times a message can
transition between the Enqueued and Invisible states. After that number of transitions, IoT Hub sets the state
of the message to Dead lettered. Similarly, IoT Hub sets the state of a message to Dead lettered after its
expiration time (see Time to live).
The How to send cloud-to-device messages with IoT Hub shows you how to send cloud-to-device messages
from the cloud and receive them on a device.
Typically, a device completes a cloud-to-device message when the loss of the message does not affect the
application logic. For example, when the device has persisted the message content locally or has successfully
executed an operation. The message could also carry transient information, whose loss would not impact the
functionality of the application. Sometimes, for long-running tasks, you can:
Complete the cloud-to-device message after persisting the task description in local storage.
Notify the solution back end with one or more device-to-cloud messages at various stages of progress of
the task.
Message feedback
When you send a cloud-to-device message, the service can request the delivery of per-message feedback
regarding the final state of that message.
If Ack is full, and you don't receive a feedback message, it means that the feedback message expired. The
service can't know what happened to the original message. In practice, a service should ensure that it can
process the feedback before it expires. The maximum expiry time is two days, which leaves time to get the
service running again if a failure occurs.
As explained in Endpoints, IoT Hub delivers feedback through a service-facing endpoint
(/messages/servicebound/feedback) as messages. The semantics for receiving feedback are the same as for
cloud-to-device messages. Whenever possible, message feedback is batched in a single message, with the
following format:
PROPERTY DESCRIPTION
ContentType application/vnd.microsoft.iothub.feedback.json
The body is a JSON -serialized array of records, each with the following properties:
PROPERTY DESCRIPTION
The service must specify a MessageId for the cloud-to-device message to be able to correlate its feedback
with the original message.
The following example shows the body of a feedback message.
[
{
"OriginalMessageId": "0987654321",
"EnqueuedTimeUtc": "2015-07-28T16:24:48.789Z",
"StatusCode": 0,
"Description": "Success",
"DeviceId": "123",
"DeviceGenerationId": "abcdefghijklmnopqrstuvwxyz"
},
{
...
},
...
]
For more information about how to set these configuration options, see Create IoT hubs.
Next steps
For information about the SDKs you can use to receive cloud-to-device messages, see Azure IoT SDKs.
To try out receiving cloud-to-device messages, see the Send cloud-to-device tutorial.
Create and read IoT Hub messages
5/29/2018 • 3 min to read • Edit Online
To support seamless interoperability across protocols, IoT Hub defines a common message format for all device-
facing protocols. This message format is used for both device-to-cloud and cloud-to-device messages.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
PROPERTY DESCRIPTION
CreationTimeUtc Date and time the message was created on a device. A device
must set this value explicitly.
Message size
IoT Hub measures message size in a protocol-agnostic way, considering only the actual payload. The size in bytes
is calculated as the sum of the following:
The body size in bytes.
The size in bytes of all the values of the message system properties.
The size in bytes of all user property names and values.
Property names and values are limited to ASCII characters, so the length of the strings equals the size in bytes.
Next steps
For information about message size limits in IoT Hub, see IoT Hub quotas and throttling.
To learn how to create and read IoT Hub messages in various programming languages, see the Get started
tutorials.
Reference - choose a communication protocol
5/29/2018 • 2 min to read • Edit Online
IoT Hub allows devices to use the following protocols for device-side communications:
MQTT
MQTT over WebSockets
AMQP
AMQP over WebSockets
HTTPS
For information about how these protocols support specific IoT Hub features, see Device-to-cloud
communications guidance and Cloud-to-device communications guidance.
The following table provides the high-level recommendations for your choice of protocol:
Consider the following points when you choose your protocol for device-side communications:
Cloud-to-device pattern. HTTPS does not have an efficient way to implement server push. As such, when
you are using HTTPS, devices poll IoT Hub for cloud-to-device messages. This approach is inefficient for both
the device and IoT Hub. Under current HTTPS guidelines, each device should poll for messages every 25
minutes or more. MQTT and AMQP support server push when receiving cloud-to-device messages. They
enable immediate pushes of messages from IoT Hub to the device. If delivery latency is a concern, MQTT or
AMQP are the best protocols to use. For rarely connected devices, HTTPS works as well.
Field gateways. When using MQTT and HTTPS, you cannot connect multiple devices (each with its own per-
device credentials) using the same TLS connection. For Field gateway scenarios that require one TLS
connection between the field gateway and IoT Hub for each connected device, these protocols are suboptimal.
Low resource devices. The MQTT and HTTPS libraries have a smaller footprint than the AMQP libraries. As
such, if the device has limited resources (for example, less than 1-MB RAM ), these protocols might be the only
protocol implementation available.
Network traversal. The standard AMQP protocol uses port 5671, and MQTT listens on port 8883. USe of
these ports could cause problems in networks that are closed to non-HTTPS protocols. Use MQTT over
WebSockets, AMQP over WebSockets, or HTTPS in this scenario.
Payload size. MQTT and AMQP are binary protocols, which result in more compact payloads than HTTPS.
WARNING
When using HTTPS, each device should poll for cloud-to-device messages every 25 minutes or more. However, during
development, it is acceptable to poll more frequently than every 25 minutes.
Port numbers
Devices can communicate with IoT Hub in Azure using various protocols. Typically, the choice of protocol is driven
by the specific requirements of the solution. The following table lists the outbound ports that must be open for a
device to be able to use a specific protocol:
PROTOCOL PORT
MQTT 8883
AMQP 5671
HTTPS 443
Once you have created an IoT hub in an Azure region, the IoT hub keeps the same IP address for the lifetime of
that IoT hub. However, if Microsoft moves the IoT hub to a different scale unit to maintain quality of service, then
it is assigned a new IP address.
Next steps
To learn more about how IoT Hub implements the MQTT protocol, see Communicate with your IoT hub using the
MQTT protocol.
Upload files with IoT Hub
5/29/2018 • 5 min to read • Edit Online
As detailed in the IoT Hub endpoints article, a device can initiate a file upload by sending a notification through
a device-facing endpoint (/devices/{deviceId}/files). When a device notifies IoT Hub that an upload is
complete, IoT Hub sends a file upload notification message through the
/messages/servicebound/filenotifications service-facing endpoint.
Instead of brokering messages through IoT Hub itself, IoT Hub instead acts as a dispatcher to an associated
Azure Storage account. A device requests a storage token from IoT Hub that is specific to the file the device
wishes to upload. The device uses the SAS URI to upload the file to storage, and when the upload is complete
the device sends a notification of completion to IoT Hub. IoT Hub checks the file upload is complete and then
adds a file upload notification message to the service-facing file notification endpoint.
Before you upload a file to IoT Hub from a device, you must configure your hub by associating an Azure
Storage account to it.
Your device can then initialize an upload and then notify IoT hub when the upload completes. Optionally, when
a device notifies IoT Hub that the upload is complete, the service can generate a notification message.
When to use
Use file upload to send media files and large telemetry batches uploaded by intermittently connected devices or
compressed to save bandwidth.
Refer to Device-to-cloud communication guidance if in doubt between using reported properties, device-to-
cloud messages, or file upload.
NOTE
The Azure IoT SDKs automatically handle retrieving the SAS URI, uploading the file, and notifying IoT Hub of a completed
upload.
{
"blobName": "{name of the file for which a SAS URI will be generated}"
}
IoT Hub returns the following data, which the device uses to upload the file:
{
"correlationId": "somecorrelationid",
"hostName": "contoso.azure-devices.net",
"containerName": "testcontainer",
"blobName": "test-device1/image.jpg",
"sasToken": "1234asdfSAStoken"
}
NOTE
This section describes deprecated functionality for how to receive a SAS URI from IoT Hub. Use the POST method
described previously.
IoT Hub has two REST endpoints to support file upload, one to get the SAS URI for storage and the other to
notify the IoT hub of a completed upload. The device initiates the file upload process by sending a GET to the
IoT hub at {iot hub}.azure-devices.net/devices/{deviceId}/files/{filename} . The IoT hub returns:
A SAS URI specific to the file to be uploaded.
A correlation ID to be used once the upload is completed.
{
"correlationId": "{correlation ID received from the initial request}",
"isSuccess": bool,
"statusCode": XXX,
"statusDescription": "Description of status"
}
The value of isSuccess is a Boolean representing whether the file was uploaded successfully. The status code
for statusCode is the status for the upload of the file to storage, and the statusDescription corresponds to the
statusCode .
Reference topics:
The following reference topics provide you with more information about uploading files from a device.
Example. This example shows the body of a file upload notification message.
{
"deviceId":"mydevice",
"blobUri":"https://{storage account}.blob.core.windows.net/{container name}/mydevice/myfile.jpg",
"blobName":"mydevice/myfile.jpg",
"lastUpdatedTime":"2016-06-01T21:22:41+00:00",
"blobSizeInBytes":1234,
"enqueuedTimeUtc":"2016-06-01T21:22:43.7996883Z"
}
fileNotifications.ttlAsIso8601 Default TTL for file upload notifications. ISO_8601 interval up to 48H
(minimum 1 minute). Default: 1 hour.
fileNotifications.lockDuration Lock duration for the file upload 5 to 300 seconds (minimum 5
notifications queue. seconds). Default: 60 seconds.
fileNotifications.maxDeliveryCount Maximum delivery count for the file 1 to 100. Default: 100.
upload notification queue.
Next steps
Now you have learned how to upload files from devices using IoT Hub, you may be interested in the following
IoT Hub developer guide topics:
Manage device identities in IoT Hub
Control access to IoT Hub
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
How to upload files from devices to the cloud with IoT Hub
Understand the identity registry in your IoT
hub
5/29/2018 • 10 min to read • Edit Online
Every IoT hub has an identity registry that stores information about the devices and modules
permitted to connect to the IoT hub. Before a device or module can connect to an IoT hub, there
must be an entry for that device or module in the IoT hub's identity registry. A device or module
must also authenticate with the IoT hub based on credentials stored in the identity registry.
The device or module ID stored in the identity registry is case-sensitive.
At a high level, the identity registry is a REST-capable collection of device or module identity
resources. When you add an entry in the identity registry, IoT Hub creates a set of per-device
resources such as the queue that contains in-flight cloud-to-device messages.
Use the identity registry when you need to:
Provision devices or modules that connect to your IoT hub.
Control per-device/per-module access to your hub's device or module-facing endpoints.
NOTE
The identity registry does not contain any application-specific metadata.
IMPORTANT
The only way to retrieve all identities in an IoT hub's identity registry is to use the Export functionality.
IMPORTANT
Only use the identity registry for device management and provisioning operations. High throughput
operations at run time should not depend on performing operations in the identity registry. For example,
checking the connection state of a device before sending a command is not a supported pattern. Make sure
to check the throttling rates for the identity registry, and the device heartbeat pattern.
Disable devices
You can disable devices by updating the status property of an identity in the identity registry.
Typically, you use this property in two scenarios:
During a provisioning orchestration process. For more information, see Device Provisioning.
If, for any reason, you think a device is compromised or has become unauthorized.
This feature is not availble for modules.
Device provisioning
The device data that a given IoT solution stores depends on the specific requirements of that
solution. But, as a minimum, a solution must store device identities and authentication keys. Azure
IoT Hub includes an identity registry that can store values for each device such as IDs, authentication
keys, and status codes. A solution can use other Azure services such as table storage, blob storage,
or Cosmos DB to store any additional device data.
Device provisioning is the process of adding the initial device data to the stores in your solution. To
enable a new device to connect to your hub, you must add a device ID and keys to the IoT Hub
identity registry. As part of the provisioning process, you might need to initialize device-specific data
in other solution stores. You can also use the Azure IoT Hub Device Provisioning Service to enable
zero-touch, just-in-time provisioning to one or more IoT hubs without requiring human intervention.
To learn more, see the provisioning service documentation.
Device heartbeat
The IoT Hub identity registry contains a field called connectionState. Only use the
connectionState field during development and debugging. IoT solutions should not query the field
at run time. For example, do not query the connectionState field to check if a device is connected
before you send a cloud-to-device message or an SMS.
If your IoT solution needs to know if a device is connected, you should implement the heartbeat
pattern.
In the heartbeat pattern, the device sends device-to-cloud messages at least once every fixed
amount of time (for example, at least once every hour). Therefore, even if a device does not have any
data to send, it still sends an empty device-to-cloud message (usually with a property that identifies
it as a heartbeat). On the service side, the solution maintains a map with the last heartbeat received
for each device. If the solution does not receive a heartbeat message within the expected time from
the device, it assumes that there is a problem with the device.
A more complex implementation could include the information from operations monitoring to
identify devices that are trying to connect or communicate but failing. When you implement the
heartbeat pattern, make sure to check IoT Hub Quotas and Throttles.
NOTE
If an IoT solution uses the connection state solely to determine whether to send cloud-to-device messages,
and messages are not broadcast to large sets of devices, consider using the simpler short expiry time
pattern. This pattern achieves the same result as maintaining a device connection state registry using the
heartbeat pattern, while being more efficient. If you request message acknowledgements, IoT Hub can notify
you about which devices are able to receive messages and which are not.
NAME VALUE
$content-type application/json
$iothub-message-source deviceLifecycleEvents
$content-encoding utf-8
iothub-message-schema deviceLifecycleNotification
Body: This section is in JSON format and represents the twin of the created device identity. For
example,
{
"deviceId":"11576-ailn-test-0-67333793211",
"etag":"AAAAAAAAAAE=",
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
NAME VALUE
$content-type application/json
$iothub-message-source moduleLifecycleEvents
$content-encoding utf-8
iothub-message-schema moduleLifecycleNotification
Body: This section is in JSON format and represents the twin of the created module identity. For
example,
{
"deviceId":"11576-ailn-test-0-67333793211",
"moduleId":"tempSensor",
"etag":"AAAAAAAAAAE=",
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
NOTE
Connection state can only represent the IoT Hub view of the status of the connection. Updates to this state
may be delayed, depending on network conditions and configurations.
Next steps
Now that you have learned how to use the IoT Hub identity registry, you may be interested in the
following IoT Hub developer guide topics:
Control access to IoT Hub
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
Get started with Azure IoT Hub
To explore using the IoT Hub Device Provisioning Service to enable zero-touch, just-in-time
provisioning, see:
Azure IoT Hub Device Provisioning Service
Control access to IoT Hub
6/6/2018 • 16 min to read • Edit Online
This article describes the options for securing your IoT hub. IoT Hub uses permissions to grant access
to each IoT hub endpoint. Permissions limit the access to an IoT hub based on functionality.
This article introduces:
The different permissions that you can grant to a device or back-end app to access your IoT hub.
The authentication process and the tokens it uses to verify permissions.
How to scope credentials to limit access to specific resources.
IoT Hub support for X.509 certificates.
Custom device authentication mechanisms that use existing device identity registries or
authentication schemes.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device
management, are only available in the standard tier of IoT hub. For more information about the basic and
standard IoT Hub tiers, see How to choose the right IoT Hub tier.
You must have appropriate permissions to access any of the IoT Hub endpoints. For example, a device
must include a token containing security credentials along with every message it sends to IoT Hub.
Authentication
Azure IoT Hub grants access to endpoints by verifying a token against the shared access policies and
identity registry security credentials.
Security credentials, such as symmetric keys, are never sent over the wire.
NOTE
The Azure IoT Hub resource provider is secured through your Azure subscription, as are all providers in the
Azure Resource Manager.
For more information about how to construct and use security tokens, see IoT Hub security tokens.
Protocol specifics
Each supported protocol, such as MQTT, AMQP, and HTTPS, transports tokens in different ways.
When using MQTT, the CONNECT packet has the deviceId as the ClientId,
{iothubhostname}/{deviceId} in the Username field, and a SAS token in the Password field.
{iothubhostname} should be the full CName of the IoT hub (for example, contoso.azure-devices.net).
When using AMQP, IoT Hub supports SASL PL AIN and AMQP Claims-Based-Security.
If you use AMQP claims-based-security, the standard specifies how to transmit these tokens.
For SASL PL AIN, the username can be:
{policyName}@sas.root.{iothubName} if using IoT hub-level tokens.
{deviceId}@sas.{iothubname} if using device-scoped tokens.
In both cases, the password field contains the token, as described in IoT Hub security tokens.
HTTPS implements authentication by including a valid token in the Authorization request header.
Example
Username (DeviceId is case-sensitive): iothubname.azure-devices.net/DeviceId
NOTE
The Azure IoT SDKs automatically generate tokens when connecting to the service. In some cases, the Azure
IoT SDKs do not support all the protocols or all the authentication methods.
Security tokens
IoT Hub uses security tokens to authenticate devices and services to avoid sending keys on the wire.
Additionally, security tokens are limited in time validity and scope. Azure IoT SDKs automatically
generate tokens without requiring any special configuration. Some scenarios do require you to
generate and use security tokens directly. Such scenarios include:
The direct use of the MQTT, AMQP, or HTTPS surfaces.
The implementation of the token service pattern, as explained in Custom device authentication.
IoT Hub also allows devices to authenticate with IoT Hub using X.509 certificates.
Security token structure
You use security tokens to grant time-bounded access to devices and services to specific functionality
in IoT Hub. To get authorization to connect to IoT Hub, devices and services must send security tokens
signed with either a shared access or symmetric key. These keys are stored with a device identity in the
identity registry.
A token signed with a shared access key grants access to all the functionality associated with the
shared access policy permissions. A token signed with a device identity's symmetric key only grants the
DeviceConnect permission for the associated device identity.
The security token has the following format:
SharedAccessSignature sig={signature-string}&se={expiry}&skn={policyName}&sr={URL-encoded-
resourceURI}
VALUE DESCRIPTION
Note on prefix: The URI prefix is computed by segment and not by character. For example /a/b is a
prefix for /a/b/c but not for /a/bc .
The following Node.js snippet shows a function called generateSasToken that computes the token
from the inputs resourceUri, signingKey, policyName, expiresInMins . The next sections detail how to
initialize the different inputs for the different token use cases.
// Use crypto
var hmac = crypto.createHmac('sha256', new Buffer(signingKey, 'base64'));
hmac.update(toSign);
var base64UriEncoded = encodeURIComponent(hmac.digest('base64'));
rawtoken = {
'sr' : uri,
'sig': signature,
'se' : str(int(ttl))
}
using System;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
public static string generateSasToken(string resourceUri, string key, string policyName, int
expiryInSeconds = 3600)
{
TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + expiryInSeconds);
if (!String.IsNullOrEmpty(policyName))
{
token += "&skn=" + policyName;
}
return token;
}
NOTE
Since the time validity of the token is validated on IoT Hub machines, the drift on the clock of the machine that
generates the token must be minimal.
IMPORTANT
The only way that IoT Hub authenticates a specific device is using the device identity symmetric key. In cases
when a shared access policy is used to access device functionality, the solution must consider the component
issuing the security token as a trusted subcomponent.
ENDPOINT FUNCTIONALITY
The result, which grants access to all functionality for device1, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices%2fdevice1&sig=13y8ejUk2z7PLmvtwR5RqlGBOVwiq7rQR3WZ5xZX3N4%3D&se=1456971697
NOTE
It is possible to generate a SAS token using the .NET device explorer tool or the cross-platform, Python-based
The IoT extension for Azure CLI 2.0 command-line utility.
The result, which grants access to all functionality for device1, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices%2fdevice1&sig=13y8ejUk2z7PLmvtwR5RqlGBOVwiq7rQR3WZ5xZX3N4%3D&se=1456971697&skn=device
A protocol gateway could use the same token for all devices simply setting the resource URI to
myhub.azure-devices.net/devices .
ENDPOINT FUNCTIONALITY
{iot hub host name}/devices Create, update, retrieve, and delete device identities.
As an example, a service generating using the pre-created shared access policy called registryRead
would create a token with the following parameters:
resource URI: {IoT hub name}.azure-devices.net/devices ,
signing key: one of the keys of the registryRead policy,
policy name: registryRead ,
any expiration time.
var endpoint ="myhub.azure-devices.net/devices";
var policyName = 'device';
var policyKey = '...';
The result, which would grant access to read all device identities, would be:
SharedAccessSignature sr=myhub.azure-
devices.net%2fdevices&sig=JdyscqTpXdEJs49elIUCcohw2DlFDR3zfH5KqGJo4r4%3D&se=1456973447&skn=registryRead
NOTE
You can use the .NET class SharedAccessSignatureBuilder or the Java class IotHubServiceSasToken to create a
token in your token service.
The token service can set the token expiration as desired. When the token expires, the IoT hub severs
the device/module connection. Then, the device/module must request a new token from the token
service. A short expiry time increases the load on both the device/module and the token service.
For a device/module to connect to your hub, you must still add it to the IoT Hub identity registry —
even though the it is using a token and not a key to connect. Therefore, you can continue to use per-
device/per-module access control by enabling or disabling device/module identities in the identity
registry. This approach mitigates the risks of using tokens with long expiry times.
Comparison with a custom gateway
The token service pattern is the recommended way to implement a custom identity
registry/authentication scheme with IoT Hub. This pattern is recommended because IoT Hub
continues to handle most of the solution traffic. However, if the custom authentication scheme is so
intertwined with the protocol, you may require a custom gateway to process all the traffic. An example
of such a scenario is usingTransport Layer Security (TLS ) and pre-shared keys (PSKs). For more
information, see the protocol gateway article.
Reference topics:
The following reference topics provide you with more information about controlling access to your IoT
hub.
PERMISSION NOTES
Next steps
Now that you have learned how to control access IoT Hub, you may be interested in the following IoT
Hub developer guide topics:
Use device twins to synchronize state and configurations
Invoke a direct method on a device
Schedule jobs on multiple devices
If you would like to try out some of the concepts described in this article, see the following IoT Hub
tutorials:
Get started with Azure IoT Hub
How to send cloud-to-device messages with IoT Hub
How to process IoT Hub device-to-cloud messages
Understand and use device twins in IoT Hub
5/29/2018 • 11 min to read • Edit Online
Device twins are JSON documents that store device state information including metadata,
configurations, and conditions. Azure IoT Hub maintains a device twin for each device that you
connect to IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more
information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Device twins
Device twins store device-related information that:
Device and back ends can use to synchronize device conditions and configuration.
The solution back end can use to query and target long-running operations.
The lifecycle of a device twin is linked to the corresponding device identity. Device twins are
implicitly created and deleted when a device identity is created or deleted in IoT Hub.
A device twin is a JSON document that includes:
Tags. A section of the JSON document that the solution back end can read from and write to.
Tags are not visible to device apps.
Desired properties. Used along with reported properties to synchronize device configuration
or conditions. The solution back end can set desired properties, and the device app can read
them. The device app can also receive notifications of changes in the desired properties.
Reported properties. Used along with desired properties to synchronize device configuration
or conditions. The device app can set reported properties, and the solution back end can read
and query them.
Device identity properties. The root of the device twin JSON document contains the read-
only properties from the corresponding device identity stored in the identity registry.
In the root object are the device identity properties, and container objects for tags and both
reported and desired properties. The properties container contains some read-only elements (
$metadata , $etag , and $version ) described in the Device twin metadata and Optimistic
concurrency sections.
Reported property example
In the previous example, the device twin contains a batteryLevel property that is reported by the
device app. This property makes it possible to query and operate on devices based on the last
reported battery level. Other examples include the device app reporting device capabilities or
connectivity options.
NOTE
Reported properties simplify scenarios where the solution back end is interested in the last known value
of a property. Use device-to-cloud messages if the solution back end needs to process device telemetry in
the form of sequences of timestamped events, such as time series.
...
"desired": {
"telemetryConfig": {
"sendFrequency": "5m"
},
...
},
...
2. The device app is notified of the change immediately if connected, or at the first reconnect.
The device app then reports the updated configuration (or an error condition using the
status property). Here is the portion of the reported properties:
...
"reported": {
"telemetryConfig": {
"sendFrequency": "5m",
"status": "success"
}
...
}
...
3. The solution back end can track the results of the configuration operation across many
devices, by querying device twins.
NOTE
The preceding snippets are examples, optimized for readability, of one way to encode a device
configuration and its status. IoT Hub does not impose a specific schema for the device twin desired and
reported properties in the device twins.
You can use twins to synchronize long-running operations such as firmware updates. For more
information on how to use properties to synchronize and track a long running operation across
devices, see Use desired properties to configure devices.
Back-end operations
The solution back end operates on the device twin using the following atomic operations, exposed
through HTTPS:
Retrieve device twin by ID. This operation returns the device twin document, including tags
and desired and reported system properties.
Partially update device twin. This operation enables the solution back end to partially
update the tags or desired properties in a device twin. The partial update is expressed as a
JSON document that adds or updates any property. Properties set to null are removed.
The following example creates a new desired property with value
{"newProperty": "newValue"} , overwrites the existing value of existingProperty with
"otherNewValue" , and removes otherOldProperty . No other changes are made to existing
desired properties or tags:
{
"properties": {
"desired": {
"newProperty": {
"nestedProperty": "newValue"
},
"existingProperty": "otherNewValue",
"otherOldProperty": null
}
}
}
Replace desired properties. This operation enables the solution back end to completely
overwrite all existing desired properties and substitute a new JSON document for
properties/desired .
Replace tags. This operation enables the solution back end to completely overwrite all
existing tags and substitute a new JSON document for tags .
Receive twin notifications. This operation allows the solution back end to be notified
when the twin is modified. To do so, your IoT solution needs to create a route and to set the
Data Source equal to twinChangeEvents. By default, no twin notifications are sent, that is,
no such routes pre-exist. If the rate of change is too high, or for other reasons such as
internal failures, the IoT Hub might send only one notification that contains all changes.
Therefore, if your application needs reliable auditing and logging of all intermediate states,
you should use device-to-cloud messages. The twin notification message includes
properties and body.
Properties
NAME VALUE
$content-type application/json
$iothub-message-source twinChangeEvents
$content-encoding utf-8
iothub-message-schema deviceLifecycleNotification
{
"properties": {
"desired": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
},
"reported": {
"$metadata": {
"$lastUpdated": "2016-02-30T16:24:48.789Z"
},
"$version": 1
}
}
}
All the preceding operations support Optimistic concurrency and require the ServiceConnect
permission, as defined in the Security article.
In addition to these operations, the solution back end can:
Query the device twins using the SQL -like IoT Hub query language.
Perform operations on large sets of device twins using jobs.
Device operations
The device app operates on the device twin using the following atomic operations:
Retrieve device twin. This operation returns the device twin document (including tags and
desired and reported system properties) for the currently connected device.
Partially update reported properties. This operation enables the partial update of the
reported properties of the currently connected device. This operation uses the same JSON
update format that the solution back end uses for a partial update of desired properties.
Observe desired properties. The currently connected device can choose to be notified of
updates to the desired properties when they happen. The device receives the same form of
update (partial or full replacement) executed by the solution back end.
All the preceding operations require the DeviceConnect permission, as defined in the Security
article.
The Azure IoT device SDKs make it easy to use the preceding operations from many languages
and platforms. For more information on the details of IoT Hub primitives for desired properties
synchronization, see Device reconnection flow.
{
...
"tags": {
"one": {
"two": {
"three": {
"four": {
"five": {
"property": "value"
}
}
}
}
}
},
...
}
This information is kept at every level (not just the leaves of the JSON structure) to preserve
updates that remove object keys.
Optimistic concurrency
Tags, desired, and reported properties all support optimistic concurrency. Tags have an ETag, as
per RFC7232, that represents the tag's JSON representation. You can use ETags in conditional
update operations from the solution back end to ensure consistency.
Device twin desired and reported properties do not have ETags, but have a $version value that is
guaranteed to be incremental. Similarly to an ETag, the version can be used by the updating party
to enforce consistency of updates. For example, a device app for a reported property or the
solution back end for a desired property.
Versions are also useful when an observing agent (such as the device app observing the desired
properties) must reconcile races between the result of a retrieve operation and an update
notification. The section Device reconnection flow provides more information.
Device reconnection flow
IoT Hub does not preserve desired properties update notifications for disconnected devices. It
follows that a device that is connecting must retrieve the full desired properties document, in
addition to subscribing for update notifications. Given the possibility of races between update
notifications and full retrieval, the following flow must be ensured:
1. Device app connects to an IoT hub.
2. Device app subscribes for desired properties update notifications.
3. Device app retrieves the full document for desired properties.
The device app can ignore all notifications with $version less or equal than the version of the full
retrieved document. This approach is possible because IoT Hub guarantees that versions always
increment.
NOTE
This logic is already implemented in the Azure IoT device SDKs. This description is useful only if the device
app cannot use any of Azure IoT device SDKs and must program the MQTT interface directly.
Next steps
Now you have learned about device twins, you may be interested in the following IoT Hub
developer guide topics:
Understand and use module twins in IoT Hub
Invoke a direct method on a device
Schedule jobs on multiple devices
To try out some of the concepts described in this article, see the following IoT Hub tutorials:
How to use the device twin
How to use device twin properties
Understand and use module twins in IoT Hub
5/29/2018 • 10 min to read • Edit Online
This article assumes you've read understand and use device twins in IoT Hub first. In IoT Hub, under each device
identity, you can create up to 20 module identities. Each module identity implicitly generates a module twin. Very
similar to device twins, module twins are JSON documents that store module state information including
metadata, configurations, and conditions. Azure IoT Hub maintains a module twin for each module that you
connect to IoT Hub.
On the device side, the IoT Hub device SDKs enable you to create modules which each opens an independent
connection to IoT Hub. This enables you to use separate namespaces for different components on your device. For
example, you have a vending machine that has three different sensors. Each sensor is controlled by different
departments in your company. You can create a module for each sensor. This way, each department is only able to
send jobs or direct methods to the sensor that they control, avoiding conflicts and user errors.
Module identity and module twin provides the same capabilities as device identity and device twin but at a finer
granularity. This finer granularity enables capable devices, such as operating system based devices or firmware
devices managing multiple components, to isolate configuration and conditions for each of those components.
Module identity and module twins provide a management separation of concerns when working with IoT devices
that have modular software components. We aim at supporting all the device twin functionality at module twin
level by module twin general availability.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Module twins
Module twins store module-related information that:
Modules on the device and IoT Hub can use to synchronize module conditions and configuration.
The solution back end can use to query and target long-running operations.
The lifecycle of a module twin is linked to the corresponding module identity. Modules twins are implicitly created
and deleted when a module identity is created or deleted in IoT Hub.
A module twin is a JSON document that includes:
Tags. A section of the JSON document that the solution back end can read from and write to. Tags are not
visible to modules on the device. Tags are set for querying purpose.
Desired properties. Used along with reported properties to synchronize module configuration or conditions.
The solution back end can set desired properties, and the module app can read them. The module app can also
receive notifications of changes in the desired properties.
Reported properties. Used along with desired properties to synchronize module configuration or conditions.
The module app can set reported properties, and the solution back end can read and query them.
Module identity properties. The root of the module twin JSON document contains the read-only properties
from the corresponding module identity stored in the identity registry.
In the root object are the module identity properties, and container objects for tags and both reported and
desired properties. The properties container contains some read-only elements ( $metadata , $etag , and
$version ) described in the Module twin metadata and Optimistic concurrency sections.
NOTE
Reported properties simplify scenarios where the solution back end is interested in the last known value of a property. Use
device-to-cloud messages if the solution back end needs to process module telemetry in the form of sequences of
timestamped events, such as time series.
...
"desired": {
"telemetryConfig": {
"sendFrequency": "5m"
},
...
},
...
2. The module app is notified of the change immediately if connected, or at the first reconnect. The module
app then reports the updated configuration (or an error condition using the status property). Here is the
portion of the reported properties:
...
"reported": {
"telemetryConfig": {
"sendFrequency": "5m",
"status": "success"
}
...
}
...
3. The solution back end can track the results of the configuration operation across many modules, by
querying module twins.
NOTE
The preceding snippets are examples, optimized for readability, of one way to encode a module configuration and its status.
IoT Hub does not impose a specific schema for the module twin desired and reported properties in the module twins.
Back-end operations
The solution back end operates on the module twin using the following atomic operations, exposed through
HTTPS:
Retrieve module twin by ID. This operation returns the module twin document, including tags and desired
and reported system properties.
Partially update module twin. This operation enables the solution back end to partially update the tags
or desired properties in a module twin. The partial update is expressed as a JSON document that adds or
updates any property. Properties set to null are removed. The following example creates a new desired
property with value {"newProperty": "newValue"} , overwrites the existing value of existingProperty with
"otherNewValue" , and removes otherOldProperty . No other changes are made to existing desired
properties or tags:
{
"properties": {
"desired": {
"newProperty": {
"nestedProperty": "newValue"
},
"existingProperty": "otherNewValue",
"otherOldProperty": null
}
}
}
Replace desired properties. This operation enables the solution back end to completely overwrite all
existing desired properties and substitute a new JSON document for properties/desired .
Replace tags. This operation enables the solution back end to completely overwrite all existing tags and
substitute a new JSON document for tags .
Receive twin notifications. This operation allows the solution back end to be notified when the twin is
modified. To do so, your IoT solution needs to create a route and to set the Data Source equal to
twinChangeEvents. By default, no twin notifications are sent, that is, no such routes pre-exist. If the rate of
change is too high, or for other reasons such as internal failures, the IoT Hub might send only one
notification that contains all changes. Therefore, if your application needs reliable auditing and logging of
all intermediate states, you should use device-to-cloud messages. The twin notification message includes
properties and body.
Properties
NAME VALUE
$content-type application/json
$iothub-message-source twinChangeEvents
$content-encoding utf-8
iothub-message-schema deviceLifecycleNotification
All the preceding operations support Optimistic concurrency and require the ServiceConnect permission, as
defined in the Security article.
In addition to these operations, the solution back end can:
Query the module twins using the SQL -like IoT Hub query language.
Module operations
The module app operates on the module twin using the following atomic operations:
Retrieve module twin. This operation returns the module twin document (including tags and desired and
reported system properties) for the currently connected module.
Partially update reported properties. This operation enables the partial update of the reported properties of
the currently connected module. This operation uses the same JSON update format that the solution back end
uses for a partial update of desired properties.
Observe desired properties. The currently connected module can choose to be notified of updates to the
desired properties when they happen. The module receives the same form of update (partial or full
replacement) executed by the solution back end.
All the preceding operations require the ModuleConnect permission, as defined in the Security article.
The Azure IoT device SDKs make it easy to use the preceding operations from many languages and platforms.
This information is kept at every level (not just the leaves of the JSON structure) to preserve updates that remove
object keys.
Optimistic concurrency
Tags, desired, and reported properties all support optimistic concurrency. Tags have an ETag, as per RFC7232, that
represents the tag's JSON representation. You can use ETags in conditional update operations from the solution
back end to ensure consistency.
Module twin desired and reported properties do not have ETags, but have a $version value that is guaranteed to
be incremental. Similarly to an ETag, the version can be used by the updating party to enforce consistency of
updates. For example, a module app for a reported property or the solution back end for a desired property.
Versions are also useful when an observing agent (such as the module app observing the desired properties)
must reconcile races between the result of a retrieve operation and an update notification. The section [Device
reconnection flow ][lnk-reconnection] provides more information.
Next steps
To try out some of the concepts described in this article, see the following IoT Hub tutorials:
Get started with IoT Hub module identity and module twin using .NET backup and .NET device
Understand and invoke direct methods from IoT
Hub
6/4/2018 • 5 min to read • Edit Online
IoT Hub gives you the ability to invoke direct methods on devices from the cloud. Direct methods
represent a request-reply interaction with a device similar to an HTTP call in that they succeed or fail
immediately (after a user-specified timeout). This approach is useful for scenarios where the course of
immediate action is different depending on whether the device was able to respond.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information
about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Each device method targets a single device. Jobs provide a way to invoke direct methods on multiple
devices, and schedule method invocation for disconnected devices.
Anyone with service connect permissions on IoT Hub may invoke a method on a device.
Direct methods follow a request-response pattern and are meant for communications that require
immediate confirmation of their result. For example, interactive control of the device, such as turning on
a fan.
Refer to Cloud-to-device communication guidance if in doubt between using desired properties, direct
methods, or cloud-to-device messages.
Method lifecycle
Direct methods are implemented on the device and may require zero or more inputs in the method
payload to correctly instantiate. You invoke a direct method through a service-facing URI (
{iot hub}/twins/{device id}/methods/ ). A device receives direct methods through a device-specific
MQTT topic ( $iothub/methods/POST/{method name}/ ) or through AMQP links ( IoThub-methodname and
IoThub-status application properties).
NOTE
When you invoke a direct method on a device, property names and values can only contain US-ASCII printable
alphanumeric, except any in the following set:
{'$', '(', ')', '<', '>', '@', ',', ';', ':', '\', '"', '/', '[', ']', '?', '=', '{', '}', SP,
HT}
.
Direct methods are synchronous and either succeed or fail after the timeout period (default: 30 seconds,
settable up to 3600 seconds). Direct methods are useful in interactive scenarios where you want a device
to act if and only if the device is online and receiving commands. For example, turning on a light from a
phone. In these scenarios, you want to see an immediate success or failure so the cloud service can act
on the result as soon as possible. The device may return some message body as a result of the method,
but it isn't required for the method to do so. There is no guarantee on ordering or any concurrency
semantics on method calls.
Direct methods are HTTPS -only from the cloud side, and MQTT or AMQP from the device side.
The payload for method requests and responses is a JSON document up to 128 KB.
{
"methodName": "reboot",
"responseTimeoutInSeconds": 200,
"payload": {
"input1": "someInput",
"input2": "anotherInput"
}
}
{
"status" : 201,
"payload" : {...}
}
Both status and body are provided by the device and used to respond with the device's own
status code and/or description.
Method invocation for IoT Edge modules
Invoking direct methods using a module ID is supported in the C# preview SDK (available here).
For this purpose, use the ServiceClient.InvokeDeviceMethodAsync() method and pass in the deviceId
and moduleId as parameters.
The AMQP message arrives on the receive link that represents the method request. It contains the
following:
The correlation ID property, which contains a request ID that should be passed back with the
corresponding method response
An application property named IoThub-methodname , which contains the name of the method being
invoked
The AMQP message body containing the method payload as JSON
Response
The device creates a sending link to return the method response on address
amqps://{hostname}:5671/devices/{deviceId}/methods/deviceBound
The method’s response is returned on the sending link and is structured as follows:
The correlation ID property, which contains the request ID passed in the method’s request message
An application property named IoThub-status , which contains the user supplied method status
The AMQP message body containing the method response as JSON
Next steps
Now you have learned how to use direct methods, you may be interested in the following IoT Hub
developer guide article:
Schedule jobs on multiple devices
If you would like to try out some of the concepts described in this article, you may be interested in the
following IoT Hub tutorial:
Use direct methods
Schedule jobs on multiple devices
5/29/2018 • 4 min to read • Edit Online
Azure IoT Hub enables a number of building blocks like device twin properties and tags and direct methods.
Typically, back-end apps enable device administrators and operators to update and interact with IoT devices
in bulk and at a scheduled time. Jobs execute device twin updates and direct methods against a set of
devices at a scheduled time. For example, an operator would use a back-end app that initiates and tracks a
job to reboot a set of devices in building 43 and floor 3 at a time that would not be disruptive to the
operations of the building.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about
the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Consider using jobs when you need to schedule and track progress any of the following activities on a set of
devices:
Update desired properties
Update tags
Invoke direct methods
Job lifecycle
Jobs are initiated by the solution back end and maintained by IoT Hub. You can initiate a job through a
service-facing URI ( {iot hub}/jobs/v2/{device id}/methods/<jobID>?api-version=2016-11-14 ) and query for
progress on an executing job through a service-facing URI (
{iot hub}/jobs/v2/<jobId>?api-version=2016-11-14 ). To refresh the status of running jobs once a job is
initiated, run a job query.
NOTE
When you initiate a job, property names and values can only contain US-ASCII printable alphanumeric, except any in
the following set: $ ( ) < > @ , ; : \ " / [ ] ? = { } SP HT .
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
{
jobId: '<jobId>',
type: 'scheduleDirectRequest',
cloudToDeviceMethod: {
methodName: '<methodName>',
payload: <payload>,
responseTimeoutInSeconds: methodTimeoutInSeconds
},
queryCondition: '<queryOrDevices>', // query condition
startTime: <jobStartTime>, // as an ISO-8601 date string
maxExecutionTimeInSeconds: <maxExecutionTimeInSeconds>
}
The query condition can also be on a single device ID or on a list of device IDs as shown in the following
examples:
IoT Hub Query Language covers IoT Hub query language in additional detail.
PUT /jobs/v2/<jobId>?api-version=2016-11-14
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
{
jobId: '<jobId>',
type: 'scheduleTwinUpdate',
updateTwin: <patch> // Valid JSON object
queryCondition: '<queryOrDevices>', // query condition
startTime: <jobStartTime>, // as an ISO-8601 date string
maxExecutionTimeInSeconds: <maxExecutionTimeInSeconds> // format TBD
}
GET /jobs/v2/query?api-version=2016-11-14[&jobType=<jobType>][&jobStatus=<jobStatus>][&pageSize=
<pageSize>][&continuationToken=<continuationToken>]
Authorization: <config.sharedAccessSignature>
Content-Type: application/json; charset=utf-8
Request-Id: <guid>
User-Agent: <sdk-name>/<sdk-version>
The continuationToken is provided from the response.
Jobs Properties
The following list shows the properties and corresponding descriptions, which can be used when querying
for jobs or job results.
PROPERTY DESCRIPTION
endTime IoT Hub provided date (ISO-8601) for when the job
completed. Valid only after the job reaches the 'completed'
state.
deviceJobStatistics properties:
deviceJobStatistics.succeededCount: Number of
devices where the job succeeded.
PROPERTY DESCRIPTION
Next steps
To try out some of the concepts described in this article, see the following IoT Hub tutorial:
Schedule and broadcast jobs
Reference - IoT Hub endpoints
5/29/2018 • 5 min to read • Edit Online
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device
management, are only available in the standard tier of IoT hub. For more information about the basic and
standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Custom endpoints
You can link existing Azure services in your subscription to your IoT hub to act as endpoints for
message routing. These endpoints act as service endpoints and are used as sinks for message routes.
Devices cannot write directly to the additional endpoints. To learn more about message routes, see the
developer guide entry on sending and receiving messages with IoT hub.
IoT Hub currently supports the following Azure services as additional endpoints:
Azure Storage containers
Event Hubs
Service Bus Queues
Service Bus Topics
IoT Hub needs write access to these service endpoints for message routing to work. If you configure
your endpoints through the Azure portal, the necessary permissions are added for you. Make sure you
configure your services to support the expected throughput. When you first configure your IoT solution,
you may need to monitor your additional endpoints and make any necessary adjustments for the actual
load.
If a message matches multiple routes that all point to the same endpoint, IoT Hub delivers message to
that endpoint only once. Therefore, you do not need to configure deduplication on your Service Bus
queue or topic. In partitioned queues, partition affinity guarantees message ordering.
For the limits on the number of endpoints you can add, see Quotas and throttling.
When using Azure Storage containers
IoT Hub only supports writing data to Azure Storage containers as blobs in the Apache Avro format.
IoT Hub batches messages and writes data to a blob whenever:
The batch reaches a certain size.
Or a certain amount of time has elapsed.
IoT Hub will write to an empty blob if there is no data to write.
IoT Hub defaults to the following file naming convention:
{iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}
You may use whatever file naming convention you wish, however you must use all listed tokens.
When using Service Bus queues and topics
Service Bus queues and topics used as IoT Hub endpoints must not have Sessions or Duplicate
Detection enabled. If either of those options are enabled, the endpoint appears as Unreachable in the
Azure portal.
Field gateways
In an IoT solution, a field gateway sits between your devices and your IoT Hub endpoints. It is typically
located close to your devices. Your devices communicate directly with the field gateway by using a
protocol supported by the devices. The field gateway connects to an IoT Hub endpoint using a protocol
that is supported by IoT Hub. A field gateway might be a dedicated hardware device or a low -power
computer running custom gateway software.
You can use Azure IoT Edge to implement a field gateway. IoT Edge offers functionality such as
multiplexing communications from multiple devices onto the same IoT Hub connection.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub query language for device twins, jobs, and message routing
Quotas and throttling
IoT Hub MQTT support
IoT Hub query language for device and
module twins, jobs, and message routing
5/29/2018 • 15 min to read • Edit Online
IoT Hub provides a powerful SQL -like language to retrieve information regarding device twins and
jobs, and message routing. This article presents:
An introduction to the major features of the IoT Hub query language, and
The detailed description of the language.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device
management, are only available in the standard tier of IoT hub. For more information about the basic and
standard IoT Hub tiers, see How to choose the right IoT Hub tier.
NOTE
Azure IoT SDKs support paging of large results.
IoT Hub allows you to retrieve device twins filtering with arbitrary conditions. For instance, to receive
device twins where the location.region tag is set to US use the following query:
SELECT * FROM devices
WHERE tags.location.region = 'US'
Boolean operators and arithmetic comparisons are supported as well. For example, to retrieve device
twins located in the US and configured to send telemetry less than every minute use the following
query:
As a convenience, it is also possible to use array constants with the IN and NIN (not in) operators.
For instance, to retrieve device twins that report WiFi or wired connectivity use the following query:
It is often necessary to identify all device twins that contain a specific property. IoT Hub supports the
function is_defined() for this purpose. For instance, to retrieve device twins that define the
connectivity property use the following query:
Refer to the WHERE clause section for the full reference of the filtering capabilities.
Grouping and aggregations are also supported. For instance, to find the count of devices in each
telemetry configuration status use the following query:
This grouping query would return a result similar to the following example:
[
{
"numberOfDevices": 3,
"status": "Success"
},
{
"numberOfDevices": 2,
"status": "Pending"
},
{
"numberOfDevices": 1,
"status": "Error"
}
]
In this example, three devices reported successful configuration, two are still applying the
configuration, and one reported an error.
Projection queries allow developers to return only the properties they care about. For example, to
retrieve the last activity time of all disconnected devices use the following query:
We don't allow join between the devices and devices.modules collections. If you want to query
module twins across devices, you do do it based on tags. This query will return all module twins
across all devices with the scanning status:
This query will return all module twins with the scanning status, but only on the specified subset of
devices.
C# example
The query functionality is exposed by the C# service SDK in the RegistryManager class. Here is an
example of a simple query:
The query object is instantiated with a page size (up to 100). Then multiple pages are retrieved by
calling the GetNextAsTwinAsync methods multiple times.
The query object exposes multiple Next values, depending on the deserialization option required by
the query. For example, device twin or job objects, or plain JSON when using projections.
Node.js example
The query functionality is exposed by the Azure IoT service SDK for Node.js in the Registry object.
Here is an example of a simple query:
var query = registry.createQuery('SELECT * FROM devices', 100);
var onResults = function(err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
// Do something with the results
results.forEach(function(twin) {
console.log(twin.deviceId);
});
if (query.hasMoreResults) {
query.nextAsTwin(onResults);
}
}
};
query.nextAsTwin(onResults);
The query object is instantiated with a page size (up to 100). Then multiple pages are retrieved by
calling the nextAsTwin method multiple times.
The query object exposes multiple Next values, depending on the deserialization option required by
the query. For example, device twin or job objects, or plain JSON when using projections.
Limitations
IMPORTANT
Query results can have a few minutes of delay with respect to the latest values in device twins. If querying
individual device twins by ID, use the retrieve device twin API. This API always contains the latest values and
has higher throttling limits.
Currently, comparisons are supported only between primitive types (no objects), for instance
... WHERE properties.desired.config = properties.reported.config is supported only if those
properties have primitive values.
Currently, this collection is queryable as devices.jobs in the IoT Hub query language.
IMPORTANT
Currently, the jobs property is never returned when querying device twins. That is, queries that contain 'FROM
devices'. The jobs property can only be accessed directly with queries using FROM devices.jobs .
For instance, to get all jobs (past and scheduled) that affect a single device, you can use the following
query:
Note how this query provides the device-specific status (and possibly the direct method response) of
each job returned. It is also possible to filter with arbitrary Boolean conditions on all object properties
in the devices.jobs collection. For instance, to retrieve all completed device twin update jobs that
were created after September 2016 for a specific device, use the following query:
Limitations
Currently, queries on devices.jobs do not support:
Projections, therefore only SELECT * is possible.
Conditions that refer to the device twin in addition to job properties (see the preceding section).
Performing aggregations, such as count, avg, group by.
{
"message": {
"systemProperties": {
"contentType": "application/json",
"contentEncoding": "utf-8",
"iothub-message-source": "deviceMessages",
"iothub-enqueuedtime": "2017-05-08T18:55:31.8514657Z"
},
"appProperties": {
"processingPath": "<optional>",
"verbose": "<optional>",
"severity": "<optional>",
"testDevice": "<optional>"
},
"body": "{\"Weather\":{\"Temperature\":50}}"
}
}
Message system properties are prefixed with the '$' symbol. User properties are always accessed
with their name. If a user property name coincides with a system property (such as $contentType ), the
user property is retrieved with the $contentType expression. You can always access the system
property using brackets {} : for instance, you can use the expression {$contentType} to access the
system property contentType . Bracketed property names always retrieve the corresponding system
property.
Remember that property names are case insensitive.
NOTE
All message properties are strings. System properties, as described in the developer guide, are currently not
available to use in queries.
For example, if you use a messageType property, you might want to route all telemetry to one
endpoint, and all alerts to another endpoint. You can write the following expression to route the
telemetry:
messageType = 'telemetry'
messageType = 'alert'
Boolean expressions and functions are also supported. This feature enables you to distinguish
between severity level, for example:
Refer to the Expression and conditions section for the full list of supported operators and functions.
Routing on message bodies
IoT Hub can only route based on message body contents if the message body is properly formed
JSON encoded in UTF -8, UTF -16, or UTF -32. Set the content type of the message to
application/json . Set the content encoding to one of the supported UTF encodings in the message
headers. If either of the headers is not specified, IoT Hub does not attempt to evaluate any query
expression involving the body against the message. If your message is not a JSON message, or if the
message does not specify the content type and content encoding, you can still use message routing to
route the message based on the message headers.
The following example shows how to create a message with a properly formed and encoded JSON
body:
string messageBody = @"{
""Weather"":{
""Temperature"":50,
""Time"":""2017-03-09T00:00:00.000Z"",
""PrevTemperatures"":[
20,
30,
40
],
""IsEnabled"":true,
""Location"":{
""Street"":""One Microsoft Way"",
""City"":""Redmond"",
""State"":""WA""
},
""HistoricalData"":[
{
""Month"":""Feb"",
""Temperature"":40
},
{
""Month"":""Jan"",
""Temperature"":30
}
]
}
}";
await deviceClient.SendEventAsync(message);
}
You can use $body in the query expression to route the message. You can use a simple body
reference, body array reference, or multiple body references in the query expression. Your query
expression can also combine a body reference with a message header reference. For example, the
following are all valid query expressions:
$body.Weather.HistoricalData[0].Month = 'Feb'
$body.Weather.Temperature = 50 AND $body.Weather.IsEnabled
length($body.Weather.Location.State) = 2
$body.Weather.Temperature = 50 AND Status = 'Active'
FROM clause
The FROM <from_specification> clause can assume only two values: FROM devices to query
device twins, or FROM devices.jobs to query job per-device details.
WHERE clause
The WHERE <filter_condition> clause is optional. It specifies one or more conditions that the
JSON documents in the FROM collection must satisfy to be included as part of the result. Any JSON
document must evaluate the specified conditions to "true" to be included in the result.
The allowed conditions are described in section Expressions and conditions.
SELECT clause
The SELECT <select_list> is mandatory and specifies what values are retrieved from the query. It
specifies the JSON values to be used to generate new JSON objects. For each element of the filtered
(and optionally grouped) subset of the FROM collection, the projection phase generates a new JSON
object. This object is constructed with the values specified in the SELECT clause.
Following is the grammar of the SELECT clause:
<projection_list> ::=
'*'
| <projection_element> AS alias [, <projection_element> AS alias]+
<projection_element> :==
attribute_name
| <projection_element> '.' attribute_name
| <aggregate>
<aggregate> :==
count()
| avg(<projection_element>)
| sum(<projection_element>)
| min(<projection_element>)
| max(<projection_element>)
Attribute_name refers to any property of the JSON document in the FROM collection. Some
examples of SELECT clauses can be found in the Getting started with device twin queries section.
Currently, selection clauses different than SELECT* are only supported in aggregate queries on
device twins.
GROUP BY clause
The GROUP BY <group_specification> clause is an optional step that executes after the filter
specified in the WHERE clause, and before the projection specified in the SELECT. It groups
documents based on the value of an attribute. These groups are used to generate aggregated values
as specified in the SELECT clause.
An example of a query using GROUP BY is:
GROUP BY <group_by_element>
<group_by_element> :==
attribute_name
| < group_by_element > '.' attribute_name
Attribute_name refers to any property of the JSON document in the FROM collection.
Currently, the GROUP BY clause is only supported when querying device twins.
<expression> ::=
<constant> |
attribute_name |
<function_call> |
<expression> binary_operator <expression> |
<create_array_expression> |
'(' <expression> ')'
<function_call> ::=
<function_name> '(' expression ')'
<constant> ::=
<undefined_constant>
| <null_constant>
| <number_constant>
| <string_constant>
| <array_constant>
To understand what each symbol in the expressions syntax stands for, refer to the following table:
SYMBOL DEFINITION
Operators
The following operators are supported:
FAMILY OPERATORS
Arithmetic +, -, *, /, %
Functions
When querying twins and jobs the only supported function is:
FUNCTION DESCRIPTION
FUNCTION DESCRIPTION
SIGN(x) Returns the positive (+1), zero (0), or negative (-1) sign
of the specified numeric expression.
In routes conditions, the following type checking and casting functions are supported:
FUNCTION DESCRIPTION
FUNCTION DESCRIPTION
Next steps
Learn how to execute queries in your apps using Azure IoT SDKs.
Reference - IoT Hub quotas and throttling
5/29/2018 • 4 min to read • Edit Online
Operation throttles
Operation throttles are rate limitations that are applied in minute ranges, and are intended to prevent
abuse. IoT Hub tries to avoid returning errors whenever possible, but starts returning exceptions if
the throttle is violated for too long.
At any given time, you can increase quotas or throttle limits by increasing the number of provisioned
units in an IoT hub.
The following table shows the enforced throttles. Values refer to an individual hub.
File upload 1.67 file upload 1.67 file upload 83.33 file upload
notifications/sec/unit notifications/sec/unit notifications/sec/unit
(100/min/unit) (100/min/unit) (5000/min/unit)
1This feature is not available in the basic tier of IoT Hub. For more information, see How to choose
the right IoT Hub.
2Throttling meter size is 8 KB.
The device connections throttle governs the rate at which new device connections can be established
with an IoT hub. The device connections throttle does not govern the maximum number of
simultaneously connected devices. The throttle depends on the number of units that are provisioned
for the IoT hub.
For example, if you buy a single S1 unit, you get a throttle of 100 connections per second. Therefore,
to connect 100,000 devices, it takes at least 1000 seconds (approximately 16 minutes). However, you
can have as many simultaneously connected devices as you have devices registered in your identity
registry.
For an in-depth discussion of IoT Hub throttling behavior, see the blog post IoT Hub throttling and
you.
IMPORTANT
Identity registry operations are intended for run-time use in device management and provisioning scenarios.
Reading or updating a large number of device identities is supported through import and export jobs.
Other limits
IoT Hub enforces other operational limits:
OPERATION LIMIT
File upload URIs 10000 SAS URIs can be out for a storage account at
one time.
10 SAS URIs/device can be out at one time.
Additional endpoints Paid SKU hubs may have 10 additional endpoints. Free
SKU hubs may have one additional endpoint.
Message routing rules Paid SKU hubs may have 100 routing rules. Free SKU
hubs may have five routing rules.
1This feature is not available in the basic tier of IoT Hub. For more information, see How to choose
the right IoT Hub.
NOTE
Currently, the maximum number of devices you can connect to a single IoT hub is 500,000. If you want to
increase this limit, contact Microsoft Support.
Latency
IoT Hub strives to provide low latency for all operations. However, due to network conditions and
other unpredictable factors it cannot guarantee a maximum latency. When designing your solution,
you should:
Avoid making any assumptions about the maximum latency of any IoT Hub operation.
Provision your IoT hub in the Azure region closest to your devices.
Consider using Azure IoT Edge to perform latency-sensitive operations on the device or on a
gateway close to the device.
Multiple IoT Hub units affect throttling as described previously, but do not provide any additional
latency benefits or guarantees. If you see unexpected increases in operation latency, contact Microsoft
Support.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub endpoints
IoT Hub query language for device twins, jobs, and message routing
IoT Hub MQTT support
Azure IoT Hub pricing information
5/29/2018 • 3 min to read • Edit Online
Azure IoT Hub pricing provides the general information on different SKUs and pricing for IoT Hub. This article
contains additional details on how the various IoT Hub functionalities are metered as messages by IoT Hub.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
Cloud-to-device messages Successfully sent messages are charged in 4-KB chunks, for
example a 6-KB message is charged 2 messages.
File uploads File transfer to Azure Storage is not metered by IoT Hub. File
transfer initiation and completion messages are charged as
messaged metered in 4-KB increments. For example,
transferring a 10-MB file is charged two messages in addition
to the Azure Storage cost.
Device and module twin reads Twin reads from the device or module and from the solution
back end are charged as messages in 512-byte chunks. For
example, reading a 6-KB twin is charged as 12 messages.
Device and module twin updates (tags and properties) Twin updates from the device or module and from the
solution back end are charged as messages in 512-byte
chunks. For example, reading a 6-KB twin is charged as 12
messages.
OPERATION BILLING INFORMATION
Device and module twin queries Queries are charged as messages depending on the result size
in 512-byte chunks.
Jobs per-device operations Jobs operations (such as twin updates, and methods) are
charged as normal. For example, a job resulting in 1000
method calls with 1-KB requests and empty-body responses
is charged 1000 messages.
NOTE
All sizes are computed considering the payload size in bytes (protocol framing is ignored). For messages, which have
properties and body, the size is computed in a protocol-agnostic way. For more information, see IoT Hub messaging
developer's guide.
Example #1
A device sends one 1-KB device-to-cloud message per minute to IoT Hub, which is then read by Azure Stream
Analytics. The solution back end invokes a method (with 512-byte payload) on the device every 10 minutes to
trigger a specific action. The device responds to the method with a result of 200 bytes.
The device consumes:
One message * 60 minutes * 24 hours = 1440 messages per day for the device-to-cloud messages.
Two request plus response * 6 times per hour * 24 hours = 288 messages for the methods.
This calculation gives a total of 1728 messages per day.
Example #2
A device sends one 100-KB device-to-cloud message every hour. It also updates its device twin with 1-KB
payloads every four hours. The solution back end, once per day, reads the 14-KB device twin and updates it with
512-byte payloads to change configurations.
The device consumes:
25 (100 KB / 4 KB ) messages * 24 hours for device-to-cloud messages.
Two messages (1 KB / 0.5 KB ) * six times per day for device twin updates.
This calculation gives a total of 612 messages per day.
The solution back end consumes 28 messages (14 KB / 0.5 KB ) to read the device twin, plus one message to
update it, for a total of 29 messages.
In total, the device and the solution back end consume 641 messages per day.
Understand and use Azure IoT Hub SDKs
6/6/2018 • 2 min to read • Edit Online
There are two categories of software development kits (SDKs) for working with IoT Hub:
Device SDKs enable you to build apps that run on your IoT devices. These apps send
telemetry to your IoT hub, and optionally receive messages, job, method, or twin updates
from your IoT hub.
Service SDKs enable you to manage your IoT hub, and optionally send messages, schedule
jobs, invoke direct methods, or send desired property updates to your IoT devices.
Learn about the benefits of developing using Azure IoT SDKs here.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device
management, are only available in the standard tier of IoT hub. For more information about the basic and
standard IoT Hub tiers, see How to choose the right IoT Hub tier.
NOTE
See the readme files in the GitHub repositories for information about using language and platform-specific
package managers to install binaries and dependencies on your development machine.
Next steps
Other reference topics in this IoT Hub developer guide include:
IoT Hub endpoints
IoT Hub query language for device twins, jobs, and message routing
Quotas and throttling
IoT Hub MQTT support
IoT Hub REST API reference
Communicate with your IoT hub using the MQTT
protocol
5/29/2018 • 11 min to read • Edit Online
IoT Hub enables devices to communicate with the IoT Hub device endpoints using:
MQTT v3.1.1 on port 8883
MQTT v3.1.1 over WebSocket on port 443.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management,
are only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see
How to choose the right IoT Hub tier.
All device communication with IoT Hub must be secured using TLS/SSL. Therefore, IoT Hub doesn’t support
non-secure connections over port 1883.
Node.js azure-iot-device-mqtt
Java IotHubClientProtocol.MQTT
C MQTT_Protocol
C# TransportType.Mqtt
Python IoTHubTransportProvider.MQTT
Migrating a device app from AMQP to MQTT
If you are using the device SDKs, switching from using AMQP to MQTT requires changing the protocol
parameter in the client initialization as stated previously.
When doing so, make sure to check the following items:
AMQP returns errors for many conditions, while MQTT terminates the connection. As a result your
exception handling logic might require some changes.
MQTT does not support the reject operations when receiving cloud-to-device messages. If your back-end
app needs to receive a response from the device app, consider using direct methods.
For example, if the name of your IoT hub is contoso.azure-devices.net and if the name of your
device is MyDevice01, the full Username field should contain:
contoso.azure-devices.net/MyDevice01/api-version=2016-11-14
For the Password field, use a SAS token. The format of the SAS token is the same as for both the
HTTPS and AMQP protocols:
SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI}
NOTE
If you use X.509 certificate authentication, SAS token passwords are not required. For more information, see
Set up X.509 security in your Azure IoT Hub
For more information about how to generate SAS tokens, see the device section of Using IoT Hub
security tokens.
When testing, you can also use the device explorer tool to quickly generate a SAS token that you can
copy and paste into your own code:
1. Go to the Management tab in Device Explorer.
2. Click SAS Token (top right).
3. On SASTokenForm, select your device in the DeviceID drop down. Set your TTL.
4. Click Generate to create your token.
The SAS token that's generated has the following structure:
HostName={your hub name}.azure-
devices.net;DeviceId=javadevice;SharedAccessSignature=SharedAccessSignature sr={your hub
name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-version%3D2016-11-
14&sig=vSgHBMUG.....Ntg%3d&se=1456481802
The part of this token to use as the Password field to connect using MQTT is:
SharedAccessSignature sr={your hub name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-
version%3D2016-11-14&sig=vSgHBMUG.....Ntg%3d&se=1456481802
For MQTT connect and disconnect packets, IoT Hub issues an event on the Operations Monitoring
channel. This event has additional information that can help you to troubleshoot connectivity issues.
The device app can specify a Will message in the CONNECT packet. The device app should use
devices/{device_id}/messages/events/{property_bag} or devices/{device_id}/messages/events/{property_bag}
as the Will topic name to define Will messages to be forwarded as a telemetry message. In this case, if the
network connection is closed, but a DISCONNECT packet was not previously received from the device, then
IoT Hub sends the Will message supplied in the CONNECT packet to the telemetry channel. The telemetry
channel can be either the default Events endpoint or a custom endpoint defined by IoT Hub routing. The
message has the iothub-MessageType property with a value of Will assigned to it.
TLS/SSL configuration
To use the MQTT protocol directly, your client must connect over TLS/SSL. Attempts to skip this step fail with
connection errors.
In order to establish a TLS connection, you may need to download and reference the DigiCert Baltimore Root
Certificate. This certificate is the one that Azure uses to secure the connection. You can find this certificate in
the Azure-iot-sdk-c repository. More information about these certificates can be found on Digicert's website.
An example of how to implement this using the Python version of the Paho MQTT library by the Eclipse
Foundation might look like the following.
First, install the Paho library from your command-line environment:
Then, implement the client in a Python script. Replace the placeholders as follows:
<local path to digicert.cer> is the path to a local file that contains the DigiCert Baltimore Root
certificate. You can create this file by copying the certificate information from certs.c in the Azure IoT SDK
for C. Include the lines -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- , remove the "
marks at the beginning and end of every line, and remove the \r\n characters at the end of every line.
<device id from device registry> is the ID of a device you added to your IoT hub.
<generated SAS token> is a SAS token for the device created as described previously in this article.
<iot hub name> the name of your IoT hub.
from paho.mqtt import client as mqtt
import ssl
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.connect(iot_hub_name+".azure-devices.net", port=8883)
NOTE
This {property_bag} element uses the same encoding as for query strings in the HTTPS protocol.
Request ID can be any valid value for a message property value, as per IoT Hub messaging developer's
guide, and status is validated as an integer.
The response body contains the properties section of the device twin. The following snippet shows the body
of the identity registry entry limited to the "properties" member, for example:
{
"properties": {
"desired": {
"telemetrySendFrequency": "5m",
"$version": 12
},
"reported": {
"telemetrySendFrequency": "5m",
"batteryLevel": 55,
"$version": 123
}
}
}
STATUS DESCRIPTION
200 Success
STATUS DESCRIPTION
{
"telemetrySendFrequency": "35m",
"batteryLevel": 60
}
STATUS DESCRIPTION
200 Success
As for property updates, null values means that the JSON object member is being deleted.
IMPORTANT
IoT Hub generates change notifications only when devices are connected. Make sure to implement the device
reconnection flow to keep the desired properties synchronized between IoT Hub and the device app.
To respond, the device sends a message with a valid JSON or empty body to the topic
$iothub/methods/res/{status}/?$rid={request id} . In this message, the request ID must match the one in the
request message, and status must be an integer.
For more information, see Direct method developer's guide.
Additional considerations
As a final consideration, if you need to customize the MQTT protocol behavior on the cloud side, you should
review the Azure IoT protocol gateway. This software enables you to deploy a high-performance custom
protocol gateway that interfaces directly with IoT Hub. The Azure IoT protocol gateway enables you to
customize the device protocol to accommodate brownfield MQTT deployments or other custom protocols.
This approach does require, however, that you run and operate a custom protocol gateway.
Next steps
To learn more about the MQTT protocol, see the MQTT documentation.
To learn more about planning your IoT Hub deployment, see:
Azure Certified for IoT device catalog
Support additional protocols
Compare with Event Hubs
Scaling, HA, and DR
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Glossary of IoT Hub terms
5/29/2018 • 19 min to read • Edit Online
This article lists some of the common terms used in the IoT Hub articles.
Azure CLI
The Azure CLI is a cross-platform, open-source, shell-based, command tool for creating and managing resources
in Microsoft Azure. This version of the CLI is implemented using Node.js.
Azure portal
The Microsoft Azure portal is a central place where you can provision and manage your Azure resources. It
organizes its content using blades.
Azure PowerShell
Azure PowerShell is a collection of cmdlets you can use to manage Azure with Windows PowerShell. You can use
the cmdlets to create, test, deploy, and manage solutions and services delivered through the Azure platform.
Azure Storage
Azure Storage is a cloud storage solution. It includes the Blob Storage service that you can use to store
unstructured object data. Some IoT Hub tutorials use blob storage.
Back-end app
In the context of IoT Hub, a back-end app is an app that connects to one of the service-facing endpoints on an IoT
hub. For example, a back-end app might retrieve device-to-cloudmessages or manage the identity registry.
Typically, a back-end app runs in the cloud, but in many of the tutorials the back-end apps are console apps
running on your local development machine.
Built-in endpoints
Every IoT hub includes a built-in endpoint that is Event Hub-compatible. You can use any mechanism that works
with Event Hubs to read device-to-cloud messages from this endpoint.
Cloud gateway
A cloud gateway enables connectivity for devices that cannot connect directly to IoT Hub. A cloud gateway is
hosted in the cloud in contrast to a field gateway that runs local to your devices. A typical use case for a cloud
gateway is to implement protocol translation for your devices.
Cloud-to-device
Refers to messages sent from an IoT hub to a connected device. Often, these messages are commands that
instruct the device to take an action. For more information, see Send and receive messages with IoT Hub.
Configuration
In the context of automatic device configuration, a configuration within IoT Hub defines the desired configuration
for a set of devices twins and provides a set of metrics to report status and progress.
Connection string
You use connection strings in your app code to encapsulate the information required to connect to an endpoint. A
connection string typically includes the address of the endpoint and security information, but connection string
formats vary across services. There are two types of connection string associated with the IoT Hub service:
Device connection strings enable devices to connect to the device-facing endpoints on an IoT hub.
IoT Hub connection strings enable back-end apps to connect to the service-facing endpoints on an IoT hub.
Custom endpoints
You can create custom endpoints on an IoT hub to deliver messages dispatched by a routing rule. Custom
endpoints connect directly to an Event hub, a Service Bus queue, or a Service Bus topic.
Custom gateway
A gateway enables connectivity for devices that cannot connect directly to IoT Hub. You can use Azure IoT Edge to
build custom gateways that implement custom logic to handle messages, custom protocol conversions, and other
processing on the edge.
Data-point message
A data-point message is a device-to-cloud message that contains telemetry data such as wind speed or
temperature.
Desired configuration
In the context of a device twin, desired configuration refers to the complete set of properties and metadata in the
device twin that should be synchronized with the device.
Desired properties
In the context of a device twin, desired properties is a subsection of the device twin that is used with reported
properties to synchronize device configuration or condition. Desired properties can only be set by a back-end app
and are observed by the device app.
Device-to-cloud
Refers to messages sent from a connected device to IoT Hub. These messages may be data-point or interactive
messages. For more information, see Send and receive messages with IoT Hub.
Device
In the context of IoT, a device is typically a small-scale, standalone computing device that may collect data or
control other devices. For example, a device might be an environmental monitoring device, or a controller for the
watering and ventilation systems in a greenhouse. The device catalog provides a list of hardware devices certified
to work with IoT Hub.
Device app
A device app runs on your device and handles the communication with your IoT hub. Typically, you use one of the
Azure IoT device SDKs when you implement a device app. In many of the IoT tutorials, you use a simulated device
for convenience.
Device condition
Refers to device state information, such as the connectivity method currently in use, as reported by a device app.
Device apps can also report their capabilities. You can query for condition and capability information using device
twins.
Device data
Device data refers to the per-device data stored in the IoT Hub identity registry. It is possible to import and export
this data.
Device explorer
The device explorer is a tool that runs on Windows and enables you to manage your devices in the identity
registry.The tool can also send and receive messages to your devices.
Device identity
The device identity is the unique identifier assigned to every device registered in the identity registry.
Module identity
The module identity is the unique identifier assigned to every module that belong to a device. Module identity is
also registered in the identity registry.
Device management
Device management encompasses the full lifecycle associated with managing the devices in your IoT solution
including planning, provisioning, configuring, monitoring, and retiring.
Device provisioning
Device provisioning is the process of adding the initial device data to the stores in your solution. To enable a new
device to connect to your hub, you must add a device ID and keys to the IoT Hub identity registry. As part of the
provisioning process, you might need to initialize device-specific data in other solution stores.
Device twin
A device twin is JSON document that stores device state information such as metadata, configurations, and
conditions. IoT Hub persists a device twin for each device that you provision in your IoT hub. Device twins enable
you to synchronize device conditions and configurations between the device and the solution back end. You can
query device twins to locate specific devices and query the status of long-running operations.
Module twin
Similar to device twin, a module twin is JSON document that stores module state information such as metadata,
configurations, and conditions. IoT Hub persists a module twin for each module identity that you provision under a
device identity in your IoT hub. Module twins enable you to synchronize module conditions and configurations
between the module and the solution back end. You can query module twins to locate specific modules and query
the status of long-running operations.
Twin queries
Device and module twin queries use the SQL -like IoT Hub query language to retrieve information from your
device twins or module twins. You can use the same IoT Hub query language to retrieve information about jobs
running in your IoT hub.
Twin synchronization
Twin synchronization uses the desired properties in your device twins or module twins to configure your devices
or modules and retrieve reported properties from them to store in the twin.
Direct method
A direct method is a way for you to trigger a method to execute on a device by invoking an API on your IoT hub.
Endpoint
An IoT hub exposes multiple endpoints that enable your apps to connect to the IoT hub. There are device-facing
endpoints that enable devices to perform operations such as sending device-to-cloud messages and receiving
cloud-to-device messages. There are service-facing management endpoints that enable back-end apps to perform
operations such as device identity management and device twin management. There are service-facing built-in
endpoints for reading device-to-cloud messages. You can create custom endpoints to receive device-to-cloud
messages dispatched by a routing rule.
Field gateway
A field gateway enables connectivity for devices that cannot connect directly to IoT Hub and is typically deployed
locally with your devices. For more information, see What is Azure IoT Hub?
Free account
You can create a free Azure account to complete the IoT Hub tutorials and experiment with the IoT Hub service
(and other Azure services).
Gateway
A gateway enables connectivity for devices that cannot connect directly to IoT Hub. See also Field Gateway, Cloud
Gateway, and Custom Gateway.
Identity registry
The identity registry is the built-in component of an IoT hub that stores information about the individual devices
permitted to connect to an IoT hub.
Interactive message
An interactive message is a cloud-to-device message that triggers an immediate action in the solution back end.
For example, a device might send an alarm about a failure that should be automatically logged in to a CRM
system.
IoT Edge
Azure IoT Edge enables cloud-driven deployment of Azure services and solution-specific code to on-premises
devices. IoT Edge devices can aggregate data from other devices to perform computing and analytics before the
data is sent to the cloud. For more information please see Azure IoT Edge.
IoT Hub
IoT Hub is a fully managed Azure service that enables reliable and secure bidirectional communications between
millions of devices and a solution back end. For more information, see What is Azure IoT Hub? Using your Azure
subscription, you can create IoT hubs to handle your IoT messaging workloads.
Job
Your solution back end can use jobs to schedule and track activities on a set of devices registered with your IoT
hub. Activities include updating device twin desired properties, updating device twin tags, and invoking direct
methods. IoT Hub also uses jobs to import to and export from the identity registry.
MQTT
MQTT is one of the messaging protocols that IoT Hub supports for communicating with devices. For more
information about the messaging protocols that IoT Hub supports, see Send and receive messages with IoT Hub.
Operations monitoring
IoT Hub operations monitoring enables you to monitor the status of operations on your IoT hub in real time. IoT
Hub tracks events across several categories of operations. You can opt into sending events from one or more
categories to an IoT Hub endpoint for processing. You can monitor the data for errors or set up more complex
processing based on data patterns.
Physical device
A physical device is a real device such as a Raspberry Pi that connects to an IoT hub. For convenience, many of the
IoT Hub tutorials use simulated devices to enable you to run samples on your local machine.
Protocol gateway
A protocol gateway is typically deployed in the cloud and provides protocol translation services for devices
connecting to IoT Hub. For more information, see What is Azure IoT Hub?
Reported configuration
In the context of a device twin, reported configuration refers to the complete set of properties and metadata in the
device twin that should be reported to the solution back end.
Reported properties
In the context of a device twin, reported properties is a subsection of the device twin used with desired properties
to synchronize device configuration or condition. Reported properties can only be set by the device app and can be
read and queried by a back-end app.
Resource group
Azure Resource Manager uses resource groups to group related resources together. You can use a resource group
to perform operations on all the resources on the group simultaneously.
Retry policy
You use a retry policy to handle transient errors when you connect to a cloud service.
Routing rules
You configure routing rules in your IoT hub to route device-to-cloud messages to a built-in endpoint or to custom
endpoints for processing by your solution back end.
SASL PLAIN
SASL PL AIN is a protocol that the AMQP protocol uses to transfer security tokens.
Simulated device
For convenience, many of the IoT Hub tutorials use simulated devices to enable you to run samples on your local
machine. In contrast, a physical device is a real device such as a Raspberry Pi that connects to an IoT hub.
Solution
A solution can refer to a Visual Studio solution that includes one or more projects. A solution might also refer to an
IoT solution that includes elements such as devices, device apps, an IoT hub, other Azure services, and back-end
apps.
Subscription
An Azure subscription is where billing takes place. Each Azure resource you create or Azure service you use is
associated with a single subscription. Many quotas also apply at the level of a subscription.
System properties
In the context of a device twin, system properties are read-only and include information regarding the device
usage such as last activity time and connection state.
Tags
In the context of a device twin, tags are device metadata stored and retrieved by the solution back end in the form
of a JSON document. Tags are not visible to apps on a device.
Telemetry
Devices collect telemetry data, such as wind speed or temperature, and use data-point messages to send the
telemetry to an IoT hub.
Token service
You can use a token service to implement an authentication mechanism for your devices. It uses an IoT Hub
shared access policy with DeviceConnect permissions to create device-scoped tokens. These tokens enable a
device to connect to your IoT hub. A device uses a custom authentication mechanism to authenticate with the
token service. IF the device authenticates successfully, the token service issues a SAS token for the device to use to
access your IoT hub.
The Azure IoT device SDK is a set of libraries designed to simplify the process of sending messages to and
receiving messages from the Azure IoT Hub service. There are different variations of the SDK, each targeting
a specific platform, but this article describes the Azure IoT device SDK for C.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management,
are only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see
How to choose the right IoT Hub tier.
The Azure IoT device SDK for C is written in ANSI C (C99) to maximize portability. This feature makes the
libraries well-suited to operate on multiple platforms and devices, especially where minimizing disk and
memory footprint is a priority.
There are a broad range of platforms on which the SDK has been tested (see the Azure Certified for IoT device
catalog for details). Although this article includes walkthroughs of sample code running on the Windows
platform, the code described in this article is identical across the range of supported platforms.
The following video presents an overview of the Azure IoT SDK for C:
This article introduces you to the architecture of the Azure IoT device SDK for C. It demonstrates how to
initialize the device library, send data to IoT Hub, and receive messages from it. The information in this article
should be enough to get started using the SDK, but also provides pointers to additional information about the
libraries.
SDK architecture
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
The latest version of the libraries can be found in the master branch of the repository:
The core implementation of the SDK is in the iothub_client folder that contains the implementation of the
lowest API layer in the SDK: the IoTHubClient library. The IoTHubClient library contains APIs
implementing raw messaging for sending messages to IoT Hub and receiving messages from IoT Hub.
When using this library, you are responsible for implementing message serialization, but other details of
communicating with IoT Hub are handled for you.
The serializer folder contains helper functions and samples that show you how to serialize data before
sending to Azure IoT Hub using the client library. The use of the serializer is not mandatory and is provided
as a convenience. To use the serializer library, you define a model that specifies the data to send to IoT Hub
and the messages you expect to receive from it. Once the model is defined, the SDK provides you with an
API surface that enables you to easily work with device-to-cloud and cloud-to-device messages without
worrying about the serialization details. The library depends on other open source libraries that implement
transport using protocols such as MQTT and AMQP.
The IoTHubClient library depends on other open source libraries:
The Azure C shared utility library, which provides common functionality for basic tasks (such as
strings, list manipulation, and IO ) needed across several Azure-related C SDKs.
The Azure uAMQP library, which is a client-side implementation of AMQP optimized for resource
constrained devices.
The Azure uMQTT library, which is a general-purpose library implementing the MQTT protocol and
optimized for resource constrained devices.
Use of these libraries is easier to understand by looking at example code. The following sections walk you
through several of the sample applications that are included in the SDK. This walkthrough should give you a
good feel for the various capabilities of the architectural layers of the SDK and an introduction to how the APIs
work.
When the device is created, the Devices list updates with all the registered devices, including the one you just
created. If you right-click your new device, you see this menu:
If you choose Copy connection string for selected device, the device connection string is copied to the
clipboard. Keep a copy of the device connection string. You need it when running the sample applications
described in the following sections.
When you've completed the steps above, you're ready to start running some code. Both samples have a
constant at the top of the main source file that enables you to enter a connection string. For example, the
corresponding line from the iothub_client_sample_mqtt application appears as follows.
NOTE
If you open this project in Visual Studio 2017, accept the prompts to retarget the project to the latest version.
This solution contains a single project. There are four NuGet packages installed in this solution:
Microsoft.Azure.C.SharedUtility
Microsoft.Azure.IoTHub.MqttTransport
Microsoft.Azure.IoTHub.IoTHubClient
Microsoft.Azure.umqtt
You always need the Microsoft.Azure.C.SharedUtility package when you are working with the SDK. This
sample uses the MQTT protocol, therefore you must include the Microsoft.Azure.umqtt and
Microsoft.Azure.IoTHub.MqttTransport packages (there are equivalent packages for AMQP and HTTPS ).
Because the sample uses the IoTHubClient library, you must also include the
Microsoft.Azure.IoTHub.IoTHubClient package in your solution.
You can find the implementation for the sample application in the iothub_client_sample_mqtt.c source file.
The following steps use this sample application to walk you through what's required to use the IoTHubClient
library.
Initialize the library
NOTE
Before you start working with the libraries, you may need to perform some platform-specific initialization. For example, if
you plan to use AMQP on Linux you must initialize the OpenSSL library. The samples in the GitHub repository call the
utility function platform_init when the client starts and call the platform_deinit function before exiting. These functions
are declared in the platform.h header file. Examine the definitions of these functions for your target platform in the
repository to determine whether you need to include any platform-specific initialization code in your client.
To start working with the libraries, first allocate an IoT Hub client handle:
You pass a copy of the device connection string you obtained from the device explorer tool to this function. You
also designate the communications protocol to use. This example uses MQTT, but AMQP and HTTPS are also
options.
When you have a valid IOTHUB_CLIENT_HANDLE, you can start calling the APIs to send and receive
messages to and from IoT Hub.
Send messages
The sample application sets up a loop to send messages to your IoT hub. The following snippet:
Creates a message.
Adds a property to the message.
Sends a message.
First, create a message:
size_t iterator = 0;
do
{
if (iterator < MESSAGE_COUNT)
{
sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}",
avgWindSpeed + (rand() % 4 + 2));
if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned
char*)msgText, strlen(msgText))) == NULL)
{
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
}
else
{
messages[iterator].messageTrackingId = iterator;
MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
(void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
{
(void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
}
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle,
SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT
Hub.\r\n", (int)iterator);
}
}
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1);
iterator++;
} while (g_continueRunning);
Every time you send a message, you specify a reference to a callback function that's invoked when the data is
sent. In this example, the callback function is called SendConfirmationCallback. The following snippet shows
this callback function:
Note the call to the IoTHubMessage_Destroy function when you're done with the message. This function
frees the resources allocated when you created the message.
Receive messages
Receiving a message is an asynchronous operation. First, you register the callback to invoke when the device
receives a message:
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) !=
IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
...
The last parameter is a void pointer to whatever you want. In the sample, it's a pointer to an integer but it could
be a pointer to a more complex data structure. This parameter enables the callback function to operate on
shared state with the caller of this function.
When the device receives a message, the registered callback function is invoked. This callback function
retrieves:
The message id and correlation id from the message.
The message content.
Any custom properties from the message.
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
int* counter = (int*)userContextCallback;
const char* buffer;
size_t size;
MAP_HANDLE mapProperties;
const char* messageId;
const char* correlationId;
// Message properties
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
{
messageId = "<null>";
}
// Message content
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
{
(void)printf("unable to retrieve the message data\r\n");
}
else
{
(void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>>
& Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
// If we receive the work 'quit' then we stop running
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
{
g_continueRunning = false;
}
}
Use the IoTHubMessage_GetByteArray function to retrieve the message, which in this example is a string.
Uninitialize the library
When you're done sending events and receiving messages, you can uninitialize the IoT library. To do so, issue
the following function call:
IoTHubClient_LL_Destroy(iotHubClientHandle);
NOTE
If you open this project in Visual Studio 2017, accept the prompts to retarget the project to the latest version.
As with the previous sample, this one includes several NuGet packages:
Microsoft.Azure.C.SharedUtility
Microsoft.Azure.IoTHub.MqttTransport
Microsoft.Azure.IoTHub.IoTHubClient
Microsoft.Azure.IoTHub.Serializer
Microsoft.Azure.umqtt
You've seen most of these packages in the previous sample, but Microsoft.Azure.IoTHub.Serializer is new.
This package is required when you use the serializer library.
You can find the implementation of the sample application in the simplesample_mqtt.c file.
The following sections walk you through the key parts of this sample.
Initialize the library
To start working with the serializer library, call the initialization APIs:
if (serializer_init(NULL) != SERIALIZER_OK)
{
(void)printf("Failed on serializer_init\r\n");
}
else
{
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle =
IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
srand((unsigned int)time(NULL));
int avgWindSpeed = 10;
if (iotHubClientHandle == NULL)
{
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
}
else
{
ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
if (myWeather == NULL)
{
(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
}
else
{
...
The call to the serializer_init function is a one-time call and initializes the underlying library. Then, you call the
IoTHubClient_LL_CreateFromConnectionString function, which is the same API as in the IoTHubClient
sample. This call sets your device connection string (this call is also where you choose the protocol you want to
use). This sample uses MQTT as the transport, but could use AMQP or HTTPS.
Finally, call the CREATE_MODEL_INSTANCE function. WeatherStation is the namespace of the model and
ContosoAnemometer is the name of the model. Once the model instance is created, you can use it to start
sending and receiving messages. However, it's important to understand what a model is.
Define the model
A model in the serializer library defines the messages that your device can send to IoT Hub and the messages,
called actions in the modeling language, which it can receive. You define a model using a set of C macros as in
the simplesample_mqtt sample application:
BEGIN_NAMESPACE(WeatherStation);
DECLARE_MODEL(ContosoAnemometer,
WITH_DATA(ascii_char_ptr, DeviceId),
WITH_DATA(int, WindSpeed),
WITH_ACTION(TurnFanOn),
WITH_ACTION(TurnFanOff),
WITH_ACTION(SetAirResistance, int, Position)
);
END_NAMESPACE(WeatherStation);
The BEGIN_NAMESPACE and END_NAMESPACE macros both take the namespace of the model as an
argument. It's expected that anything between these macros is the definition of your model or models, and the
data structures that the models use.
In this example, there is a single model called ContosoAnemometer. This model defines two pieces of data
that your device can send to IoT Hub: DeviceId and WindSpeed. It also defines three actions (messages) that
your device can receive: TurnFanOn, TurnFanOff, and SetAirResistance. Each data element has a type, and
each action has a name (and optionally a set of parameters).
The data and actions defined in the model define an API surface that you can use to send messages to IoT Hub,
and respond to messages sent to the device. Use of this model is best understood through an example.
Send messages
The model defines the data you can send to IoT Hub. In this example, that means one of the two data items
defined using the WITH_DATA macro. There are several steps required to send DeviceId and WindSpeed
values to an IoT hub. The first is to set the data you want to send:
myWeather->DeviceId = "myFirstDevice";
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
The model you defined earlier enables you to set the values by setting members of a struct. Next, serialize the
message you want to send:
This code serializes the device-to-cloud to a buffer (referenced by destination). The code then invokes the
sendMessage function to send the message to IoT Hub:
static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t
size)
{
static unsigned int messageTrackingId;
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
}
else
{
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)
(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
}
IoTHubMessage_Destroy(messageHandle);
}
messageTrackingId++;
}
The second parameter is a pointer to user context; the same pointer passed to
IoTHubClient_LL_SendEventAsync. In this case, the context is a simple counter, but it can be anything you
want.
That's all there is to sending device-to-cloud messages. The only thing left to cover is how to receive messages.
Receive messages
Receiving a message works similarly to the way messages work in the IoTHubClient library. First, you
register a message callback function:
Then, you write the callback function that's invoked when a message is received:
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
/*buffer is not zero terminated*/
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
(void)memcpy(temp, buffer, size);
temp[size] = '\0';
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
result =
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
IOTHUBMESSAGE_REJECTED;
free(temp);
}
}
return result;
}
This code is boilerplate -- it's the same for any solution. This function receives the message and takes care of
routing it to the appropriate function through the call to EXECUTE_COMMAND. The function called at this
point depends on the definition of the actions in your model.
When you define an action in your model, you're required to implement a function that's called when your
device receives the corresponding message. For example, if your model defines this action:
Note how the name of the function matches the name of the action in the model and that the parameters of
the function match the parameters specified for the action. The first parameter is always required and contains
a pointer to the instance of your model.
When the device receives a message that matches this signature, the corresponding function is called.
Therefore, aside from having to include the boilerplate code from IoTHubMessage, receiving messages is just
a matter of defining a simple function for each action defined in your model.
Uninitialize the library
When you're done sending data and receiving messages, you can uninitialize the IoT library:
...
DESTROY_MODEL_INSTANCE(myWeather);
}
IoTHubClient_LL_Destroy(iotHubClientHandle);
}
serializer_deinit();
Each of these three functions aligns with the three initialization functions described previously. Calling these
APIs ensures that you free previously allocated resources.
Next Steps
This article covered the basics of using the libraries in the Azure IoT device SDK for C. It provided you with
enough information to understand what's included in the SDK, its architecture, and how to get started working
with the Windows samples. The next article continues the description of the SDK by explaining more about the
IoTHubClient library.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Azure IoT device SDK for C – more about
IoTHubClient
5/29/2018 • 13 min to read • Edit Online
The first article in this series introduced the Azure IoT device SDK for C. That article explained that there are
two architectural layers in SDK. At the base is the IoTHubClient library which directly manages communication
with IoT Hub. There's also the serializer library that builds on top of that to provide serialization services. In this
article we'll provide additional detail on the IoTHubClient library.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
The previous article described how to use the IoTHubClient library to send events to IoT Hub and receive
messages. This article extends that discussion by explaining how to more precisely manage when you send and
receive data, introducing you to the lower-level APIs. We'll also explain how to attach properties to events (and
retrieve them from messages) using the property handling features in the IoTHubClient library. Finally, we'll
provide additional explanation of different ways to handle messages received from IoT Hub.
The article concludes by covering a couple of miscellaneous topics, including more about device credentials and
how to change the behavior of the IoTHubClient through configuration options.
We'll use the IoTHubClient SDK samples to explain these topics. If you want to follow along, see the
iothub_client_sample_http and iothub_client_sample_amqp applications that are included in the Azure IoT
device SDK for C. Everything described in the following sections is demonstrated in these samples.
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
IOTHUB_CLIENT_HANDLE iotHubClientHandle;
iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
The article also described how to receive messages by registering a callback function.
int receiveContext = 0;
IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext);
The article also showed how to free resources using code such as the following.
IoTHubClient_Destroy(iotHubClientHandle);
EVENT_INSTANCE message;
sprintf_s(msgText, sizeof(msgText), "Message_%d_From_IoTHubClient_LL_Over_HTTP", i);
message.messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText));
The first three lines create the message, and the last line sends the event. However, as mentioned previously,
"sending" the event means that the data is simply placed in a buffer. Nothing is transmitted on the network when
we call IoTHubClient_LL_SendEventAsync. In order to actually ingress the data to IoT Hub, you must call
IoTHubClient_LL_DoWork, as in this example:
while (1)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(1000);
}
This code (from the iothub_client_sample_http application) repeatedly calls IoTHubClient_LL_DoWork. Each
time IoTHubClient_LL_DoWork is called, it sends some events from the buffer to IoT Hub and it retrieves a
queued message being sent to the device. The latter case means that if we registered a callback function for
messages, then the callback is invoked (assuming any messages are queued up). We would have registered such
a callback function with code such as the following:
The reason that IoTHubClient_LL_DoWork is often called in a loop is that each time it’s called, it sends some
buffered events to IoT Hub and retrieves the next message queued up for the device. Each call isn’t guaranteed to
send all buffered events or to retrieve all queued messages. If you want to send all events in the buffer and then
continue on with other processing you can replace this loop with code such as the following:
IOTHUB_CLIENT_STATUS status;
This code calls IoTHubClient_LL_DoWork until all events in the buffer have been sent to IoT Hub. Note this
does not also imply that all queued messages have been received. Part of the reason for this is that checking for
"all" messages isn’t as deterministic an action. What happens if you retrieve "all" of the messages, but then
another one is sent to the device immediately after? A better way to deal with that is with a programmed
timeout. For example, the message callback function could reset a timer every time it’s invoked. You can then
write logic to continue processing if, for example, no messages have been received in the last X seconds.
When you’re finished ingressing events and receiving messages, be sure to call the corresponding function to
clean up resources.
IoTHubClient_LL_Destroy(iotHubClientHandle);
Basically there’s only one set of APIs to send and receive data with a background thread and another set of APIs
that does the same thing without the background thread. A lot of developers may prefer the non-LL APIs, but the
lower-level APIs are useful when the developer wants explicit control over network transmissions. For example,
some devices collect data over time and only ingress events at specified intervals (for example, once an hour or
once a day). The lower-level APIs give you the ability to explicitly control when you send and receive data from
IoT Hub. Others will simply prefer the simplicity that the lower-level APIs provide. Everything happens on the
main thread rather than some work happening in the background.
Whichever model you choose, be sure to be consistent in which APIs you use. If you start by calling
IoTHubClient_LL_CreateFromConnectionString, be sure you only use the corresponding lower-level APIs
for any follow -up work:
IoTHubClient_LL_SendEventAsync
IoTHubClient_LL_SetMessageCallback
IoTHubClient_LL_Destroy
IoTHubClient_LL_DoWork
The opposite is true as well. If you start with IoTHubClient_CreateFromConnectionString, then use the non-
LL APIs for any additional processing.
In the Azure IoT device SDK for C, see the iothub_client_sample_http application for a complete example of
the lower-level APIs. The iothub_client_sample_amqp application can be referenced for a full example of the
non-LL APIs.
Property handling
So far when we've described sending data, we've been referring to the body of the message. For example,
consider this code:
EVENT_INSTANCE message;
sprintf_s(msgText, sizeof(msgText), "Hello World");
message.messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText));
IoTHubClient_LL_SendEventAsync(iotHubClientHandle, message.messageHandle, SendConfirmationCallback, &message)
This example sends a message to IoT Hub with the text "Hello World." However, IoT Hub also allows properties
to be attached to each message. Properties are name/value pairs that can be attached to the message. For
example, we can modify the previous code to attach a property to the message:
We start by calling IoTHubMessage_Properties and passing it the handle of our message. What we get back is
a MAP_HANDLE reference that enables us to start adding properties. The latter is accomplished by calling
Map_AddOrUpdate, which takes a reference to a MAP_HANDLE, the property name, and the property value.
With this API we can add as many properties as we like.
When the event is read from Event Hubs, the receiver can enumerate the properties and retrieve their
corresponding values. For example, in .NET this would be accomplished by accessing the Properties collection on
the EventData object.
In the previous example, we’re attaching properties to an event that we send to IoT Hub. Properties can also be
attached to messages received from IoT Hub. If we want to retrieve properties from a message, we can use code
such as the following in our message callback function:
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
. . .
. . .
}
The call to IoTHubMessage_Properties returns the MAP_HANDLE reference. We then pass that reference to
Map_GetInternals to obtain a reference to an array of the name/value pairs (as well as a count of the
properties). At that point it's a simple matter of enumerating the properties to get to the values we want.
You don't have to use properties in your application. However, if you need to set them on events or retrieve them
from messages, the IoTHubClient library makes it easy.
Message handling
As stated previously, when messages arrive from IoT Hub the IoTHubClient library responds by invoking a
registered callback function. There is a return parameter of this function that deserves some additional
explanation. Here’s an excerpt of the callback function in the iothub_client_sample_http sample application:
Note that the return type is IOTHUBMESSAGE_DISPOSITION_RESULT and in this particular case we return
IOTHUBMESSAGE_ACCEPTED. There are other values we can return from this function that change how the
IoTHubClient library reacts to the message callback. Here are the options.
IOTHUBMESSAGE_ACCEPTED – The message has been processed successfully. The IoTHubClient
library will not invoke the callback function again with the same message.
IOTHUBMESSAGE_REJECTED – The message was not processed and there is no desire to do so in the
future. The IoTHubClient library should not invoke the callback function again with the same message.
IOTHUBMESSAGE_ABANDONED – The message was not processed successfully, but the IoTHubClient
library should invoke the callback function again with the same message.
For the first two return codes, the IoTHubClient library sends a message to IoT Hub indicating that the
message should be deleted from the device queue and not delivered again. The net effect is the same (the
message is deleted from the device queue), but whether the message was accepted or rejected is still recorded.
Recording this distinction is useful to senders of the message who can listen for feedback and find out if a device
has accepted or rejected a particular message.
In the last case a message is also sent to IoT Hub, but it indicates that the message should be redelivered.
Typically you’ll abandon a message if you encounter some error but want to try to process the message again. In
contrast, rejecting a message is appropriate when you encounter an unrecoverable error (or if you simply decide
you don’t want to process the message).
In any case, be aware of the different return codes so that you can elicit the behavior you want from the
IoTHubClient library.
IOTHUB_CLIENT_HANDLE iotHubClientHandle;
iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
HostName=IOTHUBNAME.IOTHUBSUFFIX;DeviceId=DEVICEID;SharedAccessKey=SHAREDACCESSKEY
There are four pieces of information in this string: IoT Hub name, IoT Hub suffix, device ID, and shared access
key. You obtain the fully qualified domain name (FQDN ) of an IoT hub when you create your IoT hub instance in
the Azure portal — this gives you the IoT hub name (the first part of the FQDN ) and the IoT hub suffix (the rest
of the FQDN ). You get the device ID and the shared access key when you register your device with IoT Hub (as
described in the previous article).
IoTHubClient_CreateFromConnectionString gives you one way to initialize the library. If you prefer, you can
create a new IOTHUB_CLIENT_HANDLE by using these individual parameters rather than the device
connection string. This is achieved with the following code:
IOTHUB_CLIENT_CONFIG iotHubClientConfig;
iotHubClientConfig.iotHubName = "";
iotHubClientConfig.deviceId = "";
iotHubClientConfig.deviceKey = "";
iotHubClientConfig.iotHubSuffix = "";
iotHubClientConfig.protocol = HTTP_Protocol;
IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_LL_Create(&iotHubClientConfig);
Next steps
This article describes in detail the behavior of the IoTHubClient library found in the Azure IoT device SDK
for C. With this information, you should have a good understanding of the capabilities of the IoTHubClient
library. The next article provides similar detail on the serializer library.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Azure IoT device SDK for C – more about serializer
5/29/2018 • 21 min to read • Edit Online
The first article in this series introduced the Azure IoT device SDK for C. The next article provided a more
detailed description of the IoTHubClient. This article completes coverage of the SDK by providing a more
detailed description of the remaining component: the serializer library.
NOTE
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are
only available in the standard tier of IoT hub. For more information about the basic and standard IoT Hub tiers, see How to
choose the right IoT Hub tier.
The introductory article described how to use the serializer library to send events to and receive messages from
IoT Hub. In this article, we extend that discussion by providing a more complete explanation of how to model your
data with the serializer macro language. The article also includes more detail about how the library serializes
messages (and in some cases how you can control the serialization behavior). We'll also describe some parameters
you can modify that determine the size of the models you create.
Finally, the article revisits some topics covered in previous articles such as message and property handling. As
we'll find out, those features work in the same way using the serializer library as they do with the IoTHubClient
library.
Everything described in this article is based on the serializer SDK samples. If you want to follow along, see the
simplesample_amqp and simplesample_http applications included in the Azure IoT device SDK for C.
You can find the Azure IoT device SDK for C GitHub repository and view details of the API in the C API
reference.
BEGIN_NAMESPACE(WeatherStation);
DECLARE_MODEL(ContosoAnemometer,
WITH_DATA(ascii_char_ptr, DeviceId),
WITH_DATA(double, WindSpeed),
WITH_ACTION(TurnFanOn),
WITH_ACTION(TurnFanOff),
WITH_ACTION(SetAirResistance, int, Position)
);
END_NAMESPACE(WeatherStation);
As you can see, the modeling language is based on C macros. You always begin your definition with
BEGIN_NAMESPACE and always end with END_NAMESPACE. It's common to name the namespace for your
company or, as in this example, the project that you're working on.
What goes inside the namespace are model definitions. In this case, there is a single model for an anemometer.
Once again, the model can be named anything, but typically this is named for the device or type of data you want
to exchange with IoT Hub.
Models contain a definition of the events you can ingress to IoT Hub (the data) as well as the messages you can
receive from IoT Hub (the actions). As you can see from the example, events have a type and a name; actions have
a name and optional parameters (each with a type).
What’s not demonstrated in this sample are additional data types that are supported by the SDK. We'll cover that
next.
NOTE
IoT Hub refers to the data a device sends to it as events, while the modeling language refers to it as data (defined using
WITH_DATA). Likewise, IoT Hub refers to the data you send to devices as messages, while the modeling language refers to
it as actions (defined using WITH_ACTION). Be aware that these terms may be used interchangeably in this article.
TYPE DESCRIPTION
bool boolean
EDM_GUID GUID
EDM_BINARY binary
Let’s start with the last data type. The DECLARE_STRUCT allows you to define complex data types, which are
groupings of the other primitive types. These groupings allow us to define a model that looks like this:
DECLARE_STRUCT(TestType,
double, aDouble,
int, aInt,
float, aFloat,
long, aLong,
int8_t, aInt8,
uint8_t, auInt8,
int16_t, aInt16,
int32_t, aInt32,
int64_t, aInt64,
bool, aBool,
ascii_char_ptr, aAsciiCharPtr,
EDM_DATE_TIME_OFFSET, aDateTimeOffset,
EDM_GUID, aGuid,
EDM_BINARY, aBinary
);
DECLARE_MODEL(TestModel,
WITH_DATA(TestType, Test)
);
Our model contains a single data event of type TestType. TestType is a complex type that includes several
members, which collectively demonstrate the primitive types supported by the serializer modeling language.
With a model like this, we can write code to send data to IoT Hub that appears as follows:
testModel->Test.aDouble = 1.1;
testModel->Test.aInt = 2;
testModel->Test.aFloat = 3.0f;
testModel->Test.aLong = 4;
testModel->Test.aInt8 = 5;
testModel->Test.auInt8 = 6;
testModel->Test.aInt16 = 7;
testModel->Test.aInt32 = 8;
testModel->Test.aInt64 = 9;
testModel->Test.aBool = true;
testModel->Test.aAsciiCharPtr = "ascii string 1";
time_t now;
time(&now);
testModel->Test.aDateTimeOffset = GetDateTimeOffset(now);
EDM_GUID guid = { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F } };
testModel->Test.aGuid = guid;
Basically, we’re assigning a value to every member of the Test structure and then calling SendAsync to send the
Test data event to the cloud. SendAsync is a helper function that sends a single data event to IoT Hub:
void SendAsync(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const void *dataEvent)
{
unsigned char* destination;
size_t destinationSize;
if (SERIALIZE(&destination, &destinationSize, *(const unsigned char*)dataEvent) ==
{
// null terminate the string
char* destinationAsString = (char*)malloc(destinationSize + 1);
if (destinationAsString != NULL)
{
memcpy(destinationAsString, destination, destinationSize);
destinationAsString[destinationSize] = '\0';
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromString(destinationAsString);
if (messageHandle != NULL)
{
IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)0);
IoTHubMessage_Destroy(messageHandle);
}
free(destinationAsString);
}
free(destination);
}
}
This function serializes the given data event and sends it to IoT Hub using IoTHubClient_SendEventAsync. This
is the same code discussed in previous articles (SendAsync encapsulates the logic into a convenient function).
One other helper function used in the previous code is GetDateTimeOffset. This function transforms the given
time into a value of type EDM_DATE_TIME_OFFSET:
If you run this code, the following message is sent to IoT Hub:
Note that the serialization is JSON, which is the format generated by the serializer library. Also note that each
member of the serialized JSON object matches the members of the TestType that we defined in our model. The
values also exactly match those used in the code. However, note that the binary data is base64-encoded: "AQID" is
the base64 encoding of {0x01, 0x02, 0x03}.
This example demonstrates the advantage of using the serializer library -- it enables us to send JSON to the
cloud, without having to explicitly deal with serialization in our application. All we have to worry about is setting
the values of the data events in our model and then calling simple APIs to send those events to the cloud.
With this information, we can define models that include the range of supported data types, including complex
types (we could even include complex types within other complex types). However, he serialized JSON generated
by the example above brings up an important point. How we send data with the serializer library determines
exactly how the JSON is formed. That particular point is what we'll cover next.
BEGIN_NAMESPACE(Contoso);
DECLARE_STRUCT(TemperatureEvent,
int, Temperature,
EDM_DATE_TIME_OFFSET, Time);
DECLARE_STRUCT(HumidityEvent,
int, Humidity,
EDM_DATE_TIME_OFFSET, Time);
DECLARE_MODEL(Thermostat,
WITH_DATA(TemperatureEvent, Temperature),
WITH_DATA(HumidityEvent, Humidity)
);
END_NAMESPACE(Contoso);
Note that the model includes two data events: Temperature and Humidity. Unlike previous examples, the type of
each event is a structure defined using DECLARE_STRUCT. TemperatureEvent includes a temperature
measurement and a timestamp; HumidityEvent contains a humidity measurement and a timestamp. This model
gives us a natural way to model the data for the scenario described above. When we send an event to the cloud,
we'll either send a temperature/timestamp or a humidity/timestamp pair.
We can send a temperature event to the cloud using code such as the following:
time_t now;
time(&now);
thermostat->Temperature.Temperature = 75;
thermostat->Temperature.Time = GetDateTimeOffset(now);
We'll use hard-coded values for temperature and humidity in the sample code, but imagine that we’re actually
retrieving these values by sampling the corresponding sensors on the thermostat.
The code above uses the GetDateTimeOffset helper that was introduced previously. For reasons that will
become clear later, this code explicitly separates the task of serializing and sending the event. The previous code
serializes the temperature event into a buffer. Then, sendMessage is a helper function (included in
simplesample_amqp) that sends the event to IoT Hub:
static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
{
static unsigned int messageTrackingId;
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle != NULL)
{
IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)
(uintptr_t)messageTrackingId);
IoTHubMessage_Destroy(messageHandle);
}
free((void*)buffer);
}
This code is a subset of the SendAsync helper described in the previous section, so we won’t go over it again
here.
When we run the previous code to send the Temperature event, this serialized form of the event is sent to IoT Hub:
{"Temperature":75, "Time":"2015-09-17T18:45:56Z"}
We're sending a temperature which is of type TemperatureEvent and that struct contains a Temperature and
Time member. This is directly reflected in the serialized data.
Similarly, we can send a humidity event with this code:
thermostat->Humidity.Humidity = 45;
thermostat->Humidity.Time = GetDateTimeOffset(now);
if (SERIALIZE(&destination, &destinationSize, thermostat->Humidity) == IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
{"Humidity":45, "Time":"2015-09-17T18:45:56Z"}
Again, this is as expected.
With this model, you can imagine how additional events can easily be added. You define more structures using
DECLARE_STRUCT, and include the corresponding event in the model using WITH_DATA.
Now, let’s modify the model so that it includes the same data but with a different structure.
Model 2
Consider this alternative model to the one above:
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
In this case we've eliminated the DECLARE_STRUCT macros and are simply defining the data items from our
scenario using simple types from the modeling language.
Just for the moment let’s ignore the Time event. With that aside, here’s the code to ingress Temperature:
time_t now;
time(&now);
thermostat->Temperature = 75;
{"Temperature":75}
And the code for sending the Humidity event appears as follows:
thermostat->Humidity = 45;
if (SERIALIZE(&destination, &destinationSize, thermostat->Humidity) == IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
{"Humidity":45}
So far there are still no surprises. Now let's change how we use the SERIALIZE macro.
The SERIALIZE macro can take multiple data events as arguments. This enables us to serialize the Temperature
and Humidity event together and send them to IoT Hub in one call:
if (SERIALIZE(&destination, &destinationSize, thermostat->Temperature, thermostat->Humidity) == IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
You might guess that the result of this code is that two data events are sent to IoT Hub:
[
{"Temperature":75},
{"Humidity":45}
]
In other words, you might expect that this code is the same as sending Temperature and Humidity separately.
It’s just a convenience to pass both events to SERIALIZE in the same call. However, that’s not the case. Instead, the
code above sends this single data event to IoT Hub:
{"Temperature":75, "Humidity":45}
This may seem strange because our model defines Temperature and Humidity as two separate events:
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
More to the point, we didn’t model these events where Temperature and Humidity are in the same structure:
DECLARE_STRUCT(TemperatureAndHumidityEvent,
int, Temperature,
int, Humidity,
);
DECLARE_MODEL(Thermostat,
WITH_DATA(TemperatureAndHumidityEvent, TemperatureAndHumidity),
);
If we used this model, it would be easier to understand how Temperature and Humidity would be sent in the
same serialized message. However it may not be clear why it works that way when you pass both data events to
SERIALIZE using model 2.
This behavior is easier to understand if you know the assumptions that the serializer library is making. To make
sense of this let’s go back to our model:
DECLARE_MODEL(Thermostat,
WITH_DATA(int, Temperature),
WITH_DATA(int, Humidity),
WITH_DATA(EDM_DATE_TIME_OFFSET, Time)
);
Think of this model in object-oriented terms. In this case we’re modeling a physical device (a thermostat) and that
device includes attributes like Temperature and Humidity.
We can send the entire state of our model with code such as the following:
if (SERIALIZE(&destination, &destinationSize, thermostat->Temperature, thermostat->Humidity, thermostat->Time)
== IOT_AGENT_OK)
{
sendMessage(iotHubClientHandle, destination, destinationSize);
}
Assuming the values of Temperature, Humidity and Time are set, we would see an event like this sent to IoT Hub:
Sometimes you may only want to send some properties of the model to the cloud (this is especially true if your
model contains a large number of data events). It’s useful to send only a subset of data events, such as in our
earlier example:
{"Temperature":75, "Time":"2015-09-17T18:45:56Z"}
This generates exactly the same serialized event as if we had defined a TemperatureEvent with a Temperature
and Time member, just as we did with model 1. In this case we were able to generate exactly the same serialized
event by using a different model (model 2) because we called SERIALIZE in a different way.
The important point is that if you pass multiple data events to SERIALIZE, then it assumes each event is a
property in a single JSON object.
The best approach depends on you and how you think about your model. If you’re sending "events" to the cloud
and each event contains a defined set of properties, then the first approach makes a lot of sense. In that case you
would use DECLARE_STRUCT to define the structure of each event and then include them in your model with
the WITH_DATA macro. Then you send each event as we did in the first example above. In this approach you
would only pass a single data event to SERIALIZER.
If you think about your model in an object-oriented fashion, then the second approach may suit you. In this case,
the elements defined using WITH_DATA are the "properties" of your object. You pass whatever subset of events
to SERIALIZE that you like, depending on how much of your "object’s" state you want to send to the cloud.
Nether approach is right or wrong. Just be aware of how the serializer library works, and pick the modeling
approach that best fits your needs.
Message handling
So far this article has only discussed sending events to IoT Hub, and hasn't addressed receiving messages. The
reason for this is that what we need to know about receiving messages has largely been covered in an earlier
article. Recall from that article that you process messages by registering a message callback function:
You then write the callback function that’s invoked when a message is received:
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void*
userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
{
/*buffer is not zero terminated*/
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
{
memcpy(temp, buffer, size);
temp[size] = '\0';
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
result =
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
IOTHUBMESSAGE_REJECTED;
free(temp);
}
}
return result;
}
This implementation of IoTHubMessage calls the specific function for each action in your model. For example, if
your model defines this action:
The action name must exactly match an action defined in your model. The parameter names must match as well.
Also note case sensitivity. Name and Parameters are always uppercase. Make sure to match the case of your
action name and parameters in your model. In this example, the action name is "SetAirResistance" and not
"setairresistance".
The two other actions TurnFanOn and TurnFanOff can be invoked by sending these messages to a device:
This section described everything you need to know when sending events and receiving messages with the
serializer library. Before moving on, let's cover some parameters you can configure that control how large your
model is.
Macro configuration
If you’re using the Serializer library an important part of the SDK to be aware of is found in the azure-c-shared-
utility library. If you have cloned the Azure-iot-sdk-c repository from GitHub using the --recursive option, then you
will find this shared utility library here:
.\\c-utility
If you have not cloned the library, you can find it here.
Within the shared utility library, you will find the following folder:
azure-c-shared-utility\\macro\_utils\_h\_generator.
The program in this solution generates the macro_utils.h file. There’s a default macro_utils.h file included with the
SDK. This solution allows you to modify some parameters and then recreate the header file based on these
parameters.
The two key parameters to be concerned with are nArithmetic and nMacroParameters which are defined in
these two lines found in macro_utils.tt:
<#int nArithmetic=1024;#>
<#int nMacroParameters=124;/*127 parameters in one macro definition in C99 in chapter 5.2.4.1 Translation
limits*/#>
These values are the default parameters included with the SDK. Each parameter has the following meaning:
nMacroParameters – Controls how many parameters you can have in one DECL ARE_MODEL macro
definition.
nArithmetic – Controls the total number of members allowed in a model.
The reason these parameters are important is because they control how large your model can be. For example,
consider this model definition:
DECLARE_MODEL(MyModel,
WITH_DATA(int, MyData)
);
As mentioned previously, DECLARE_MODEL is just a C macro. The names of the model and the WITH_DATA
statement (yet another macro) are parameters of DECLARE_MODEL. nMacroParameters defines how many
parameters can be included in DECLARE_MODEL. Effectively, this defines how many data event and action
declarations you can have. As such, with the default limit of 124 this means that you can define a model with a
combination of about 60 actions and data events. If you try to exceed this limit, you'll receive compiler errors that
look similar to this:
The nArithmetic parameter is more about the internal workings of the macro language than your application. It
controls the total number of members you can have in your model, including DECLARE_STRUCT macros. If you
start seeing compiler errors such as this, then you should try increasing nArithmetic:
If you want to change these parameters, modify the values in the macro_utils.tt file, recompile the
macro_utils_h_generator.sln solution, and run the compiled program. When you do so, a new macro_utils.h file is
generated and placed in the .\common\inc directory.
In order to use the new version of macro_utils.h, remove the serializer NuGet package from your solution and in
its place include the serializer Visual Studio project. This enables your code to compile against the source code of
the serializer library. This includes the updated macro_utils.h. If you want to do this for simplesample_amqp,
start by removing the NuGet package for the serializer library from the solution:
Then add this project to your Visual Studio solution:
.\c\serializer\build\windows\serializer.vcxproj
Now when you compile your solution, the updated macro_utils.h is included in your binary.
Note that increasing these values high enough can exceed compiler limits. To this point, the nMacroParameters is
the main parameter with which to be concerned. The C99 spec specifies that a minimum of 127 parameters are
allowed in a macro definition. The Microsoft compiler follows the spec exactly (and has a limit of 127), so you
won't be able to increase nMacroParameters beyond the default. Other compilers might allow you to do so (for
example, the GNU compiler supports a higher limit).
So far we've covered just about everything you need to know about how to write code with the serializer library.
Before concluding, let's revisit some topics from previous articles that you may be wondering about.
Additional topics
A few other topics worth mentioning again are property handling, using alternate device credentials, and
configuration options. These are all topics covered in a previous article. The main point is that all of these features
work in the same way with the serializer library as they do with the IoTHubClient library. For example, if you
want to attach properties to an event from your model, you use IoTHubMessage_Properties and
Map_AddorUpdate, the same way as described previously:
Whether the event was generated from the serializer library or created manually using the IoTHubClient library
does not matter.
For the alternate device credentials, using IoTHubClient_LL_Create works just as well as
IoTHubClient_CreateFromConnectionString for allocating an IOTHUB_CLIENT_HANDLE.
Finally, if you're using the serializer library, you can set configuration options with IoTHubClient_LL_SetOption
just as you did when using the IoTHubClient library.
A feature that is unique to the serializer library are the initialization APIs. Before you can start working with the
library, you must call serializer_init:
serializer_init(NULL);
This is done just before you call IoTHubClient_CreateFromConnectionString.
Similarly, when you're done working with the library, the last call you’ll make is to serializer_deinit:
serializer_deinit();
Otherwise, all of the other features listed above work the same in the serializer library as they do in the
IoTHubClient library. For more information about any of these topics, see the previous article in this series.
Next steps
This article describes in detail the unique aspects of the serializer library contained in the Azure IoT device SDK
for C. With the information provided you should have a good understanding of how to use models to send events
and receive messages from IoT Hub.
This also concludes the three-part series on how to develop applications with the Azure IoT device SDK for C.
This should be enough information to not only get you started but give you a thorough understanding of how the
APIs work. For additional information, there are a few samples in the SDK not covered here. Otherwise, the SDK
documentation is a good resource for additional information.
To learn more about developing for IoT Hub, see the Azure IoT SDKs.
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Develop for constrained devices using Azure IoT
SDKs
5/25/2018 • 3 min to read • Edit Online
Next steps
To learn more about Azure IoT C SDK architecture:
Azure IoT C SDK source code
Azure IoT device SDK for C introduction
Develop for mobile devices using Azure IoT SDKs
5/29/2018 • 2 min to read • Edit Online
Things in the Internet of Things may refer to a wide range of devices with varying capability: sensors,
microcontrollers, smart devices, industrial gateways, and even mobile devices. A mobile device can be an IoT
device, where it is sending device-to-cloud telemetry and managed by the cloud. It can also be the device running a
back-end service application, which manages other IoT devices. In both cases, Azure IoT Hub SDKs can be used to
develop applications that work for mobile devices.
Next steps
IoT Hub REST API reference
Azure IoT C SDK source code
Query Avro data using Azure Data Lake Analytics
6/1/2018 • 4 min to read • Edit Online
This article is about how to query Avro data for efficiently routing messages from Azure IoT Hub to Azure services.
Following the blog post announcement—Azure IoT Hub message routing: now with routing on message body, IoT
Hub supports routing on either properties or the message body. See also Routing on message bodies.
The challenge has been that when Azure IoT Hub routes messages to blob storage, IoT Hub writes the content in
Avro format, which has both message body and message properties. Note that IoT Hub only supports writing data
to blob storage in the Avro data format, and this format is not used for any other endpoints. See When using Azure
Storage containers. While the Avro format is great for data/message preservation, it's challenging for querying the
data. In comparison, JSON or CSV format is much easier for querying data.
To solve this, you can use many of the big data patterns for both transforming and scaling data to address non-
relational big data needs and formats. One of the patterns, a “pay per query” pattern, is Azure Data Lake Analytics
(ADL A). It is the focus of this article. Though you could easily execute the query in Hadoop or other solutions,
ADL A is often better suited for this “pay per query” approach. There is an “extractor” for Avro in U -SQL. See U -
SQL Avro Example.
3. Set up an Azure Data Lake Store (ADLS ) and an Azure Data Lake Analytics instance. While Azure IoT Hub
does not route to an Azure Data Lake Store, ADL A requires one.
4. In ADL A, configure the Azure Blob Storage as an additional store, the same Blob Storage that Azure IoT
Hub routes data to.
5. As discussed in U -SQL Avro Example, there are 4 DLLs that are needed. Upload these files to a location in
your ADLS.
@rs =
EXTRACT
EnqueuedTimeUtc string,
Body byte[]
FROM @input_file
@cnt =
SELECT EnqueuedTimeUtc AS time, Encoding.UTF8.GetString(Body) AS jsonmessage
FROM @rs;
Running the script shown below, ADL A took 5 minutes when limited to 10 Analytic Units and processed
177 files, summarizing the output to a CSV file.
Viewing the output, you can see the Avro content has converted to a CSV file. Continue to step 8 if you want
to parse the JSON.
8. Most IoT messages are in JSON format. Adding the following lines, you can parse the message into JSON,
so you can add the WHERE clauses and only output the needed data.
@jsonify = SELECT
Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(Encoding.UTF8.GetString(Body)) AS
message FROM @rs;
/*
@cnt =
SELECT EnqueuedTimeUtc AS time, Encoding.UTF8.GetString(Body) AS jsonmessage
FROM @rs;
@cnt =
SELECT message["message"] AS iotmessage,
message["event"] AS msgevent,
message["object"] AS msgobject,
message["status"] AS msgstatus,
message["host"] AS msghost
FROM @jsonify;
9. Viewing the output, you now see columns for each item in the select command.
Next steps
In this tutorial, you learned how to query Avro data for efficiently routing messages from Azure IoT Hub to Azure
services.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
To learn more about message routing in IoT Hub, see Send and receive messages with IoT Hub.
Send messages from the cloud to your device with
IoT Hub (.NET)
5/29/2018 • 6 min to read • Edit Online
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to
create an IoT hub, provision a device identity in it, and code a device app that sends device-to-cloud messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device
from IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two .NET console apps:
SimulatedDevice, a modified version of the app created in Get started with IoT Hub, which connects to
your IoT hub and receives cloud-to-device messages.
SendCloudToDevice, which sends a cloud-to-device message to the device app through IoT Hub, and
then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure
IoT Hub, see the IoT Hub developer guide.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received message: {0}",
Encoding.ASCII.GetString(receivedMessage.GetBytes()));
Console.ResetColor();
await deviceClient.CompleteAsync(receivedMessage);
}
}
The ReceiveAsync method asynchronously returns the received message at the time that it is received
by the device. It returns null after a specifiable timeout period (in this case, the default of one minute is
used). When the app receives a null, it should continue to wait for new messages. This requirement is
the reason for the if (receivedMessage == null) continue line.
The call to CompleteAsync() notifies IoT Hub that the message has been successfully processed. The
message can be safely removed from the device queue. If something happened that prevented the
device app from completing the processing of the message, IoT Hub delivers it again. It is then
important that message processing logic in the device app is idempotent, so that receiving the same
message multiple times produces the same result. An application can also temporarily abandon a
message, which results in IoT hub retaining the message in the queue for future consumption. Or, the
application can reject a message, which permanently removes the message from the queue. For more
information about the cloud-to-device message lifecycle, see the IoT Hub developer guide.
NOTE
When using HTTPS instead of MQTT or AMQP as a transport, the ReceiveAsync method returns immediately.
The supported pattern for cloud-to-device messages with HTTPS is intermittently connected devices that check
for messages infrequently (less than every 25 minutes). Issuing more HTTPS receives results in IoT Hub throttling
the requests. For more information about the differences between MQTT, AMQP and HTTPS support, and IoT
Hub throttling, see the IoT Hub developer guide.
2. Add the following method in the Main method, right before the Console.ReadLine() line:
ReceiveC2dAsync();
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Substitute the placeholder value with the IoT hub
connection string from Get started with IoT Hub:
This method sends a new cloud-to-device message to the device with the ID, myFirstDevice . Change
this parameter only if you modified it from the one used in Get started with IoT Hub.
7. Finally, add the following lines to the Main method:
Console.WriteLine("Send Cloud-to-Device message\n");
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
8. From within Visual Studio, right-click your solution, and select Set StartUp projects.... Select Multiple
startup projects, then select the Start action for ReadDeviceToCloudMessages, SimulatedDevice, and
SendCloudToDevice.
9. Press F5. All three applications should start. Select the SendCloudToDevice windows, and press
Enter. You should see the message being received by the device app.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received feedback: {0}", string.Join(", ",
feedbackBatch.Records.Select(f => f.StatusCode)));
Console.ResetColor();
await feedbackReceiver.CompleteAsync(feedbackBatch);
}
}
Note this receive pattern is the same one used to receive cloud-to-device messages from the device app.
2. Add the following method in the Main method, right after the
serviceClient = ServiceClient.CreateFromConnectionString(connectionString) line:
ReceiveFeedbackAsync();
3. To request feedback for the delivery of your cloud-to-device message, you have to specify a property in
the SendCloudToDeviceMessageAsync method. Add the following line, right after the
var commandMessage = new Message(...); line:
commandMessage.Ack = DeliveryAcknowledgement.Full;
4. Run the apps by pressing F5. You should see all three applications start. Select the
SendCloudToDevice windows, and press Enter. You should see the message being received by the
device app, and after a few seconds, the feedback message being received by your
SendCloudToDevice application.
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring
solution accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Send cloud-to-device messages with IoT Hub (Java)
6/6/2018 • 5 min to read • Edit Online
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create
an IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud
messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device
from IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Java console apps:
simulated-device, a modified version of the app created in Get started with IoT Hub, which connects to
your IoT hub and receives cloud-to-device messages.
send-c2d-messages, which sends a cloud-to-device message to the simulated device app through IoT Hub,
and then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure
IoT Hub, see the Azure IoT Developer Center.
return IotHubMessageResult.COMPLETE;
}
}
3. Modify the main method to create an AppMessageCallback instance and call the
setMessageCallback method before it opens the client as follows:
NOTE
If you use HTTPS instead of MQTT or AMQP as the transport, the DeviceClient instance checks for messages from
IoT Hub infrequently (less than every 25 minutes). For more information about the differences between MQTT,
AMQP and HTTPS support, and IoT Hub throttling, see the IoT Hub developer guide.
4. To build the simulated-device app using Maven, execute the following command at the command
prompt in the simulated-device folder:
NOTE
You can check for the latest version of iot-service-client using Maven search.
import com.microsoft.azure.sdk.iot.service.*;
import java.io.IOException;
import java.net.URISyntaxException;
7. Add the following class-level variables to the App class, replacing {yourhubconnectionstring} and
{yourdeviceid} with the values your noted earlier:
8. Replace the main method with the following code. This code connects to your IoT hub, sends a message
to your device, and then waits for an acknowledgment that the device received and processed the
message:
if (serviceClient != null) {
serviceClient.open();
FeedbackReceiver feedbackReceiver = serviceClient
.getFeedbackReceiver();
if (feedbackReceiver != null) feedbackReceiver.open();
serviceClient.send(deviceId, messageToSend);
System.out.println("Message sent to device");
9. To build the simulated-device app using Maven, execute the following command at the command
prompt in the simulated-device folder:
2. At a command prompt in the send-c2d-messages folder, run the following command to send a cloud-to-
device message and wait for a feedback acknowledgment:
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create
an IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud
messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device
from IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Node.js console apps:
SimulatedDevice, a modified version of the app created in Get started with IoT Hub, which connects to
your IoT hub and receives cloud-to-device messages.
SendCloudToDeviceMessage, which sends a cloud-to-device message to the simulated device app
through IoT Hub, and then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure
IoT Hub, see the Azure IoT Developer Center.
NOTE
If you use HTTPS instead of MQTT or AMQP as the transport, the DeviceClient instance checks for messages
from IoT Hub infrequently (less than every 25 minutes). For more information about the differences between
MQTT, AMQP and HTTPS support, and IoT Hub throttling, see the IoT Hub developer guide.
npm init
2. At your command prompt in the sendcloudtodevicemessage folder, run the following command to
install the azure-iothub package:
5. Add the following code to SendCloudToDeviceMessage.js file. Replace the "{iot hub connection
string}" placeholder value with the IoT Hub connection string for the hub you created in the Get started
with IoT Hub tutorial. Replace the "{device id}" placeholder with the device ID of the device you added in
the Get started with IoT Hub tutorial:
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.constructor.name);
};
}
7. Add the following function to print delivery feedback messages to the console:
8. Add the following code to send a message to your device and handle the feedback message when the
device acknowledges the cloud-to-device message:
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
serviceClient.getFeedbackReceiver(receiveFeedback);
var message = new Message('Cloud to device message.');
message.ack = 'full';
message.messageId = "My Message ID";
console.log('Sending message: ' + message.getData());
serviceClient.send(targetDevice, message, printResultFor('send'));
}
});
node SimulatedDevice.js
2. At a command prompt in the sendcloudtodevicemessage folder, run the following command to send a
cloud-to-device message and wait for the acknowledgment feedback:
node SendCloudToDeviceMessage.js
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement
retry policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Send cloud-to-device messages with IoT Hub
(Python)
5/29/2018 • 6 min to read • Edit Online
Introduction
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Get started with IoT Hub tutorial shows how to create
an IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud
messages.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
This tutorial builds on Get started with IoT Hub. It shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device
from IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this tutorial, you run two Python console apps:
SimulatedDevice.py, a modified version of the app created in Get started with IoT Hub, which connects to
your IoT hub and receives cloud-to-device messages.
SendCloudToDeviceMessage.py, which sends a cloud-to-device message to the simulated device app
through IoT Hub, and then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the Azure IoT Developer Center.
import time
import sys
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError
RECEIVE_CONTEXT = 0
WAIT_COUNT = 10
RECEIVED_COUNT = 0
RECEIVE_CALLBACKS = 0
3. Add the following code to SimulatedDevice.py file. Replace the "{deviceConnectionString}" placeholder
value with the device connection string for the device you created in the Get started with IoT Hub tutorial:
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
client.set_message_callback(receive_message_callback, RECEIVE_CONTEXT)
return client
def print_last_message_time(client):
try:
last_message = client.get_last_message_receive_time()
print ( "Last Message: %s" % time.asctime(time.localtime(last_message)) )
print ( "Actual time : %s" % time.asctime() )
except IoTHubClientError as iothub_client_error:
if iothub_client_error.args[0].result == IoTHubClientResult.INDEFINITE_TIME:
print ( "No message received" )
else:
print ( iothub_client_error )
5. Add the following code to initialize the client and wait to recieve the cloud-to-device message:
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
client.set_message_callback(receive_message_callback, RECEIVE_CONTEXT)
return client
def iothub_client_sample_run():
try:
client = iothub_client_init()
while True:
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
status = client.get_send_status()
print ( "Send status: %s" % status )
time.sleep(10)
status_counter += 1
print_last_message_time(client)
iothub_client_sample_run()
import random
import sys
import iothub_service_client
from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
OPEN_CONTEXT = 0
FEEDBACK_CONTEXT = 1
MESSAGE_COUNT = 1
AVG_WIND_SPEED = 10.0
MSG_TXT = "{\"service client sent a message\": %.2f}"
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
def open_complete_callback(context):
print ( 'open_complete_callback called with context: {0}'.format(context) )
5. Add the following code to send a message to your device and handle the feedback message when the
device acknowledges the cloud-to-device message:
def iothub_messaging_sample_run():
try:
iothub_messaging = IoTHubMessaging(CONNECTION_STRING)
iothub_messaging.open(open_complete_callback, OPEN_CONTEXT)
try:
# Try Python 2.xx first
raw_input("Press Enter to continue...\n")
except:
pass
# Use Python 3.xx in the case of exception
input("Press Enter to continue...\n")
iothub_messaging.close()
if __name__ == '__main__':
print ( "Starting the IoT Hub Service Client Messaging Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_messaging_sample_run()
2. At the command prompt, run the following command to listen for cloud-to-device messages:
python SimulatedDevice.py
3. Open a new command prompt and install the Azure IoT Hub Service SDK for Python.
4. At a command prompt, run the following command to send a cloud-to-device message and wait for the
message feedback:
python SendCloudToDeviceMessage.py
Next steps
In this tutorial, you learned how to send and receive cloud-to-device messages.
To see examples of complete end-to-end solutions that use IoT Hub, see Azure IoT Remote Monitoring solution
accelerator.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.
Send cloud-to-device messages with IoT Hub (iOS)
5/29/2018 • 5 min to read • Edit Online
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications
between millions of devices and a solution back end. The Send telemetry from a device to an IoT hub article
shows how to create an IoT hub, provision a device identity in it, and code a simulated device app that sends
device-to-cloud messages.
This article shows you how to:
From your solution back end, send cloud-to-device messages to a single device through IoT Hub.
Receive cloud-to-device messages on a device.
From your solution back end, request delivery acknowledgement (feedback) for messages sent to a device
from IoT Hub.
You can find more information on cloud-to-device messages in the IoT Hub developer guide.
At the end of this article, you run two Swift iOS projects:
sample-device, the same app created in Send telemetry from a device to an IoT hub, which connects to your
IoT hub and receives cloud-to-device messages.
sample-service, which sends a cloud-to-device message to the simulated device app through IoT Hub, and
then receives its delivery acknowledgement.
NOTE
IoT Hub has SDK support for many device platforms and languages (including C, Java, and Javascript) through Azure IoT
device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT
Hub, see the Azure IoT Developer Center.
cd quickstart/sample-device
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in
the podfile file:
pod install
Along with installing the pods required for your project, the installation command also created an XCode
workspace file that is already configured to use the pods for dependencies.
Run the sample device application
1. Retrieve the connection string for your device. You can copy this string from the Azure portal in the device
details blade, or retrieve it with the following CLI command:
3. Expand the MQTT Client Sample project and then folder of the same name.
4. Open ViewController.swift for editing in XCode.
5. Search for the connectionString variable and update the value with the device connection string that you
copied in the first step.
6. Save your changes.
7. Run the project in the device emulator with the Build and run button or the key combo command + r.
cd quickstart/sample-service
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in
the podfile file:
pod install
Along with installing the pods required for your project, the installation command also created an XCode
workspace file that is already configured to use the pods for dependencies.
Run the sample service application
1. Retrieve the service connection string for your IoT hub. You can copy this string from the Azure portal
from the iothubowner policy in the Shared access policies blade, or retrieve it with the following CLI
command:
open AzureIoTServiceSample.xcworkspace
3. Expand the AzureIoTServiceSample project and then expand the folder of the same name.
4. Open ViewController.swift for editing in XCode.
5. Search for the connectionString variable and update the value with the service connection string that you
copied previously.
6. Save your changes.
7. In Xcode, change the emulator settings to a different iOS device than you used to run the IoT device.
XCode cannot run multiple emulators of the same type.
8. Run the project in the device emulator with the Build and run button or the key combo Command + r.
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device
twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have two Node.js console apps:
AddTagsAndQuery.js, a Node.js back-end app, which adds tags and queries device twins.
TwinSimulatedDevice.js, a Node.js app, which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use a Node.js tool called iothub-explorer to create a device identity for this tutorial. Device IDs
are case sensitive.
1. Run the following in your command-line environment:
npm install -g iothub-explorer@latest
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the addtagsandqueryapp folder, run the following command to install the
azure-iothub package:
npm install azure-iothub --save
3. Using a text editor, create a new AddTagsAndQuery.js file in the addtagsandqueryapp folder.
4. Add the following code to the AddTagsAndQuery.js file, and substitute the {iot hub connection string}
placeholder with the IoT Hub connection string you copied when you created your hub:
'use strict';
var iothub = require('azure-iothub');
var connectionString = '{iot hub connection string}';
var registry = iothub.Registry.fromConnectionString(connectionString);
twin.update(patch, function(err) {
if (err) {
console.error('Could not update twin: ' + err.constructor.name + ': ' + err.message);
} else {
console.log(twin.deviceId + ' twin updated successfully');
queryTwins();
}
});
}
});
The Registry object exposes all the methods required to interact with device twins from the service. The
previous code first initializes the Registry object, then retrieves the device twin for myDeviceId, and finally
updates its tags with the desired location information.
After updating the tags it calls the queryTwins function.
5. Add the following code at the end of AddTagsAndQuery.js to implement the queryTwins function:
var queryTwins = function() {
var query = registry.createQuery("SELECT * FROM devices WHERE tags.location.plant = 'Redmond43'",
100);
query.nextAsTwin(function(err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
console.log("Devices in Redmond43: " + results.map(function(twin) {return
twin.deviceId}).join(','));
}
});
The previous code executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
The previous code, when it creates the query object, specifies a maximum number of returned documents.
The query object contains a hasMoreResults boolean property that you can use to invoke the nextAsTwin
methods multiple times to retrieve all results. A method called next is available for results that are not
device twins, for example, results of aggregation queries.
6. Run the application with:
node AddTagsAndQuery.js
You should see one device in the results for the query asking for all devices located in Redmond43 and
none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of the
query in the previous section.
npm init
2. At your command prompt in the reportconnectivity folder, run the following command to install the
azure-iot-device, and azure-iot-device-mqtt package:
3. Using a text editor, create a new ReportConnectivity.js file in the reportconnectivity folder.
4. Add the following code to the ReportConnectivity.js file, and substitute the {device connection string}
placeholder with the device connection string you copied when you created the myDeviceId device identity:
'use strict';
var Client = require('azure-iot-device').Client;
var Protocol = require('azure-iot-device-mqtt').Mqtt;
client.open(function(err) {
if (err) {
console.error('could not open IotHub client');
} else {
console.log('client opened');
client.getTwin(function(err, twin) {
if (err) {
console.error('could not get twin');
} else {
var patch = {
connectivity: {
type: 'cellular'
}
};
twin.properties.reported.update(patch, function(err) {
if (err) {
console.error('could not update twin');
} else {
console.log('twin state reported');
process.exit();
}
});
}
});
}
});
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for myDeviceId and updates its
reported property with the connectivity information.
5. Run the device app
node ReportConnectivity.js
node AddTagsAndQuery.js
This time myDeviceId should appear in both query results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's
identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device app to
report device connectivity information in the device twin. You also learned how to query this information using the
SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app), with the Use direct methods
tutorial.
Get started with device twins (.NET/Node)
5/29/2018 • 9 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device
twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have a .NET and a Node.js console app:
AddTagsAndQuery.sln, a .NET back-end app, which adds tags and queries device twins.
TwinSimulatedDevice.js, a Node.js app which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use a Node.js tool called iothub-explorer to create a device identity for this tutorial. Device IDs
are case sensitive.
1. Run the following in your command-line environment:
npm install -g iothub-explorer@latest
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the IoT
Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device app
to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the previous section.
The RegistryManager class exposes all the methods required to interact with device twins from the service.
The previous code first initializes the registryManager object, then retrieves the device twin for
myDeviceId, and finally updates its tags with the desired location information.
After updating, it executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
Note that the previous code, when it creates the query object, specifies a maximum number of returned
documents. The query object contains a HasMoreResults boolean property that you can use to invoke the
GetNextAsTwinAsync methods multiple times to retrieve all results. A method called GetNextAsJson is
available for results that are not device twins, for example, results of aggregation queries.
7. Finally, add the following lines to the Main method:
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddTagsAndQuery().Wait();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
8. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
AddTagsAndQuery project is Start. Build the solution.
9. Run this application by right-clicking on the AddTagsAndQuery project and selecting Debug, followed by
Start new instance. You should see one device in the results for the query asking for all devices located in
Redmond43 and none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of the
query in the previous section.
npm init
2. At your command prompt in the reportconnectivity folder, run the following command to install the
azure-iot-device, and azure-iot-device-mqtt package:
3. Using a text editor, create a new ReportConnectivity.js file in the reportconnectivity folder.
4. Add the following code to the ReportConnectivity.js file, and substitute the placeholder for device
connection string with the one you copied when you created the myDeviceId device identity:
'use strict';
var Client = require('azure-iot-device').Client;
var Protocol = require('azure-iot-device-mqtt').Mqtt;
client.open(function(err) {
if (err) {
console.error('could not open IotHub client');
} else {
console.log('client opened');
client.getTwin(function(err, twin) {
if (err) {
console.error('could not get twin');
} else {
var patch = {
connectivity: {
type: 'cellular'
}
};
twin.properties.reported.update(patch, function(err) {
if (err) {
console.error('could not update twin');
} else {
console.log('twin state reported');
process.exit();
}
});
}
});
}
});
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for myDeviceId and updates its
reported property with the connectivity information.
5. Run the device app
node ReportConnectivity.js
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's
identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device app to
report device connectivity information in the device twin. You also learned how to query this information using the
SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Get started with device twins (.NET/.NET)
5/29/2018 • 10 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device
twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have these .NET console apps:
CreateDeviceIdentity, a .NET app which creates a device identity and associated security key to connect your
simulated device app.
AddTagsAndQuery, a .NET back-end app which adds tags and queries device twins.
ReportConnectivity, a .NET device app which simulates a device that connects to your IoT hub with the device
identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use the Azure portal to create a device identity in the identity registry in your IoT hub. A device
cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity
registry" section of the IoT Hub developer guide. Use the IoT Devices panel in the portal to generate a unique
device ID and key for your device to use to identify itself to IoT Hub. Device IDs are case-sensitive.
1. Sign in to the Azure portal.
2. Select All resources and find your IoT hub resource.
3. When your IoT hub resource is opened, click the IoT Devices tool, and then click Add at the top.
4. Provide a name for your new device, such as myDeviceId, and click Save. This action creates a new device
identity for your IoT hub.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the previous section.
The RegistryManager class exposes all the methods required to interact with device twins from the service.
The previous code first initializes the registryManager object, then retrieves the device twin for
myDeviceId, and finally updates its tags with the desired location information.
After updating, it executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
Note that the previous code, when it creates the query object, specifies a maximum number of returned
documents. The query object contains a HasMoreResults boolean property that you can use to invoke the
GetNextAsTwinAsync methods multiple times to retrieve all results. A method called GetNextAsJson is
available for results that are not device twins, for example, results of aggregation queries.
7. Finally, add the following lines to the Main method:
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddTagsAndQuery().Wait();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
8. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
AddTagsAndQuery project is Start. Build the solution.
9. Run this application by right-clicking on the AddTagsAndQuery project and selecting Debug, followed by
Start new instance. You should see one device in the results for the query asking for all devices located in
Redmond43 and none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of the
query in the previous section.
2. In Solution Explorer, right-click the ReportConnectivity project, and then click Manage NuGet
Packages....
3. In the NuGet Package Manager window, select Browse and search for microsoft.azure.devices.client.
Select Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use. This
procedure downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the previous section.
The Client object exposes all the methods you require to interact with device twins from the device. The
code shown above, initializes the Client object, and then retrieves the device twin for myDeviceId.
7. Add the following method to the Program class:
public static async void ReportConnectivity()
{
try
{
Console.WriteLine("Sending connectivity data as reported property");
The code above updates myDeviceId's reported property with the connectivity information.
8. Finally, add the following lines to the Main method:
try
{
InitClient();
ReportConnectivity();
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
9. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
ReportConnectivity project is Start. Build the solution.
10. Run this application by right-clicking on the ReportConnectivity project and selecting Debug, followed by
Start new instance. You should see it getting the twin information, and then sending connectivity as a
reported property.
11. Now that the device reported its connectivity information, it should appear in both queries. Run the .NET
AddTagsAndQuery app to run the queries again. This time myDeviceId should appear in both query
results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's
identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device app to
report device connectivity information in the device twin. You also learned how to query this information using the
SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Get started with device twins (Java)
5/29/2018 • 11 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device
twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
In this tutorial, you create two Java console apps:
add-tags-query, a Java back-end app that adds tags and queries device twins.
simulated-device, a Java device app that that connects to your IoT hub and reports its connectivity condition
using a reported property.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use the Azure portal to create a device identity in the identity registry in your IoT hub. A device
cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity
registry" section of the IoT Hub developer guide. Use the IoT Devices panel in the portal to generate a unique
device ID and key for your device to use to identify itself to IoT Hub. Device IDs are case-sensitive.
1. Sign in to the Azure portal.
2. Select All resources and find your IoT hub resource.
3. When your IoT hub resource is opened, click the IoT Devices tool, and then click Add at the top.
4. Provide a name for your new device, such as myDeviceId, and click Save. This action creates a new device
identity for your IoT hub.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your IoT
hub connection string you noted in the Create an IoT Hub section:
10. Update the main method signature to include the following throws clause:
public static void main( String[] args ) throws IOException
11. Add the following code to the main method to create the DeviceTwin and DeviceTwinDevice objects.
The DeviceTwin object handles the communication with your IoT hub. The DeviceTwinDevice object
represents the device twin with its properties and tags:
try {
// Code goes here
} catch (IotHubException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
13. To update the region and plant device twin tags in your device twin, add the following code in the try
block:
// Retrieve the device twin with the tag values from IoT Hub
System.out.println("Device twin after update:");
twinClient.getTwin(device);
System.out.println(device);
14. To query the device twins in IoT hub, add the following code to the try block after the code you added in
the previous step. The code runs two queries. Each query returns a maximum of 100 devices:
// Query the device twins in IoT Hub
System.out.println("Devices in Redmond:");
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.32</version>
</dependency>
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use Java
1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
8. Add the following class-level variables to the App class. Replacing {youriothubname} with your IoT hub
name, and {yourdevicekey} with the device key value you generated in the Create a device identity section:
This sample app uses the protocol variable when it instantiates a DeviceClient object.
9. Add the following code to the main method to:
Create a device client to communicate with IoT Hub.
Create a Device object to store the device twin properties.
10. Add the following code to the main method to create a connectivityType reported property and send it to
IoT Hub:
try {
// Open the DeviceClient and start the device twin services.
client.open();
client.startDeviceTwin(new DeviceTwinStatusCallBack(), null, dataCollector, null);
11. Add the following code to the end of the main method. Waiting for the Enter key allows time for IoT Hub to
report the status of the device twin operations:
dataCollector.clean();
client.close();
3. At a command prompt in the add-tags-query folder, run the following command to run the add-tags-query
service app a second time:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
Now your device has sent the connectivityType property to IoT Hub, the second query returns your
device.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's
identity registry. You added device metadata as tags from a back-end app, and wrote a device app to report device
connectivity information in the device twin. You also learned how to query the device twin information using the
SQL -like IoT Hub query language.
Use the following resources to learn how to:
Send telemetry from devices with the Get started with IoT Hub tutorial.
Control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Get started with device twins (Python)
5/29/2018 • 10 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand device
twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have two Python console apps:
AddTagsAndQuery.py, a Python back-end app, which adds tags and queries device twins.
ReportConnectivity.py, a Python app, which simulates a device that connects to your IoT hub with the device
identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
NOTE
The pip packages for azure-iothub-service-client and azure-iothub-device-client are currently available only for
Windows OS. For Linux/Mac OS, please refer to the Linux and Mac OS-specific sections on the Prepare your development
environment for Python post.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
import sys
import iothub_service_client
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
from iothub_service_client import IoTHubDeviceTwin, IoTHubError
4. Add the following code, replacing the placeholder for [IoTHub Connection String] and [Device Id] with the
connection string for the IoT hub and the device id you created in the previous sections.
UPDATE_JSON = "{\"properties\":{\"desired\":{\"location\":\"Redmond\"}}}"
UPDATE_JSON_SEARCH = "\"location\":\"Redmond\""
UPDATE_JSON_CLIENT_SEARCH = "\"connectivity\":\"cellular\""
iothub_registry_statistics = iothub_registry_manager.get_statistics()
print ( "Total device count :
{0}".format(iothub_registry_statistics.totalDeviceCount) )
print ( "Enabled device count :
{0}".format(iothub_registry_statistics.enabledDeviceCount) )
print ( "Disabled device count :
{0}".format(iothub_registry_statistics.disabledDeviceCount) )
print ( "" )
number_of_devices = iothub_registry_statistics.totalDeviceCount
dev_list = iothub_registry_manager.get_device_list(number_of_devices)
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
print ( "" )
The Registry object exposes all the methods required to interact with device twins from the service. The
code first initializes the Registry object, then updates the device twin for deviceId, and finally runs two
queries. The first selects only the device twins of devices located in the Redmond43 plant, and the second
refines the query to select only the devices that are also connected through cellular network.
6. Add the following code at the end of AddTagsAndQuery.py to implement the
iothub_service_sample_run function:
if __name__ == '__main__':
print ( "Starting the IoT Hub Device Twins Python service sample..." )
iothub_service_sample_run()
python AddTagsAndQuery.py
You should see one device in the results for the query asking for all devices located in Redmond43 and
none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of the
query in the previous section.
import time
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult,
IoTHubError
4. Add the following code, replacing the placeholder for [IoTHub Device Connection String] with the
connection string for the IoT hub device you created in the previous sections.
TIMER_COUNT = 5
TWIN_CONTEXT = 0
SEND_REPORTED_STATE_CONTEXT = 0
5. Add the following code to the ReportConnectivity.py file to implement the device twins functionality:
def device_twin_callback(update_state, payload, user_context):
print ( "" )
print ( "Twin callback called with:" )
print ( " updateStatus: %s" % update_state )
print ( " payload: %s" % payload )
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
return client
def iothub_client_sample_run():
try:
client = iothub_client_init()
if client.protocol == IoTHubTransportProvider.MQTT:
print ( "Sending data as reported property..." )
reported_state = "{\"connectivity\":\"cellular\"}"
client.send_reported_state(reported_state, len(reported_state),
send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
while True:
print ( "Press Ctrl-C to exit" )
status_counter = 0
while status_counter <= TIMER_COUNT:
status = client.get_send_status()
time.sleep(10)
status_counter += 1
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for your device and updates its
reported property with the connectivity information.
6. Add the following code at the end of ReportConnectivity.py to implement the iothub_client_sample_run
function:
if __name__ == '__main__':
print ( "Starting the IoT Hub Device Twins Python client sample..." )
iothub_client_sample_run()
python ReportConnectivity.py
You should see confirmation the device twins were updated.
8. Now that the device reported its connectivity information, it should appear in both queries. Go back and run
the queries again:
python AddTagsAndQuery.py
This time your {Device Id} should appear in both query results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT hub's
identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device app to
report device connectivity information in the device twin. You also learned how to query this information using the
registry.
Use the following resources to learn how to:
Send telemetry from devices with the Get started with IoT Hub tutorial,
Configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
Control devices interactively (such as turning on a fan from a user-controlled app), with the Use direct methods
tutorial.
Upload files from your device to the cloud with IoT
Hub using .NET
6/6/2018 • 5 min to read • Edit Online
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub. It shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub and Send Cloud-to-Device messages with IoT Hub tutorials show the basic device-
to-cloud and cloud-to-device messaging functionality of IoT Hub. The Process Device-to-Cloud messages tutorial
describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some scenarios you
cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub
accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upload files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two .NET console apps:
SimulatedDevice, a modified version of the app created in the Send Cloud-to-Device messages with IoT Hub
tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
ReadFileUploadNotification, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, Java, and Javascript) through Azure IoT device SDKs.
Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.
3. In the Program.cs file, add the following statements at the top of the file:
using System.IO;
watch.Stop();
Console.WriteLine("Time to upload file: {0}ms\n", watch.ElapsedMilliseconds);
}
The UploadToBlobAsync method takes in the file name and stream source of the file to be uploaded and
handles the upload to storage. The console app displays the time it takes to upload the file.
5. Add the following method in the Main method, right before the Console.ReadLine() line:
SendToBlobAsync();
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Substitute the placeholder value with the IoT hub connection
string from [Get started with IoT Hub]:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received file upload noticiation: {0}", string.Join(", ",
fileUploadNotification.BlobName));
Console.ResetColor();
await notificationReceiver.CompleteAsync(fileUploadNotification);
}
}
Note this receive pattern is the same one used to receive cloud-to-device messages from the device app.
7. Finally, add the following lines to the Main method:
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The tutorial shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub and Send Cloud-to-Device messages with IoT Hub tutorials show the basic device-
to-cloud and cloud-to-device messaging functionality of IoT Hub. The Process Device-to-Cloud messages tutorial
describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some scenarios you
cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub
accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Java console apps:
simulated-device, a modified version of the app created in the [Send Cloud-to-Device messages with IoT Hub]
tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
read-file-upload-notification, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, and Javascript) through Azure IoT device SDKs.
Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.
4. To process file upload status callback messages, add the following nested class to the App class:
5. To upload images to IoT Hub, add the following method to the App class to upload images to IoT Hub:
// Use IoT Hub to upload a file asynchronously to Azure blob storage.
private static void uploadFile(String fullFileName) throws FileNotFoundException, IOException
{
File file = new File(fullFileName);
InputStream inputStream = new FileInputStream(file);
long streamLength = file.length();
6. Modify the main method to call the uploadFile method as shown in the following snippet:
client.open();
try
{
// Get the filename and start the upload.
String fullFileName = System.getProperty("user.dir") + File.separator + fileName;
uploadFile(fullFileName);
System.out.println("File upload started with success");
}
catch (Exception e)
{
System.out.println("Exception uploading file: " + e.getCause() + " \nERROR: " + e.getMessage());
}
7. Use the following command to build the simulated-device app and check for errors:
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
import com.microsoft.azure.sdk.iot.service.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
private static final String connectionString = "{Your IoT Hub connection string}";
private static final IotHubServiceClientProtocol protocol = IotHubServiceClientProtocol.AMQPS;
private static FileUploadNotificationReceiver fileUploadNotificationReceiver = null;
8. To print information about the file upload to the console, add the following nested class to the App class:
9. To start the thread that listens for file upload notifications, add the following code to the main method:
public static void main(String[] args) throws IOException, URISyntaxException, Exception {
ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, protocol);
if (serviceClient != null) {
serviceClient.open();
The following screenshot shows the output from the simulated-device app:
The following screenshot shows the output from the read-file-upload-notification app:
You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from devices.
You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Simulating a device with IoT Edge
Upload files from your device to the cloud with IoT
Hub
5/29/2018 • 5 min to read • Edit Online
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The tutorial shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub tutorial demonstrates the basic device-to-cloud messaging functionality of IoT Hub.
However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-
cloud messages that IoT Hub accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Node.js console apps:
SimulatedDevice.js, which uploads a file to storage using a SAS URI provided by your IoT hub.
ReadFileUploadNotification.js, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, Javascript, Python, and Java) through Azure IoT
device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure
IoT Hub.
npm init
2. At your command prompt in the simulateddevice folder, run the following command to install the azure-
iot-device Device SDK package and azure-iot-device-mqtt package:
'use strict';
var fs = require('fs');
var mqtt = require('azure-iot-device-mqtt').Mqtt;
var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
NOTE
For the sake of simplicity the connection string is included in the code: this is not a recommended practice and
depending on your use-case and architecture you may want to consider more secure ways of storing this secret.
7. Create a callback and use the uploadToBlob function to upload the file.
npm init
2. At your command prompt in the fileuploadnotification folder, run the following command to install the
azure-iothub SDK package:
NOTE
For the sake of simplicity the connection string is included in the code: this is not a recommended practice and
depending on your use-case and architecture you may want to consider more secure ways of storing this secret.
7. Open the client and use the getFileNotificationReceiver function to receive status updates.
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
serviceClient.getFileNotificationReceiver(function receiveFileUploadNotification(err, receiver){
if (err) {
console.error('error getting the file notification receiver: ' + err.toString());
} else {
receiver.on('message', function (msg) {
console.log('File upload from device:')
console.log(msg.getData().toString('utf-8'));
});
}
});
}
});
node FileUploadNotification.js
node SimulatedDevice.js
The following screenshot shows the output from the SimulatedDevice app:
The following screenshot shows the output from the FileUploadNotification app:
You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from devices.
You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
Upload files from your device to the cloud with IoT
Hub
5/29/2018 • 6 min to read • Edit Online
This tutorial follows how to use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The
tutorial shows you how to:
Securely provide a storage container for uploading a file.
Use the Python client to upload a file through your IoT hub.
The Get started with IoT Hub tutorial demonstrates the basic device-to-cloud messaging functionality of IoT Hub.
However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-
cloud messages that IoT Hub accepts. When you need to upland files from a device, you can still use the security
and reliability of IoT Hub.
NOTE
IoT Hub Python SDK currently only supports uploading character-based files such as .txt files.
At the end of this tutorial you run the Python console app:
FileUpload.py, which uploads a file to storage using the Python Device SDK.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, Javascript, Python, and Java) through Azure IoT
device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure
IoT Hub.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys
to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your
application needs to store other device-specific metadata, it should use an application-specific store. For more information,
see IoT Hub developer guide.
import time
import sys
import iothub_client
import os
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult,
IoTHubError
5. Add the following code to connect the client and upload the file. Also include the main routine:
def iothub_file_upload_sample_run():
try:
print ( "IoT Hub file upload sample, press Ctrl-C to exit" )
f = open(PATHTOFILE, "r")
content = f.read()
print ( "" )
print ( "File upload initiated..." )
while True:
time.sleep(30)
if __name__ == '__main__':
print ( "Simulating a file upload using the Azure IoT Hub Device SDK for Python" )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_file_upload_sample_run()
NOTE
IoT Hub Python SDK currently only supports uploading character-based files such as .txt files.
python FileUpload.py
2. The following screenshot shows the output from the FileUpload app:
3. You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from devices.
You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
Get started with device twins (Node)
5/29/2018 • 9 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and
conditions). IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand
device twins for more information about device twins, and to the IoT Hub query language reference for
querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its
connectivity channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have two Node.js console apps:
AddTagsAndQuery.js, a Node.js back-end app, which adds tags and queries device twins.
TwinSimulatedDevice.js, a Node.js app, which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and
back-end apps.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create.
You can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use a Node.js tool called iothub-explorer to create a device identity for this tutorial. Device
IDs are case sensitive.
1. Run the following in your command-line environment:
npm install -g iothub-explorer@latest
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the addtagsandqueryapp folder, run the following command to install the
azure-iothub package:
npm install azure-iothub --save
3. Using a text editor, create a new AddTagsAndQuery.js file in the addtagsandqueryapp folder.
4. Add the following code to the AddTagsAndQuery.js file, and substitute the {iot hub connection
string} placeholder with the IoT Hub connection string you copied when you created your hub:
'use strict';
var iothub = require('azure-iothub');
var connectionString = '{iot hub connection string}';
var registry = iothub.Registry.fromConnectionString(connectionString);
twin.update(patch, function(err) {
if (err) {
console.error('Could not update twin: ' + err.constructor.name + ': ' + err.message);
} else {
console.log(twin.deviceId + ' twin updated successfully');
queryTwins();
}
});
}
});
The Registry object exposes all the methods required to interact with device twins from the service. The
previous code first initializes the Registry object, then retrieves the device twin for myDeviceId, and
finally updates its tags with the desired location information.
After updating the tags it calls the queryTwins function.
5. Add the following code at the end of AddTagsAndQuery.js to implement the queryTwins function:
var queryTwins = function() {
var query = registry.createQuery("SELECT * FROM devices WHERE tags.location.plant =
'Redmond43'", 100);
query.nextAsTwin(function(err, results) {
if (err) {
console.error('Failed to fetch the results: ' + err.message);
} else {
console.log("Devices in Redmond43: " + results.map(function(twin) {return
twin.deviceId}).join(','));
}
});
The previous code executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
The previous code, when it creates the query object, specifies a maximum number of returned
documents. The query object contains a hasMoreResults boolean property that you can use to invoke
the nextAsTwin methods multiple times to retrieve all results. A method called next is available for
results that are not device twins, for example, results of aggregation queries.
6. Run the application with:
node AddTagsAndQuery.js
You should see one device in the results for the query asking for all devices located in Redmond43 and
none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of
the query in the previous section.
npm init
2. At your command prompt in the reportconnectivity folder, run the following command to install the
azure-iot-device, and azure-iot-device-mqtt package:
3. Using a text editor, create a new ReportConnectivity.js file in the reportconnectivity folder.
4. Add the following code to the ReportConnectivity.js file, and substitute the {device connection
string} placeholder with the device connection string you copied when you created the myDeviceId
device identity:
'use strict';
var Client = require('azure-iot-device').Client;
var Protocol = require('azure-iot-device-mqtt').Mqtt;
client.open(function(err) {
if (err) {
console.error('could not open IotHub client');
} else {
console.log('client opened');
client.getTwin(function(err, twin) {
if (err) {
console.error('could not get twin');
} else {
var patch = {
connectivity: {
type: 'cellular'
}
};
twin.properties.reported.update(patch, function(err) {
if (err) {
console.error('could not update twin');
} else {
console.log('twin state reported');
process.exit();
}
});
}
});
}
});
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for myDeviceId and updates
its reported property with the connectivity information.
5. Run the device app
node ReportConnectivity.js
node AddTagsAndQuery.js
This time myDeviceId should appear in both query results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device
app to report device connectivity information in the device twin. You also learned how to query this information
using the SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app), with the Use direct
methods tutorial.
Get started with device twins (.NET/Node)
5/29/2018 • 9 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand
device twins for more information about device twins, and to the IoT Hub query language reference for querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have a .NET and a Node.js console app:
AddTagsAndQuery.sln, a .NET back-end app, which adds tags and queries device twins.
TwinSimulatedDevice.js, a Node.js app which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use a Node.js tool called iothub-explorer to create a device identity for this tutorial. Device IDs
are case sensitive.
1. Run the following in your command-line environment:
npm install -g iothub-explorer@latest
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the previous section.
The RegistryManager class exposes all the methods required to interact with device twins from the
service. The previous code first initializes the registryManager object, then retrieves the device twin for
myDeviceId, and finally updates its tags with the desired location information.
After updating, it executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
Note that the previous code, when it creates the query object, specifies a maximum number of returned
documents. The query object contains a HasMoreResults boolean property that you can use to invoke
the GetNextAsTwinAsync methods multiple times to retrieve all results. A method called
GetNextAsJson is available for results that are not device twins, for example, results of aggregation
queries.
7. Finally, add the following lines to the Main method:
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddTagsAndQuery().Wait();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
8. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
AddTagsAndQuery project is Start. Build the solution.
9. Run this application by right-clicking on the AddTagsAndQuery project and selecting Debug, followed
by Start new instance. You should see one device in the results for the query asking for all devices
located in Redmond43 and none for the query that restricts the results to devices that use a cellular
network.
In the next section, you create a device app that reports the connectivity information and changes the result of the
query in the previous section.
npm init
2. At your command prompt in the reportconnectivity folder, run the following command to install the
azure-iot-device, and azure-iot-device-mqtt package:
3. Using a text editor, create a new ReportConnectivity.js file in the reportconnectivity folder.
4. Add the following code to the ReportConnectivity.js file, and substitute the placeholder for device
connection string with the one you copied when you created the myDeviceId device identity:
'use strict';
var Client = require('azure-iot-device').Client;
var Protocol = require('azure-iot-device-mqtt').Mqtt;
client.open(function(err) {
if (err) {
console.error('could not open IotHub client');
} else {
console.log('client opened');
client.getTwin(function(err, twin) {
if (err) {
console.error('could not get twin');
} else {
var patch = {
connectivity: {
type: 'cellular'
}
};
twin.properties.reported.update(patch, function(err) {
if (err) {
console.error('could not update twin');
} else {
console.log('twin state reported');
process.exit();
}
});
}
});
}
});
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for myDeviceId and updates
its reported property with the connectivity information.
5. Run the device app
node ReportConnectivity.js
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device
app to report device connectivity information in the device twin. You also learned how to query this information
using the SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Get started with device twins (.NET/.NET)
5/29/2018 • 10 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand
device twins for more information about device twins, and to the IoT Hub query language reference for
querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have these .NET console apps:
CreateDeviceIdentity, a .NET app which creates a device identity and associated security key to connect
your simulated device app.
AddTagsAndQuery, a .NET back-end app which adds tags and queries device twins.
ReportConnectivity, a .NET device app which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
2. In Solution Explorer, right-click the AddTagsAndQuery project, and then click Manage NuGet Packages....
3. In the NuGet Package Manager window, select Browse and search for microsoft.azure.devices.
Select Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This
procedure downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the previous section.
The RegistryManager class exposes all the methods required to interact with device twins from the
service. The previous code first initializes the registryManager object, then retrieves the device twin for
myDeviceId, and finally updates its tags with the desired location information.
After updating, it executes two queries: the first selects only the device twins of devices located in the
Redmond43 plant, and the second refines the query to select only the devices that are also connected
through cellular network.
Note that the previous code, when it creates the query object, specifies a maximum number of returned
documents. The query object contains a HasMoreResults boolean property that you can use to invoke
the GetNextAsTwinAsync methods multiple times to retrieve all results. A method called
GetNextAsJson is available for results that are not device twins, for example, results of aggregation
queries.
7. Finally, add the following lines to the Main method:
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddTagsAndQuery().Wait();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
8. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
AddTagsAndQuery project is Start. Build the solution.
9. Run this application by right-clicking on the AddTagsAndQuery project and selecting Debug, followed
by Start new instance. You should see one device in the results for the query asking for all devices
located in Redmond43 and none for the query that restricts the results to devices that use a cellular
network.
In the next section, you create a device app that reports the connectivity information and changes the result of
the query in the previous section.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the previous section.
The Client object exposes all the methods you require to interact with device twins from the device. The
code shown above, initializes the Client object, and then retrieves the device twin for myDeviceId.
7. Add the following method to the Program class:
The code above updates myDeviceId's reported property with the connectivity information.
8. Finally, add the following lines to the Main method:
try
{
InitClient();
ReportConnectivity();
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
9. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
ReportConnectivity project is Start. Build the solution.
10. Run this application by right-clicking on the ReportConnectivity project and selecting Debug, followed
by Start new instance. You should see it getting the twin information, and then sending connectivity as a
reported property.
11. Now that the device reported its connectivity information, it should appear in both queries. Run the .NET
AddTagsAndQuery app to run the queries again. This time myDeviceId should appear in both query
results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device
app to report device connectivity information in the device twin. You also learned how to query this information
using the SQL -like IoT Hub query language.
Use the following resources to learn how to:
send telemetry from devices with the Get started with IoT Hub tutorial,
configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Get started with device twins (Java)
5/29/2018 • 11 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and conditions).
IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand
device twins for more information about device twins, and to the IoT Hub query language reference for
querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
In this tutorial, you create two Java console apps:
add-tags-query, a Java back-end app that adds tags and queries device twins.
simulated-device, a Java device app that that connects to your IoT hub and reports its connectivity
condition using a reported property.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
8. When your new IoT hub is ready, click its tile in the Azure portal to open its properties window. Now that
you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub. Click Shared access policies.
9. In Shared access policies, select the iothubowner policy. Copy the IoT Hub Connection string---
primary key to use later. For more information, see Access control in the "IoT Hub developer guide."
Create a device identity
In this section, you use the Azure portal to create a device identity in the identity registry in your IoT hub. A
device cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the
"Identity registry" section of the IoT Hub developer guide. Use the IoT Devices panel in the portal to generate a
unique device ID and key for your device to use to identify itself to IoT Hub. Device IDs are case-sensitive.
1. Sign in to the Azure portal.
2. Select All resources and find your IoT hub resource.
3. When your IoT hub resource is opened, click the IoT Devices tool, and then click Add at the top.
4. Provide a name for your new device, such as myDeviceId, and click Save. This action creates a new
device identity for your IoT hub.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your
IoT hub connection string you noted in the Create an IoT Hub section:
10. Update the main method signature to include the following throws clause:
public static void main( String[] args ) throws IOException
11. Add the following code to the main method to create the DeviceTwin and DeviceTwinDevice objects.
The DeviceTwin object handles the communication with your IoT hub. The DeviceTwinDevice object
represents the device twin with its properties and tags:
try {
// Code goes here
} catch (IotHubException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
13. To update the region and plant device twin tags in your device twin, add the following code in the try
block:
// Retrieve the device twin with the tag values from IoT Hub
System.out.println("Device twin after update:");
twinClient.getTwin(device);
System.out.println(device);
14. To query the device twins in IoT hub, add the following code to the try block after the code you added in
the previous step. The code runs two queries. Each query returns a maximum of 100 devices:
// Query the device twins in IoT Hub
System.out.println("Devices in Redmond:");
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.32</version>
</dependency>
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
8. Add the following class-level variables to the App class. Replacing {youriothubname} with your IoT hub
name, and {yourdevicekey} with the device key value you generated in the Create a device identity
section:
This sample app uses the protocol variable when it instantiates a DeviceClient object.
9. Add the following code to the main method to:
Create a device client to communicate with IoT Hub.
Create a Device object to store the device twin properties.
10. Add the following code to the main method to create a connectivityType reported property and send it
to IoT Hub:
try {
// Open the DeviceClient and start the device twin services.
client.open();
client.startDeviceTwin(new DeviceTwinStatusCallBack(), null, dataCollector, null);
11. Add the following code to the end of the main method. Waiting for the Enter key allows time for IoT Hub
to report the status of the device twin operations:
dataCollector.clean();
client.close();
3. At a command prompt in the add-tags-query folder, run the following command to run the add-tags-
query service app a second time:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
Now your device has sent the connectivityType property to IoT Hub, the second query returns your
device.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You added device metadata as tags from a back-end app, and wrote a device app to report
device connectivity information in the device twin. You also learned how to query the device twin information
using the SQL -like IoT Hub query language.
Use the following resources to learn how to:
Send telemetry from devices with the Get started with IoT Hub tutorial.
Control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct
methods tutorial.
Get started with device twins (Python)
5/29/2018 • 10 min to read • Edit Online
Device twins are JSON documents that store device state information (metadata, configurations, and
conditions). IoT Hub persists a device twin for each device that connects to it.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Additionally, the solution back end can query device twins based on all the above data. Refer to Understand
device twins for more information about device twins, and to the IoT Hub query language reference for
querying.
This tutorial shows you how to:
Create a back-end app that adds tags to a device twin, and a simulated device app that reports its connectivity
channel as a reported property on the device twin.
Query devices from your back-end app using filters on the tags and properties previously created.
At the end of this tutorial, you will have two Python console apps:
AddTagsAndQuery.py, a Python back-end app, which adds tags and queries device twins.
ReportConnectivity.py, a Python app, which simulates a device that connects to your IoT hub with the
device identity created earlier, and reports its connectivity condition.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
NOTE
The pip packages for azure-iothub-service-client and azure-iothub-device-client are currently available only for
Windows OS. For Linux/Mac OS, please refer to the Linux and Mac OS-specific sections on the Prepare your development
environment for Python post.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
import sys
import iothub_service_client
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
from iothub_service_client import IoTHubDeviceTwin, IoTHubError
4. Add the following code, replacing the placeholder for [IoTHub Connection String] and [Device Id] with
the connection string for the IoT hub and the device id you created in the previous sections.
UPDATE_JSON = "{\"properties\":{\"desired\":{\"location\":\"Redmond\"}}}"
UPDATE_JSON_SEARCH = "\"location\":\"Redmond\""
UPDATE_JSON_CLIENT_SEARCH = "\"connectivity\":\"cellular\""
iothub_registry_statistics = iothub_registry_manager.get_statistics()
print ( "Total device count :
{0}".format(iothub_registry_statistics.totalDeviceCount) )
print ( "Enabled device count :
{0}".format(iothub_registry_statistics.enabledDeviceCount) )
print ( "Disabled device count :
{0}".format(iothub_registry_statistics.disabledDeviceCount) )
print ( "" )
number_of_devices = iothub_registry_statistics.totalDeviceCount
dev_list = iothub_registry_manager.get_device_list(number_of_devices)
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
print ( "" )
The Registry object exposes all the methods required to interact with device twins from the service. The
code first initializes the Registry object, then updates the device twin for deviceId, and finally runs two
queries. The first selects only the device twins of devices located in the Redmond43 plant, and the second
refines the query to select only the devices that are also connected through cellular network.
6. Add the following code at the end of AddTagsAndQuery.py to implement the
iothub_service_sample_run function:
if __name__ == '__main__':
print ( "Starting the IoT Hub Device Twins Python service sample..." )
iothub_service_sample_run()
python AddTagsAndQuery.py
You should see one device in the results for the query asking for all devices located in Redmond43 and
none for the query that restricts the results to devices that use a cellular network.
In the next section, you create a device app that reports the connectivity information and changes the result of
the query in the previous section.
import time
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider,
IoTHubClientResult, IoTHubError
4. Add the following code, replacing the placeholder for [IoTHub Device Connection String] with the
connection string for the IoT hub device you created in the previous sections.
TIMER_COUNT = 5
TWIN_CONTEXT = 0
SEND_REPORTED_STATE_CONTEXT = 0
5. Add the following code to the ReportConnectivity.py file to implement the device twins functionality:
def device_twin_callback(update_state, payload, user_context):
print ( "" )
print ( "Twin callback called with:" )
print ( " updateStatus: %s" % update_state )
print ( " payload: %s" % payload )
def iothub_client_init():
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
return client
def iothub_client_sample_run():
try:
client = iothub_client_init()
if client.protocol == IoTHubTransportProvider.MQTT:
print ( "Sending data as reported property..." )
reported_state = "{\"connectivity\":\"cellular\"}"
client.send_reported_state(reported_state, len(reported_state),
send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
while True:
print ( "Press Ctrl-C to exit" )
status_counter = 0
while status_counter <= TIMER_COUNT:
status = client.get_send_status()
time.sleep(10)
status_counter += 1
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
The Client object exposes all the methods you require to interact with device twins from the device. The
previous code, after it initializes the Client object, retrieves the device twin for your device and updates its
reported property with the connectivity information.
6. Add the following code at the end of ReportConnectivity.py to implement the
iothub_client_sample_run function:
if __name__ == '__main__':
print ( "Starting the IoT Hub Device Twins Python client sample..." )
iothub_client_sample_run()
python ReportConnectivity.py
You should see confirmation the device twins were updated.
8. Now that the device reported its connectivity information, it should appear in both queries. Go back and
run the queries again:
python AddTagsAndQuery.py
This time your {Device Id} should appear in both query results.
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You added device metadata as tags from a back-end app, and wrote a simulated device
app to report device connectivity information in the device twin. You also learned how to query this information
using the registry.
Use the following resources to learn how to:
Send telemetry from devices with the Get started with IoT Hub tutorial,
Configure devices using device twin's desired properties with the Use desired properties to configure devices
tutorial,
Control devices interactively (such as turning on a fan from a user-controlled app), with the Use direct
methods tutorial.
Get started with IoT Hub module identity and module
twin using the portal and .NET device
5/29/2018 • 5 min to read • Edit Online
NOTE
Module identities and module twins are similar to Azure IoT Hub device identity and device twin, but provide finer granularity.
While Azure IoT Hub device identity and device twin enable the back-end application to configure a device and provides
visibility on the device’s conditions, a module identity and module twin provide these capabilities for individual components of
a device. On capable devices with multiple components, such as operating system based devices or firmware devices, it allows
for isolated configuration and conditions for each component.
NOTE
For information about the Azure IoT SDKs that you can use to build both applications to run on devices, and your solution
back end, see Azure IoT SDKs.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
Save and click the just created module identity. You can see the module identity details. Save the connect string -
primary key. It will be used in the next section where you set up your module on the device.
2. Install the latest Azure IoT Hub .NET device SDK - Module identity and module twin is in public
preview. It's only availble in the IoT Hub prerelease device SDKs. In Visual Studio, open tools > Nuget
package manager > manage Nuget packages for solution. Search Microsoft.Azure.Devices.Client. Make sure
you've checked include prerelease check box. Select the latest version and install. Now you have access to all
the module features.
3. Get your module connection string -- now if you login to Azure portal. Navigate to your IoT Hub and
click IoT Devices. Find myFirstDevice, open it and you see myFirstModule was successfuly created. Copy the
module connection string. It is needed in the next step.
4. Create UpdateModuleTwinReportedProperties console app Add the following using statements at
the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
Add the following fields to the Program class. Replace the placeholder value with the module connection
string.
await Client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
}
try
{
Client = ModuleClient.CreateFromConnectionString(ModuleConnectionString, transport);
Client.SetConnectionStatusChangesHandler(ConnectionStatusChangeHandler);
Client.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChanged, null).Wait();
Console.WriteLine("Retrieving twin");
var twinTask = Client.GetTwinAsync();
twinTask.Wait();
var twin = twinTask.Result;
Console.WriteLine(JsonConvert.SerializeObject(twin));
Client.UpdateReportedPropertiesAsync(reportedProperties);
}
catch (AggregateException ex)
{
Console.WriteLine("Error in sample: {0}", ex);
}
This code sample shows you how to retrieve the module twin and update reported properties with AMQP
protocol. In public preview, we only support AMQP for module twin operations.
Next steps
To continue getting started with IoT Hub and to explore other IoT scenarios, see:
Get started with IoT Hub module identity and module twin using .NET backup and .NET device
Getting started with IoT Edge
Get started with IoT Hub module identity and
module twin using .NET back end and .NET device
5/29/2018 • 8 min to read • Edit Online
NOTE
Module identities and module twins are similar to Azure IoT Hub device identity and device twin, but provide finer
granularity. While Azure IoT Hub device identity and device twin enable the back-end application to configure a device and
provides visibility on the device’s conditions, a module identity and module twin provide these capabilities for individual
components of a device. On capable devices with multiple components, such as operating system based devices or firmware
devices, it allows for isolated configuration and conditions for each component.
At the end of this tutorial, you have two .NET console apps:
CreateIdentities, which creates a device identity, a module identity and associated security key to connect your
device and module clients.
UpdateModuleTwinReportedProperties, which sends updated module twin reported properties to your IoT
Hub.
NOTE
For information about the Azure IoT SDKs that you can use to build both applications to run on devices, and your solution
back end, see Azure IoT SDKs.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
You have now created your IoT hub, and you have the host name and IoT Hub connection string that you need to
complete the rest of this tutorial.
3. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;
4. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the previous section.
try
{
device = await registryManager.AddDeviceAsync(new Device(deviceID));
}
catch (DeviceAlreadyExistsException)
{
device = await registryManager.GetDeviceAsync(deviceID);
}
try
{
module = await registryManager.AddModuleAsync(new Module(deviceID, moduleID));
}
catch (ModuleAlreadyExistsException)
{
module = await registryManager.GetModuleAsync(deviceID, moduleID);
}
The AddDeviceAsync() method creates a device identity with ID myFirstDevice. (If that device ID already
exists in the identity registry, the code simply retrieves the existing device information.) The app then
displays the primary key for that identity. You use this key in the simulated device app to connect to your IoT
hub.
The AddModuleAsync() method creates a module identity with ID myFirstModule under device
myFirstDevice. (If that module ID already exists in the identity registry, the code simply retrieves the
existing module information.) The app then displays the primary key for that identity. You use this key in the
simulated module app to connect to your IoT hub.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any
sensitive information while naming it.
1. Run this application, and make a note of the device key and module key.
NOTE
The IoT Hub identity registry only stores device and module identities to enable secure access to the IoT hub. The identity
registry stores device IDs and keys to use as security credentials. The identity registry also stores an enabled/disabled flag for
each device that you can use to disable access for that device. If your application needs to store other device-specific
metadata, it should use an application-specific store. There is no enabled/disabled flag for module identities. For more
information, see IoT Hub developer guide.
2. Install the latest Azure IoT Hub .NET device SDK - Module identity and module twin is in public
preview. It's only availble in the IoT Hub prerelease device SDKs. In Visual Studio, open tools > Nuget
package manager > manage Nuget packages for solution. Search Microsoft.Azure.Devices.Client. Make
sure you've checked include prerelease check box. Select the latest version and install. Now you have access
to all the module features.
3. Get your module connection string -- now if you login to Azure portal. Navigate to your IoT Hub and
click IoT Devices. Find myFirstDevice, open it and you see myFirstModule was successfuly created. Copy the
module connection string. It is needed in the next step.
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System.Threading.Tasks;
using Newtonsoft.Json;
Add the following fields to the Program class. Replace the placeholder value with the module connection
string.
await Client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
}
try
{
Client = ModuleClient.CreateFromConnectionString(ModuleConnectionString, transport);
Client.SetConnectionStatusChangesHandler(ConnectionStatusChangeHandler);
Client.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChanged, null).Wait();
Console.WriteLine("Retrieving twin");
var twinTask = Client.GetTwinAsync();
twinTask.Wait();
var twin = twinTask.Result;
Console.WriteLine(JsonConvert.SerializeObject(twin.Properties));
Client.UpdateReportedPropertiesAsync(reportedProperties);
}
catch (AggregateException ex)
{
Console.WriteLine("Error in sample: {0}", ex);
}
This code sample shows you how to retrieve the module twin and update reported properties with AMQP
protocol. In public preview, we only support AMQP for module twin operations.
5. In addition to the above Main method, you can add below code block to send event to IoT Hub from your
module:
Next steps
To continue getting started with IoT Hub and to explore other IoT scenarios, see:
Getting started with device management
Getting started with IoT Edge
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
Get started with device management (Node)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app
can work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a Node.js console app that calls the reboot direct method in the simulated device app through your
IoT hub.
At the end of this tutorial, you have two Node.js console apps:
dmpatterns_getstarted_device.js, which connects to your IoT hub with the device identity created earlier,
receives a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
dmpatterns_getstarted_service.js, which calls a direct method in the simulated device app, displays the
response, and displays the updated reported properties.
To complete this tutorial, you need the following:
Node.js version 4.0.x or later,
Prepare your development environment describes how to install Node.js for this tutorial on either Windows
or Linux.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of
minutes.)
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information
while naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the
azure-iot-device Device SDK package and azure-iot-device-mqtt package:
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the connection string
with your device connection string.
6. Add the following function to implement the direct method on the device
var onReboot = function(request, response) {
7. Open the connection to your IoT hub and start the direct method listener:
client.open(function(err) {
if (err) {
console.error('Could not open IotHub client');
} else {
console.log('Client opened. Waiting for reboot method.');
client.onDeviceMethod('reboot', onReboot);
}
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
npm init
2. At your command prompt in the triggerrebootondevice folder, run the following command to install
the azure-iothub Device SDK package and azure-iot-device-mqtt package:
'use strict';
5. Add the following variable declarations and replace the placeholder values:
6. Add the following function to invoke the device method to reboot the target device:
var methodParams = {
methodName: methodName,
payload: null,
timeoutInSeconds: 30
};
7. Add the following function to query for the device and get the last reboot time:
var queryTwinLastReboot = function() {
if (twin.properties.reported.iothubDM != null)
{
if (err) {
console.error('Could not query twins: ' + err.constructor.name + ': ' +
err.message);
} else {
var lastRebootTime = twin.properties.reported.iothubDM.reboot.lastReboot;
console.log('Last reboot time: ' + JSON.stringify(lastRebootTime, null, 2));
}
} else
console.log('Waiting for device to report last reboot time.');
});
};
8. Add the following code to call the functions that trigger the reboot direct method and query for the last
reboot time:
startRebootDevice();
setInterval(queryTwinLastReboot, 2000);
node dmpatterns_getstarted_device.js
2. At the command prompt in the triggerrebootondevice folder, run the following command to trigger
the remote reboot and query for the device twin to find the last reboot time.
node dmpatterns_getstarted_service.js
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported
properties to report the last reboot time from the device, and queried the device twin to discover the last
reboot time of the device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air
firmware update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (.NET/Node)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app
can work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a .NET console app that calls the reboot direct method in the simulated device app through your IoT
hub.
At the end of this tutorial, you have a Node.js console device app and a .NET (C#) console back-end app:
dmpatterns_getstarted_device.js, which connects to your IoT hub with the device identity created earlier,
receives a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
TriggerReboot, which calls a direct method in the simulated device app, displays the response, and displays the
updated reported properties.
To complete this tutorial, you need the following:
Visual Studio 2015 or Visual Studio 2017.
Node.js version 4.0.x or later,
Prepare your development environment describes how to install Node.js for this tutorial on either Windows
or Linux.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of
minutes.)
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
2. In Solution Explorer, right-click the TriggerReboot project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection
string for the hub that you created in the section "Create an IoT hub."
6. Add the following method to the Program class. This code gets the device twin for the rebooting device
and outputs the reported properties.
7. Add the following method to the Program class. This code initiates the reboot on the device using a
direct method.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartReboot().Wait();
QueryTwinRebootReported().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the
azure-iot-device Device SDK package and azure-iot-device-mqtt package:
3. Using a text editor, create a new dmpatterns_getstarted_device.js file in the manageddevice folder.
4. Add the following 'require' statements at the start of the dmpatterns_getstarted_device.js file:
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the connection string
with your device connection string.
6. Add the following function to implement the direct method on the device
var onReboot = function(request, response) {
7. Add the following code to open the connection to your IoT hub and start the direct method listener:
client.open(function(err) {
if (err) {
console.error('Could not open IotHub client');
} else {
console.log('Client opened. Waiting for reboot method.');
client.onDeviceMethod('reboot', onReboot);
}
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_getstarted_device.js
2. Run the C# console app TriggerReboot. Right-click the TriggerReboot project, select Debug, and then
select Start new instance.
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported
properties to report the last reboot time from the device, and queried the device twin to discover the last reboot
time of the device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (.NET/.NET)
5/29/2018 • 10 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app
can work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a .NET console app that calls the reboot direct method in the simulated device app through your IoT
hub.
At the end of this tutorial, you have two .NET console apps:
SimulateManagedDevice, which connects to your IoT hub with the device identity created earlier, receives
a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
TriggerReboot, which calls a direct method in the simulated device app, displays the response, and displays
the updated reported properties.
To complete this tutorial, you need the following:
Visual Studio 2015 or Visual Studio 2017.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of
minutes.)
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
2. In Solution Explorer, right-click the TriggerReboot project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the IoT Hub
connection string for the hub that you created in the section "Create an IoT hub."
6. Add the following method to the Program class. This code gets the device twin for the rebooting device
and outputs the reported properties.
7. Add the following method to the Program class. This code initiates the reboot on the device using a
direct method.
NOTE
This tutorial performs only a single query for the device's reported properties. In production code, we recommend polling
to detect changes in the reported properties.
2. In Solution Explorer, right-click the SimulateManagedDevice project, and then click Manage NuGet
Packages....
3. In the NuGet Package Manager window, select Browse and search for
microsoft.azure.devices.client. Select Install to install the Microsoft.Azure.Devices.Client package,
and accept the terms of use. This procedure downloads, installs, and adds a reference to the Azure IoT
device SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the previous section.
7. Finally, add the following code to the Main method to open the connection to your IoT hub and initialize
the method listener:
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Exiting...");
8. In the Visual Studio Solution Explorer, right-click your solution, and then click Set StartUp Projects....
Select Single startup project, and then select the SimulateManagedDevice project in the dropdown
menu. Build the solution.
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported
properties to report the last reboot time from the device, and queried the device twin to discover the last reboot
time of the device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (Java)
5/29/2018 • 12 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app
can work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that implements a direct method to reboot the device. Direct methods are
invoked from the cloud.
Create an app that invokes the reboot direct method in the simulated device app through your IoT hub. This
app then monitors the reported properties from the device to see when the reboot operation is complete.
At the end of this tutorial, you have two Java console apps:
simulated-device. This app:
Connects to your IoT hub with the device identity created earlier.
Receives a reboot direct method call.
Simulates a physical reboot.
Reports the time of the last reboot through a reported property.
trigger-reboot. This app:
Calls a direct method in the simulated device app.
Displays the response to the direct method call sent by the simulated device
Displays the updated reported properties.
NOTE
For information about the SDKs that you can use to build applications to run on devices and your solution back end, see
Azure IoT SDKs.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod;
import com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your
IoT hub connection string you noted in the Create an IoT Hub section:
10. To implement a thread that reads the reported properties from the device twin every 10 seconds, add the
following nested class to the App class:
12. To invoke the reboot direct method on the simulated device, add the following code to the main method:
System.out.println("Starting sample...");
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
try
{
System.out.println("Invoke reboot direct method");
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout,
null);
if(result == null)
{
throw new IOException("Invoke direct method reboot returns null");
}
System.out.println("Invoked reboot on device");
System.out.println("Status for device: " + result.getStatus());
System.out.println("Message from device: " + result.getPayload());
}
catch (IotHubException e)
{
System.out.println(e.getMessage());
}
13. To start the thread to poll the reported properties from the simulated device, add the following code to
the main method:
14. To enable you to stop the app, add the following code to the main method:
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.32</version>
</dependency>
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
8. Add the following class-level variables to the App class. Replace {yourdeviceconnectionstring} with the
device connection string you noted in the Create a device identity section:
private static final int METHOD_SUCCESS = 200;
private static final int METHOD_NOT_DEFINED = 404;
9. To implement a callback handler for direct method status events, add the following nested class to the
App class:
10. To implement a callback handler for device twin status events, add the following nested class to the App
class:
11. To implement a callback handler for property events, add the following nested class to the App class:
12. To implement a thread to simulate the device reboot, add the following nested class to the App class. The
thread sleeps for five seconds and then sets the lastReboot reported property:
protected static class RebootDeviceThread implements Runnable {
public void run() {
try {
System.out.println("Rebooting...");
Thread.sleep(5000);
Property property = new Property("lastReboot", LocalDateTime.now());
Set<Property> properties = new HashSet<Property>();
properties.add(property);
client.sendReportedProperties(properties);
System.out.println("Rebooted");
}
catch (Exception ex) {
System.out.println("Exception in reboot thread: " + ex.getMessage());
}
}
}
13. To implement the direct method on the device, add the following nested class to the App class. When the
simulated app receives a call to the reboot direct method, it returns an acknowledgement to the caller
and then starts a thread to process the reboot:
14. Modify the signature of the main method to throw the following exceptions:
15. To instantiate a DeviceClient, add the following code to the main method:
16. To start listening for direct method calls, add the following code to the main method:
try
{
client.open();
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(),
null);
client.startDeviceTwin(new DeviceTwinStatusCallback(), null, new PropertyCallback(), null);
System.out.println("Subscribed to direct methods and polling for reported properties. Waiting...");
}
catch (Exception e)
{
System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \n" +
e.getMessage());
client.close();
System.out.println("Shutting down...");
}
17. To shut down the device simulator, add the following code to the main method:
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported
properties to report the last reboot time from the device, and queried the device twin to discover the last reboot
time of the device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
Get started with device management (Python)
5/29/2018 • 9 min to read • Edit Online
Back-end apps can use Azure IoT Hub primitives, such as device twin and direct methods, to remotely start and
monitor device management actions on devices. This tutorial shows you how a back-end app and a device app
can work together to initiate and monitor a remote device reboot using IoT Hub.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update)
from a back-end app in the cloud. The device is responsible for:
Handling the method request sent from IoT Hub.
Initiating the corresponding device-specific action on the device.
Providing status updates through reported properties to IoT Hub.
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device
management actions.
This tutorial shows you how to:
Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
Create a simulated device app that contains a direct method that reboots that device. Direct methods are
invoked from the cloud.
Create a Python console app that calls the reboot direct method in the simulated device app through your IoT
hub.
At the end of this tutorial, you have two Python console apps:
dmpatterns_getstarted_device.py, which connects to your IoT hub with the device identity created earlier,
receives a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
dmpatterns_getstarted_service.py, which calls a direct method in the simulated device app, displays the
response, and displays the updated reported properties.
To complete this tutorial, you need the following:
Python 2.x or 3.x. Make sure to use the 32-bit or 64-bit installation as required by your setup. When
prompted during the installation, make sure to add Python to your platform-specific environment variable. If
you are using Python 2.x, you may need to install or upgrade pip, the Python package management system.
Install the azure-iothub-device-client package, using the command
pip install azure-iothub-device-client
Install the azure-iothub-service-client package, using the command
pip install azure-iothub-service-client
If you are using Windows OS, then Visual C++ redistributable package to allow the use of native DLLs from
Python.
An active Azure account. (If you don't have an account, you can create a free account in just a couple of
minutes.)
Create an IoT hub
Create an IoT hub for your simulated device app to connect to. The following steps show you how to complete
this task by using the Azure portal.
1. Sign in to the Azure portal.
2. Select Create a resource > Internet of Things > IoT Hub.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
import random
import time, datetime
import sys
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider,
IoTHubClientResult, IoTHubError, DeviceMethodReturnValue
3. Add variables including a CONNECTION_STRING variable and the client intialization. Replace the
connection string with your device connection string.
CONNECTION_STRING = "{deviceConnectionString}"
PROTOCOL = IoTHubTransportProvider.MQTT
WAIT_COUNT = 5
SEND_REPORTED_STATE_CONTEXT = 0
METHOD_CONTEXT = 0
SEND_REPORTED_STATE_CALLBACKS = 0
METHOD_CALLBACKS = 0
4. Add the following function callbacks to implement the direct method on the device.
if method_name == "rebootDevice":
print ( "Rebooting device..." )
time.sleep(20)
current_time = str(datetime.datetime.now())
reported_state = "{\"rebootTime\":\"" + current_time + "\"}"
CLIENT.send_reported_state(reported_state, len(reported_state), send_reported_state_callback,
SEND_REPORTED_STATE_CONTEXT)
device_method_return_value = DeviceMethodReturnValue()
device_method_return_value.response = "{ \"Response\": \"This is the response from the device\" }"
device_method_return_value.status = 200
return device_method_return_value
def iothub_client_init():
if CLIENT.protocol == IoTHubTransportProvider.MQTT or client.protocol ==
IoTHubTransportProvider.MQTT_WS:
CLIENT.set_device_method_callback(device_method_callback, METHOD_CONTEXT)
def iothub_client_sample_run():
try:
iothub_client_init()
while True:
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
time.sleep(10)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Python sample..." )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_client_sample_run()
3. Add the following variable declarations. Only replace placeholder values for IoTHubConnectionString and
deviceId.
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
METHOD_NAME = "rebootDevice"
METHOD_PAYLOAD = "{\"method_number\":\"42\"}"
TIMEOUT = 60
WAIT_COUNT = 10
4. Add the following function to invoke the device method to reboot the target device, then query for the
device twins and get the last reboot time.
def iothub_devicemethod_sample_run():
try:
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
print ( "" )
print ( "Invoking device to reboot..." )
print ( "" )
print ( "Successfully invoked the device to reboot." )
print ( "" )
print ( response.payload )
while True:
print ( "" )
print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" )
status_counter = 0
while status_counter <= WAIT_COUNT:
twin_info = iothub_twin_method.get_twin(DEVICE_ID)
if twin_info.find("rebootTime") != -1:
print ( "Last reboot time: " +
twin_info[twin_info.find("rebootTime")+11:twin_info.find("rebootTime")+37])
else:
print ("Waiting for device to report last reboot time...")
time.sleep(5)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Service Client DeviceManagement Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_devicemethod_sample_run()
python dmpatterns_getstarted_device.py
2. At another command prompt, run the following command to trigger the remote reboot and query for the
device twin to find the last reboot time.
python dmpatterns_getstarted_service.py
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote reboot on a device. You used the reported
properties to report the last reboot time from the device, and queried the device twin to discover the last reboot
time of the device from the cloud.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
To continue getting started with IoT Hub, see Getting started with IoT Edge.
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
1 min to read •
Edit Online
Use device management to initiate a device
firmware update (Node/Node)
5/29/2018 • 10 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and provides guidance
and shows you how to do an end-to-end simulated firmware update. This pattern is used in the firmware
update implementation for the Intel Edison device sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information
while naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with
the IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the
device app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the triggerfwupdateondevice folder, run the following command to
install the azure-iot-hub package:
'use strict';
5. Add the following variable declarations and replace the placeholder values:
6. Add the following function to find and display the value of the firmwareUpdate reported property.
var queryTwinFWUpdateReported = function() {
registry.getTwin(deviceToUpdate, function(err, twin){
if (err) {
console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
} else {
console.log((JSON.stringify(twin.properties.reported.iothubDM.firmwareUpdate)) + "\n");
}
});
};
7. Add the following function to invoke the firmwareUpdate method to reboot the target device:
var methodParams = {
methodName: methodName,
payload: payloadData,
timeoutInSeconds: 30
};
8. Finally, Add the following function to code to start the firmware update sequence and start periodically
showing the reported properties:
startFirmwareUpdateDevice();
setInterval(queryTwinFWUpdateReported, 500);
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the
azure-iot-device and azure-iot-device-mqtt Device SDK packages:
npm install azure-iot-device azure-iot-device-mqtt --save
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Replace the
{yourdeviceconnectionstring} placeholder with the connection string you previously made a note of in
the "Create a device identity" section previously:
twin.properties.reported.update(patch, function(err) {
if (err) throw err;
console.log('twin state reported: ' + firmwareUpdateValue.status);
});
};
7. Add the following functions that simulate downloading and applying the firmware image:
callback(error, image);
}
if (!imageData) {
error = {message: 'Apply image failed because of missing image data.'};
}
callback(error);
}
8. Add the following function that updates the firmware update status through the reported properties to
waiting. Typically, devices are informed of an available update and an administrator defined policy
causes the device to start downloading and applying the update. This function is where the logic to
enable that policy should run. For simplicity, the sample waits for four seconds before proceeding to
download the firmware image:
reportFWUpdateThroughTwin(twin, {
fwPackageUri: fwPackageUriVal,
status: 'waiting',
error : null,
startedWaitingTime : now.toISOString()
});
setTimeout(callback, 4000);
};
9. Add the following function that updates the firmware update status through the reported properties to
downloading. The function then simulates a firmware download and finally updates the firmware
update status to either downloadFailed or downloadComplete:
reportFWUpdateThroughTwin(twin, {
status: 'downloading',
});
setTimeout(function() {
// Simulate download
simulateDownloadImage(fwPackageUriVal, function(err, image) {
if (err)
{
reportFWUpdateThroughTwin(twin, {
status: 'downloadfailed',
error: {
code: error_code,
message: error_message,
}
});
}
else {
reportFWUpdateThroughTwin(twin, {
status: 'downloadComplete',
downloadCompleteTime: now.toISOString(),
});
}, 4000);
}
10. Add the following function that updates the firmware update status through the reported properties to
applying. The function then simulates applying the firmware image and finally updates the firmware
update status to either applyFailed or applyComplete:
var applyImage = function(twin, imageData, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
status: 'applying',
startedApplyingImage : now.toISOString()
});
setTimeout(function() {
}
});
setTimeout(callback, 4000);
}, 4000);
}
11. Add the following function that handles the firmwareUpdate direct method and initiates the multi-
stage firmware update process:
var onFirmwareUpdate = function(request, response) {
}
});
}
12. Finally, add the following code that connects to your IoT hub:
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client');
} else {
console.log('Client connected to IoT Hub. Waiting for firmwareUpdate direct method.');
}
client.onDeviceMethod('firmwareUpdate', onFirmwareUpdate);
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement
retry policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_fwupdate_device.js
2. At the command prompt in the triggerfwupdateondevice folder, run the following command to
trigger the remote reboot and query for the device twin to find the last reboot time.
node dmpatterns_fwupdate_service.js
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the
reported properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device
firmware update (.NET/Node)
5/29/2018 • 10 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do
an end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
2. In Solution Explorer, right-click the TriggerFWUpdate project, and then click Manage NuGet
Packages....
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the multiple placeholder values with the IoT Hub
connection string for the hub that you created in the previous section and the Id of your device.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartFirmwareUpdate().Wait();
QueryTwinFWUpdateReported().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
9. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for
TriggerFWUpdate project is Start.
10. Build the solution.
npm init
2. At your command prompt in the manageddevice folder, run the following command to install the azure-
iot-device and azure-iot-device-mqtt Device SDK packages:
'use strict';
twin.properties.reported.update(patch, function(err) {
if (err) throw err;
console.log('twin state reported: ' + firmwareUpdateValue.status);
});
};
7. Add the following functions that simulate downloading and applying the firmware image:
callback(error, image);
}
if (!imageData) {
error = {message: 'Apply image failed because of missing image data.'};
}
callback(error);
}
8. Add the following function that updates the firmware update status through the reported properties to
waiting. Typically, devices are informed of an available update and an administrator defined policy causes
the device to start downloading and applying the update. This function is where the logic to enable that
policy should run. For simplicity, the sample waits for four seconds before proceeding to download the
firmware image:
var waitToDownload = function(twin, fwPackageUriVal, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
fwPackageUri: fwPackageUriVal,
status: 'waiting',
error : null,
startedWaitingTime : now.toISOString()
});
setTimeout(callback, 4000);
};
9. Add the following function that updates the firmware update status through the reported properties to
downloading. The function then simulates a firmware download and finally updates the firmware update
status to either downloadFailed or downloadComplete:
reportFWUpdateThroughTwin(twin, {
status: 'downloading',
});
setTimeout(function() {
// Simulate download
simulateDownloadImage(fwPackageUriVal, function(err, image) {
if (err)
{
reportFWUpdateThroughTwin(twin, {
status: 'downloadfailed',
error: {
code: error_code,
message: error_message,
}
});
}
else {
reportFWUpdateThroughTwin(twin, {
status: 'downloadComplete',
downloadCompleteTime: now.toISOString(),
});
}, 4000);
}
10. Add the following function that updates the firmware update status through the reported properties to
applying. The function then simulates applying the firmware image and finally updates the firmware
update status to either applyFailed or applyComplete:
var applyImage = function(twin, imageData, callback) {
var now = new Date();
reportFWUpdateThroughTwin(twin, {
status: 'applying',
startedApplyingImage : now.toISOString()
});
setTimeout(function() {
}
});
setTimeout(callback, 4000);
}, 4000);
}
11. Add the following function that handles the firmwareUpdate direct method and initiates the multi-stage
firmware update process:
var onFirmwareUpdate = function(request, response) {
}
});
}
12. Finally, add the following code that connects to your IoT hub:
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client');
} else {
console.log('Client connected to IoT Hub. Waiting for firmwareUpdate direct method.');
}
client.onDeviceMethod('firmwareUpdate', onFirmwareUpdate);
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
node dmpatterns_fwupdate_device.js
2. In Visual Studio, right-click on the TriggerFWUpdate project, select Debug and Start new instance.
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device
firmware update (.NET/.NET)
6/4/2018 • 12 min to read • Edit Online
Introduction
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do
an end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
2. In Solution Explorer, right-click the TriggerFWUpdate project, and then click Manage NuGet Packages.
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure
downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the multiple placeholder values with the IoT Hub
connection string for the hub that you created in the previous section and the ID of your device.
6. Add the following method to the Program class. This method polls the device twin for updated status
every 500 milliseconds. It writes to the console only when status has actually changed. For this sample, to
prevent consuming extra IoT Hub messages in your subscription, polling stops when the device reports a
status of applyComplete or an error.
public static async Task QueryTwinFWUpdateReported(DateTime startTime)
{
DateTime lastUpdated = startTime;
while (true)
{
Twin twin = await registryManager.GetTwinAsync(targetDevice);
if (twin.Properties.Reported.GetLastUpdated().ToUniversalTime() >
lastUpdated.ToUniversalTime())
{
lastUpdated = twin.Properties.Reported.GetLastUpdated().ToUniversalTime();
Console.WriteLine("\n" + twin.Properties.Reported["iothubDM"].ToJson());
8. Finally, add the following lines to the Main method. This creates a registry manager to read the device
twin with, starts the polling task on a worker thread, and then triggers the firmware update.
registryManager = RegistryManager.CreateFromConnectionString(connString);
StartFirmwareUpdate().Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
2. In Solution Explorer, right-click the SimulateDeviceFWUpdate project, and then click Manage NuGet
Packages.
3. In the NuGet Package Manager window, select Browse and search for
microsoft.azure.devices.client. Select Install to install the Microsoft.Azure.Devices.Client package,
and accept the terms of use. This procedure downloads, installs, and adds a reference to the Azure IoT
device SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Newtonsoft.Json.Linq;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the Create a device identity section.
static string DeviceConnectionString = "HostName=<yourIotHubName>.azure-devices.net;DeviceId=
<yourIotDeviceName>;SharedAccessKey=<yourIotDeviceAccessKey>";
static DeviceClient Client = null;
6. Add the following method to report status back to the cloud through the device twin:
iothubDM["firmwareUpdate"] = fwUpdateValue;
patch["iothubDM"] = iothubDM;
await Client.UpdateReportedPropertiesAsync(patch);
Console.WriteLine("Twin state reported: {0}", fwUpdateValue["status"]);
}
catch
{
Console.WriteLine("Error updating device twin");
throw;
}
}
await Task.Delay(4000);
return Encoding.ASCII.GetBytes(image);
8. Add the following method to simulate applying the firmware image to the device:
await Task.Delay(4000);
9. Add the following method to simulate waiting to download the firmware image. Update status to waiting
and clear other firmware update properties on the twin. These properties are cleared to remove any
existing values from prior firmware updates. This is necessary because reported properties are sent as a
PATCH operation (a delta) and not a PUT operation (a complete set of properties that replaces all of the
previous values). Typically, devices are informed of an available update and an administrator defined
policy causes the device to start downloading and applying the update. This function is where the logic to
enable that policy should run.
await Task.Delay(2000);
}
10. Add the following method to perform the download. It updates the status to downloading through the
device twin, calls the simulate download method, and reports a status of downloadComplete or
downloadFailed through the twin depending on the results of the download operation.
11. Add the following method to apply the image. It updates the status to applying through the device twin,
calls the simulate apply image method, and updates status to applyComplete or applyFailed through
the twin depending on the results of the apply operation.
static async Task applyImage(Twin twin, byte[] imageData)
{
try
{
TwinCollection statusUpdate = new TwinCollection();
statusUpdate["status"] = "applying";
statusUpdate["startedApplyingImage"] = DateTime.Now;
await reportFwUpdateThroughTwin(twin, statusUpdate);
await simulateApplyImage(imageData);
12. Add the following method to sequence the firmware update operation from waiting to download the
image through applying the image to the device:
13. Add the following method to handle the updateFirmware direct method from the cloud. It extracts the
URL to the firmware update from the message payload and passes it to the doUpdate task, which it
starts on another threadpool thread. It then immediately returns the method response to the cloud.
14. Finally, add the following code to the Main method to open the connection to your IoT hub and initialize
the method listener:
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Exiting...");
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and shows you how to do
an end-to-end simulated firmware update. This pattern is used in the firmware update implementation for the
Raspberry Pi device implementation sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.5.22</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod;
import com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin;
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with
your IoT hub connection string you noted in the Create an IoT Hub section:
10. To implement a method that reads the reported properties from the device twin, add the following to the
App class:
public static void ShowReportedProperties()
{
try
{
DeviceTwin deviceTwins = DeviceTwin.createFromConnectionString(iotHubConnectionString);
DeviceTwinDevice twinDevice = new DeviceTwinDevice(deviceId);
while (!firmwareUpdated)
{
if (timeoutCycle > 5)
{
System.out.println("Operation timed out");
break;
}
Thread.sleep(1000);
deviceTwins.getTwin(twinDevice);
if(reportedProperties.contains("status=waiting"))
{
System.out.println("Waiting on device...");
}
else if(reportedProperties.contains("status=downloadComplete"))
{
System.out.println("Download complete, applying firmware...");
}
else if (reportedProperties.contains("status=applyComplete"))
{
System.out.println("Firmware applied");
System.out.println("Get reported properties from device twin");
System.out.println(twinDevice.reportedPropertiesToString());
firmwareUpdated = true;
}
else
{
timeoutCycle++;
}
}
} catch (Exception ex) {
System.out.println("Exception reading reported properties: " + ex.getMessage());
}
}
11. Modify the signature of the main method to throw the following exceptions:
12. To invoke the firmwareUpdate direct method on the simulated device, add the following code to the main
method:
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
try
{
String payload = "https://someurl";
if(result == null)
{
throw new IOException("Invoke direct method reboot returns null");
}
13. To poll the reported properties from the simulated device, add the following code to the main method:
ShowReportedProperties();
14. To enable you to stop the app, add the following code to the main method:
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;
8. Add the following class-level variables to the App class. Replace {yourdeviceconnectionstring} with
your device connection string you noted in the Create a Device Identity section:
9. To implement direct method functionality, provide callbacks by adding the following nested classes to the
App class:
10. To implement device twin functionality, provide callbacks by adding the following nested classes to the
App class:
11. To implement the firmware update, add the following nested class to the App class:
protected static class FirmwareUpdateThread implements Runnable {
public void run() {
try {
HashMap initialUpdate = new HashMap();
Property sentProperty = new Property("firmwareUpdate", initialUpdate);
Set<Property> sentPackage = new HashSet<Property>();
initialUpdate.put("status", "waiting");
initialUpdate.put("fwPackageUri", downloadURL);
initialUpdate.put("startedWaitingTime", LocalDateTime.now().toString());
sentPackage.add(sentProperty);
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
System.out.println("Download complete");
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
System.out.println("Apply complete");
client.sendReportedProperties(sentPackage);
Thread.sleep(5000);
client.sendReportedProperties(sentPackage);
}
catch (Exception ex) {
System.out.println("Exception in reboot thread: " + ex.getMessage());
}
}
}
12. Modify the signature of the main method to throw the following exceptions:
13. To initiate the direct methods and device twins routine, add the following code to the main method:
client = new DeviceClient(connString, protocol);
try
{
client.open();
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(),
null);
client.startDeviceTwin(new DeviceTwinStatusCallback(), null, new PropertyCallback(), null);
System.out.println("Client connected to IoT Hub. Waiting for firmwareUpdate direct method.");
}
catch (Exception e)
{
System.out.println("On exception, shutting down \n" + " Cause: " + e.getCause() + " \n" +
e.getMessage());
client.close();
System.out.println("Shutting down...");
}
14. To enable you to stop the app, add the following code to the end of the main method:
2. At a command prompt in the firmware-update folder, run the following command to invoke the
firmware update and query the device twins on your simulated device from your IoT hub:
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
3. You can see the simulated device responding to the direct method in the console.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Use device management to initiate a device
firmware update (Python/Python)
5/29/2018 • 9 min to read • Edit Online
In the Get started with device management tutorial, you saw how to use the device twin and direct methods
primitives to remotely reboot a device. This tutorial uses the same IoT Hub primitives and provides guidance and
shows you how to do an end-to-end simulated firmware update. This pattern is used in the firmware update
implementation for the Intel Edison device sample.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
import sys
import time
import iothub_service_client
from iothub_service_client import IoTHubDeviceTwin, IoTHubDeviceMethod, IoTHubError
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
METHOD_NAME = "firmwareUpdate"
METHOD_PAYLOAD = "{\"fwPackageUri\":\"test.com\"}"
TIMEOUT = 60
MESSAGE_COUNT = 5
4. Add the following function to call the direct method and display the value of the firmwareUpdate
reported property. Also add the main routine:
def iothub_firmware_sample_run():
try:
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
print ( "" )
print ( "Direct Method called." )
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
print ( "" )
print ( "Device Twin queried, press Ctrl-C to exit" )
while True:
twin_info = iothub_twin_method.get_twin(DEVICE_ID)
if "\"firmwareStatus\":\"standBy\"" in twin_info:
print ( "Waiting on device..." )
elif "\"firmwareStatus\":\"downloading\"" in twin_info:
print ( "Downloading firmware..." )
elif "\"firmwareStatus\":\"applying\"" in twin_info:
print ( "Download complete, applying firmware..." )
elif "\"firmwareStatus\":\"completed\"" in twin_info:
print ( "Firmware applied" )
print ( "" )
print ( "Get reported properties from device twin:" )
print ( twin_info )
break
else:
print ( "Unknown status" )
status_counter = 0
while status_counter <= MESSAGE_COUNT:
time.sleep(1)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub firmware update Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_firmware_sample_run()
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubError, DeviceMethodReturnValue
SEND_REPORTED_STATE_CONTEXT = 0
METHOD_CONTEXT = 0
MESSAGE_COUNT = 10
PROTOCOL = IoTHubTransportProvider.MQTT
CONNECTION_STRING = "{deviceConnectionString}"
CLIENT = IoTHubClient(CONNECTION_STRING, PROTOCOL)
4. Add the following functions that are used to provide reported properties updates and implement the
direct method:
device_method_return_value = DeviceMethodReturnValue()
device_method_return_value.response = "{ \"Response\": \"Firmware update started\" }"
device_method_return_value.status = 200
return device_method_return_value
5. Add the following functions that simulate downloading and applying the firmware image:
def simulate_download_image(image_url):
time.sleep(15)
print ( "Downloading image from: " + image_url )
simulate_apply_image(image_url)
def simulate_apply_image(image_url):
print ( "Applying image from: " + image_url )
simulate_complete_image()
def simulate_complete_image():
print ( "Image applied." )
6. Add the following function that initializes the device twin's reported properties and wait for the direct
method to be called. Also add the main routine:
def iothub_firmware_sample_run():
try:
CLIENT.set_device_method_callback(device_method_callback, METHOD_CONTEXT)
while True:
status_counter = 0
while status_counter <= MESSAGE_COUNT:
time.sleep(10)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Python firmware update sample..." )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_firmware_sample_run()
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
python dmpatterns_fwupdate_device.py
2. At another command prompt, run the following command to trigger the remote reboot and query for the
device twin to find the last reboot time.
python dmpatterns_fwupdate_service.py
3. You see the device response to the direct method in the console. Then note the change in reported
properties throughout the firmware update.
Next steps
In this tutorial, you used a direct method to trigger a remote firmware update on a device and used the reported
properties to follow the progress of the firmware update.
To learn how to extend your IoT solution and schedule method calls on multiple devices, see the Schedule and
broadcast jobs tutorial.
Schedule and broadcast jobs (Node)
5/29/2018 • 8 min to read • Edit Online
Azure IoT Hub is a fully managed service that enables a back-end app to create and track jobs that schedule
and update millions of devices. Jobs can be used for the following actions:
Update desired properties
Update tags
Invoke direct methods
Conceptually, a job wraps one of these actions and tracks the progress of execution against a set of devices,
which is defined by a device twin query. For example, a back-end app can use a job to invoke a reboot method
on 10,000 devices, specified by a device twin query and scheduled at a future time. That application can then
track progress as each of those devices receive and execute the reboot method.
Learn more about each of these capabilities in these articles:
Device twin and properties: Get started with device twins and Tutorial: How to use device twin properties
Direct methods: IoT Hub developer guide - direct methods and Tutorial: direct methods
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the
basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information
while naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on
your subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with
the IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the
device app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
npm init
2. At your command prompt in the simDevice folder, run the following command to install the azure-
iot-device Device SDK package and azure-iot-device-mqtt package:
3. Using a text editor, create a new simDevice.js file in the simDevice folder.
4. Add the following 'require' statements at the start of the simDevice.js file:
'use strict';
console.log('Locking Door!');
};
7. Add the following code to register the handler for the lockDoor method.
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client.');
} else {
console.log('Client connected to IoT Hub. Register handler for lockDoor direct method.');
client.onDeviceMethod('lockDoor', onLockDoor);
}
});
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement
retry policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
npm init
2. At your command prompt in the scheduleJobService folder, run the following command to install
the azure-iothub Device SDK package and azure-iot-device-mqtt package:
3. Using a text editor, create a new scheduleJobService.js file in the scheduleJobService folder.
4. Add the following 'require' statements at the start of the
dmpatterns_gscheduleJobServiceetstarted_service.js file:
'use strict';
5. Add the following variable declarations and replace the placeholder values:
6. Add the following function that is used to monitor the execution of the job:
7. Add the following code to schedule the job that calls the device method:
var methodParams = {
methodName: 'lockDoor',
payload: null,
responseTimeoutInSeconds: 15 // Timeout after 15 seconds if device is unable to process method
};
var twinPatch = {
etag: '*',
properties: {
desired: {
building: '43',
floor: 3
}
}
};
node simDevice.js
2. At the command prompt in the scheduleJobService folder, run the following command to trigger the
jobs to lock the door and update the twin
node scheduleJobService.js
3. You see the device response to the direct method in the console.
Next steps
In this tutorial, you used a job to schedule a direct method to a device and the update of the device twin's
properties.
To continue getting started with IoT Hub and device management patterns such as remote over the air
firmware update, see:
Tutorial: How to do a firmware update
To continue getting started with IoT Hub, see Getting started with Azure IoT Edge.
Schedule and broadcast jobs (.NET/Node.js)
5/29/2018 • 8 min to read • Edit Online
Use Azure IoT Hub to schedule and track jobs that update millions of devices. Use jobs to:
Update desired properties
Update tags
Invoke direct methods
A job wraps one of these actions and tracks the execution against a set of devices that is defined by a device twin
query. For example, a back-end app can use a job to invoke a direct method on 10,000 devices that reboots the
devices. You specify the set of devices with a device twin query and schedule the job to run at a future time. The
job tracks progress as each of the devices receive and execute the reboot direct method.
To learn more about each of these capabilities, see:
Device twin and properties: Get started with device twins and Tutorial: How to use device twin properties
Direct methods: IoT Hub developer guide - direct methods and Tutorial: Use direct methods
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
2. Then, run the following command to login to your hub. Substitute {iot hub connection string} with the
IoT Hub connection string you previously copied:
iothub-explorer login "{iot hub connection string}"
3. Finally, create a new device identity called myDeviceId with the command:
iothub-explorer create myDeviceId --connection-string
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
Make a note of the device connection string from the result. This device connection string is used by the device
app to connect to your IoT Hub as a device.
Refer to Getting started with IoT Hub to programmatically create device identities.
Schedule jobs for calling a direct method and sending device twin
updates
In this section, you create a .NET console app (using C#) that uses jobs to call the lockDoor direct method and
send desired property updates to multiple devices.
1. In Visual Studio, add a Visual C# Windows Classic Desktop project to the current solution by using the
Console Application project template. Name the project ScheduleJob.
2. In Solution Explorer, right-click the ScheduleJob project, and then click Manage NuGet Packages....
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This step downloads,
installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following using statement if not already present in the default statements.
using System.Threading;
using System.Threading.Tasks;
6. Add the following fields to the Program class. Replace the placeholder with the IoT Hub connection string
for the hub that you created in the previous section.
jobClient = JobClient.CreateFromConnectionString(connString);
StartMethodJob(methodJobId);
MonitorJob(methodJobId).Wait();
Console.WriteLine("Press ENTER to run the next job.");
Console.ReadLine();
StartTwinUpdateJob(twinUpdateJobId);
MonitorJob(twinUpdateJobId).Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
11. In the Solution Explorer, open the Set StartUp projects... and make sure the Action for ScheduleJob
project is Start. Build the solution.
2. At your command prompt in the simDevice folder, run the following command to install the azure-iot-
device and azure-iot-device-mqtt packages:
3. Using a text editor, create a new simDevice.js file in the simDevice folder.
4. Add the following 'require' statements at the start of the simDevice.js file:
'use strict';
5. Add a connectionString variable and use it to create a Client instance. Make sure to replace the
placeholders with values appropriate to your setup.
console.log('Locking Door!');
};
7. Add the following code to register the handler for the lockDoor method.
client.open(function(err) {
if (err) {
console.error('Could not connect to IotHub client.');
} else {
console.log('Client connected to IoT Hub. Waiting for lockDoor direct method.');
client.onDeviceMethod('lockDoor', onLockDoor);
}
});
node simDevice.js
2. Run the C# console app ScheduleJob by right-clicking on the ScheduleJob project, then selecting
Debug and Start new instance.
3. You see the output from both device and back-end apps.
Next steps
In this tutorial, you used a job to schedule a direct method to a device and the update of the device twin's
properties.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, read Tutorial: How to do a firmware update.
To learn about deploying AI to edge devices with Azure IoT Edge, see Getting started with IoT Edge.
Schedule and broadcast jobs (.NET/.NET)
5/29/2018 • 8 min to read • Edit Online
Use Azure IoT Hub to schedule and track jobs that update millions of devices. Use jobs to:
Update desired properties
Update tags
Invoke direct methods
A job wraps one of these actions and tracks the execution against a set of devices that is defined by a device twin
query. For example, a back-end app can use a job to invoke a direct method on 10,000 devices that reboots the
devices. You specify the set of devices with a device twin query and schedule the job to run at a future time. The
job tracks progress as each of the devices receive and execute the reboot direct method.
To learn more about each of these capabilities, see:
Device twin and properties: Get started with device twins and Tutorial: How to use device twin properties
Direct methods: IoT Hub developer guide - direct methods and Tutorial: Use direct methods
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If
your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
2. In Solution Explorer, right-click the SimulateDeviceMethods project, and then click Manage NuGet
Packages....
3. In the NuGet Package Manager window, select Browse and search for microsoft.azure.devices.client.
Select Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use. This
procedure downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its
dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
5. Add the following fields to the Program class. Replace the placeholder value with the device connection
string that you noted in the previous section:
7. Add the following to implement the device twins listener on the device:
8. Finally, add the following code to the Main method to open the connection to your IoT hub and initialize
the method listener:
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Waiting for direct method call and device twin update\n Press enter to exit.");
Console.ReadLine();
Console.WriteLine("Exiting...");
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as connection retry), as suggested in the MSDN article Transient Fault Handling.
Schedule jobs for calling a direct method and sending device twin
updates
In this section, you create a .NET console app (using C#) that uses jobs to call the LockDoor direct method and
send desired property updates to multiple devices.
1. In Visual Studio, add a Visual C# Windows Classic Desktop project to the current solution by using the
Console Application project template. Name the project ScheduleJob.
2. In Solution Explorer, right-click the ScheduleJob project, and then click Manage NuGet Packages....
3. In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select
Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This step downloads,
installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.
4. Add the following using statements at the top of the Program.cs file:
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
5. Add the following using statement if not already present in the default statements.
using System.Threading;
using System.Threading.Tasks;
6. Add the following fields to the Program class. Replace the placeholders with the IoT Hub connection string
for the hub that you created in the previous section and the name of your device.
twin.Properties.Desired["LocationUpdate"] = DateTime.UtcNow;
jobClient = JobClient.CreateFromConnectionString(connString);
StartMethodJob(methodJobId);
MonitorJob(methodJobId).Wait();
Console.WriteLine("Press ENTER to run the next job.");
Console.ReadLine();
StartTwinUpdateJob(twinUpdateJobId);
MonitorJob(twinUpdateJobId).Wait();
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
Use Azure IoT Hub to schedule and track jobs that update millions of devices. Use jobs to:
Update desired properties
Update tags
Invoke direct methods
A job wraps one of these actions and tracks the execution against a set of devices. A device twin query defines the
set of devices the job executes against. For example, a back-end app can use a job to invoke a direct method on
10,000 devices that reboots the devices. You specify the set of devices with a device twin query and schedule the
job to run at a future time. The job tracks progress as each of the devices receive and execute the reboot direct
method.
To learn more about each of these capabilities, see:
Device twin and properties: Get started with device twins
Direct methods: IoT Hub developer guide - direct methods and Tutorial: Use direct methods
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
NOTE
The article Azure IoT SDKs provides information about the Azure IoT SDKs that you can use to build both device and back-
end apps.
Prerequisites
To complete this tutorial, you need:
The latest Java SE Development Kit 8
Maven 3
An active Azure account. (If you don't have an account, you can create a free account in just a couple of
minutes.)
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If
your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
You can also use the IoT extension for Azure CLI 2.0 tool to add a device to your IoT hub.
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
<type>jar</type>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
5. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice;
import com.microsoft.azure.sdk.iot.service.devicetwin.Pair;
import com.microsoft.azure.sdk.iot.service.devicetwin.Query;
import com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery;
import com.microsoft.azure.sdk.iot.service.jobs.JobClient;
import com.microsoft.azure.sdk.iot.service.jobs.JobResult;
import com.microsoft.azure.sdk.iot.service.jobs.JobStatus;
import java.util.Date;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
9. Add the following class-level variables to the App class. Replace {youriothubconnectionstring} with your
IoT hub connection string you noted in the Create an IoT Hub section:
10. Add the following method to the App class to schedule a job to update the Building and Floor desired
properties in the device twin:
private static JobResult scheduleJobSetDesiredProperties(JobClient jobClient, String jobId) {
DeviceTwinDevice twin = new DeviceTwinDevice(deviceId);
Set<Pair> desiredProperties = new HashSet<Pair>();
desiredProperties.add(new Pair("Building", 43));
desiredProperties.add(new Pair("Floor", 3));
twin.setDesiredProperties(desiredProperties);
// Optimistic concurrency control
twin.setETag("*");
11. To schedule a job to call the lockDoor method, add the following method to the App class:
12. To monitor a job, add the following method to the App class:
private static void monitorJob(JobClient jobClient, String jobId) {
try {
JobResult jobResult = jobClient.getJob(jobId);
if(jobResult == null)
{
System.out.println("No JobResult for: " + jobId);
return;
}
// Check the job result until it's completed
while(jobResult.getJobStatus() != JobStatus.completed)
{
Thread.sleep(100);
jobResult = jobClient.getJob(jobId);
System.out.println("Status " + jobResult.getJobStatus() + " for job " + jobId);
}
System.out.println("Final status " + jobResult.getJobStatus() + " for job " + jobId);
} catch (Exception e) {
System.out.println("Exception monitoring job: " + jobId);
System.out.println(e.getMessage());
return;
}
}
13. To query for the details of the jobs you ran, add the following method:
14. Update the main method signature to include the following throws clause:
15. To run and monitor two jobs sequentially, add the following code to the main method:
// Record the start time
String start = Instant.now().toString();
// Create JobClient
JobClient jobClient = JobClient.createFromConnectionString(iotHubConnectionString);
System.out.println("JobClient created with success");
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.32</version>
</dependency>
NOTE
You can check for the latest version of iot-device-client using Maven search.
4. Add the following build node after the dependencies node. This configuration instructs Maven to use
Java 1.8 to build the app:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
import com.microsoft.azure.sdk.iot.device.*;
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
8. Add the following class-level variables to the App class. Replacing {youriothubname} with your IoT hub
name, and {yourdevicekey} with the device key value you generated in the Create a device identity section:
This sample app uses the protocol variable when it instantiates a DeviceClient object.
9. To print device twin notifications to the console, add the following nested class to the App class:
10. To print direct method notifications to the console, add the following nested class to the App class:
11. To handle direct method calls from IoT Hub, add the following nested class to the App class:
// Handler for direct method calls from IoT Hub
protected static class DirectMethodCallback
implements DeviceMethodCallback {
@Override
public DeviceMethodData call(String methodName, Object methodData, Object context) {
DeviceMethodData deviceMethodData;
switch (methodName) {
case "lockDoor": {
System.out.println("Executing direct method: " + methodName);
deviceMethodData = new DeviceMethodData(METHOD_SUCCESS, "Executed direct method " +
methodName);
break;
}
default: {
deviceMethodData = new DeviceMethodData(METHOD_NOT_DEFINED, "Not defined direct method " +
methodName);
}
}
// Notify IoT Hub of result
return deviceMethodData;
}
}
12. Update the main method signature to include the following throws clause:
14. To start the device client services, add the following code to the main method:
try {
// Open the DeviceClient
// Start the device twin services
// Subscribe to direct method calls
client.open();
client.startDeviceTwin(new DeviceTwinStatusCallBack(), null, dataCollector, null);
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(),
null);
} catch (Exception e) {
System.out.println("Exception, shutting down \n" + " Cause: " + e.getCause() + " \n" +
e.getMessage());
dataCollector.clean();
client.closeNow();
System.out.println("Shutting down...");
}
15. To wait for the user to press the Enter key before shutting down, add the following code to the end of the
main method:
3. The device app handles the desired property change and the direct method call:
Next steps
In this tutorial, you configured a new IoT hub in the Azure portal, and then created a device identity in the IoT
hub's identity registry. You created a back-end app to run two jobs. The first job set desired property values, and
the second job called a direct method.
Use the following resources to learn how to:
Send telemetry from devices with the Get started with IoT Hub tutorial.
Control devices interactively (such as turning on a fan from a user-controlled app) with the Use direct methods
tutorial.
Schedule and broadcast jobs (Python)
5/29/2018 • 9 min to read • Edit Online
Azure IoT Hub is a fully managed service that enables a back-end app to create and track jobs that schedule and
update millions of devices. Jobs can be used for the following actions:
Update desired properties
Update tags
Invoke direct methods
Conceptually, a job wraps one of these actions and tracks the progress of execution against a set of devices, which
is defined by a device twin query. For example, a back-end app can use a job to invoke a reboot method on 10,000
devices, specified by a device twin query and scheduled at a future time. That application can then track progress
as each of those devices receive and execute the reboot method.
Learn more about each of these capabilities in these articles:
Device twin and properties: Get started with device twins and Tutorial: How to use device twin properties
Direct methods: IoT Hub developer guide - direct methods and Tutorial: direct methods
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If
your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
2. Using a text editor, create a new simDevice.py file in your working directory.
3. Add the following import statements and variables at the start of the simDevice.py file. Replace
deviceConnectionString with the connection string of the device you created above:
import time
import sys
import iothub_client
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubError, DeviceMethodReturnValue
METHOD_CONTEXT = 0
TWIN_CONTEXT = 0
WAIT_COUNT = 10
PROTOCOL = IoTHubTransportProvider.MQTT
CONNECTION_STRING = "{deviceConnectionString}"
device_method_return_value = DeviceMethodReturnValue()
device_method_return_value.response = "{ \"Response\": \"lockDoor called successfully\" }"
device_method_return_value.status = 200
return device_method_return_value
6. Add the following code to register the handler for the lockDoor method. Also include the main routine:
def iothub_jobs_sample_run():
try:
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
client.set_device_method_callback(device_method_callback, METHOD_CONTEXT)
client.set_device_twin_callback(device_twin_callback, TWIN_CONTEXT)
while True:
status_counter = 0
while status_counter <= WAIT_COUNT:
time.sleep(10)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub Python jobs sample..." )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_jobs_sample_run()
NOTE
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as an exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
Schedule jobs for calling a direct method and updating a device twin's
properties
In this section, you create a Python console app that initiates a remote lockDoor on a device using a direct
method and update the device twin's properties.
1. At your command prompt, run the following command to install the azure-iot-service-client package:
2. Using a text editor, create a new scheduleJobService.py file in your working directory.
3. Add the following import statements and variables at the start of the scheduleJobService.py file:
import sys
import time
import threading
import uuid
import iothub_service_client
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
from iothub_service_client import IoTHubDeviceTwin, IoTHubDeviceMethod, IoTHubError
CONNECTION_STRING = "{IoTHubConnectionString}"
DEVICE_ID = "{deviceId}"
METHOD_NAME = "lockDoor"
METHOD_PAYLOAD = "{\"lockTime\":\"10m\"}"
UPDATE_JSON = "{\"properties\":{\"desired\":{\"building\":43,\"floor\":3}}}"
TIMEOUT = 60
WAIT_COUNT = 5
def query_condition(device_id):
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
number_of_devices = 10
dev_list = iothub_registry_manager.get_device_list(number_of_devices)
5. Add the following methods to run the jobs that call the direct method and device twin:
def device_method_job(job_id, device_id, wait_time, execution_time):
print ( "" )
print ( "Scheduling job: " + str(job_id) )
time.sleep(wait_time)
if query_condition(device_id):
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
print ( "" )
print ( "Direct method " + METHOD_NAME + " called." )
if query_condition(device_id):
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
print ( "" )
print ( "Device twin updated." )
6. Add the following code to schedule the jobs and update job status. Also include the main routine:
def iothub_jobs_sample_run():
try:
method_thr_id = uuid.uuid4()
method_thr = threading.Thread(target=device_method_job, args=(method_thr_id, DEVICE_ID, 20,
TIMEOUT), kwargs={})
method_thr.start()
print ( "" )
print ( "Direct method called with Job Id: " + str(method_thr_id) )
twin_thr_id = uuid.uuid4()
twin_thr = threading.Thread(target=device_twin_job, args=(twin_thr_id, DEVICE_ID, 10, TIMEOUT),
kwargs={})
twin_thr.start()
print ( "" )
print ( "Device twin called with Job Id: " + str(twin_thr_id) )
while True:
print ( "" )
if method_thr.is_alive():
print ( "...job " + str(method_thr_id) + " still running." )
else:
print ( "...job " + str(method_thr_id) + " complete." )
if twin_thr.is_alive():
print ( "...job " + str(twin_thr_id) + " still running." )
else:
print ( "...job " + str(twin_thr_id) + " complete." )
status_counter = 0
while status_counter <= WAIT_COUNT:
time.sleep(1)
status_counter += 1
if __name__ == '__main__':
print ( "Starting the IoT Hub jobs Python sample..." )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_jobs_sample_run()
python simDevice.py
2. At another command prompt in your working directory, run the following command to trigger the jobs to
lock the door and update the twin:
python scheduleJobService.py
3. You see the device responses to the direct method and device twins update in the console.
Next steps
In this tutorial, you used a job to schedule a direct method to a device and the update of the device twin's
properties.
To continue getting started with IoT Hub and device management patterns such as remote over the air firmware
update, see:
Tutorial: How to do a firmware update
To continue getting started with IoT Hub, see Getting started with Azure IoT Edge.
Upload files from your device to the cloud with IoT
Hub using .NET
6/6/2018 • 5 min to read • Edit Online
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub. It shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub and Send Cloud-to-Device messages with IoT Hub tutorials show the basic device-
to-cloud and cloud-to-device messaging functionality of IoT Hub. The Process Device-to-Cloud messages
tutorial describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some
scenarios you cannot easily map the data your devices send into the relatively small device-to-cloud messages
that IoT Hub accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upload files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two .NET console apps:
SimulatedDevice, a modified version of the app created in the Send Cloud-to-Device messages with IoT
Hub tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
ReadFileUploadNotification, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, Java, and Javascript) through Azure IoT device SDKs.
Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.
2. Right-click on the image, and then click Properties. Make sure that Copy to Output Directory is set to
Copy always.
3. In the Program.cs file, add the following statements at the top of the file:
using System.IO;
watch.Stop();
Console.WriteLine("Time to upload file: {0}ms\n", watch.ElapsedMilliseconds);
}
The UploadToBlobAsync method takes in the file name and stream source of the file to be uploaded and
handles the upload to storage. The console app displays the time it takes to upload the file.
5. Add the following method in the Main method, right before the Console.ReadLine() line:
SendToBlobAsync();
NOTE
For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry
policies (such as exponential backoff ), as suggested in the MSDN article Transient Fault Handling.
using Microsoft.Azure.Devices;
5. Add the following fields to the Program class. Substitute the placeholder value with the IoT hub
connection string from [Get started with IoT Hub]:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received file upload noticiation: {0}", string.Join(", ",
fileUploadNotification.BlobName));
Console.ResetColor();
await notificationReceiver.CompleteAsync(fileUploadNotification);
}
}
Note this receive pattern is the same one used to receive cloud-to-device messages from the device app.
7. Finally, add the following lines to the Main method:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from
devices. You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Upload files from your device to the cloud with IoT
Hub
6/6/2018 • 6 min to read • Edit Online
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The tutorial shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub and Send Cloud-to-Device messages with IoT Hub tutorials show the basic device-
to-cloud and cloud-to-device messaging functionality of IoT Hub. The Process Device-to-Cloud messages tutorial
describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some scenarios
you cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub
accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Java console apps:
simulated-device, a modified version of the app created in the [Send Cloud-to-Device messages with IoT
Hub] tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
read-file-upload-notification, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, and Javascript) through Azure IoT device SDKs.
Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.
4. To process file upload status callback messages, add the following nested class to the App class:
5. To upload images to IoT Hub, add the following method to the App class to upload images to IoT Hub:
// Use IoT Hub to upload a file asynchronously to Azure blob storage.
private static void uploadFile(String fullFileName) throws FileNotFoundException, IOException
{
File file = new File(fullFileName);
InputStream inputStream = new FileInputStream(file);
long streamLength = file.length();
6. Modify the main method to call the uploadFile method as shown in the following snippet:
client.open();
try
{
// Get the filename and start the upload.
String fullFileName = System.getProperty("user.dir") + File.separator + fileName;
uploadFile(fullFileName);
System.out.println("File upload started with success");
}
catch (Exception e)
{
System.out.println("Exception uploading file: " + e.getCause() + " \nERROR: " + e.getMessage());
}
7. Use the following command to build the simulated-device app and check for errors:
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.7.23</version>
</dependency>
NOTE
You can check for the latest version of iot-service-client using Maven search.
import com.microsoft.azure.sdk.iot.service.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
private static final String connectionString = "{Your IoT Hub connection string}";
private static final IotHubServiceClientProtocol protocol = IotHubServiceClientProtocol.AMQPS;
private static FileUploadNotificationReceiver fileUploadNotificationReceiver = null;
8. To print information about the file upload to the console, add the following nested class to the App class:
9. To start the thread that listens for file upload notifications, add the following code to the main method:
public static void main(String[] args) throws IOException, URISyntaxException, Exception {
ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, protocol);
if (serviceClient != null) {
serviceClient.open();
The following screenshot shows the output from the simulated-device app:
The following screenshot shows the output from the read-file-upload-notification app:
You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from
devices. You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Simulating a device with IoT Edge
Upload files from your device to the cloud with IoT
Hub
5/29/2018 • 5 min to read • Edit Online
This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to
use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The tutorial shows you how to:
Securely provide a device with an Azure blob URI for uploading a file.
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Get started with IoT Hub tutorial demonstrates the basic device-to-cloud messaging functionality of IoT Hub.
However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-
cloud messages that IoT Hub accepts. For example:
Large files that contain images
Videos
Vibration data sampled at high frequency
Some form of preprocessed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack.
When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Node.js console apps:
SimulatedDevice.js, which uploads a file to storage using a SAS URI provided by your IoT hub.
ReadFileUploadNotification.js, which receives file upload notifications from your IoT hub.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, Javascript, Python, and Java) through Azure IoT
device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure
IoT Hub.
npm init
2. At your command prompt in the simulateddevice folder, run the following command to install the azure-
iot-device Device SDK package and azure-iot-device-mqtt package:
'use strict';
var fs = require('fs');
var mqtt = require('azure-iot-device-mqtt').Mqtt;
var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
NOTE
For the sake of simplicity the connection string is included in the code: this is not a recommended practice and
depending on your use-case and architecture you may want to consider more secure ways of storing this secret.
7. Create a callback and use the uploadToBlob function to upload the file.
npm init
2. At your command prompt in the fileuploadnotification folder, run the following command to install the
azure-iothub SDK package:
NOTE
For the sake of simplicity the connection string is included in the code: this is not a recommended practice and
depending on your use-case and architecture you may want to consider more secure ways of storing this secret.
7. Open the client and use the getFileNotificationReceiver function to receive status updates.
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
serviceClient.getFileNotificationReceiver(function receiveFileUploadNotification(err, receiver){
if (err) {
console.error('error getting the file notification receiver: ' + err.toString());
} else {
receiver.on('message', function (msg) {
console.log('File upload from device:')
console.log(msg.getData().toString('utf-8'));
});
}
});
}
});
node FileUploadNotification.js
node SimulatedDevice.js
The following screenshot shows the output from the SimulatedDevice app:
The following screenshot shows the output from the FileUploadNotification app:
You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from
devices. You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
Upload files from your device to the cloud with IoT
Hub
5/29/2018 • 6 min to read • Edit Online
This tutorial follows how to use the file upload capabilities of IoT Hub to upload a file to Azure blob storage. The
tutorial shows you how to:
Securely provide a storage container for uploading a file.
Use the Python client to upload a file through your IoT hub.
The Get started with IoT Hub tutorial demonstrates the basic device-to-cloud messaging functionality of IoT
Hub. However, in some scenarios you cannot easily map the data your devices send into the relatively small
device-to-cloud messages that IoT Hub accepts. When you need to upland files from a device, you can still use
the security and reliability of IoT Hub.
NOTE
IoT Hub Python SDK currently only supports uploading character-based files such as .txt files.
At the end of this tutorial you run the Python console app:
FileUpload.py, which uploads a file to storage using the Python Device SDK.
NOTE
IoT Hub supports many device platforms and languages (including C, .NET, Javascript, Python, and Java) through Azure IoT
device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure
IoT Hub.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
5. In the device list, click the newly created device and copy the Connection string---primary key to use
later.
NOTE
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and
keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device.
If your application needs to store other device-specific metadata, it should use an application-specific store. For more
information, see IoT Hub developer guide.
import time
import sys
import iothub_client
import os
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider,
IoTHubClientResult, IoTHubError
5. Add the following code to connect the client and upload the file. Also include the main routine:
def iothub_file_upload_sample_run():
try:
print ( "IoT Hub file upload sample, press Ctrl-C to exit" )
f = open(PATHTOFILE, "r")
content = f.read()
print ( "" )
print ( "File upload initiated..." )
while True:
time.sleep(30)
if __name__ == '__main__':
print ( "Simulating a file upload using the Azure IoT Hub Device SDK for Python" )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_file_upload_sample_run()
NOTE
IoT Hub Python SDK currently only supports uploading character-based files such as .txt files.
python FileUpload.py
2. The following screenshot shows the output from the FileUpload app:
3. You can use the portal to view the uploaded file in the storage container you configured:
Next steps
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from
devices. You can continue to explore IoT hub features and scenarios with the following articles:
Create an IoT hub programmatically
Introduction to C SDK
Azure IoT SDKs
Create an IoT hub using the Azure portal
5/29/2018 • 5 min to read • Edit Online
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming
it.
Shared access policies: These policies define the permissions for devices and services to connect to IoT Hub.
You can access these policies by clicking Shared access policies under General. In this blade, you can either
modify existing policies or add a new policy.
Create a policy
Click Add to open a blade. Here you can enter the new policy name and the permissions that you want to
associate with this policy, as shown in the following figure:
There are several permissions that can be associated with these shared policies. The Registry read and
Registry write policies grant read and write access rights to the identity registry. Choosing the write
option automatically chooses the read option.
The Service connect policy grants permission to access service endpoints such as Receive device-to-
cloud. The Device connect policy grants permissions for sending and receiving messages using the IoT
Hub device-side endpoints.
Click Create to add this newly created policy to the existing list.
Endpoints
Click Endpoints to display a list of endpoints for the IoT hub that you are modifying. There are two types of
endpoints: endpoints that are built into the IoT hub, and endpoints that you add to the IoT hub after its creation.
Built-in endpoints
There are two built-in endpoints: Cloud to device feedback and Events.
Cloud to device feedback settings: This setting has two subsettings: Cloud to Device TTL (time-to-live)
and Retention time (in hours) for the messages. When your first create an IoT hub, both these settings have
the default value of one hour. To adjust these settings, use the sliders or type the values.
Events settings: This setting has several subsettings, some of which are read-only. The following list
describes these settings:
Partitions: A default value is set when the IoT hub is created. You can change the number of
partitions through this setting.
Event Hub-compatible name and endpoint: When the IoT hub is created, an Event Hub is
created internally that you may need access to under certain circumstances. You cannot customize
the Event Hub-compatible name and endpoint values but you can copy them by clicking Copy.
Retention Time: Set to one day by default but you can change it using the drop-down list. This
value is in days for the device-to-cloud setting.
Consumer Groups: Consumer groups enable multiple readers to read messages independently
from the IoT hub. Every IoT hub is created with a default consumer group. However, you can add
or delete consumer groups to your IoT hubs using this setting.
NOTE
The default consumer group cannot be edited or deleted.
Custom endpoints
You can add custom endpoints on your IoT hub using the portal. From the Endpoints blade, click Add at the
top to open the Add endpoint blade. Enter the required information, then click OK. Your custom endpoint is
now listed in the main Endpoints blade.
You can read more about custom endpoints in Reference - IoT hub endpoints.
Routes
Click Routes to manage how IoT Hub dispatches your device-to-cloud messages.
You can add routes to your IoT hub by clicking Add at the top of the Routes* blade, entering the required
information, and clicking OK. Your route is then listed in the main Routes blade. You can edit a route by clicking
it in the list of routes. To enable a route, click it in the list of routes and set the Enabled toggle to Off. To save the
change, click OK at the bottom of the blade.
Next steps
Follow these links to learn more about managing Azure IoT Hub:
Bulk manage IoT devices
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Secure your IoT solution from the ground up
Create an IoT hub using the New-AzureRmIotHub
cmdlet
5/29/2018 • 2 min to read • Edit Online
Introduction
You can use Azure PowerShell cmdlets to create and manage Azure IoT hubs. This tutorial shows you how to
create an IoT hub with PowerShell.
NOTE
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and classic.
This article covers using the Azure Resource Manager deployment model.
Connect-AzureRmAccount
If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure subscriptions
associated with your credentials. Use the following command to list the Azure subscriptions available for you to
use:
Get-AzureRMSubscription
Use the following command to select subscription that you want to use to run the commands to create your IoT
hub. You can use either the subscription name or ID from the output of the previous command:
Select-AzureRMSubscription `
-SubscriptionName "{your subscription name}"
((Get-AzureRmResourceProvider `
-ProviderNamespace Microsoft.Devices).ResourceTypes `
| Where-Object ResourceTypeName -eq IoTHubs).Locations
To create a resource group for your IoT hub in one of the supported locations for IoT Hub, use the following
command. This example creates a resource group called MyIoTRG1 in the East US region:
New-AzureRmIotHub `
-ResourceGroupName MyIoTRG1 `
-Name MyTestIoTHub `
-SkuName S1 -Units 1 `
-Location "East US"
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming
it.
You can list all the IoT hubs in your subscription using the following command:
Get-AzureRmIotHub
The previous example adds an S1 Standard IoT Hub for which you are billed. You can delete the IoT hub using the
following command:
Remove-AzureRmIotHub `
-ResourceGroupName MyIoTRG1 `
-Name MyTestIoTHub
Alternatively, you can remove a resource group and all the resources it contains using the following command:
Next steps
Now you have deployed an IoT hub using a PowerShell cmdlet, you may want to explore further:
Discover other PowerShell cmdlets for working with your IoT hub.
Read about the capabilities of the IoT Hub resource provider REST API.
To learn more about developing for IoT Hub, see the following articles:
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Create an IoT hub using the Azure CLI 2.0
5/29/2018 • 2 min to read • Edit Online
Introduction
You can use Azure CLI 2.0 (az.py) to create and manage Azure IoT hubs programmatically. This article shows you
how to use the Azure CLI 2.0 (az.py) to create an IoT hub.
You can complete the task using one of the following CLI versions:
Azure CLI (azure.js) – the CLI for the classic and resource management deployment models.
Azure CLI 2.0 (az.py) - the next generation CLI for the resource management deployment model as described
in this article.
To complete this tutorial, you need the following:
An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
Azure CLI 2.0.
az login
Follow the instructions to authenticate using the code and sign in to your Azure account through a web
browser.
2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure accounts
associated with your credentials. Use the following command to list the Azure accounts available for you to
use:
az account list
Use the following command to select subscription that you want to use to run the commands to create
your IoT hub. You can use either the subscription name or ID from the output of the previous command:
2. Run the following command to create an IoT hub in your resource group, using a globally unique name for
your IoT hub:
az iot hub create --name {your iot hub name} --resource-group {your resource group name} --sku S1
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
NOTE
The previous command creates an IoT hub in the S1 pricing tier for which you are billed. For more information, see Azure
IoT Hub pricing.
az iot hub delete --name {your iot hub name} --resource-group {your resource group name}
To delete a resource group and all its resources, run the following command:
Next steps
To learn more about developing for IoT Hub, see the following articles:
IoT Hub developer guide
To further explore the capabilities of IoT Hub, see:
Using the Azure portal to manage IoT Hub
Create an IoT hub using the Azure CLI
5/29/2018 • 2 min to read • Edit Online
Introduction
You can use Azure CLI (azure.js) to create and manage Azure IoT hubs programmatically. This article shows you
how to use the Azure CLI (azure.js) to create an IoT hub.
You can complete the task using one of the following CLI versions:
Azure CLI (azure.js) – the CLI for the classic and resource management deployment models as described in
this article.
Azure CLI 2.0 (az.py) - the next generation CLI for the resource management deployment model.
To complete this tutorial, you need the following:
An active Azure account. If you don't have an account, you can create a free account in just a couple of
minutes.
Azure CLI 0.10.4 or later. If you already have the Azure CLI installed, you can validate the current version at
the command prompt with the following command:
azure --version
NOTE
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and classic.
The Azure CLI must be in Azure Resource Manager mode:
azure login
To set the subscription context under which you want to run the rest of the commands use:
TIP
The article Use the Azure CLI to manage Azure resources and resource groups provides more information about how to
use the Azure CLI to manage Azure resources.
resource-group. The resource group name. The format is case insensitive alphanumeric, underscore, and
hyphen, 1-64 length.
name. The name of the IoT hub to be created. The format is case insensitive alphanumeric and hyphen, 3-50
length.
location. The location (azure region/datacenter) to provision the IoT hub.
sku-name. The name of the sku, one of: [F1, S1, S2, S3]. For details about each sku, see Azure IoT Hub
pricing. Currently, basic tiers are only available through the portal.
units. The number of provisioned units. For details about unit limits, see Azure IoT Hub pricing.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming
it.
To see all the parameters available for creation, you can use the help command in command prompt:
Quick example: To create an IoT Hub called exampleIoTHubName in the resource group
exampleResourceGroup, run the following command:
NOTE
This Azure CLI command creates an S1 Standard IoT Hub for which you are billed. You can delete the IoT hub
exampleIoTHubName using following command:
Next steps
To learn more about developing for IoT Hub, see the following article:
IoT SDKs
To further explore the capabilities of IoT Hub, see:
Using the Azure portal to manage IoT Hub
Create an IoT hub using the resource provider REST
API (.NET)
5/29/2018 • 7 min to read • Edit Online
You can use the IoT Hub resource provider REST API to create and manage Azure IoT hubs programmatically.
This tutorial shows you how to use the IoT Hub resource provider REST API to create an IoT hub from a C#
program.
NOTE
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and classic.
This article covers using the Azure Resource Manager deployment model.
Connect-AzureRmAccount
2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure
subscriptions associated with your credentials. Use the following command to list the Azure subscriptions
available for you to use:
Get-AzureRMSubscription
Use the following command to select subscription that you want to use to run the commands to manage
your IoT hub. You can use either the subscription name or ID from the output of the previous command:
Select-AzureRMSubscription `
-SubscriptionName "{your subscription name}"
3. Make a note of your TenantId and SubscriptionId. You need them later.
4. Create a new Azure Active Directory application using the following command, replacing the place holders:
{Display name}: a display name for your application such as MySampleApp
{Home page URL }: the URL of the home page of your app such as http://mysampleapp/home. This
URL does not need to point to a real application.
{Application identifier}: A unique identifier such as http://mysampleapp. This URL does not need
to point to a real application.
{Password}: A password that you use to authenticate with your app.
5. Make a note of the ApplicationId of the application you created. You need this later.
6. Create a new service principal using the following command, replacing {MyApplicationId} with the
ApplicationId from the previous step:
7. Set up a role assignment using the following command, replacing {MyApplicationId} with your
ApplicationId.
You have now finished creating the Azure AD application that enables you to authenticate from your custom C#
application. You need the following values later in this tutorial:
TenantId
SubscriptionId
ApplicationId
Password
6. In Program.cs, add the following static variables replacing the placeholder values. You made a note of
ApplicationId, SubscriptionId, TenantId, and Password earlier in this tutorial. Resource group name
is the name of the resource group you use when you create the IoT hub. You can use a pre-existing or a
new resource group. IoT Hub name is the name of the IoT Hub you create, such as MyIoTHub. The
name of your IoT hub must be globally unique. Deployment name is a name for the deployment, such as
Deployment_01.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
if (token == null)
{
Console.WriteLine("Failed to obtain the token");
return;
}
2. Create a ResourceManagementClient object that uses the token by adding the following code to the end
of the Main method:
var creds = new TokenCredentials(token.AccessToken);
var client = new ResourceManagementClient(creds);
client.SubscriptionId = subscriptionId;
3. Create, or obtain a reference to, the resource group you are using:
2. Add the following code to the CreateIoTHub method. This code creates an HttpClient object with the
authentication token in the headers:
3. Add the following code to the CreateIoTHub method. This code describes the IoT hub to create and
generates a JSON representation. For the current list of locations that support IoT Hub see Azure Status:
4. Add the following code to the CreateIoTHub method. This code submits the REST request to Azure. The
code then checks the response and retrieves the URL you can use to monitor the state of the deployment
task:
var content = new StringContent(JsonConvert.SerializeObject(description), Encoding.UTF8,
"application/json");
var requestUri =
string.Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.de
vices/IotHubs/{2}?api-version=2016-02-03", subscriptionId, rgName, iotHubName);
var result = client.PutAsync(requestUri, content).Result;
if (!result.IsSuccessStatusCode)
{
Console.WriteLine("Failed {0}", result.Content.ReadAsStringAsync().Result);
return;
}
5. Add the following code to the end of the CreateIoTHub method. This code uses the asyncStatusUri
address retrieved in the previous step to wait for the deployment to complete:
string body;
do
{
Thread.Sleep(10000);
HttpResponseMessage deploymentstatus = client.GetAsync(asyncStatusUri).Result;
body = deploymentstatus.Content.ReadAsStringAsync().Result;
} while (body == "{\"status\":\"Running\"}");
6. Add the following code to the end of the CreateIoTHub method. This code retrieves the keys of the IoT
hub you created and prints them to the console:
var listKeysUri =
string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.De
vices/IotHubs/{2}/IoTHubKeys/listkeys?api-version=2016-02-03", subscriptionId, rgName, iotHubName);
var keysresults = client.PostAsync(listKeysUri, null).Result;
CreateIoTHub(token.AccessToken);
Console.ReadLine();
Next steps
Now you have deployed an IoT hub using the resource provider REST API, you may want to explore further:
Read about the capabilities of the IoT Hub resource provider REST API.
Read Azure Resource Manager overview to learn more about the capabilities of Azure Resource Manager.
To learn more about developing for IoT Hub, see the following articles:
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Create an IoT hub using Azure Resource Manager
template (PowerShell)
5/29/2018 • 3 min to read • Edit Online
You can use Azure Resource Manager to create and manage Azure IoT hubs programmatically. This tutorial
shows you how to use an Azure Resource Manager template to create an IoT hub with PowerShell.
NOTE
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and
classic. This article covers using the Azure Resource Manager deployment model.
TIP
The article Using Azure PowerShell with Azure Resource Manager provides more information about how to use
PowerShell and Azure Resource Manager templates to create Azure resources.
Connect-AzureRmAccount
If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure subscriptions
associated with your credentials. Use the following command to list the Azure subscriptions available for you to
use:
Get-AzureRMSubscription
Use the following command to select subscription that you want to use to run the commands to create your IoT
hub. You can use either the subscription name or ID from the output of the previous command:
Select-AzureRMSubscription `
-SubscriptionName "{your subscription name}"
You can use the following commands to discover where you can deploy an IoT hub and the currently supported
API versions:
((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Devices).ResourceTypes | Where-Object
ResourceTypeName -eq IoTHubs).Locations
((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Devices).ResourceTypes | Where-Object
ResourceTypeName -eq IoTHubs).ApiVersions
Create a resource group to contain your IoT hub using the following command in one of the supported
locations for IoT Hub. This example creates a resource group called MyIoTRG1:
2. Save the Azure Resource Manager template file on your local machine. This example assumes you save
it in a folder called c:\templates.
3. Run the following command to deploy your new IoT hub, passing the name of your IoT hub as a
parameter. In this example, the name of the IoT hub is abcmyiothub . The name of your IoT hub must be
globally unique:
4. The output displays the keys for the IoT hub you created.
5. To verify your application added the new IoT hub, visit the Azure portal and view your list of resources.
Alternatively, use the Get-AzureRmResource PowerShell cmdlet.
NOTE
This example application adds an S1 Standard IoT Hub for which you are billed. You can delete the IoT hub through the
Azure portal or by using the Remove-AzureRmResource PowerShell cmdlet when you are finished.
Next steps
Now you have deployed an IoT hub using an Azure Resource Manager template with PowerShell, you may
want to explore further:
Read about the capabilities of the IoT Hub resource provider REST API.
Read Azure Resource Manager overview to learn more about the capabilities of Azure Resource Manager.
To learn more about developing for IoT Hub, see the following articles:
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Create an IoT hub using Azure Resource Manager
template (.NET)
5/29/2018 • 7 min to read • Edit Online
You can use Azure Resource Manager to create and manage Azure IoT hubs programmatically. This tutorial
shows you how to use an Azure Resource Manager template to create an IoT hub from a C# program.
NOTE
Azure has two different deployment models for creating and working with resources: Azure Resource Manager and classic.
This article covers using the Azure Resource Manager deployment model.
Connect-AzureRmAccount
2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure
subscriptions associated with your credentials. Use the following command to list the Azure subscriptions
available for you to use:
Get-AzureRMSubscription
Use the following command to select subscription that you want to use to run the commands to manage
your IoT hub. You can use either the subscription name or ID from the output of the previous command:
Select-AzureRMSubscription `
-SubscriptionName "{your subscription name}"
3. Make a note of your TenantId and SubscriptionId. You need them later.
4. Create a new Azure Active Directory application using the following command, replacing the place
holders:
{Display name}: a display name for your application such as MySampleApp
{Home page URL }: the URL of the home page of your app such as http://mysampleapp/home.
This URL does not need to point to a real application.
{Application identifier}: A unique identifier such as http://mysampleapp. This URL does not need
to point to a real application.
{Password}: A password that you use to authenticate with your app.
5. Make a note of the ApplicationId of the application you created. You need this later.
6. Create a new service principal using the following command, replacing {MyApplicationId} with the
ApplicationId from the previous step:
7. Set up a role assignment using the following command, replacing {MyApplicationId} with your
ApplicationId.
You have now finished creating the Azure AD application that enables you to authenticate from your custom C#
application. You need the following values later in this tutorial:
TenantId
SubscriptionId
ApplicationId
Password
using System;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
6. In Program.cs, add the following static variables replacing the placeholder values. You made a note of
ApplicationId, SubscriptionId, TenantId, and Password earlier in this tutorial. Your Azure Storage
account name is the name of the Azure Storage account where you store your Azure Resource Manager
template files. Resource group name is the name of the resource group you use when you create the IoT
hub. The name can be a pre-existing or new resource group. Deployment name is a name for the
deployment, such as Deployment_01.
if (token == null)
{
Console.WriteLine("Failed to obtain the token");
return;
}
2. Create a ResourceManagementClient object that uses the token by adding the following code to the
end of the Main method:
3. Create, or obtain a reference to, the resource group you are using:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"hubName": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2016-02-03",
"type": "Microsoft.Devices/IotHubs",
"name": "[parameters('hubName')]",
"location": "East US",
"sku": {
"name": "S1",
"tier": "Standard",
"capacity": 1
},
"properties": {
"location": "East US"
}
}
],
"outputs": {
"hubKeys": {
"value": "[listKeys(resourceId('Microsoft.Devices/IotHubs', parameters('hubName')), '2016-02-
03')]",
"type": "object"
}
}
}
3. In Solution Explorer, right-click on your project, click Add, and then click New Item. Add a JSON file
called parameters.json to your project.
4. Replace the contents of parameters.json with the following parameter information that sets a name for
the new IoT hub such as {your initials}mynewiothub. The IoT hub name must be globally unique so it
should include your name or initials:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"hubName": { "value": "mynewiothub" }
}
}
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
5. In Server Explorer, connect to your Azure subscription, and in your Azure Storage account create a
container called templates. In the Properties panel, set the Public Read Access permissions for the
templates container to Blob.
6. In Server Explorer, right-click on the templates container and then click View Blob Container. Click the
Upload Blob button, select the two files, parameters.json and templates.json, and then click Open to
upload the JSON files to the templates container. The URLs of the blobs containing the JSON data are:
8. Add the following code to the CreateIoTHub method to submit the template and parameter files to the
Azure Resource Manager:
9. Add the following code to the CreateIoTHub method that displays the status and the keys for the new
IoT hub:
if (state != "Succeeded")
{
Console.WriteLine("Failed to create iothub");
}
Console.WriteLine(createResponse.Properties.Outputs);
NOTE
This example application adds an S1 Standard IoT Hub for which you are billed. You can delete the IoT hub through the
Azure portal or by using the Remove-AzureRmResource PowerShell cmdlet when you are finished.
Next steps
Now you have deployed an IoT hub using an Azure Resource Manager template with a C# program, you may
want to explore further:
Read about the capabilities of the IoT Hub resource provider REST API.
Read Azure Resource Manager overview to learn more about the capabilities of Azure Resource Manager.
To learn more about developing for IoT Hub, see the following articles:
Introduction to C SDK
Azure IoT SDKs
To further explore the capabilities of IoT Hub, see:
Deploying AI to edge devices with Azure IoT Edge
Configure IoT Hub file uploads using the Azure
portal
5/29/2018 • 1 min to read • Edit Online
File upload
To use the file upload functionality in IoT Hub, you must first associate an Azure Storage account with your hub.
Select File upload to display a list of file upload properties for the IoT hub that is being modified.
Storage container: Use the Azure portal to select a blob container in an Azure Storage account in your current
Azure subscription to associate with your IoT Hub. If necessary, you can create an Azure Storage account on the
Storage accounts blade and blob container on the Containers blade. IoT Hub automatically generates SAS
URIs with write permissions to this blob container for devices to use when they upload files.
Receive notifications for uploaded files: Enable or disable file upload notifications via the toggle.
SAS TTL: This setting is the time-to-live of the SAS URIs returned to the device by IoT Hub. Set to one hour by
default but can be customized to other values using the slider.
File notification settings default TTL: The time-to-live of a file upload notification before it is expired. Set to
one day by default but can be customized to other values using the slider.
File notification maximum delivery count: The number of times the IoT Hub attempts to deliver a file
upload notification. Set to 10 by default but can be customized to other values using the slider.
Next steps
For more information about the file upload capabilities of IoT Hub, see Upload files from a device in the IoT Hub
developer guide.
Follow these links to learn more about managing Azure IoT Hub:
Bulk manage IoT devices
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Secure your IoT solution from the ground up
Configure IoT Hub file uploads using PowerShell
5/29/2018 • 3 min to read • Edit Online
To use the file upload functionality in IoT Hub, you must first associate an Azure storage account with your IoT
hub. You can use an existing storage account or create a new one.
To complete this tutorial, you need the following:
An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
Azure PowerShell cmdlets.
An Azure IoT hub. If you don't have an IoT hub, you can use the New -AzureRmIoTHub cmdlet to create one or
use the portal to Create an IoT hub.
An Azure storage account. If you don't have an Azure storage account, you can use the Azure Storage
PowerShell cmdlets to create one or use the portal to Create a storage account.
Connect-AzureRmAccount
2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure
subscriptions associated with your credentials. Use the following command to list the Azure subscriptions
available for you to use:
Get-AzureRMSubscription
Use the following command to select subscription that you want to use to run the commands to manage
your IoT hub. You can use either the subscription name or ID from the output of the previous command:
Select-AzureRMSubscription `
-SubscriptionName "{your subscription name}"
Get-AzureRmStorageAccountKey `
-Name {your storage account name} `
-ResourceGroupName {your storage account resource group}
Make a note of the key1 storage account key value. You need it in the following steps.
You can either use an existing blob container for your file uploads or create new one:
To list the existing blob containers in your storage account, use the following commands:
$ctx = New-AzureStorageContext `
-StorageAccountName {your storage account name} `
-StorageAccountKey {your storage account key}
Get-AzureStorageContainer -Context $ctx
To create a blob container in your storage account, use the following commands:
$ctx = New-AzureStorageContext `
-StorageAccountName {your storage account name} `
-StorageAccountKey {your storage account key}
New-AzureStorageContainer `
-Name {your new container name} `
-Permission Off `
-Context $ctx
Set-AzureRmIotHub `
-ResourceGroupName "{your iot hub resource group}" `
-Name "{your iot hub name}" `
-FileUploadNotificationTtl "01:00:00" `
-FileUploadSasUriTtl "01:00:00" `
-EnableFileUploadNotifications $true `
-FileUploadStorageConnectionString "DefaultEndpointsProtocol=https;AccountName={your storage account
name};AccountKey={your storage account key};EndpointSuffix=core.windows.net" `
-FileUploadContainerName "{your blob container name}" `
-FileUploadNotificationMaxDeliveryCount 10
Next steps
For more information about the file upload capabilities of IoT Hub, see Upload files from a device.
Follow these links to learn more about managing Azure IoT Hub:
Bulk manage IoT devices
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Secure your IoT solution from the ground up
Configure IoT Hub file uploads using Azure CLI
5/29/2018 • 4 min to read • Edit Online
To use the file upload functionality in IoT Hub, you must first associate an Azure Storage account with your IoT
hub. You can use an existing storage account or create a new one.
To complete this tutorial, you need the following:
An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
Azure CLI 2.0.
An Azure IoT hub. If you don't have an IoT hub, you can use the az iot hub create command to create one or
use the portal to [Create an IoT hub][lnk-portal-hub].
An Azure Storage account. If you don't have an Azure Storage account, you can use the Azure CLI 2.0 -
Manage storage accounts to create one or use the portal to Create a storage account.
az login
Follow the instructions to authenticate using the code and sign in to your Azure account through a web
browser.
2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure accounts
associated with your credentials. Use the following command to list the Azure accounts available for you to
use:
az account list
Use the following command to select subscription that you want to use to run the commands to create your
IoT hub. You can use either the subscription name or ID from the output of the previous command:
az storage account show-connection-string --name {your storage account name} --resource-group {your storage
account resource group}
Make a note of the connectionString value. You need it in the following steps.
You can either use an existing blob container for your file uploads or create new one:
To list the existing blob containers in your storage account, use the following command:
To create a blob container in your storage account, use the following command:
az storage container create --name {container name} --connection-string "{your storage account
connection string}"
File upload
You can now configure your IoT hub to enable file upload functionality using your storage account details.
The configuration requires the following values:
Storage container: A blob container in an Azure storage account in your current Azure subscription to associate
with your IoT hub. You retrieved the necessary storage account information in the preceding section. IoT Hub
automatically generates SAS URIs with write permissions to this blob container for devices to use when they
upload files.
Receive notifications for uploaded files: Enable or disable file upload notifications.
SAS TTL: This setting is the time-to-live of the SAS URIs returned to the device by IoT Hub. Set to one hour by
default.
File notification settings default TTL: The time-to-live of a file upload notification before it is expired. Set to
one day by default.
File notification maximum delivery count: The number of times the IoT Hub attempts to deliver a file upload
notification. Set to 10 by default.
Use the following Azure CLI commands to configure the file upload settings on your IoT hub:
In a bash shell use:
az iot hub update --name {your iot hub name} --set properties.storageEndpoints.'$default'.connectionString="
{your storage account connection string}"
az iot hub update --name {your iot hub name} --set properties.storageEndpoints.'$default'.containerName="{your
storage container name}"
az iot hub update --name {your iot hub name} --set
properties.storageEndpoints.'$default'.sasTtlAsIso8601=PT1H0M0S
az iot hub update --name {your iot hub name} --set properties.enableFileUploadNotifications=true
az iot hub update --name {your iot hub name} --set
properties.messagingEndpoints.fileNotifications.maxDeliveryCount=10
az iot hub update --name {your iot hub name} --set
properties.messagingEndpoints.fileNotifications.ttlAsIso8601=PT1H0M0S
az iot hub update --name {your iot hub name} --set properties.enableFileUploadNotifications=true
az iot hub update --name {your iot hub name} --set
properties.messagingEndpoints.fileNotifications.maxDeliveryCount=10
az iot hub update --name {your iot hub name} --set
properties.messagingEndpoints.fileNotifications.ttlAsIso8601=PT1H0M0S
You can review the file upload configuration on your IoT hub using the following command:
Next steps
For more information about the file upload capabilities of IoT Hub, see Upload files from a device.
Follow these links to learn more about managing Azure IoT Hub:
Bulk manage IoT devices
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Secure your IoT solution from the ground up
Monitor the health of Azure IoT Hub and diagnose
problems quickly
6/7/2018 • 8 min to read • Edit Online
Businesses that implement Azure IoT Hub expect reliable performance from their resources. To help you maintain
a close watch on your operations, IoT Hub is fully integrated with Azure Monitor and Azure Resource Health.
These two services work in tandem to provide you with the data you need to keep your IoT solutions up and
running in a healthy state.
Azure Monitor is a single source of monitoring and logging for all your Azure services. You can send the
diagnostic logs that Azure Monitor generates to Log Analytics, Event Hubs, or Azure Storage for custom
processing. Azure Monitor's metrics and diagnostics settings give you visibility into the performance of your
resources. Continue reading this article to learn how to Use Azure Monitor with your IoT hub.
IMPORTANT
The events emitted by the IoT Hub service using Azure Monitor diagnostic logs are not guaranteed to be reliable or
ordered. Some events might be lost or delivered out of order. Diagnostic logs also aren't meant to be real-time, and it may
take several minutes for events to be logged to your choice of destination.
Azure Resource Health helps you diagnose and get support when an Azure issues impacts your resources. A
personalized dashboard provides current and past health status for your IoT Hubs. Continue reading this article to
learn how to Use Azure Resource Health with your IoT hub.
In addition to integrating with these two services, IoT Hub also provides its own metrics that you can use to
understand the state of your IoT resources. To learn more, see Understand IoT Hub metrics.
Connect-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName <subscription that includes your IoT Hub>
Set-AzureRmDiagnosticSetting -ResourceId <your resource Id> -ServiceBusRuleId <your service bus rule Id> -
Enabled $true
New settings take effect in about 10 minutes. After that, logs appear in the configured archival target on the
Diagnostics settings blade. For more information about configuring diagnostics, see Collect and consume log
data from your azure resources.
Understand the logs
Azure Monitor tracks different operations that occur in IoT Hub. Each category has a schema that defines how
events in that category are reported.
Connections
The connections category tracks device connect and disconnect events from an IoT hub as well as errors. Tracking
this category is useful for identifying unauthorized connection attempts and for tracking when a connection is lost
for devices in areas of poor connectivity.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "deviceConnect",
"category": "Connections",
"level": "Information",
"properties": "{\"deviceId\":\"<deviceId>\",\"protocol\":\"<protocol>\",\"authType\":\"
{\\\"scope\\\":\\\"device\\\",\\\"type\\\":\\\"sas\\\",\\\"issuer\\\":\\\"iothub\\\",\\\"acceptingIpFilterRule
\\\":null}\",\"maskedIpAddress\":\"<maskedIpAddress>\"}",
"location": "Resource location"
}
Cloud-to-device commands
The cloud-to-device commands category tracks errors that occur at the IoT hub and are related to the cloud-to-
device message pipeline. This category includes errors that occur when sending cloud-to-device messages (such
as unauthorized sender), receiving cloud-to-device messages (such as delivery count exceeded), and receiving
cloud-to-device message feedback (such as feedback expired). This category does not catch errors from a device
that improperly handles a cloud-to-device message if the cloud-to-device message was delivered successfully.
{
"time": " UTC timestamp",
"resourceId": "Resource Id",
"operationName": "messageExpired",
"category": "C2DCommands",
"level": "Error",
"resultType": "Event status",
"resultDescription": "MessageDescription",
"properties": "{\"deviceId\":\"<deviceId>\",\"messageId\":\"<messageId>\",\"messageSizeInBytes\":\"
<messageSize>\",\"protocol\":\"Amqp\",\"deliveryAcknowledgement\":\"<None, NegativeOnly, PositiveOnly,
Full>\",\"deliveryCount\":\"0\",\"expiryTime\":\"<timestamp>\",\"timeInSystem\":\"<timeInSystem>\",\"ttl\":
<ttl>, \"EventProcessedUtcTime\":\"<UTC timestamp>\",\"EventEnqueuedUtcTime\":\"<UTC timestamp>\",
\"maskedIpAddresss\": \"<maskedIpAddress>\", \"statusCode\": \"4XX\"}",
"location": "Resource location"
}
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "get",
"category": "DeviceIdentityOperations",
"level": "Error",
"resultType": "Event status",
"resultDescription": "MessageDescription",
"properties": "{\"maskedIpAddress\":\"<maskedIpAddress>\",\"deviceId\":\"<deviceId>\",
\"statusCode\":\"4XX\"}",
"location": "Resource location"
}
Routes
The message routing category tracks errors that occur during message route evaluation and endpoint health as
perceived by IoT Hub. This category includes events such as when a rule evaluates to "undefined", when IoT Hub
marks an endpoint as dead, and any other errors received from an endpoint. This category does not include
specific errors about the messages themselves (such as device throttling errors), which are reported under the
"device telemetry" category.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "endpointUnhealthy",
"category": "Routes",
"level": "Error",
"properties": "{\"deviceId\": \"<deviceId>\",\"endpointName\":\"<endpointName>\",\"messageId\":
<messageId>,\"details\":\"<errorDetails>\",\"routeName\": \"<routeName>\"}",
"location": "Resource location"
}
Device telemetry
The device telemetry category tracks errors that occur at the IoT hub and are related to the telemetry pipeline.
This category includes errors that occur when sending telemetry events (such as throttling) and receiving
telemetry events (such as unauthorized reader). This category cannot catch errors caused by code running on the
device itself.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "ingress",
"category": "DeviceTelemetry",
"level": "Error",
"resultType": "Event status",
"resultDescription": "MessageDescription",
"properties": "{\"deviceId\":\"<deviceId>\",\"batching\":\"0\",\"messageSizeInBytes\":\"
<messageSizeInBytes>\",\"EventProcessedUtcTime\":\"<UTC timestamp>\",\"EventEnqueuedUtcTime\":\"<UTC
timestamp>\",\"partitionId\":\"1\"}",
"location": "Resource location"
}
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "read",
"category": "C2DTwinOperations",
"level": "Information",
"durationMs": "1",
"properties": "{\"deviceId\":\"<deviceId>\",\"sdkVersion\":\"<sdkVersion>\",\"messageSize\":\"
<messageSize>\"}",
"location": "Resource location"
}
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "update",
"category": "D2CTwinOperations",
"level": "Information",
"durationMs": "1",
"properties": "{\"deviceId\":\"<deviceId>\",\"protocol\":\"<protocol>\",\"authenticationType\":\"
{\\\"scope\\\":\\\"device\\\",\\\"type\\\":\\\"sas\\\",\\\"issuer\\\":\\\"iothub\\\",\\\"acceptingIpFilterRule
\\\":null}\"}",
"location": "Resource location"
}
Twin queries
The twin queries category reports on query requests for device twins that are initiated in the cloud.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "query",
"category": "TwinQueries",
"level": "Information",
"durationMs": "1",
"properties": "{\"query\":\"<twin query>\",\"sdkVersion\":\"<sdkVersion>\",\"messageSize\":\"
<messageSize>\",\"pageSize\":\"<pageSize>\", \"continuation\":\"<true, false>\", \"resultSize\":\"
<resultSize>\"}",
"location": "Resource location"
}
Jobs operations
The jobs operations category reports on job requests to update device twins or invoke direct methods on multiple
devices. These requests are initiated in the cloud.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "jobCompleted",
"category": "JobsOperations",
"level": "Information",
"durationMs": "1",
"properties": "{\"jobId\":\"<jobId>\", \"sdkVersion\": \"<sdkVersion>\",\"messageSize\":
<messageSize>,\"filter\":\"DeviceId IN ['1414ded9-b445-414d-89b9-
e48e8c6285d5']\",\"startTimeUtc\":\"Wednesday, September 13, 2017\",\"duration\":\"0\"}",
"location": "Resource location"
}
Direct Methods
The direct methods category tracks request-reponse interactions sent to individual devices. These requests are
initiated in the cloud.
{
"time": "UTC timestamp",
"resourceId": "Resource Id",
"operationName": "send",
"category": "DirectMethods",
"level": "Information",
"durationMs": "1",
"properties": "{\"deviceId\":\"<deviceId>\", \"RequestSize\": 1, \"ResponseSize\": 1, \"sdkVersion\":
\"2017-07-11\"}",
"location": "Resource location"
}
}
}
}
}
Next steps
Understand IoT Hub metrics
IoT remote monitoring and notifications with Azure Logic Apps connecting your IoT hub and mailbox
Migrate your IoT Hub from operations monitoring to
diagnostics settings
5/30/2018 • 2 min to read • Edit Online
Customers using operations monitoring to track the status of operations in IoT Hub can migrate that workflow to
Azure diagnostics settings, a feature of Azure Monitor. Diagnostics settings supply resource-level diagnostic
information for many Azure services.
The operations monitoring functionality of IoT Hub is deprecated, and will be removed in the future. This article
provides steps to move your workloads from operations monitoring to diagnostics settings. For more information
about the deprecation timeline, see Monitor your Azure IoT solutions with Azure Monitor and Azure Resource
Health.
Connect-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName <subscription that includes your IoT Hub>
Set-AzureRmDiagnosticSetting -ResourceId <your resource Id> -ServiceBusRuleId <your service bus rule Id> -
Enabled $true
New settings take effect in about 10 minutes. After that, logs appear in the configured archival target on the
Diagnostics settings blade. For more information about configuring diagnostics, see Collect and consume log
data from your azure resources.
Turn off operations monitoring
Once you have tested the new diagnostics settings on your workflow, you can turn off the operations monitoring
feature.
1. In your IoT Hub menu, select Operations monitoring.
2. Under each monitoring category, select None.
3. Save the operations monitoring changes.
Next steps
Monitor the health of Azure IoT Hub and diagnose problems quickly
IoT Hub operations monitoring
5/29/2018 • 6 min to read • Edit Online
IoT Hub operations monitoring enables you to monitor the status of operations on your IoT hub in real time.
IoT Hub tracks events across several categories of operations. You can opt into sending events from one or
more categories to an endpoint of your IoT hub for processing. You can monitor the data for errors or set up
more complex processing based on data patterns.
NOTE
IoT Hub operations monitoring is deprecated and will be removed from IoT Hub on October 10, 2018. For monitoring
the operations and health of IoT Hub, see Monitor the health of Azure IoT Hub and diagnose problems quickly. For more
information about the deprecation timeline, see Monitor your Azure IoT solutions with Azure Monitor and Azure
Resource Health.
IMPORTANT
IoT Hub operations monitoring does not guarantee reliable or ordered delivery of events. Depending on IoT Hub
underlying infrastructure, some events might be lost or delivered out of order. Use operations monitoring to generate
alerts based on error signals such as failed connection attempts, or high-frequency disconnections for specific devices.
You should not rely on operations monitoring events to create a consistent store for device state, e.g. a store tracking
connected or disconnected state of a device.
{
"time": "UTC timestamp",
"operationName": "create",
"category": "DeviceIdentityOperations",
"level": "Error",
"statusCode": 4XX,
"statusDescription": "MessageDescription",
"deviceId": "device-ID",
"durationMs": 1234,
"userAgent": "userAgent",
"sharedAccessPolicy": "accessPolicy"
}
Device telemetry
The device telemetry category tracks errors that occur at the IoT hub and are related to the telemetry pipeline.
This category includes errors that occur when sending telemetry events (such as throttling) and receiving
telemetry events (such as unauthorized reader). This category cannot catch errors caused by code running on
the device itself.
{
"messageSizeInBytes": 1234,
"batching": 0,
"protocol": "Amqp",
"authType": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\"}",
"time": "UTC timestamp",
"operationName": "ingress",
"category": "DeviceTelemetry",
"level": "Error",
"statusCode": 4XX,
"statusType": 4XX001,
"statusDescription": "MessageDescription",
"deviceId": "device-ID",
"EventProcessedUtcTime": "UTC timestamp",
"PartitionId": 1,
"EventEnqueuedUtcTime": "UTC timestamp"
}
Connections
The connections category tracks errors that occur when devices connect or disconnect from an IoT hub.
Tracking this category is useful for identifying unauthorized connection attempts and for tracking when a
connection is lost for devices in areas of poor connectivity.
{
"durationMs": 1234,
"authType": "{\"scope\":\"hub\",\"type\":\"sas\",\"issuer\":\"iothub\"}",
"protocol": "Amqp",
"time": " UTC timestamp",
"operationName": "deviceConnect",
"category": "Connections",
"level": "Error",
"statusCode": 4XX,
"statusType": 4XX001,
"statusDescription": "MessageDescription",
"deviceId": "device-ID"
}
File uploads
The file upload category tracks errors that occur at the IoT hub and are related to file upload functionality. This
category includes:
Errors that occur with the SAS URI, such as when it expires before a device notifies the hub of a completed
upload.
Failed uploads reported by the device.
Errors that occur when a file is not found in storage during IoT Hub notification message creation.
This category cannot catch errors that directly occur while the device is uploading a file to storage.
{
"authType": "{\"scope\":\"hub\",\"type\":\"sas\",\"issuer\":\"iothub\"}",
"protocol": "HTTP",
"time": " UTC timestamp",
"operationName": "ingress",
"category": "fileUpload",
"level": "Error",
"statusCode": 4XX,
"statusType": 4XX001,
"statusDescription": "MessageDescription",
"deviceId": "device-ID",
"blobUri": "http//bloburi.com",
"durationMs": 1234
}
Message routing
The message routing category tracks errors that occur during message route evaluation and endpoint health as
perceived by IoT Hub. This category includes events such as when a rule evaluates to "undefined", when IoT
Hub marks an endpoint as dead, and any other errors received from an endpoint. This category does not
include specific errors about the messages themselves (such as device throttling errors), which are reported
under the "device telemetry" category.
{
"messageSizeInBytes": 1234,
"time": "UTC timestamp",
"operationName": "ingress",
"category": "routes",
"level": "Error",
"deviceId": "device-ID",
"messageId": "ID of message",
"routeName": "myroute",
"endpointName": "myendpoint",
"details": "ExternalEndpointDisabled"
}
View events
You can use the iothub -explorer tool to quickly test that your IoT hub is generating monitoring events. To install
the tool, see the instructions in the iothub-explorer GitHub repository.
1. Make sure the Connections monitoring category is set to Verbose in the portal.
2. At a command-prompt, run the following command to read from the monitoring endpoint:
3. In another command-prompt, run the following command to simulate a device sending device-to-cloud
messages:
iothub-explorer simulate-device {your device name} --send "My test message" --login {your
iothubowner connection string}
4. The first command-prompt shows the monitoring events as the simulated device connects to your IoT
hub.
3. Choose Shared access policies, then choose service. Make a note of the Primary key value:
The following C# code sample is taken from a Visual Studio Windows Classic Desktop C# console app. The
project has the WindowsAzure.ServiceBus NuGet package installed.
Replace the connection string placeholder with a connection string that uses the Event Hub-
compatible endpoint and service Primary key values you noted previously as shown in the following
example:
Replace the monitoring endpoint name placeholder with the Event Hub-compatible name value you
noted previously.
class Program
{
static string connectionString = "{your monitoring endpoint connection string}";
static string monitoringEndpointName = "{your monitoring endpoint name}";
static EventHubClient eventHubClient;
eventHubClient = EventHubClient.CreateFromConnectionString(connectionString,
monitoringEndpointName);
var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
CancellationTokenSource cts = new CancellationTokenSource();
var tasks = new List<Task>();
Console.ReadLine();
Console.WriteLine("Exiting...");
cts.Cancel();
Task.WaitAll(tasks.ToArray());
}
if (eventData != null)
{
string data = Encoding.UTF8.GetString(eventData.GetBytes());
Console.WriteLine("Message received. Partition: {0} Data: '{1}'", partition, data);
}
}
}
}
Next steps
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Azure IoT Hub get started with real devices
5/29/2018 • 1 min to read • Edit Online
You can use Azure IoT Hub and the Azure IoT device SDKs to build Internet of Things (IoT) solutions:
Azure IoT Hub is a fully managed service in the cloud that securely connects, monitors, and manages your IoT
devices. Use the Azure IoT Device SDKs to implement your IoT devices.
Use an IoT gateway in more complex IoT scenarios. For example, where you need to consider factors such as
legacy devices, bandwidth costs, security and privacy policies, or edge data processing. In these scenarios, use
Azure IoT Edge to implement a gateway that connects devices to your IoT hub.
Manage your IoT device Azure CLI 2.0 and the IoT extension
Next steps
When you have completed these tutorials, you can further explore the capabilities of IoT Hub in the Developer
guide. You can find additional tutorials in the How To section.
Connect Raspberry Pi online simulator to Azure IoT
Hub (Node.js)
5/29/2018 • 4 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Raspberry Pi online simulator. You then learn
how to seamlessly connect the Pi simulator to the cloud by using Azure IoT Hub.
If you have physical devices, visit Connect Raspberry Pi to Azure IoT Hub to get started.
What you do
Learn the basics of Raspberry Pi online simulator.
Create an IoT hub.
Register a device for Pi in your IoT hub.
Run a sample application on Pi to send simulated sensor data to your IoT hub.
Connect simulated Raspberry Pi to an IoT hub that you create. Then you run a sample application with the
simulator to generate sensor data. Finally, you send the sensor data to your IoT hub.
NOTE
The Raspberry Pi web simulator is now available in preview version. We'd like to hear your voice in the Gitter Chatroom.
The source code is public on Github.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Azure IoT Hub get started with physical devices
tutorials
5/29/2018 • 1 min to read • Edit Online
These tutorials introduce you to Azure IoT Hub and the device SDKs. The tutorials cover common IoT scenarios to
demonstrate the capabilities of IoT Hub. The tutorials also illustrate how to combine IoT Hub with other Azure
services and tools to build more powerful IoT solutions. The tutorials listed in the following table show you how to
create physical IoT devices.
Manage your IoT device Azure CLI 2.0 and the IoT extension
Next steps
When you have completed these tutorials, you can further explore the capabilities of IoT Hub in the Developer
guide. You can find additional tutorials in the How To section.
Connect Raspberry Pi to Azure IoT Hub (Python)
5/29/2018 • 9 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Raspberry Pi that's running Raspbian. You then
learn how to seamlessly connect your devices to the cloud by using Azure IoT Hub. For Windows 10 IoT Core
samples, go to the Windows Dev Center.
Don't have a kit yet? Try Raspberry Pi online simulator. Or buy a new kit here.
What you do
Create an IoT hub.
Register a device for Pi in your IoT hub.
Setup Raspberry Pi.
Run a sample application on Pi to send sensor data to your IoT hub.
Connect Raspberry Pi to an IoT hub that you create. Then you run a sample application on Pi to collect
temperature and humidity data from a BME280 sensor. Finally, you send the sensor data to your IoT hub.
NOTE
These items are optional because the code sample support simulated sensor data.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy
the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
Set up Raspberry Pi
Install the Raspbian operating system for Pi
Prepare the microSD card for installation of the Raspbian image.
1. Download Raspbian.
a. Download Raspbian Jessie with Desktop (the .zip file).
b. Extract the Raspbian image to a folder on your computer.
2. Install Raspbian to the microSD card.
a. Download and install the Etcher SD card burner utility.
b. Run Etcher and select the Raspbian image that you extracted in step 1.
c. Select the microSD card drive. Note that Etcher may have already selected the correct drive.
d. Click Flash to install Raspbian to the microSD card.
e. Remove the microSD card from your computer when installation is complete. It's safe to remove the
microSD card directly because Etcher automatically ejects or unmounts the microSD card upon
completion.
f. Insert the microSD card into Pi.
Enable SSH and I2C
1. Connect Pi to the monitor, keyboard and mouse, start Pi and then log in Raspbian by using pi as the user
name and raspberry as the password.
2. Click the Raspberry icon > Preferences > Raspberry Pi Configuration.
3. On the Interfaces tab, set I2C and SSH to Enable, and then click OK. If you don't have physical sensors
and want to use simulated sensor data, this step is optional.
NOTE
To enable SSH and I2C, you can find more reference documents on raspberrypi.org and RASPI-CONFIG.
NOTE
The default username is pi , and the password is raspberry .
cd iot-hub-python-raspberrypi-client-app
nano config.py
There are 5 macros in this file you can configurate. The first one is MESSAGE_TIMESPAN , which defines the
time interval (in milliseconds) between two messages that send to cloud. The second one SIMULATED_DATA ,
which is a Boolean value for whether to use simulated sensor data or not. I2C_ADDRESS is the I2C address
which your BME280 sensor is connected. GPIO_PIN_ADDRESS is the GPIO address for your LED. The last
one is BLINK_TIMESPAN , which defined the timespan when your LED is turned on in milliseconds.
If you don't have the sensor, set the SIMULATED_DATA value to True to make the sample application
create and use simulated sensor data.
3. Save and exit by pressing Control-O > Enter > Control-X.
Build and run the sample application
1. Build the sample application by running the following command. Because the Azure IoT SDKs for Python
are wrappers on top of the Azure IoT Device C SDK, you will need to compile the C libraries if you want or
need to generate the Python libraries from source code.
NOTE
You can also specify the version you want by running sudo ./setup.sh [--python-version|-p] [2.7|3.4|3.5] .
If you run script without parameter, the script will automatically detect the version of python installed (Search
sequence 2.7->3.4->3.5). Make sure your Python version keeps consistent during building and running.
NOTE
On building the Python client library (iothub_client.so) on Linux devices that have less than 1GB RAM, you may see
build getting stuck at 98% while building iothub_client_python.cpp as shown below
[ 98%] Building CXX object
python/src/CMakeFiles/iothub_client_python.dir/iothub_client_python.cpp.o
. If you run into this issue, check the memory consumption of the device using free -m command in another
terminal window during that time. If you are running out of memory while compiling iothub_client_python.cpp file,
you may have to temporarily increase the swap space to get more available memory to successfully build the
Python client-side device SDK library.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub. To see the messages that your
Raspberry Pi has sent to your IoT hub or send messages to your Raspberry Pi in a command line interface, see
the Manage cloud device messaging with iothub-explorer tutorial.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Raspberry Pi to Azure IoT Hub (Node.js)
5/29/2018 • 8 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Raspberry Pi that's running Raspbian. You
then learn how to seamlessly connect your devices to the cloud by using Azure IoT Hub. For Windows 10
IoT Core samples, go to the Windows Dev Center.
Don't have a kit yet? Try Raspberry Pi online simulator. Or buy a new kit here.
What you do
Create an IoT hub.
Register a device for Pi in your IoT hub.
Set up Raspberry Pi.
Run a sample application on Pi to send sensor data to your IoT hub.
NOTE
If you don't have the optional items, you can use simulated sensor data.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information
while naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on
your subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create.
You can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then
copy the Connection string---primary key of your IoT hub. For more information, see Control access to
IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT
hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
Set up Raspberry Pi
Install the Raspbian operating system for Pi
Prepare the microSD card for installation of the Raspbian image.
1. Download Raspbian.
a. Download Raspbian Stretch (the .zip file).
WARNING
Please use above link to download raspbian-2017-07-5 zip image. The latest version of Raspbian images
has some known issues with Wiring-Pi Node, which might cause failure in your next steps.
1. Extract the Raspbian image to a folder on your computer.
NOTE
To enable SSH and I2C, you can find more reference documents on raspberrypi.org and Adafruit.com.
NOTE
The default username is pi and the password is raspberry .
node -v
If the version is lower than 4.x, or if there is no Node.js on your Pi, install the latest version.
curl -sL http://deb.nodesource.com/setup_4.x | sudo -E bash
sudo apt-get -y install nodejs
4. Install all packages for the sample. The installation includes Azure IoT device SDK, BME280 Sensor
library, and Wiring Pi library.
cd iot-hub-node-raspberrypi-client-app
sudo npm install
NOTE
It might take several minutes to finish this installation process depending on your network connection.
nano config.json
There are two items in this file you can configure. The first one is interval , which defines the time
interval (in milliseconds) between messages sent to the cloud. The second one is simulatedData ,
which is a Boolean value for whether to use simulated sensor data or not.
If you don't have the sensor, set the simulatedData value to true to make the sample application
create and use simulated sensor data.
2. Save and exit by typing Control-O > Enter > Control-X.
Run the sample application
Run the sample application by running the following command:
sudo node index.js '<YOUR AZURE IOT HUB DEVICE CONNECTION STRING>'
NOTE
Make sure you copy-paste the device connection string into the single quotes.
You should see the following output that shows the sensor data and the messages that are sent to your IoT
hub.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub. To see the messages that
your Raspberry Pi has sent to your IoT hub, or to send messages to your Raspberry Pi in a command-line
interface, see the Manage cloud device messaging with iothub-explorer tutorial.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Raspberry Pi to Azure IoT Hub (C)
5/29/2018 • 7 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Raspberry Pi that's running Raspbian. You then
learn how to seamlessly connect your devices to the cloud by using Azure IoT Hub. For Windows 10 IoT Core
samples, go to the Windows Dev Center.
Don't have a kit yet? Try Raspberry Pi online simulator. Or buy a new kit here.
What you do
Create an IoT hub.
Register a device for Pi in your IoT hub.
Setup Raspberry Pi.
Run a sample application on Pi to send sensor data to your IoT hub.
Connect Raspberry Pi to an IoT hub that you create. Then you run a sample application on Pi to collect
temperature and humidity data from a BME280 sensor. Finally, you send the sensor data to your IoT hub.
NOTE
These items are optional because the code sample support simulated sensor data.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy
the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
Setup Raspberry Pi
Install the Raspbian operating system for Pi
Prepare the microSD card for installation of the Raspbian image.
1. Download Raspbian.
a. Download Raspbian Jessie with Desktop (the .zip file).
b. Extract the Raspbian image to a folder on your computer.
2. Install Raspbian to the microSD card.
a. Download and install the Etcher SD card burner utility.
b. Run Etcher and select the Raspbian image that you extracted in step 1.
c. Select the microSD card drive. Note that Etcher may have already selected the correct drive.
d. Click Flash to install Raspbian to the microSD card.
e. Remove the microSD card from your computer when installation is complete. It's safe to remove the
microSD card directly because Etcher automatically ejects or unmounts the microSD card upon
completion.
f. Insert the microSD card into Pi.
Enable SSH and SPI
1. Connect Pi to the monitor, keyboard and mouse, start Pi and then log in Raspbian by using pi as the user
name and raspberry as the password.
2. Click the Raspberry icon > Preferences > Raspberry Pi Configuration.
3. On the Interfaces tab, set SPI and SSH to Enable, and then click OK. If you don't have physical sensors
and want to use simulated sensor data, this step is optional.
NOTE
To enable SSH and SPI, you can find more reference documents on raspberrypi.org and RASPI-CONFIG.
NOTE
The default username is pi , and the password is raspberry .
cd ./iot-hub-c-raspberrypi-client-app
sudo chmod u+x setup.sh
sudo ./setup.sh
NOTE
If you don't have a physical BME280, you can use '--simulated-data' as command line parameter to simulate
temperature&humidity data. sudo ./setup.sh --simulated-data
NOTE
Make sure you copy-paste the device connection string into the single quotes.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub. To see the messages that your
Raspberry Pi has sent to your IoT hub or send messages to your Raspberry Pi in a command line interface, see
the Manage cloud device messaging with iothub-explorer tutorial.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect IoT DevKit AZ3166 to Azure IoT Hub in the
cloud
5/29/2018 • 9 min to read • Edit Online
You can use the MXChip IoT DevKit to develop and prototype Internet of Things (IoT) solutions that take
advantage of Microsoft Azure services. It includes an Arduino-compatible board with rich peripherals and
sensors, an open-source board package, and a growing projects catalog.
What you do
Connect the DevKit to an Azure IoT hub that you create, collect the temperature and humidity data from sensors,
and send the data to the IoT hub.
Don't have a DevKit yet? Try DevKit simulator or get one.
When the connection succeeds, the DevKit reboots in a few seconds. You then see the Wi-Fi name and IP
address on the screen:
NOTE
The IP address displayed in the photo might not match the actual IP address assigned and displayed on the DevKit screen.
This is normal, because Wi-Fi uses DHCP to dynamically assign IPs.
After Wi-Fi is configured, your credentials will persist on the device for that connection, even if the device is
unplugged. For example, if you configure the DevKit for Wi-Fi in your home and then take the DevKit to the
office, you will need to reconfigure AP mode (starting at the step in the "Enter AP Mode" section) to connect the
DevKit to your office Wi-Fi.
If you need a firmware upgrade, the screen will show the current and latest firmware versions. To upgrade, follow
the Upgrade firmware guide.
NOTE
This is a one-time effort. After you start developing on the DevKit and upload your app, the latest firmware will come with
your app.
The .zip file contains the following tools and packages. If you already have some components installed, the script
will detect and skip them.
Node.js and Yarn: Runtime for the setup script and automated tasks.
Azure CLI 2.0 MSI: Cross-platform command-line experience for managing Azure resources. The MSI
contains dependent Python and pip.
Visual Studio Code (VS Code): Lightweight code editor for DevKit development.
Visual Studio Code extension for Arduino: Extension that enables Arduino development in Visual Studio
Code.
Arduino IDE: Tool that the extension for Arduino relies on.
DevKit Board Package: Tool chains, libraries, and projects for the DevKit.
ST-Link Utility: Essential tools and drivers.
Run the installation script
In Windows File Explorer, locate the .zip file and extract it. Find install.cmd , right-click it, and select Run as
administrator.
Install drivers
The VS Code for Arduino extension relies on the Arduino IDE. If this is the first time you are installing the
Arduino IDE, you're prompted to install relevant drivers:
Installation should take around 10 minutes, depending on your internet speed. After the installation is complete,
you should see Visual Studio Code and Arduino IDE shortcuts on your desktop.
NOTE
Occasionally, when you start VS Code, you're prompted with an error that it cannot find the Arduino IDE or related board
package. To solve it, close VS Code and restart the Arduino IDE. VS Code should then locate the Arduino IDE path correctly.
macOS
We encourage you to use one-click installation experience to prepare the development environment. If you
encounter any problems, you can follow the manual steps to get it done.
Install Homebrew
NOTE
If you have installed Homebrew, you can skip this step.
The .zip file contains the following tools and packages. If you already have some components installed, the script
will detect and skip them.
Node.js and Yarn: Runtime for the setup script and automated tasks.
Azure CLI 2.0: Cross-platform command-line experience for managing Azure resources.
Visual Studio Code (VS Code): Lightweight code editor for DevKit development.
Visual Studio Code extension for Arduino: Extension that enables Arduino development in Visual Studio
Code.
Arduino IDE: Tool that the extension for Arduino relies on.
DevKit Board Package: Tool chains, libraries, and projects for the DevKit.
ST-Link Utility: Essential tools and drivers.
Run the installation script
In the Finder, locate the .zip and extract it:
Launch Terminal app, locate the folder you extract .zip file and run:
./install.sh
NOTE
If you meet Homebrew permission error, run brew doctor to get it fixed. Check FAQ for more details.
You now have all the necessary tools and packages installed for macOS.
Open the project folder
Start VS Code
Make sure your DevKit is not connected. Start VS Code first and connect the DevKit to your computer. VS Code
automatically finds the DevKit and opens an introduction page:
NOTE
Occasionally, when you start VS Code, you're prompted with an error that it cannot find the Arduino IDE or related board
package. Close VS Code and restart the Arduino IDE. VS Code should then locate the Arduino IDE path correctly.
NOTE
Occasionally, you get error "Error: AZ3166: Unknown package". This is due to the board package index is not refreshed.
Check this FAQ steps to solve it.
macOS
1. Put DevKit into configuration mode: Hold down button A, then push and release the reset button. The screen
displays 'Configuration'.
2. Use Cmd+P to run task device-upload .
This is to set the connection string that retrieves from task cloud-provision step.
Then VS Code starts verifying and uploading the Arduino sketch:
The DevKit reboots and starts running the code.
NOTE
Occasionally, you get error "Error: AZ3166: Unknown package". This is due to the board package index is not refreshed.
Check this FAQ steps to solve it.
2. Click power plug icon on the status bar to open the Serial Monitor:
3. On the status bar, click the number that represents the Baud Rate and set to 115200 :
The sample application is running successfully when you see the following results:
The Serial Monitor displays the same information as the content in the screenshot below.
The LED on MXChip IoT DevKit is blinking.
Next steps
You have successfully connected an MXChip IoT DevKit to your IoT hub, and you have sent the captured sensor
data to your IoT hub.
To continue getting started with Azure IoT Hub and to explore other IoT scenarios, see:
Manage cloud device messaging with iothub-explorer
Save IoT Hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from Azure IoT Hub
Use Web Apps to visualize real-time sensor data from Azure IoT Hub
Weather forecast using the sensor data from your IoT hub in Azure Machine Learning
Device management with iothub-explorer
Remote monitoring and notifications with Logic Apps
Connect Intel Edison to Azure IoT Hub (Node.js)
5/29/2018 • 8 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Intel Edison. You then learn how to seamlessly
connect your devices to the cloud by using Azure IoT Hub.
Don't have a kit yet? Start here
What you do
Setup Intel Edison and and Grove modules.
Create an IoT hub.
Register a device for Edison in your IoT hub.
Run a sample application on Edison to send sensor data to your IoT hub.
Connect Intel Edison to an IoT hub that you create. Then you run a sample application on Edison to collect
temperature and humidity data from a Grove temperature sensor. Finally, you send the sensor data to your IoT
hub.
NOTE
These items are optional because the code sample support simulated sensor data.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy
the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
3. Use the two hex nuts (included in the package) to secure the module to the expansion board.
4. Insert a screw in one of the four corner holes on the expansion board. Twist and tighten one of the white
plastic spacers onto the screw.
2. A green LED (labeled DS1 on the Arduino* expansion board) should light up and stay lit.
3. Wait one minute for the board to finish booting up.
NOTE
If you do not have a DC power supply, you can still power the board through a USB port. See
Connect Edison to your computer section for details. Powering your board in this fashion may result in
unpredictable behavior from your board, especially when using Wi-Fi or driving motors.
2. Plug the micro USB cable into the top micro USB port.
3. Plug the other end of USB cable into your computer.
4. You will know that your board is fully initialized when your computer mounts a new drive (much like
inserting a SD card into your computer).
NOTE
Make sure that Edison is connected to the same network as your computer. Your computer connects to your Edison by
using the IP address.
Congratulations! You've successfully configured Edison.
3. Then navigate to the repo folder to run the following command to install all packages, it may take serval
minutes to complete.
cd iot-hub-node-intel-edison-client-app
npm install
nano config.json
There are two macros in this file you can configurate. The first one is INTERVAL , which defines the time
interval between two messages that send to cloud. The second one simulatedData ,which is a Boolean
value for whether to use simulated sensor data or not.
If you don't have the sensor, set the simulatedData value to true to make the sample application
create and use simulated sensor data.
2. Save and exit by pressing Control-O > Enter > Control-X.
3. Run the sample application by running the following command:
sudo node index.js '<your Azure IoT hub device connection string>'
NOTE
Make sure you copy-paste the device connection string into the single quotes.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Intel Edison to Azure IoT Hub (C)
5/29/2018 • 8 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with Intel Edison. You then learn how to seamlessly
connect your devices to the cloud by using Azure IoT Hub.
Don't have a kit yet? Start here
What you do
Setup Intel Edison and and Grove modules.
Create an IoT hub.
Register a device for Edison in your IoT hub.
Run a sample application on Edison to send sensor data to your IoT hub.
Connect Intel Edison to an IoT hub that you create. Then you run a sample application on Edison to collect
temperature and humidity data from a Grove temperature sensor. Finally, you send the sensor data to your IoT
hub.
NOTE
These items are optional because the code sample support simulated sensor data.
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy
the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid
any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
3. Use the two hex nuts (included in the package) to secure the module to the expansion board.
4. Insert a screw in one of the four corner holes on the expansion board. Twist and tighten one of the white
plastic spacers onto the screw.
2. A green LED (labeled DS1 on the Arduino* expansion board) should light up and stay lit.
3. Wait one minute for the board to finish booting up.
NOTE
If you do not have a DC power supply, you can still power the board through a USB port. See
Connect Edison to your computer section for details. Powering your board in this fashion may result in
unpredictable behavior from your board, especially when using Wi-Fi or driving motors.
2. Plug the micro USB cable into the top micro USB port.
3. Plug the other end of USB cable into your computer.
4. You will know that your board is fully initialized when your computer mounts a new drive (much like
inserting a SD card into your computer).
NOTE
Make sure that Edison is connected to the same network as your computer. Your computer connects to your Edison by
using the IP address.
Congratulations! You've successfully configured Edison.
3. Then navigate to the repo folder to run the following command to build Azure IoT SDK
cd iot-hub-c-intel-edison-client-app
sed -i -e 's/\r$//' buildSDK.sh
chmod 755 buildSDK.sh
./buildSDK.sh
nano config.h
There are two macros in this file you can configurate. The first one is INTERVAL , which defines the time
interval between two messages that send to cloud. The second one SIMULATED_DATA ,which is a Boolean
value for whether to use simulated sensor data or not.
If you don't have the sensor, set the SIMULATED_DATA value to 1 to make the sample application create
and use simulated sensor data.
2. Save and exit by pressing Control-O > Enter > Control-X.
Build and run the sample application
1. Build the sample application by running the following command:
NOTE
Make sure you copy-paste the device connection string into the single quotes.
You should see the following output that shows the sensor data and the messages that are sent to your IoT hub.
Next steps
You’ve run a sample application to collect sensor data and send it to your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Adafruit Feather HUZZAH ESP8266 to
Azure IoT Hub in the cloud
5/29/2018 • 8 min to read • Edit Online
What you do
Connect Adafruit Feather HUZZAH ESP8266 to an IoT hub that you create. Then you run a sample application
on ESP8266 to collect the temperature and humidity data from a DHT22 sensor. Finally, you send the sensor
data to your IoT hub.
NOTE
If you're using other ESP8266 boards, you can still follow these steps to connect it to your IoT hub. Depending on the
ESP8266 board you're using, you might need to reconfigure the LED_PIN . For example, if you're using ESP8266 from AI-
Thinker, you might change it from 0 to 2 . Don't have a kit yet? Get it from the Azure website.
NOTE
The Arduino IDE version used by Visual Studio Code extension for Arduino has to be version 1.6.8 or later. Earlier versions
don't work with the AzureIoT library.
The following items are optional in case you don’t have a sensor. You also have the option of using simulated
sensor data.
An Adafruit DHT22 temperature and humidity sensor
A breadboard
M/M jumper wires
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
For more information, see Adafruit DHT22 sensor setup and Adafruit Feather HUZZAH Esp8266 Pinouts.
Now your Feather Huzzah ESP8266 should be connected with a working sensor.
Connect Feather HUZZAH ESP8266 to your computer
As shown next, use the Micro USB to Type A USB cable to connect Feather HUZZAH ESP8266 to your
computer.
ls -l /dev/ttyUSB*
ls -l /dev/ttyACM*
<group-owner-name> is the group owner name you obtained in the previous step. <username> is your
Ubuntu user name.
3. Sign out of Ubuntu, and then sign in again for the change to appear.
Collect sensor data and send it to your IoT hub
In this section, you deploy and run a sample application on Feather HUZZAH ESP8266. The sample application
blinks the LED on Feather HUZZAH ESP8266, and sends the temperature and humidity data collected from the
DHT22 sensor to your IoT hub.
Get the sample application from GitHub
The sample application is hosted on GitHub. Clone the sample repository that contains the sample application
from GitHub. To clone the sample repository, follow these steps:
1. Open a command prompt or a terminal window.
2. Go to a folder where you want the sample application to be stored.
3. Run the following command:
Install the package for Feather HUZZAH ESP8266 in the Visual Studio Code:
1. Open the folder where the sample application is stored.
2. Open the app.ino file in the app folder in the Visual Studio Code.
Next steps
You have successfully connected a Feather HUZZAH ESP8266 to your IoT hub, and sent the captured sensor
data to your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Sparkfun ESP8266 Thing Dev to Azure IoT
Hub in the cloud
5/29/2018 • 8 min to read • Edit Online
NOTE
If you are using other ESP8266 boards, you can still follow these steps to connect it to your IoT hub. Depending on the
ESP8266 board you are using, you may need to reconfigure the LED_PIN . For example, if you are using ESP8266 from
AI-Thinker, you may change it from 0 to 2 . Don't have a kit yet?: Click here
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
Connect ESP8266 Thing Dev with the sensor and your computer
Connect a DHT22 temperature and humidity sensor to ESP8266 Thing Dev
Use the breadboard and jumper wires to make the connection as follows. If you don’t have a sensor, skip this
section because you can use simulated sensor data instead.
For sensor pins, we will use the following wiring:
For more information, see: DHT22 sensor setup and Sparkfun ESP8266 Thing Dev specification
Now your Sparkfun ESP8266 Thing Dev should be connected with a working sensor.
Connect Sparkfun ESP8266 Thing Dev to your computer
Use the Micro USB to Type A USB cable to connect Sparkfun ESP8266 Thing Dev to your computer as follows.
ls -l /dev/ttyUSB*
ls -l /dev/ttyACM*
<group-owner-name> is the group owner name you obtained in the previous step. <username> is your
Ubuntu user name.
3. Log out Ubuntu and log in it again for the change to take effect.
Install the package for Sparkfun ESP8266 Thing Dev in Arduino IDE:
1. Open the folder where the sample application is stored.
2. Open the app.ino file in the app folder in Arduino IDE.
3. In the Arduino IDE, click File > Preferences.
4. In the Preferences dialog box, click the icon next to the Additional Boards Manager URLs text box.
5. In the pop-up window, enter the following URL, and then click OK.
http://arduino.esp8266.com/stable/package_esp8266com_index.json
NOTE
If you are using macOS you could probably see the following messages during uploading.
warning: espcomm_sync failed , error: espcomm_open failed . Please open your ternimal window and finish below
actions to solve this issue.
cd /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns
sudo mv AppleUSBFTDI.kext AppleUSBFTDI.disabled
sudo touch /System/Library/Extensions
NOTE
The credential information is stored in the EEPROM of Sparkfun ESP8266 Thing Dev. If you click the reset button on the
Sparkfun ESP8266 Thing Dev board, the sample application asks you if you want to erase the information. Enter Y to
have the information erased and you are asked to provide the information again.
Next steps
You have successfully connected a Sparkfun ESP8266 Thing Dev to your IoT hub and sent the captured sensor
data to your IoT hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
Connect Adafruit Feather M0 WiFi to Azure IoT
Hub in the cloud
5/29/2018 • 8 min to read • Edit Online
In this tutorial, you begin by learning the basics of working with your Arduino board. You then learn how to
seamlessly connect your devices to the cloud by using Azure IoT Hub.
What you do
Connect Adafruit Feather M0 WiFi to an IoT hub that you create. Then you run a sample application on M0 WiFi
to collect the temperature and humidity data from a BME280. Finally, you send the sensor data to your IoT hub.
To complete this operation, you need the following parts from your Feather M0 WiFi Starter Kit:
The Feather M0 WiFi board
A Micro USB to Type A USB cable
You also need the following things for your development environment:
An active Azure subscription. If you don't have an Azure account, create a free Azure trial account in just a
few minutes.
A Mac or PC that is running Windows or Ubuntu.
A wireless network for Feather M0 WiFi to connect to.
An Internet connection to download the configuration tool.
Arduino IDE version 1.6.8 or later. Earlier versions don't work with the Azure IoT Hub library.
If you don’t have a sensor, the following items are optional. You also have the option of using simulated sensor
data:
A BME280 temperature and humidity sensor
A breadboard
M/M jumper wires
3. In the IoT hub pane, enter the following information for your IoT hub:
Subscription: Choose the subscription that you want to use to create this IoT hub.
Resource group: Create a resource group to host the IoT hub or use an existing one. For more
information, see Use resource groups to manage your Azure resources.
Region: Select the closest location to you.
Name: Create a name for your IoT hub. If the name you enter is available, a green check mark
appears.
IMPORTANT
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while
naming it.
4. Select Next: Size and scale to continue creating your IoT hub.
5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your
subscription. For more information, see the Pricing and scale tier.
6. Select Review + create.
7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You
can monitor the progress in the Notifications pane.
Now that you have created an IoT hub, locate the important information that you use to connect devices and
applications to your IoT hub.
In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy
the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.
NOTE
You do not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the
tutorials or different IoT scenarios after you complete this set-up.
Register a device in the IoT hub for your device
1. In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
2. Enter a Device ID for the new device. Device IDs are case sensitive.
IMPORTANT
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to
avoid any sensitive information while naming it.
3. Click Save.
4. After the device is created, open the device from the list in the IoT devices pane.
5. Copy the Connection string---primary key to use later.
For more information, see Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout
and Adafruit Feather M0 WiFi pinouts.
Now your Feather M0 WiFi should be connected with a working sensor.
ls -l /dev/ttyUSB*
ls -l /dev/ttyACM*
In the previous step, you obtained the group owner name <group-owner-name> . Your Ubuntu user name is
<username> .
3. For the change to appear, sign out of Ubuntu and then sign in again.
3. Click File > Preferences (Windows/Linux) or Arduino > Preferences (Mac) and copy and paste the link
below into the Additional Boards Manager URLs option in the Arduino IDE preferences.
https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
4. Click Tools > Board > Boards Manager, and then install the Arduino SAMD Boards version 1.6.2 or
later.
5. Then in the same window, install Adafruit SAMD Boards package to add the board file definitions.
6. Click Tools > Board > Adafruit M0 WiFi.
7. Install drivers (for Windows only). When you plug in Feather M0 WiFi, you might need to install a driver.
Click the download link on the webpage to download the driver installer. Follow the steps to install the
drivers you want.
Install necessary libraries
1. In the Arduino IDE, click Sketch > Include Library > Manage Libraries.
2. Search for the following library names one by one. For each library that you find, click Install:
RTCZero
NTPClient
AzureIoTHub
AzureIoTUtility
AzureIoTProtocol_HTTP
ArduinoJson
Adafruit BME280 Library
Adafruit Unified Sensor
3. Manually install Adafruit_WINC1500 . Go to this website and click Clone or download > Download ZIP.
Then in your Arduino IDE, go to Sketch > Include Library > Add .zip Library and add the zip file.
Use the sample application if you don’t have a real BME280 sensor
If you don’t have a real BME280 sensor, the sample application can simulate temperature and humidity data. To
set up the sample application to use simulated data, follow these steps:
1. Open the config.h file in the app folder.
2. Locate the following line of code and change the value from false to true :
NOTE
The credential information is stored in the EEPROM of Feather M0 WiFi. If you click the reset button on the Feather M0
WiFi board, the sample application asks if you want to erase the information. Enter Y to erase the information. You're
asked to provide the information a second time.
Next steps
You have successfully connected Feather M0 WiFi to your IoT hub and sent the captured sensor data to your IoT
hub.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
Manage cloud device messaging with iothub-explorer
Save your Azure IoT hub messages to Azure data storage
Use Power BI to visualize real-time sensor data from your IoT hub
Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
Manage devices with iothub-explorer
Use Logic Apps for remote monitoring and notifications
How to upgrade your IoT hub
5/29/2018 • 1 min to read • Edit Online
As your IoT solution grows, Azure IoT Hub is ready to help you scale up. Azure IoT Hub offers two tiers, basic (B )
and standard (S ), to accommodate customers that want to use different features. Within each tier are three sizes
(1, 2, and 3) that determine the number of messages that can be sent each day.
When you have more devices and need more capabilities, there are three ways to adjust your IoT hub to suit your
needs:
Add units within the IoT hub. For example, each additional unit in a B1 IoT hub allows for an additional
400,000 messages per day.
Change the size of the IoT hub. For example, migrate from the B1 tier to the B2 tier to increase the amount of
messages that each unit can support per day.
Upgrade to a higher tier. For example, upgrade from the B1 tier to the S1 tier for the same messaging capacity
but with the advanced features that come in the standard tier.
These changes can all occur without interrupting existing operations.
If you want to downgrade your IoT hub, you can remove units and reduce the size of the IoT hub. However, you
cannot downgrade to a lower tier. For example, you can move from the S2 tier to the S1 tier, but not from the S2
tier to the B1 tier.
These examples are meant to help you understand how to adjust your IoT hub as your solution changes. For
specific information about each tier's capabilities you should always refer to Azure IoT Hub pricing.
4. To change the number of units in your hub, enter a new value under IoT Hub units.
5. Select Save to save your changes.
Your IoT hub is now adjusted, and your configurations are unchanged.
Next steps
Get more details about How to choose the right IoT Hub tier.
Understand IoT Hub metrics
5/29/2018 • 6 min to read • Edit Online
IoT Hub metrics give you better data about the state of the Azure IoT resources in your Azure subscription. IoT
Hub metrics enable you to assess the overall health of the IoT Hub service and the devices connected to it. User-
facing statistics are important because they help you see what is going on with your IoT hub and help root-cause
issues without needing to contact Azure support.
Metrics are enabled by default. You can view IoT Hub metrics from the Azure portal.
3. From the metrics blade, you can view the metrics for your IoT hub and create custom views of your
metrics. You can choose to send your metrics data to an Event Hubs endpoint or an Azure Storage
account by clicking Diagnostics settings.
IoT Hub metrics and how to use them
IoT Hub provides several metrics to give you an overview of the health of your hub and the total number of
connected devices. You can combine information from multiple metrics to paint a bigger picture of the state of
the IoT hub. The following table describes the metrics each IoT hub tracks, and how each metric relates to the
overall status of the IoT hub.
d2c.twin.read.failure Failed twin reads Count Total The count of all failed
from devices device-initiated twin
reads.
d2c.twin.update.failur Failed twin updates Count Total The count of all failed
e from devices device-initiated twin
updates.
c2d.methods.failure Failed direct method Count Total The count of all failed
invocations direct method calls.
c2d.twin.read.failure Failed twin reads Count Total The count of all failed
from back end back-end-initiated
twin reads.
c2d.twin.update.failur Failed twin updates Count Total The count of all failed
e from back end back-end-initiated
twin updates.
METRIC METRIC DISPLAY NAME UNIT AGGREGATION TYPE DESCRIPTION
twinQueries.failure Failed twin queries Count Total The count of all failed
twin queries.
jobs.listJobs.failure Failed calls to list jobs Count Total The count of all failed
calls to list jobs.
jobs.queryJobs.failure Failed job queries Count Total The count of all failed
calls to query jobs.
Next steps
Now that you’ve seen an overview of IoT Hub metrics, follow this link to learn more about managing Azure IoT
Hub:
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
Use IP filters
5/29/2018 • 3 min to read • Edit Online
Security is an important aspect of any IoT solution based on Azure IoT Hub. Sometimes you need to explicitly
specify the IP addresses from which devices can connect as part of your security configuration. The IP filter feature
enables you to configure rules for rejecting or accepting traffic from specific IPv4 addresses.
When to use
There are two specific use-cases when it is useful to block the IoT Hub endpoints for certain IP addresses:
Your IoT hub should receive traffic only from a specified range of IP addresses and reject everything else. For
example, you are using your IoT hub with Azure Express Route to create private connections between an IoT
hub and your on-premises infrastructure.
You need to reject traffic from IP addresses that have been identified as suspicious by the IoT hub administrator.
Default setting
By default, the IP Filter grid in the portal for an IoT hub is empty. This default setting means that your hub accepts
connections any IP address. This default setting is equivalent to a rule that accepts the 0.0.0.0/0 IP address range.
After you save the rule, you see an alert notifying you that the update is in progress.
The Add option is disabled when you reach the maximum of 10 IP filter rules.
You can edit an existing rule by double-clicking the row that contains the rule.
NOTE
Rejecting IP addresses can prevent other Azure Services (such as Azure Stream Analytics, Azure Virtual Machines, or the
Device Explorer in the portal) from interacting with the IoT hub.
WARNING
If you use Azure Stream Analytics (ASA) to read messages from an IoT hub with IP filtering enabled, use the Event Hub-
compatible name and endpoint of your IoT Hub in the ASA connection string.
Automatic device management in Azure IoT Hub automates many of the repetitive and complex tasks of
managing large device fleets over the entirety of their lifecycles. With automatic device management, you can
target a set of devices based on their properties, define a desired configuration, and let IoT Hub update devices
whenever they come into scope. This is performed using an automatic device configuration, which will also allow
you to summarize completion and compliance, handle merging and conflicts, and roll out configurations in a
phased approach.
NOTE
The features described in this article are only available in the standard tier of IoT hub. For more information about the basic
and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Automatic device configurations work by updating a set of device twins with desired properties and reporting a
summary based on device twin reported properties. It introduces a new class and JSON document called a
Configuration which has three parts:
The target condition defines the scope of device twins to be updated. The target condition is specified as
a query on device twin tags and/or reported properties.
The target content defines the desired properties to be added or updated in the targeted device twins.
The content includes a path to the section of desired properties to be changed.
The metrics define the summary counts of various configuration states such as Success, In Progress, and
Error. Custom metrics are specified as queries on device twin reported properties. System metrics are
default metrics that measure twin update status, such as the number of device twins that are targeted and
the number of twins that have been successfully updated.
NOTE
During preview, this feature is not available for IoT Hubs in East US, West US, North Europe, and West Europe regions.
Create a configuration
1. In the Azure portal, go to your IoT hub.
2. Select IoT device configuration (preview).
3. Select Add Configuration.
There are five steps to create a configuration. The following sections walk through each one.
Step 1: Name and Label
1. Give your configuration a unique name that is up to 128 lowercase letters. Avoid spaces and the following
invalid characters: & ^ [ ] { } \ | " < > / .
2. Add labels to help track your configurations. Labels are Name, Value pairs that describe your configuration.
For example, HostPlatform, Linux or Version, 3.0.1 .
3. Select Next to move to step two.
Step 2: Specify Settings
This section specifies the target content to be set in targeted device twins. There are two inputs for each set of
settings. The first is the device twin path, which is the path to the JSON section within the twin desired properties
that will be set. The second is the JSON content to be inserted in that section. For example, set the Device Twin
Path and Content to the following:
You can also set individual settings by specifying the entire path in the Device Twin Path and the value in the
Content with no brackets. For example, set the Device Twin Path to properties.desired.chiller-water.temperature
and set the Content to: 66
If two or more configurations target the same Device Twin Path, the Content from the highest priority
configuration will apply (priority is defined in Step 4).
If you wish to remove a property, specify the property value to null .
You can add additional settings by selecting Add Device Twin Setting
Step 3: Specify Metrics (optional)
Metrics provide summary counts of the various states that a device may report back as a result of applying
configuration content. For example, you may create a metric for pending settings changes, a metric for errors, and
a metric for successful settings changes.
1. Enter a name for Metric Name
2. Enter a query for Metric Criteria. The query is based on device twin reported properties. The metric
represents the number of rows returned by the query.
For example: SELECT deviceId FROM devices WHERE properties.reported.chillerWaterSettings.status='pending'
You can include a clause that the configuration was applied, for example:
SELECT deviceId FROM devices WHERE configurations.[[yourconfigname]].status='Applied' including the double
brackets.
Step 4: Target Devices
Use the tags property from your device twins to target the specific devices that should receive this configuration.
You can also target devices by device twin reported properties.
Since multiple configurations may target the same device, you should give each configuration a priority number.
If there's ever a conflict, the configuration with the highest priority wins.
1. Enter a positive integer for the configuration Priority. Highest numerical value is considered the highest
priority. If two configurations have the same priority number, the one that was created most recently wins.
2. Enter a Target condition to determine which devices will be targeted with this configuration. The condition is
based on device twin tags or device twin reported properties and should match the expression format. For
example, tags.environment='test' or properties.reported.chillerProperties.model='4000x' .
3. Select Next to move on to the final step.
Step 5: Review Configuration
Review your configuration information, then select Submit.
Monitor a configuration
To view the details of a configuration and monitor the devices running it, use the following steps:
1. In the Azure portal, go to your IoT hub.
2. Select IoT device configuration (preview).
3. Inspect the configuration list. For each configuration, you can view the following details:
ID - the name of the configuration.
Target condition - the query used to define targeted devices.
Priority - the priority number assigned to the configuration.
Creation time - the timestamp from when the configuration was created. This timestamp is used to
break ties when two configurations have the same priority.
System metrics - metrics that are calculated by IoT Hub and cannot be customized by developers.
Targeted specifies the number of device twins that match the target condition. Applies specified the
number of device twins that have been modified by the configuration, which can include partial
modifications in the event that a separate, higher priority configuration also made changes.
Custom metrics - metrics that have been specified by the developer as queries against device twin
reported properties. Up to five custom metrics can be defined per configuration.
4. Select the configuration that you want to monitor.
5. Inspect the configuration details. You can use tabs to view specific details about the devices that received the
configuration:
Target Condition - the devices that match the target condition.
Metrics - a list of system metrics and custom metrics. You can view a list of devices that are counted for
each metric by selecting the metric in the drop-down and then selecting View Devices.
Device Twin Settings - the device twin settings that are set by the configuration.
Configuration Labels - key-value pairs used to describe a configuration. Labels have no impact on
functionality.
Modify a configuration
When you modify a configuration, the changes immediately replicate to all targeted devices.
If you update the target condition, the following updates occur:
If a device twin didn't meet the old target condition, but meets the new target condition and this configuration
is the highest priority for that device twin, then this configuration is applied to the device twin.
If a device twin no longer meets the target condition, the settings from the configuration will be removed and
the device twin will be modified by the next highest priority configuration.
If a device twin currently running this configuration no longer meets the target condition and doesn't meet the
target condition of any other configurations, then the settings from the configuration will be removed and no
other changes will be made on the twin.
To modify a configuration, use the following steps:
1. In the Azure portal, go to your IoT hub.
2. Select IoT device configuration (preview).
3. Select the configuration that you want to modify.
4. Make updates to the following fields:
Target condition
Labels
Priority
Metrics
5. Select Save.
6. Follow the steps in [Monitor a configuration][anchor-monitor] to watch the changes roll out.
Delete a configuration
When you delete a configuration, any device twins take on their next highest priority configuration. If device twins
don't meet the target condition of any other configuration, then no other settings are applied.
1. In the Azure portal, go to your IoT hub.
2. Select IoT device configuration (preview).
3. Use the checkbox to select the configuration that you want to delete.
4. Select Delete.
5. A prompt will ask you to confirm.
Next steps
In this article, you learned how configure and monitor IoT devices at scale. Follow these links to learn more about
managing Azure IoT Hub:
Manage your IoT Hub device identities in bulk
IoT Hub metrics
Operations monitoring
To further explore the capabilities of IoT Hub, see:
IoT Hub developer guide
Deploying AI to edge devices with Azure IoT Edge
To explore using the IoT Hub Device Provisioning Service to enable zero-touch, just-in-time provisioning, see:
Azure IoT Hub Device Provisioning Service
Summary of customer data request features
5/29/2018 • 2 min to read • Edit Online
The Azure IoT Hub is a REST API-based cloud service targeted at enterprise customers that enables secure, bi-
directional communication between millions of devices and a partitioned Azure service.
NOTE
This article provides steps for how to delete personal data from the device or service and can be used to support your
obligations under the GDPR. If you’re looking for general info about GDPR, see the GDPR section of the Service Trust portal.
Individual devices are assigned a device identifier (device ID ) by a tenant administrator. Device data is based on the
assigned device ID. Microsoft maintains no information and has no access to data that would allow device ID to
user correlation.
Many of the devices managed in Azure IoT Hub are not personal devices, for example an office thermostat or
factory robot. Customers may, however, consider some devices to be personally identifiable and at their discretion
may maintain their own asset or inventory tracking methods that tie devices to individuals. Azure IoT Hub
manages and stores all data associated with devices as if it were personal data.
Tenant administrators can use either the Azure portal or the service's REST APIs to fulfill information requests by
exporting or deleting data associated with a device ID.
If you use the routing feature of the Azure IoT Hub service to forward device messages to other services, then data
requests must be performed by the tenant admin for each routing endpoint in order to complete a full request for a
given device. See each endpoint's reference documentation for further details. For more information about
supported endpoints, see Reference - IoT Hub endpoints.
If you use the Azure Event Grid integration feature of the Azure IoT Hub service, then data requests must be
performed by the tenant admin for each subscriber of these events. For more information, see React to IoT Hub
events by using Event Grid.
If you use the Azure Monitor integration feature of the Azure IoT Hub service to create diagnostic logs, then data
requests must be performed by the tenant admin against the stored logs. For more information, see Monitor the
health of Azure IoT Hub.