Quickstart: Send telemetry from a device to an IoT hub and read it with a back-end application (Java)
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.
Open Azure Cloud Shell
Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Select Copy to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:
|Select Try It in the upper-right corner of a code block.|
|Open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal.|
If you don’t have an Azure subscription, create a free account before you begin.
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:
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:
Run the following command to add the Microsoft Azure IoT Extension for Azure CLI to your Cloud Shell instance. The IOT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.
az extension add --name azure-cli-iot-ext
Download the sample Java project from https://github.com/Azure-Samples/azure-iot-samples-java/archive/master.zip and extract the ZIP archive.
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
Log in to the Azure portal.
Choose +Create a resource, then choose Internet of Things.
Click Iot Hub from the list on the right. You see the first screen for creating an IoT hub.
Fill in the fields.
Subscription: Select the subscription to use for your IoT hub.
Resource Group: You can create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list. For more information, see Manage Azure Resource Manager resource groups.
Region: This is the region in which you want your hub to be located. Select the location closest to you from the dropdown list.
IoT Hub Name: Put in the name for your IoT Hub. This name must be globally unique. If the name you enter is available, a green check mark appears.
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.
Click Next: Size and scale to continue creating your IoT hub.
On this screen, you can take the defaults and just click Review + create at the bottom.
Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. Each Azure subscription can create one IoT Hub in the free tier.
IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.
For details about the other tier options, see Choosing the right IoT Hub tier.
Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs only need four partitions.
Click Review + create to review your choices. You see something similar to this screen.
Click Create to create your new IoT hub. Creating the hub takes a few minutes.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure Cloud Shell to register a simulated device.
Run the following command in Azure Cloud Shell to create the device identity.
YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.
MyJavaDevice: The name of the device you're registering. Use MyJavaDevice as shown. If you choose a different name for your device, you need to use that name throughout this article, and update the device name in the sample applications before you run them.
az iot hub device-identity create --hub-name YourIoTHubName --device-id MyJavaDevice
Run the following commands in Azure Cloud Shell to get the device connection string for the device you just registered: **YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.
az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id MyJavaDevice --output table
Make a note of the device connection string, which looks like:
You use this value later in the quickstart.
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:
**YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.
az iot hub show --query properties.eventHubEndpoints.events.endpoint --name YourIoTHubName az iot hub show --query properties.eventHubEndpoints.events.path --name YourIoTHubName az iot hub policy show --name iothubowner --query primaryKey --hub-name YourIoTHubName
Make a note of these three values, which you use later in the quickstart.
Send simulated telemetry
The simulated device application connects to a device-specific endpoint on your IoT hub and sends simulated temperature and humidity telemetry.
In a local terminal window, navigate to the root folder of the sample Java project. Then navigate to the iot-hub\Quickstarts\simulated-device folder.
Open the src/main/java/com/microsoft/docs/iothub/samples/SimulatedDevice.java file in a text editor of your choice.
Replace the value of the
connStringvariable with the device connection string you made a note of previously. Then save your changes to SimulatedDevice.java file.
In the local terminal window, run the following commands to install the required libraries and build the simulated device application:
mvn clean package
In the local terminal window, run the following commands to run the simulated device application:
java -jar target/simulated-device-1.0.0-with-deps.jar
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.
In another local terminal window, navigate to the root folder of the sample Java project. Then navigate to the iot-hub\Quickstarts\read-d2c-messages folder.
Open the src/main/java/com/microsoft/docs/iothub/samples/ReadDeviceToCloudMessages.java file in a text editor of your choice. Update the following variables and save your changes to the file.
Replace the value of the variable with the Event Hubs-compatible endpoint you made a note of previously.
Replace the value of the variable with the Event Hubs-compatible path you made a note of previously.
Replace the value of the variable with the iothubowner primary key you made a note of previously.
In the local terminal window, run the following commands to install the required libraries and build the back-end application:
mvn clean package
In the local terminal window, run the following commands to run the back-end application:
java -jar target/read-d2c-messages-1.0.0-with-deps.jar
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 will be continuing to the next recommended article, you can keep the resources you've already created and reuse them.
Otherwise, you can delete the Azure resources created in this article to avoid charges.
Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the IoT Hub inside an existing resource group that contains resources you want to keep, only delete the IoT Hub resource itself instead of deleting the resource group.
To delete a resource group by name:
Sign in to the Azure portal and click Resource groups.
In the Filter by name... textbox, type the name of the resource group containing your IoT Hub.
To the right of your resource group in the result list, click ... then Delete resource group.
You will be asked to confirm the deletion of the resource group. Type the name of your resource group again to confirm, and then click Delete. After a few moments, the resource group and all of its contained resources are deleted.
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.
Send feedback about: