Skip to content

Latest commit

 

History

History
125 lines (82 loc) · 5.34 KB

iot-pnp-service-java.md

File metadata and controls

125 lines (82 loc) · 5.34 KB
author ms.author ms.service ms.topic ms.date
dominicbetts
dobett
iot-pnp
include
11/20/2020

IoT Plug and Play simplifies IoT by enabling you to interact with a device's capabilities without knowledge of the underlying device implementation. This quickstart shows you how to use Java to connect to and control an IoT Plug and Play device that's connected to your solution.

Prerequisites

[!INCLUDE iot-pnp-prerequisites]

To complete this quickstart on Windows, install the following software on your local Windows environment:

Clone the SDK repository with the sample code

If you completed Quickstart: Connect a sample IoT Plug and Play device application running on Windows to IoT Hub (Java), you've already cloned the repository.

Open a command prompt in the directory of your choice. Execute the following command to clone the Microsoft Azure IoT SDKs for Java GitHub repository into this location:

git clone https://github.com/Azure/azure-iot-sdk-java.git

Build and run the sample device

[!INCLUDE iot-pnp-environment]

To learn more about the sample configuration, see the sample readme.

In this quickstart, you use a sample thermostat device that's written in Java as the IoT Plug and Play device. To run the sample device:

  1. Open a terminal window and navigate to the local folder that contains the Microsoft Azure IoT SDK for Java repository you cloned from GitHub.

  2. This terminal window is used as your device terminal. Go to the root folder of your cloned repository. Install all the dependencies by running the following command:

  3. Run the following command to build the sample device application:

    mvn install -T 2C -DskipTests
  4. To run the sample device application, navigate to the device\iot-device-samples\pnp-device-sample\thermostat-device-sample folder and run the following command:

    mvn exec:java -Dexec.mainClass="samples.com.microsoft.azure.sdk.iot.device.Thermostat"

The device is now ready to receive commands and property updates, and has started sending telemetry data to the hub. Keep the sample device running as you complete the next steps.

Run the sample solution

In Set up your environment for the IoT Plug and Play quickstarts and tutorials you created two environment variables to configure the sample to connect to your IoT hub and device:

  • IOTHUB_CONNECTION_STRING: the IoT hub connection string you made a note of previously.
  • IOTHUB_DEVICE_ID: "my-pnp-device".

In this quickstart, you use a sample IoT solution written in Java to interact with the sample device you just set up.

Note

This sample uses the com.microsoft.azure.sdk.iot.service namespace from the IoT Hub service client. To learn more about the APIs, including the digital twins API, see the service developer guide.

  1. Open another terminal window to use as your service terminal.

  2. In the cloned Java SDK repository, navigate to the service\iot-service-samples\pnp-service-sample\thermostat-service-sample folder.

  3. To run the sample service application, run the following command:

    mvm exec:java -Dexec.mainClass="samples.com.microsoft.azure.sdk.iot.service.Thermostat"

Get device twin

The following code snippet shows how to retrieve the device twin in the service:

 // Get the twin and retrieve model Id set by Device client.
DeviceTwinDevice twin = new DeviceTwinDevice(deviceId);
twinClient.getTwin(twin);
System.out.println("Model Id of this Twin is: " + twin.getModelId());

Update a device twin

The following code snippet shows you how to use a patch to update properties through the device twin:

String propertyName = "targetTemperature";
double propertyValue = 60.2;
twin.setDesiredProperties(Collections.singleton(new Pair(propertyName, propertyValue)));
twinClient.updateTwin(twin);

The device output shows how the device responds to this property update.

Invoke a command

The following code snippet shows you call a command on the device:

// The method to invoke for a device without components should be "methodName" as defined in the DTDL.
String methodToInvoke = "getMaxMinReport";
System.out.println("Invoking method: " + methodToInvoke);

Long responseTimeout = TimeUnit.SECONDS.toSeconds(200);
Long connectTimeout = TimeUnit.SECONDS.toSeconds(5);

// Invoke the command.
String commandInput = ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(5).format(DateTimeFormatter.ISO_DATE_TIME);
MethodResult result = methodClient.invoke(deviceId, methodToInvoke, responseTimeout, connectTimeout, commandInput);
if(result == null)
{
    throw new IOException("Method result is null");
}

System.out.println("Method result status is: " + result.getStatus());

The device output shows how the device responds to this command.