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 for Python and is included in the Azure IoT Hub Device SDK for Python. 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, you need Python 3.7 on your development machine. You can download the latest recommended version for multiple platforms from python.org. You can check your Python version with the following command:
python --version
In your local python environment install the package as follows:
pip install azure-iot-device
Clone the Python SDK IoT repository and check out master:
git clone https://github.com/Azure/azure-iot-sdk-python
The azure-iot-sdk-python\azure-iot-device\samples\pnp folder contains the sample code for the IoT Plug and Play device. This quickstart uses the simple_thermostat.py file. This sample code implements an IoT Plug and Play compatible device and uses the Azure IoT Python Device Client Library.
Open the simple_thermostat.py file in a text editor. Notice how it:
-
Defines a single device twin model identifier (DTMI) that uniquely represents the Thermostat. A DTMI must be known to the user and varies dependent on the scenario of device implementation. For the current sample, the model represents a thermostat that has telemetry, properties, and commands associated with monitoring temperature.
-
Has functions to define command handler implementations. You write these handlers to define how the device responds to command requests.
-
Has a function to define a command response. You create command response functions to send a response back to your IoT hub.
-
Defines an input keyboard listener function to let you quit the application.
-
Has a main function. The main function:
-
Uses the device SDK to create a device client and connect to your IoT hub.
-
Updates properties. The model we are using, Thermostat, defines
targetTemperature
andmaxTempSinceLastReboot
as the two properties for our Thermostat, so that is what we will be using. Properties are updated using thepatch_twin_reported_properties
method defined on thedevice_client
. -
Starts listening for command requests using the execute_command_listener function. The function sets up a 'listener' to listen for commands coming from the service. When you set up the listener you provide a
method_name
,user_command_handler
, andcreate_user_response_handler
.- The
user_command_handler
function defines what the device should do when it receives a command. For instance, if your alarm goes off, the effect of receiving this command is you wake up. Think of this as the 'effect' of the command being invoked. - The
create_user_response_handler
function creates a response to be sent to your IoT hub when a command executes successfully. For instance, if your alarm goes off, you respond by hitting snooze, which is feedback to the service. Think of this as the reply you give to the service. You can view this response in the portal.
- The
-
Starts sending telemetry. The pnp_send_telemetry is defined in the pnp_methods.py file. The sample code uses a loop to call this function every eight seconds.
-
Disables all the listeners and tasks, and exist the loop when you press Q or q.
-
[!INCLUDE iot-pnp-environment]
To learn more about the sample configuration, see the sample readme.
Now that you've seen the code, use the following command to run the sample:
python simple_thermostat.py
You see the following output, which indicates the device is sending telemetry data to the hub, and is now ready to receive commands and property updates:
Listening for command requests and property updates
Press Q to quit
Sending telemetry for temperature
Sent message
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]