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.
Terminal OS: Ubuntu18.04LTS (started in VMware) Python:3.6.9
Follow the steps below to build a cloud ⇔ client communication environment.
Go to AWS IoT and click Get Started.
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.
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.
Register things. Name the "thing" appropriately. This time, I chose "test01".
Download the connection kit. Copy the downloaded file (connect_device_package.zip) to your device (Ubuntu in this case).
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.
** 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.
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.
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!".
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".
Thank you for your support.
I implemented a simple AWS IoT and experienced client ⇔ cloud communication. Next time, I would like to exchange sensor information.
About MQTT https://myenigma.hatenablog.com/entry/2019/10/27/194549
Recommended Posts