Azure-Samples / iot-hub-python-get-started

This Python sample app is used by the getting started content to demonstrate how easy it is to create an IoTHub Instance on Azure portal, and register a new device for the IoTHub, then send a message from device (simulated device app) to cloud.
MIT License
28 stars 18 forks source link

services: iot-hub platforms: python author: msonecode

How to get started for using IoT Hub REST API in Python

About this sample

The sample project demonstrates how to get started for using IoT Hub REST API in Python.

The REST APIs for IoT Hub offer programmatic access to the device and messaging services, as well as the resource provider, in IoT Hub. You can access messaging services within an IoT service running in Azure, or you can directly over the Internet from any application that can send an HTTP/HTTPS request and receive an HTTP/HTTPS response. You can visit the IoT Hub REST API references on Microsoft Azure official site to learn more about these APIs, requirements, parameters, request and response via HTTP methods.

In the sample project, steps at below will be proivded for us to get started.

  1. Create an IoT Hub through Azure Portal.
  2. Get the connection information from the settings on Azure portal.
  3. A simple Python script shows how to create a device identity at the IoT Hub, list all registered device identities from IoT Hub.
  4. A simple Python script shows how to send messages from the simulated device application to IoT Hub.

Create an IoT Hub through Azure Portal

To do this, refer to the article Create an IoT Hub through Azure Portal.

Get the connection information

Building the sample

You can use Eclipse with PyDev Plugin to import the sample project.

Preparation

Import the sample project

  1. Open the Eclipse.

  2. Click File -> Import.

    Click File -> Import

  3. Select General -> Existing Projects into Workspace, then click Next.

    Select General -> Existing Projects into Workspace

  4. Select the sample project path via Browse, then click Finish.

    Select the sample project path via Browse

  5. See the PyDev Package Explorer.

    See the PyDev Package Explorer

Running the sample

There are two scripts separately at the service and device directories:

You can open the Python script on Eclipse, and click the right mouse button at the script window, then click the Run As -> 2 Python Run to run the script, then see the result at the Console Window.

Click the Run As -> 2 Python Run to run the script

Code sample running result

The results for running service/deviceManager.py:

  1. The result for calling the function createDeviceId(deviceId)

    • Creating an non-existed device identity, the result as below.
      (u'{"deviceId":"iotdevice3","generationId":"635937464376399955","etag":"MA==","connectionState":"Disconnected","status":"enabled","statusReason":null,"connectionStateUpdatedTime":"0001-01-01T00:00:00","statusUpdatedTime":"0001-01-01T00:00:00","lastActivityTime":"0001-01-01T00:00:00","cloudToDeviceMessageCount":0,"authentication":{"symmetricKey":{"primaryKey":"PqZ70GzRIOv8Mfap31nzDjwqsRwt8X6VTLDUM48qDGk=","secondaryKey":"gZfLMBuwgNDOuYbJZNK8ZXLGCm5WJba4CVGvXBV/0qM="}}}', 200)
    • If trying to create an existed device identity, an error information will be shown.
    (u'{"Message":"ErrorCode:DeviceAlreadyExists;A device with ID \'iotdevice1\' is already registered.\r\nTracking Id:218b497e330b41119036553381cc63de-G:GatewayWorkerRole.11-B:1-P:8a500395-cf20-45f1-831b-e0a49c9bd5fa-TimeStamp:03/16/2016 17:26:37\r\nErrorCode:DeviceAlreadyExists"}', 409)
  2. The result for calling the function retrieveDeviceId(deviceId)

    (u'{"deviceId":"iotdevice1","generationId":"635928930091746067","etag":"MA==","connectionState":"Disconnected","status":"enabled","statusReason":null,"connectionStateUpdatedTime":"2016-03-16T09:46:02.5862088","statusUpdatedTime":"0001-01-01T00:00:00","lastActivityTime":"2016-03-16T09:46:02.5862088","cloudToDeviceMessageCount":0,"authentication":{"symmetricKey":{"primaryKey":"F+x9Sg9zVZC+TWnrQ1vXm0sYH/SAtvv6Wa5WhWnHdQo=","secondaryKey":"vZaSU6/8Mah4Chu28Vzx07/Feqe1a2EeDeUNEo9EY10="}}}', 200)
  3. The result for calling the function listDeviceId() lists all the registered device identities.

    (u'[{"deviceId":"iotdevice1","generationId":"635928930091746067","etag":"MA==","connectionState":"Disconnected","status":"enabled","statusReason":null,"connectionStateUpdatedTime":"2016-03-16T09:46:02.5862088","statusUpdatedTime":"0001-01-01T00:00:00","lastActivityTime":"2016-03-16T09:46:02.5862088","cloudToDeviceMessageCount":0,"authentication":{"symmetricKey":{"primaryKey":"F+x9Sg9zVZC+TWnrQ1vXm0sYH/SAtvv6Wa5WhWnHdQo=","secondaryKey":"vZaSU6/8Mah4Chu28Vzx07/Feqe1a2EeDeUNEo9EY10="}}}]', 200)

The result for running device/d2cMsgSender.py includes the empty content and status code 204 from the response if message is sent successfully:

(u'', 204)