Skip to content

Latest commit

 

History

History
270 lines (188 loc) · 10.7 KB

iot-hub-python-python-device-management-get-started.md

File metadata and controls

270 lines (188 loc) · 10.7 KB
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

Get started with device management (Python)

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

Create an IoT hub

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

Retrieve connection string for IoT hub

[!INCLUDE iot-hub-include-find-connection-string]

Create a simulated device app

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
  1. Using a text editor, create a dmpatterns_getstarted_device.py file.

  2. 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
  3. 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
  4. 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
  5. 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()
  6. 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.

Trigger a remote reboot on the device using a direct method

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.

  1. Using a text editor, create a dmpatterns_getstarted_service.py file.

  2. 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
  3. 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
  4. 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()
  5. Save and close the dmpatterns_getstarted_service.py file.

Run the apps

You are now ready to run the apps.

  1. At the command prompt, run the following command to begin listening for the reboot direct method.

    python dmpatterns_getstarted_device.py
    
  2. 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
    
  3. You see the device response to the direct method in the console.

[!INCLUDE iot-hub-dm-followup]