Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
183 lines (111 sloc) 9.43 KB
title description author manager ms.author ms.service services ms.devlang ms.topic ms.custom ms.date
Quickstart: Control a device from Azure IoT Hub with Java
In this quickstart, you run two sample Java applications. One application is a back-end application that can remotely control devices connected to your hub. The other application simulates a device connected to your hub that can be controlled remotely.
wesmc7777
philmea
wesmc
iot-hub
iot-hub
java
quickstart
mvc, seo-java-august2019, seo-java-september2019
06/21/2019

Quickstart: Control a device connected to an Azure IoT hub with Java

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

IoT Hub is an Azure service that enables you to manage your IoT devices from the cloud, and ingest high volumes of device telemetry to the cloud for storage or processing. In this quickstart, you use a direct method to control a simulated device connected to your Azure IoT hub with a Java application. 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.

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

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 on your development machine.

You can download Java SE Development Kit 8 for multiple platforms from Java long-term support for Azure and Azure Stack. Make sure you select Java 8 under Long-term support to get to downloads for JDK 8.

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

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

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.

Create an IoT hub

If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step.

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

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

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

    You use this value later in the quickstart.

Retrieve the service connection string

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:

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

az iot hub show-connection-string --policy-name service --name {YourIoTHubName} --output table

Make a note of the service connection string, which looks like:

HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}

You use this value later in the quickstart. This service connection string is different from the device connection string you noted in the previous step.

Listen for direct method calls

The simulated device application connects to a device-specific endpoint on your IoT hub, sends simulated telemetry, and listens for direct method calls from your hub. In this quickstart, the direct method call from the hub tells the device to change the interval at which it sends telemetry. The simulated device sends an acknowledgment back to your hub after it executes the direct method.

  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-2 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-2-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 the telemetry sent by the device 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 acknowledgments. An IoT Hub back-end application typically runs in the cloud.

  1. In another local 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 earlier. Then save your changes to BackEndApplication.java.

  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/back-end-application-1.0.0-with-deps.jar
    

    The following screenshot shows the output as the application makes a direct method call to the device and receives an acknowledgment:

    Output as the application makes a direct method call through your IoT hub

    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:

    Console message from device shows the rate at which it changes

Clean up resources

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

Next steps

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

[!div class="nextstepaction"] Tutorial: Route telemetry to different endpoints for processing

You can’t perform that action at this time.