IoT Bytes

Bits and Bytes of IoT

Device Shadows – Part 3 (RESTful API Based Prototype using Python)

Pradeep Singh | 9th Apr 2017

shadow

This is the third article from the series of  AWS IoT Device/Thing Shadow articles. Here in this article, we shall leverage some information/code covered in previous articles without repetition. Following are the fist two articles (prerequisites) –

Device Shadows – Part 1 (MQTT Topics and REST APIs)
Device Shadows – Part 2 (MQTT Based Prototype using Python and Raspberry Pi)

1. AWS ‘Signature Version 4’ Process:

When you send HTTP requests to AWS, you must sign the requests with your access key ID and secret access key, so that AWS can identify who sent them.

When you write custom code to send HTTP requests to AWS, you need to include code to sign the requests. Python script aws_sig_ver_4.py (we will use it for shadow REST API calls) is a simple implementation of the ‘Signature Ver 4’ signing process.

For detailed process of signing an AWS HTTP Request, you may go through the following link –

http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

2. Python Scripts for Shadow REST APIs:

I have written Python Scripts for Get, Update (or Create), and Delete Shadow REST API calls. The following image shows the information flow between the REST API scripts and MQTT Based script running on Raspberry Pi (Refer to previous article – Part 2 for details) –

RESTful AWS Shadow

2.1 Python Script Prerequisites:

Make sure your system fulfills the following dependencies (prerequisites) before executing REST API scripts –

  • Python ver 2.7
  • Python “Requests” module. You can install it using pip command “pip install requests” or “sudo pip install requests“.
  • Use AWS IAM Module to create a new user with “AWSIoTDataAccess” (Read / Write Permission) or “AWSIoTFullAccess” (Read / Write / DeleteRead / Write / Delete Permission) permission and get hold of  “Access Key” and “Secret Key” for this user.
  • As shown the previous image, these REST API scripts will leverage Raspberry Pi setup with LED_Controller.py Script. For the details of this setup, please refer to previous article – Part 2.

2.2 Python Scripts:

Python scripts for Shadow REST calls are available on GitHub Octocat. Following are the details for these scripts –

  • aws_sig_ver_4.py: This python script returns Headers for REST Calls; including Authorization header with information signed with “Signature Version 4” process.
  • REST_update_shadow.py: Python script to update (or create) device shadow state document.
  • REST_get_shadow.py: Python script to get the device shadow state document.
  • REST_delete_shadow.py: Python script to delete device shadow state document.

Conclusion:

With three parts of this article (Part 1, Part 2, and this one), we have covered Device Shadow basics and real implementation with Python scripts for both MQTT and REST-API based interfaces. Now you should have enough knowledge to use AWS IoT Device Shadows in your projects. In case you have any questions and feedback, please share them here.

2 thoughts on “Device Shadows – Part 3 (RESTful API Based Prototype using Python)

Leave a comment