author | ms.author | ms.service | ms.topic | ms.date |
---|---|---|---|---|
dominicbetts |
dobett |
iot-pnp |
include |
11/20/2020 |
This quickstart shows you how to build a sample IoT Plug and Play device application, connect it to your IoT hub, and use the Azure IoT explorer tool to view the telemetry it sends. The sample application is written in Java and is included in the Azure IoT device SDK for Java. A solution builder can use the Azure IoT explorer tool to understand the capabilities of an IoT Plug and Play device without the need to view any device code.
[!INCLUDE iot-pnp-prerequisites]
To complete this quickstart on Windows, install the following software on your local Windows environment:
- Java SE Development Kit 8. In Java long-term support for Azure and Azure Stack, under Long-term support, select Java 8.
- Apache Maven 3.
In this quickstart, you prepare a development environment you can use to clone and build the Azure IoT Hub Device Java SDK.
Open a command prompt in the directory of your choice. Execute the following command to clone the Azure IoT Java SDKs and Libraries GitHub repository into this location:
git clone https://github.com/Azure/azure-iot-sdk-java.git
On Windows, navigate to the root folder of the cloned Java SDK repository.
Run the following command to build the sample application:
mvn install -T 2C -DskipTests
[!INCLUDE iot-pnp-environment]
To learn more about the sample configuration, see the sample readme.
Navigate to the \device\iot-device-samples\pnp-device-sample\thermostat-device-sample folder.
To run the sample application, 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 running as you complete the next steps.
After the device client sample starts, use the Azure IoT explorer tool to verify it's working.
[!INCLUDE iot-pnp-iot-explorer.md]
This sample implements a simple IoT Plug and Play thermostat device. The model this sample implements doesn't use IoT Plug and Play components. The DTDL model file for the thermostat device defines the telemetry, properties, and commands the device implements.
The device code uses the standard DeviceClient
class to connect to your IoT hub. The device sends the model ID of the DTDL model it implements in the connection request. A device that sends a model ID is an IoT Plug and Play device:
private static void initializeDeviceClient() throws URISyntaxException, IOException {
ClientOptions options = new ClientOptions();
options.setModelId(MODEL_ID);
deviceClient = new DeviceClient(deviceConnectionString, protocol, options);
deviceClient.registerConnectionStatusChangeCallback((status, statusChangeReason, throwable, callbackContext) -> {
log.debug("Connection status change registered: status={}, reason={}", status, statusChangeReason);
if (throwable != null) {
log.debug("The connection status change was caused by the following Throwable: {}", throwable.getMessage());
throwable.printStackTrace();
}
}, deviceClient);
deviceClient.open();
}
The model ID is stored in the code as shown in the following snippet:
private static final String MODEL_ID = "dtmi:com:example:Thermostat;1";
The code that updates properties, handles commands, and sends telemetry is identical to the code for a device that doesn't use the IoT Plug and Play conventions.
The sample uses a JSON library to parse JSON objects in the payloads sent from your IoT hub:
import com.google.gson.Gson;
...
Date since = new Gson().fromJson(jsonRequest, Date.class);