Getting started with AWS IoT easily in Python

Introduction

https://aws.amazon.com/jp/iot/

AWS IoT, which is a relatively new service of Amazon Web Service, is evolving day by day as it begins to appear. I would like to experience AWS IoT using Mac and Python even if I don't have a device.

Setup in AWS Console

Select AWS IoT in the Amazon Web Service console and select "Configuring a device" from the Connect menu.

スクリーンショット 2017-01-08 11.42.04.png

Select "Linux / OS X" as the Platform and select Python.

スクリーンショット 2017-01-08 11.42.40.png

The name of Thing can be anything, but here it is "test_thing". You can add types and attributes to the thing by selecting "show optional configuration (this can be done later)". This makes it easier to find things later, but it's not necessary first.

スクリーンショット 2017-01-08 11.45.07.png

If you set only the Thing name, the client certificate and policy will be created accordingly. With the client certificate created here, AWS IoT features can be accessed from Python applications.

When you press the "Linux / OS X" button, information such as the client certificate and private key will be downloaded.

スクリーンショット 2017-01-08 11.45.51.png

Python application

The processing after the downloaded zip file will be as follows.

スクリーンショット 2017-01-08 11.46.22.png

In my environment, I got a Permission error in ./start.sh. It seems that the necessary libraries are installed in the sample, but when I run it again with sudo, it succeeds.

$ cd aws-iot-device-sdk-python/
$ sudo python setup.py install

trouble shooting

I tried running the sample app again using start.sh. Internally, the client certificate, root certificate, and private key are set and executed with the following commands.

python aws-iot-device-sdk-python/samples/basicPubSub/basicPubSub.py -e XXXXXXXX.iot.ap-northeast-1.amazonaws.com -r root-CA.crt -c test_thing.cert.pem -k test_thing.private.key

But unfortunately I got the following error:

ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:590)

It seems that the version of OpenSSL is old, so I changed the version. http://ytooyama.hatenadiary.jp/entry/2016/06/23/004429 https://teratail.com/questions/47299 http://stackoverflow.com/questions/35473033/how-to-fix-openssl-error-with-aws-cli-iot-api

Operation check

When start.sh is executed again, the Python application sends and receives the following log message to the sdk / test / Python topic once every few seconds.

2017-01-08 16:54:54,102 - AWSIoTPythonSDK.core.protocol.mqttCore - DEBUG - Try to put a publish request 178 in the TCP stack.
2017-01-08 16:54:54,102 - AWSIoTPythonSDK.core.protocol.mqttCore - DEBUG - Publish request 178 succeeded.
Received a new message: 
New Message 176
from topic: 
sdk/test/Python
--------------

If you check it on the AWS IoT dashboard, the graph will be output as shown below.

スクリーンショット 2017-01-08 17.02.36.png

in conclusion

It's very convenient because you can emulate a device with just a Mac and a Python app without a device. AWS IoT is becoming more and more convenient.

Recommended Posts

Getting started with AWS IoT easily in Python
1.1 Getting Started with Python
Getting Started with Python
Settings for getting started with MongoDB in python
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Get started with Python in Blender
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Python for PHPer-Super Basics
Getting started with Dynamo from Python boto
Get Started with TopCoder in Python (2020 Edition)
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Getting started with Python with 100 knocks on language processing
Django 1.11 started with Python3.6
Getting started with Android!
[Translation] Getting Started with Rust for Python Programmers
Getting started with apache2
Getting Started with Golang 1
Getting Started with Django 1
Easily log in to AWS with multiple accounts
Getting Started with Optimization
Easily beep with python
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
AWS CDK with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Materials to read when getting started with Python
Getting Started with Django 2
Getting Started with python3 # 2 Learn about types and variables
Fleet provisioning with AWS IoT SDK for Python v2
Super Primer to python-Getting started with python3.5 in 3 minutes
Getting Started with Google App Engine for Python & PHP
Translate Getting Started With TensorFlow
Easily format JSON in Python
Getting Started with Tkinter 2: Buttons
Easily serverless with Python with chalice
Scraping with selenium in Python
Getting Started with Go Assembly
[Python] Scraping in AWS Lambda
Working with LibreOffice in Python
Getting Started with PKI with Golang ―― 4
Scraping with chromedriver in python
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python