Implementing AWS IoT Project

Once the project has been configured, Go to Simuli Virtual Lab and start the Raspberry Pi instance we just created. Click on the Open button Under the instance name. A new tab with the built-in development environment opens up. On the left side, we have the code editor window along with the terminal at the bottom. On the right side, we have our Raspberry pi with the sensors below it.

Now go to the terminal and type the following commands to install pip, which is the python package manager.

sudo apt update
sudo apt-get install python3-pip

Once that is done we will use pip3 to install the AWS IoT Python SDK package, which is needed for our program to communicate with the AWS thing created in the portal. Type the following command to install the package.

pip3 install AWSIoTPythonSDK

Also, the smbus library is required for the code to work.

pip3 install smbus

The code for the project has been made available via GitHub. To get the code, we need to clone the repository into our instance. Type the following command in the terminal:

git clone https://github.com/simuli-lab/RaspberryPi-AWS.git

All the required project files will now be cloned to a local folder with the name aws_iot . Let's change the working directory of the terminal to that folder.

cd RaspberryPi-AWS/aws_iot

In the File Explorer pane right-click on the aws_iot folder, click on Upload Files... and then upload all the certificates you downloaded during the setup.

Open the rasp_aws.py file in the IDE by clicking on it in the File Explorer pane. Here we need to configure the endpoint present on line 124.

myMQTTClient.configureEndpoint("<Endpoint>", 8883)

Go to the AWS portal here go to Settings , under the subtopic Device Data Endpoint copy the endpoint and then in the code replace <Endpoint> with that.

Now we need to configure the credentials, replace <RootCA certificate>,<private key> and <Security Certificate> with your certificate/key file names.

myMQTTClient.configureCredentials("/home/pi/aws_iot/<RootCA certificate>", "/home/pi/aws_iot/<private key>", "/home/pi/aws_iot/<Security Certificate>")

On line 141 replace <Topic> with a topic name of your choice to which you will subscribe in AWS.

topic="<Topic>"

Now to subscribe to the topic go to the AWS IoT portal, there in the left pane under the dropdown Test click on MQTT Test Client. Here, under the subtopic Subscribe to a Topic enter the topic you choose in the code and click on Subscribe.

Now we are ready to run our program. Type the following command in the terminal.

python3 rasp_aws.py

The real-time Lux readings will now be recorded and sent to AWS IoT Portal.

Last updated