title | description | author | manager | ms.service | services | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|
Get started with Azure IoT Hub device management (Python) | Microsoft Docs |
How to use IoT Hub device management to initiate a remote device reboot. You use the Azure IoT SDK for Python to implement a simulated device app that includes a direct method and a service app that invokes the direct method. |
kgremban |
timlt |
iot-hub |
iot-hub |
python |
conceptual |
01/02/2018 |
kgremban |
[!INCLUDE iot-hub-selector-dm-getstarted]
This tutorial shows you how to:
- Use the Azure portal to create an IoT Hub and create a device identity in your IoT hub.
- Create a simulated device app that contains a direct method that reboots that device. Direct methods are invoked from the cloud.
- Create a Python console app that calls the reboot direct method in the simulated device app through your IoT hub.
At the end of this tutorial, you have two Python console apps:
dmpatterns_getstarted_device.py, which connects to your IoT hub with the device identity created earlier, receives a reboot direct method, simulates a physical reboot, and reports the time for the last reboot.
dmpatterns_getstarted_service.py, which calls a direct method in the simulated device app, displays the response, and displays the updated reported properties.
To complete this tutorial, you need the following:
- Python 2.x or 3.x. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable. If you are using Python 2.x, you may need to install or upgrade pip, the Python package management system.
- Install the azure-iothub-device-client package, using the command
pip install azure-iothub-device-client
- Install the azure-iothub-service-client package, using the command
pip install azure-iothub-service-client
- Install the azure-iothub-device-client package, using the command
- If you are using Windows OS, then Visual C++ redistributable package to allow the use of native DLLs from Python.
- An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
[!INCLUDE iot-hub-include-create-hub]
[!INCLUDE iot-hub-include-find-connection-string]
In this section, you will:
- Create a Python console app that responds to a direct method called by the cloud
- Simulate a device reboot
- Use the reported properties to enable device twin queries to identify devices and when they last rebooted
-
Using a text editor, create a dmpatterns_getstarted_device.py file.
-
Add the following
import
statements at the start of the dmpatterns_getstarted_device.py file.import random import time, datetime import sys import iothub_client from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult, IoTHubError, DeviceMethodReturnValue
-
Add variables including a CONNECTION_STRING variable and the client intialization. Replace the connection string with your device connection string.
CONNECTION_STRING = "{deviceConnectionString}" PROTOCOL = IoTHubTransportProvider.MQTT CLIENT = IoTHubClient(CONNECTION_STRING, PROTOCOL) WAIT_COUNT = 5 SEND_REPORTED_STATE_CONTEXT = 0 METHOD_CONTEXT = 0 SEND_REPORTED_STATE_CALLBACKS = 0 METHOD_CALLBACKS = 0
-
Add the following function callbacks to implement the direct method on the device.
def send_reported_state_callback(status_code, user_context): global SEND_REPORTED_STATE_CALLBACKS print ( "Device twins updated." ) def device_method_callback(method_name, payload, user_context): global METHOD_CALLBACKS if method_name == "rebootDevice": print ( "Rebooting device..." ) time.sleep(20) print ( "Device rebooted." ) current_time = str(datetime.datetime.now()) reported_state = "{\"rebootTime\":\"" + current_time + "\"}" CLIENT.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT) print ( "Updating device twins: rebootTime" ) device_method_return_value = DeviceMethodReturnValue() device_method_return_value.response = "{ \"Response\": \"This is the response from the device\" }" device_method_return_value.status = 200 return device_method_return_value
-
Start the direct method listener and wait.
def iothub_client_init(): if CLIENT.protocol == IoTHubTransportProvider.MQTT or client.protocol == IoTHubTransportProvider.MQTT_WS: CLIENT.set_device_method_callback(device_method_callback, METHOD_CONTEXT) def iothub_client_sample_run(): try: iothub_client_init() while True: print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" ) status_counter = 0 while status_counter <= WAIT_COUNT: time.sleep(10) status_counter += 1 except IoTHubError as iothub_error: print ( "Unexpected error %s from IoTHub" % iothub_error ) return except KeyboardInterrupt: print ( "IoTHubClient sample stopped" ) if __name__ == '__main__': print ( "Starting the IoT Hub Python sample..." ) print ( " Protocol %s" % PROTOCOL ) print ( " Connection string=%s" % CONNECTION_STRING ) iothub_client_sample_run()
-
Save and close the dmpatterns_getstarted_device.py file.
Note
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry policies (such as an exponential backoff), as suggested in the article, Transient Fault Handling.
In this section, you create a Python console app that initiates a remote reboot on a device using a direct method. The app uses device twin queries to discover the last reboot time for that device.
-
Using a text editor, create a dmpatterns_getstarted_service.py file.
-
Add the following
import
statements at the start of the dmpatterns_getstarted_service.py file.import sys, time import iothub_service_client from iothub_service_client import IoTHubDeviceMethod, IoTHubError, IoTHubDeviceTwin
-
Add the following variable declarations. Only replace placeholder values for IoTHubConnectionString and deviceId.
CONNECTION_STRING = "{IoTHubConnectionString}" DEVICE_ID = "{deviceId}" METHOD_NAME = "rebootDevice" METHOD_PAYLOAD = "{\"method_number\":\"42\"}" TIMEOUT = 60 WAIT_COUNT = 10
-
Add the following function to invoke the device method to reboot the target device, then query for the device twins and get the last reboot time.
def iothub_devicemethod_sample_run(): try: iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING) iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING) print ( "" ) print ( "Invoking device to reboot..." ) response = iothub_device_method.invoke(DEVICE_ID, METHOD_NAME, METHOD_PAYLOAD, TIMEOUT) print ( "" ) print ( "Successfully invoked the device to reboot." ) print ( "" ) print ( response.payload ) while True: print ( "" ) print ( "IoTHubClient waiting for commands, press Ctrl-C to exit" ) status_counter = 0 while status_counter <= WAIT_COUNT: twin_info = iothub_twin_method.get_twin(DEVICE_ID) if twin_info.find("rebootTime") != -1: print ( "Last reboot time: " + twin_info[twin_info.find("rebootTime")+11:twin_info.find("rebootTime")+37]) else: print ("Waiting for device to report last reboot time...") time.sleep(5) status_counter += 1 except IoTHubError as iothub_error: print ( "" ) print ( "Unexpected error {0}".format(iothub_error) ) return except KeyboardInterrupt: print ( "" ) print ( "IoTHubDeviceMethod sample stopped" ) if __name__ == '__main__': print ( "Starting the IoT Hub Service Client DeviceManagement Python sample..." ) print ( " Connection string = {0}".format(CONNECTION_STRING) ) print ( " Device ID = {0}".format(DEVICE_ID) ) iothub_devicemethod_sample_run()
-
Save and close the dmpatterns_getstarted_service.py file.
You are now ready to run the apps.
-
At the command prompt, run the following command to begin listening for the reboot direct method.
python dmpatterns_getstarted_device.py
-
At another command prompt, run the following command to trigger the remote reboot and query for the device twin to find the last reboot time.
python dmpatterns_getstarted_service.py
-
You see the device response to the direct method in the console.
[!INCLUDE iot-hub-dm-followup]