Git Product home page Git Product logo

cookiecutter-iot-hub-documentdb-python-get-started's Introduction

services platforms author
iot-hub, documentdb
python
msonecode

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

About this sample

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

The REST APIs for IoT Hub offer programmatic access to the device and messaging services, as well as the resource provder, in IoT Hub. You can access messaging services from within an IoT service running in Azure, or 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 offical site to learn more about these APIs, requirements, parameters, request and response via HTTP methods.

The Azure DocumentDB has Python SDK. You can create DocumentDB database, collection and document via SDK in the sample.

In the sample project, it will show these steps below for helping us to get started.

  1. Create an IoT Hub through Azure Portal.
  2. Get the connection information from the settings on Azure portal.
  3. Create a DocumentDB through Azure Portal
  4. Get the connection information of DocuementDB from Azure portal.
  5. A simple Python script shows how to create a device identity at the IoT Hub, list all registered device identities from IoT Hub.
  6. A simple Python script shows how to send messages from the simulated device application to IoT Hub.
  7. A simple Python script shows how to create database, create collection, create document and get document by id.

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

  • Getting the IoT Hub Hostname

    Copy the IoT Hub Hostname at the dashboard.

    Copy the IoT Hub Hostname at the dashboard

  • Getting the Shared access key and Connection string of the policy

    Click the Key button to show the Shared access policies, then select one policy, then copy the Shared access key and Connection string.

    Click the Key button to show the Shared access policies

    Copy the Shared access key and Connection string

Create DocumentDB through Azure Portal

To do this, refer to article Create a DocumentDB account.

Get DocumentDB connection and key

  • Get Connection URI and Key Click the copy for URI and Primary Key

Building the sample

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

Preparation

  • Run the installdependency.cmd to install dependency or change to the directory containing requirements.txt and run comman pip install -r requirements.txt

  • Install the Python package requests via command pip install requests

  • Download Eclipse

  • Install PyDev plugin via Eclipse Marketplace, click Help -> Eclipse Marketplace, then input PyDev in the search box and enter, then click the install button and follow the tips to install until Finish and restart Eclipse.

    Install PyDev plugin via Eclipse Marketplace

    Input PyDev in the search box and enter

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:

  • service/deviceManager.py: Using Device Indentities REST APIs to create a new device identity, retrieve a device identity, and list device identities.

  • device/d2cMsgSender.py: Using Send device-to-cloud message API to send device-to-cloud message from the simulated device application to IoT Hub.

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() list all registed 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)
    
  4. The result for calling the function readDocument (DeviceManager.HOST, DeviceManager.KEY, DeviceManager.DATABASE, DeviceManager.COLLECTION, deviceId) list document message stored in DocumentDB whose Id equals to deviceId

    {u'_self': u'dbs/SoRMAA==/colls/SoRMAJUDHwE=/docs/SoRMAJUDHwEEAAAAAAAAAA==/', u'_ts': 1482827695, u'_rid': u'SoRMAJUDHwEEAAAAAAAAAA==', u'_attachments': u'attachments/', u'_etag': u'"0000ab06-0000-0000-0000-586227cb0000"', u'message': u'Hello, IoT Hub', u'id': u'iotdevice1'}
    

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

(u'', 204)

cookiecutter-iot-hub-documentdb-python-get-started's People

Contributors

msonecode avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cookiecutter-iot-hub-documentdb-python-get-started's Issues

No description on this repo

Because this repository has no description, you get a poor display in Visual Studio:

image

Descriptions are also searched, so having a detailed description will make it easier for users to find this.

] Fatal Python error: init_gresql'

] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00003e00 (most recent call first): ----- Cloning template 'tiangolo/full-stack-fastapi-postgresql' ----- Cloning into 'full-stack-fastapi-postgresql'... ----- Successfully cloned template 'tiangolo/full-stack-fastapi-postgresql' to 'C:\Users\hsher.cookiecutters\full-stack-fastapi-postgresql' ----- Looking for redirected environment at 'C:\Users\hsher\AppData\Local\Microsoft\CookiecutterTools\env' Failed to look for redirected environment at 'C:\Users\hsher\AppData\Local\Microsoft\CookiecutterTools\env' Python path configuration: PYTHONHOME = (not set) PYTHONPATH = (not set) program name = 'C:\Users\hsher\AppData\Local\Programs\Python\Python310\python.exe' isolated = 0 environment = 1 user site = 1 import site = 1 sys._base_executable = 'C:\Users\hsher\AppData\Local\Programs\Python\Python310\python.exe' sys.base_prefix = '' sys.base_exec_prefix = '' sys.platlibdir = 'lib' sys.executable = 'C:\Users\hsher\AppData\Local\Programs\Python\Python310\python.exe' sys.prefix = '' sys.exec_prefix = '' sys.path = [ 'C:\Users\hsher\AppData\Local\Programs\Python\Python310\python310.zip', 'C:\Users\hsher\AppData\Local\Programs\Python\Python31

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.