Getting Started with IoT on AWS
Nilojan Tharmarajah, Lead Cloud Practice Architect, Rackspace Technology
For some time now I have been wanting to get my hands on an IoT device and start playing around with them to understand how to integrate it with AWS IoT Core. I could have used AWS Greengrass for testing, but I preferred the novelty of using a physical board to carry out my tests.
IoT boards are readily available online from a Raspberry Pi Zero to an ESP32, both of which are inexpensive. An online search will give you plenty of options to choose from. For this demo I will be using an ESP32 board.
Objective
For this demo I will be setting up my ESP32 to connect with AWS IoT Core, subscribe to a topic and then publish a message to that topic. I will set up the MQTT test client on AWS IoT Core to view the published messages.
As a bonus, I will send a message back to the ESP32 by publishing a message from the AWS MQTT client.
Prerequisites
- AWS account
- ESP32 board
- Arduino IDE which will be used to configure the ESP32 board
AWS IoT Core setup
We need to create a Thing in AWS IoT Core. In this case, the Thing will represent the ESP32 board.
Once logged into your AWS console, go to the AWS IoT Core service page.
In the IoT Core homepage, go to Things under Manage > All devices then select create things on the top right-hand side of the page.
We are going to create a single thing
Give it a name and leave the rest as default
Autogenerate new certificates
All IoT devices need to have a policy attached to it in order to perform its intended action. On the next page, we are going to create a new policy and then attach it to the device.
In fact, the policy is attached to a certificate that we will be creating after this step.
For this policy we need our ESP32 to be able to:
- iot:Connect
- iot:Subscribe
- iot:Receive
- iot:Publish
In the Create policy page, give policy a name then select JSON from the Policy document section and replace the default document with the following:
Save it and return to your IoT Core page where you should see the new policy you just created. If not, refresh the page.
Select create thing.
You will be prompted to download certificates and keys. This is the only time you can download these. Make sure you download and keep them safe.
The policy you created earlier is now attached to this certificate.
We have successfully created our thing. Now let’s setup the ESP32 using the Arduino IDE.
Arduino Setup for ESP32
Before starting we need to setup a few things on our IDE. We need to download the MQTT library and ArduinoJSON library from the Arduino library manager. Choose the latest version for both the libraries.
App for ESP32
There are many simple demos you can find online to get started with programming ESP32 devices. For this demo we will be using the code provided here - https://github.com/yash-sanghvi/ESP32/tree/master/AWS_IOT
Configuration
We need to configure the ESP32 to connect to our WiFi and then connect to the AWS IoT Core using the certificates we downloaded earlier and our unique endpoint address which we will get later on.
To start, lets create the file config.h in your Arduino IDE. Replace YOUR_THING_NAME, YOUR_SSID, YOUR_PASSWORD with your details.
To get your unique endpoint address, you can run the following command if you have configured your AWS CLI:
aws iot describe-endpoint --endpoint-type iot:Data-ATS
Otherwise, you can go to the console to retrieve it
Finally, copy and paste the contents of the certificates you downloaded earlier in the respected sections of the file.
What This App Does
A Quick description on what this app (AWS_IOT.ino) is going to do. It will establish a connection from your ESP32 with AWS IoT Core using the config.h configuration with the MQTT client.
Once connected to the WiFi it will connect with the AWS IoT broker via the endpoint mentioned in your config.h file using the MQTT protocol. Once connected it will subscribe the esp32/sub topic where we can later see the messages coming through in the console from the publishMessage() function every 4 seconds using loop() function.
Upload app
Verify and upload the app via the Arduino IDE.
As soon as the upload is complete you should see lights flashing on your ESP32. This confirms that your device has connected to your WiFi and transmitting data.
You can confirm this by viewing the serial monitor.
Troubleshooting
ESP32 not connecting to IoT Core endpoint:
- In my experience this is usually due to a copy and paste error with the certificates or incorrect WiFi details.
Arduino port not located once device connected:
- Usually due to the type of cable you are using, I got ‘lucky’ on the 4th cable not before trying to install different drivers. Nevertheless, ensure you do have the right UART driver for your ESP32. For reference, my one was a CP210x port.
Validate messages
To confirm we are receiving the messages, go to the MQTT test client within IoT Core console and subscribe to the topic our app created, esp32/pub.
Hit subscribe and shortly after you should start seeing messages coming through
This confirms our ESP32 has securely connected to IoT Core using the MQTT protocol and we are receiving messages.
For one last test, let’s try send a message back to our ESP32 and view the message in the serial monitor.
In the MQTT test client, go to Publish to a topic and enter the topic name esp32/sub
Once you hit publish, in your ESP32 serial monitor you should see the above payload message appear
The receiving of this messages is made possible because of the mqtt_client.loop() function which is called within a loop to keep the connection alive between ESP32 and the AWS broker.
Conclusion
We have successfully programmed our ESP32 to send and receive messages to AWS IoT Core.
Recent Posts
Google Cloud Hybrid Networking Patterns — Part 1
October 17th, 2024
Google Cloud Hybrid Networking Patterns — Part 3
October 17th, 2024
Google Cloud Hybrid Networking Patterns — Part 2
October 17th, 2024
How Rackspace Leverages AWS Systems Manager
October 9th, 2024
Windows Server preventing time sync with Rackspace NTP
October 7th, 2024