Skip to content

Latest commit

 

History

History
171 lines (105 loc) · 8.66 KB

quickstart-send-telemetry-java.md

File metadata and controls

171 lines (105 loc) · 8.66 KB
title description author manager ms.author ms.service services ms.devlang ms.topic ms.custom ms.date
Quickstart: Send telemetry to Azure IoT Hub with Java
In this quickstart, you run two sample Java applications to send simulated telemetry to an IoT hub and to read telemetry from the IoT hub for processing in the cloud.
wesmc7777
philmea
wesmc
iot-hub
iot-hub
java
quickstart
mvc
seo-java-august2019
seo-java-september2019
mqtt
devx-track-java
05/26/2020

Quickstart: Send telemetry to an Azure IoT hub and read it with a Java application

[!INCLUDE iot-hub-quickstarts-1-selector]

In this quickstart, you send telemetry to Azure IoT Hub and read it with a Java application. 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. This 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.

Prerequisites

You can verify the current version of Java on your development machine using the following command:

java -version

You can verify the current version of Maven on your development machine using the following command:

mvn --version

[!INCLUDE cloud-shell-try-it.md]

Add Azure IoT Extension

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-iot

[!INCLUDE iot-hub-cli-version-info]

Create an IoT hub

[!INCLUDE iot-hub-include-create-hub]

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.

  1. Run the following command in Azure Cloud Shell to create the device identity.

    YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.

    MyJavaDevice: This is the name of the device you're registering. It's recommended to use MyJavaDevice as shown. If you choose a different name for your device, you'll also need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az iot hub device-identity create --hub-name {YourIoTHubName} --device-id MyJavaDevice
    
  2. Run the following command 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 chose 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:

    HostName={YourIoTHubName}.azure-devices.net;DeviceId=MyJavaDevice;SharedAccessKey={YourSharedAccessKey}

    You'll use this value later in the quickstart.

  3. You also need the Event Hubs-compatible endpoint, Event Hubs-compatible path, and service 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 chose 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 service --query primaryKey --hub-name {YourIoTHubName}
    

    Make a note of these three values, which you'll 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.

  1. 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.

  2. 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 connString variable with the device connection string you made a note of earlier. Then save your changes to SimulatedDevice.java.

  3. In the local terminal window, run the following commands to install the required libraries and build the simulated device application:

    mvn clean package
    
  4. 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:

    Output from telemetry sent by the device 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 local terminal window, navigate to the root folder of the sample Java project. Then navigate to the iot-hub\Quickstarts\read-d2c-messages folder.

  2. 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.

    Variable Value
    EVENT_HUBS_COMPATIBLE_ENDPOINT Replace the value of the variable with the Event Hubs-compatible endpoint you made a note of earlier.
    EVENT_HUBS_COMPATIBLE_PATH Replace the value of the variable with the Event Hubs-compatible path you made a note of earlier.
    IOT_HUB_SAS_KEY Replace the value of the variable with the service primary key you made a note of earlier.
  3. In the local terminal window, run the following commands to install the required libraries and build the back-end application:

    mvn clean package
    
  4. 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:

    Output as back-end application receives telemetry sent to your IoT hub

Clean up resources

[!INCLUDE iot-hub-quickstarts-clean-up-resources]

Next steps

In this quickstart, you set up 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.

[!div class="nextstepaction"] Quickstart: Control a device connected to an IoT hub