[PYTHON] I tried AWS Iot

Introduction

This article is for people who want to experience AWS IoT for the time being. This is an article that tries to connect the cloud and your device. 図1.png

Environment

Terminal OS: Ubuntu18.04LTS (started in VMware) Python:3.6.9

Construction procedure

Follow the steps below to build a cloud ⇔ client communication environment.

Connect to AWS IoT

Go to AWS IoT and click Get Started. キャプチャ.PNG

An explanation will appear, but don't worry, click "How to get started". I'm still too bad at Japanese and I don't know what I'm saying. 2.PNG

How do you connect to AWS IoT?

Select the platform (OS) of the connected device and the programming language of the SDK for connection. This time I will use Ubuntu as a client, so Select "Linux / OS X" as the platform. Any SDK is fine, but this time I chose "Python". Finally press Next.

3.PNG

Registration of things

Register things. Name the "thing" appropriately. This time, I chose "test01". 4.PNG

Download connection kit

Download the connection kit. Copy the downloaded file (connect_device_package.zip) to your device (Ubuntu in this case). 5.PNG

The connection kit includes ・ SDK: aws-iot-device-sdk-python -AWS certificate: root-CA.crt -Private key for "things": test01.private.key -Public key for "things": test01.public.key ・ Certificate for "things": test01.cert.pem Is included, and it is completely exhausted. Moreover, just by executing the script file (start.sh) inside, it will do everything from installing the SDK to executing the sample file (basicPubSub.py). By the way, the AWS certificate differs depending on the region (country where the server group is installed), but if you are accessing the Japan region (ap-northeast-1), the certificate of the Japan region will be downloaded without permission.

Device connection and testing

** Don't press Done yet. ** ** In the directory containing the downloaded file, execute the commands in the red frame in order. If you look at other sites, there are people who do it with administrator privileges (sudo), but user privileges are okay. 6.PNG

However, in many cases, I think that the library is insufficient. In my case, I was angry that I didn't have permission to create the AWSIoTPythonSDK folder.

error: could not create '/usr/local/lib/python3.6/dist-packages/AWSIoTPythonSDK': Permission denied

Actually, this error looks like a permission error, but it is actually an error that occurs because the AWSIoTPython SDK is not installed. Enter the following command and install the AWS IoTPython SDK to fix it.

pip install AWSIoTPythonSDK

If you don't have pip, install it with the following command.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Reference URL: https://pip.pypa.io/en/stable/installing/

If the execution is successful, the following content will be output to the Ubuntu console screen. Make sure Hello World! Is published.

2020-10-11 21:09:20,862 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2020-10-11 21:09:20,862 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2020-10-11 21:09:20,862 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: sdk/test/Python qos: 1
2020-10-11 21:09:20,862 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2020-10-11 21:09:20,927 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2020-10-11 21:09:20,927 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2020-10-11 21:09:20,927 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2020-10-11 21:09:20,927 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2020-10-11 21:09:22,930 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2020-10-11 21:09:22,930 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom puback (QoS>0) event callback...
2020-10-11 21:09:22,954 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2020-10-11 21:09:22,955 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2020-10-11 21:09:22,955 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2020-10-11 21:09:22,955 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2020-10-11 21:09:22,983 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2020-10-11 21:09:22,984 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
Received a new message: 
b'{"message": "Hello World!", "sequence": 0}'
from topic: 
sdk/test/Python
--------------

If the IoT device has successfully published, it will be displayed on the screen as shown in the image below. a.PNG

This time, on the contrary, publish from the cloud to the IoT device. Enter the appropriate characters in the "Step 4: Send a message to the device" box. This time, I added "Long time no see!". b.PNG

Click Send Message. If all goes well, the following content will be output to the terminal where you ran start.sh.

Received a new message: 
b'Long time no see!'
from topic: 
sdk/test/Python
--------------

When you press "Finish", the final confirmation screen will appear. Don't worry, press "Finish". 7.PNG

Thank you for your support.

Summary

I implemented a simple AWS IoT and experienced client ⇔ cloud communication. Next time, I would like to exchange sensor information.

Reference URL

About MQTT https://myenigma.hatenablog.com/entry/2019/10/27/194549

Recommended Posts

I tried AWS Iot
I tried AWS CDK!
I tried using AWS Chalice
I tried scraping
I tried PyQ
I tried AutoKeras
I tried papermill
I tried django-slack
I tried spleeter
I tried cgo
I tried using parameterized
I tried using argparse
I tried using AWS Rekognition's Detect Labels API
I tried using mimesis
I tried using anytree
I tried competitive programming
I tried running pymc
I tried ARP spoofing
I tried using aiomysql
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried PyCaret2.0 (pycaret-nightly)
I tried using openpyxl
I tried deep learning
I tried using Ipython
I tried to debug.
I tried using PyCaret
I tried using cron
I tried Kivy's mapview
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried connecting AWS Lambda with other services
I tried using PyCaret
I tried moving EfficientDet
I tried shell programming
I tried using Heapq
I tried using doctest
I tried running TensorFlow
I tried Auto Gluon
I tried using folium
I tried using jinja2
I tried Bayesian optimization!
I touched AWS Chalice
I tried using folium
I tried using time-window
I tried running the app on the IoT platform "Rimotte"
I tried Amazon Comprehend sentiment analysis with AWS CLI.
I tried running TensorFlow in AWS Lambda environment: Preparation
I tried to get an AMI using AWS Lambda
AWS Lambda now supports Python so I tried it
[Introduction to AWS] I tried playing with voice-text conversion ♪
I tried Value Iteration Networks
I tried fp-growth with python
I tried scraping with Python
I tried AutoGluon's Image Classification