当前位置:网站首页>Raspberry pie + AWS IOT introductory experiment

Raspberry pie + AWS IOT introductory experiment

2022-06-26 02:06:00 DexterLien

Preface

It's been a long time since I tossed the hardware , Turn over and eat ashes 1 Years of raspberry pie 3B Experience with AWS IoT Linkage of the Internet of things , Refer to official documentation :
https://docs.aws.amazon.com/iot/latest/developerguide/connecting-to-existing-device.html

AWS preparation

AWS IoT Communication with hardware depends on X.509 The certificate is encrypted and passed policy The policy of , So we need to AWS The console does the following preparations :

establish policy

open Amazon IoT Console , successively Security - Policies - Create policy

 Insert picture description here

Add respectively iot:Coonect, iot:Receive, iot:Publish, iot:Subscribe Of Allow jurisdiction , For the convenience of experiment , resource No specific restrictions , direct * Pass through

 Insert picture description here

establish thing

Create good policy Then go back to the left navigation bar All devices - Things - Create things

 Insert picture description here

Because the experiment is just a device , So choose Create single thing

 Insert picture description here

Name it , Note that this name cannot be modified , Others keep the default settings and continue

 Insert picture description here

Select the recommended auto generate certificate

 Insert picture description here

Check the... You just created policy continue

 Insert picture description here

Download the certificate / secret key

Download the generated certificate and key file respectively , Note that public and private key files can only be downloaded from this interface , You can't download it after you close it , Need to regenerate new .

 Insert picture description here
Altogether 5 File , Corresponding description :

file name explain
xxxx-certificate.pem Equipment certificate , Used to bind to the target physical device
xxxx-private.pem Private key
xxxx-public.pem Public key
AmazonRootCA1.pemAmazon trust services endpoint Root certificate (RSA 2048 bit)
AmazonRootCA3.pemAmazon trust services endpoint Root certificate (ECC 256 bit)

What will be used in this experiment is RSA 2048 bit Algorithm AmazonRootCA1.pem File as root certificate , You also need xxxx-certificate.pem Equipment certificates and xxxx-private.pem Private key , These three files will be used later . After downloading, click Done Close the pop-up window .

Go back to the bottom of the left navigation bar again Settings, Copy Endpoint The corresponding web address will be used behind the Notepad . IoT The preparation of the console has come to an end .
 Insert picture description here

Hardware preparation

I don't remember the raspberry pie TF What's in the card , Direct use Raspberry Pi Imager Redrawn the latest version of Raspberry Pi OS Lite (32-bit), This version is based on Debian 11 Bullseye Demonic . SSH Connect to start the following operation :

#  Replace the domestic image first 
sudo sed -i 's|raspbian.raspberrypi.org|mirrors.ustc.edu.cn/raspbian|g' /etc/apt/sources.list
sudo sed -i 's|//archive.raspberrypi.org|//mirrors.ustc.edu.cn/archive.raspberrypi.org|g' /etc/apt/sources.list.d/raspi.list

#  Update and install dependencies 
sudo apt update
sudo apt upgrade -y
sudo apt install -y cmake libssl-dev git python3-pip

#  Use bean petals pip Mirror image 
pip config set global.index-url https://pypi.doubanio.com/simple

#  install  AWS IoT Device SDK
cd ~
pip install awsiotsdk
git clone https://github.com/aws/aws-iot-device-sdk-python-v2.git

# github If you have difficulty accessing, you can use the following gitee Image on 
# git clone https://gitee.com/mirrors_aws/aws-iot-device-sdk-python-v2.git

Hello IoT World

IoT Communication uses MQTT agreement 1 The following test program will use MQTT Create a library called topic_1 The theme of , And send the 10 Messages to this topic .

MQTT(Message Queuing Telemetry Transport, Message queuing telemetry transmission protocol ), It's based on publishing / subscribe (publish/subscribe) Mode " Lightweight " Communication protocol , The agreement is built on TCP/IP Agreement on , from IBM stay 1999 Released in .MQTT The biggest advantage is , With very little code and limited bandwidth , Provide real-time and reliable message services for connecting remote devices . As a low cost 、 Low bandwidth instant messaging protocol , Make it in the Internet of things 、 Small equipment 、 Mobile applications are widely used .

First, put the first step in AWS Three certificates generated in the console / Upload the key file to raspberry pie ~/certs In the folder , It is convenient to rename later :

file name explain
certificate.pem Equipment certificate , Used to bind to the target physical device
private.pem Private key
AmazonRootCA1.pemAmazon trust services endpoint Root certificate (RSA 2048 bit)
/home/pi/certs/
├── AmazonRootCA1.pem
├── certificate.pem.crt
└── private.pem.key

call aws-iot-device-sdk-python-v2 Demo application included in , Pay attention to your-iot-endpoint Replace with the first step at AWS Console preparation final Settings The URL copied to notepad in

cd ~/aws-iot-device-sdk-python-v2/samples

python3 pubsub.py --topic topic_1 --ca_file ~/certs/AmazonRootCA1.pem --cert ~/certs/certificate.pem.crt --key ~/certs/private.pem.key --endpoint your-iot-endpoint

The following information is output successfully :
 Insert picture description here
go back to AWS IoT The console checks the messages sent by raspberry pie , Left navigation bar : MQTT test client - Topic filter Input topic_1 - Subscribe, Subscribe again after the raspberry pie SSH Execute the above test procedure in , It can be observed that the console synchronously receives the Hello World news .
 Insert picture description here

summary

This experiment only carried out the most basic physical equipment and AWS IoT Interconnected and through MQTT Protocol delivery / Receive a message , For practical application scenarios, more commercial hardware devices have been packaged with simpler configuration interfaces , The whole idea is the same , Mainly in AWS The console is well configured policy, Definition / add to thing, Create a certificate and associate it correctly on the device .


  1. MQTT Protocol is introduced https://www.runoob.com/w3cnote/mqtt-intro.html

原网站

版权声明
本文为[DexterLien]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260027583118.html