A (human) index that likes to code
Also drinks way too much coffee
Published Jun 22, 2019 12:00
You read the title, let’s get started. For this tutorial, we will be using the Arduino IDE. This should be possible with ESP-IDF, too, because ESP-MQTT is included as part of the ESP-IDF.
This tutorial was created on Ubuntu 18.04.
Before the tutorial begins, please download the following pre-requisites (the version numbers are the versions used to create this tutorial):
File > Preferences | Source: Me
https://dl.espressif.com/dl/package_esp32_index.json
.
Adding a board manager url | Source: Me
esp32
on the search bar. You should find the esp32 package. Install version 1.0.2
.
Board Manager | Source: Me
ESP32 Package | Source: Me
PubSubClient
, and install version 2.7.0
.
Manage Libraries | Source: Me
Install PubSubClient library | Source: Me
Click on it to change the board | Source: Me
Run through all the installation steps for Python. If you are on Ubuntu, run sudo apt install python python-serial
.
Click on Services | Source: Me
IoT Core | Source: Me
Create IoT Policy | Source: Me
If you already have policies, use this button instead | Source: Me
iot:*
into the Action field, key in *
under the Resource ARN field, and finally, check the ‘Allow’ box under Effect. Should you wish to restrict your policy more for higher security, or prevent other authorized (yes, authorized) users from using your topic, please refer to this AWS Documentation to construct your own policy. After checking your fields, press Create.
Values for the wizard | Source: Me
Click on Create | Source: Me
Or click on Register a Thing | Source: Me
Create a single thing | Source: Me
Name your thing, and press create | Source: Me
Certificate Creation | Source: Me
Download the cert, private key, CA cert, and activate before continuing. | Source: Me
Register thing | Source: Me
Click into the thing you created | Source: Me
Caption
Note down the endpoint | Source: Me
MQTT Client on AWS IoT Console | Source: Me
SSID
: The SSID of the access point to connect to.Password
: The password of the access point to connect to.aws_iot_hostname
: The hostname you noted down during Step Uno.aws_iot_sub_topic
: The topic this device should subscribe to. For this tutorial, we’ll use topic/hello
, however, when following the tutorial with your friends, please have unique topics.aws_iot_pub_topic
: The topic this device should publish to. For this tutorial, it’ll be another/topic/hello
, however, when following the tutorial with your friends, please have unique topics.ca_certificate
: Copy the contents of the CA certificate you downloaded (file should be AmazonRootCA1.pem
) using any text editor like Notepad or Vim, and paste it into the textbox located below this list. Click on Make into C++ String
, and copy the contents of the textbox into the configuration option.iot_certificate
: Copy the contents of the certificate you downloaded (file should be *-certificate.pem.crt
) using any text editor like Notepad or Vim, and paste it into the textbox located below this list. Click on Make into C++ String
, and copy the contents of the textbox into the configuration option.iot_privatekey
: Copy the contents of the private key you downloaded (file should be *-private.pem.key
) using any text editor like Notepad or Vim, and paste it into the textbox located below this list. Click on Make into C++ String
, and copy the contents of the textbox into the configuration option.another/topic/echo
in the Subscription topic textbox, and click on Subscribe to topic
.
Subscribe to the topic | Source: Me
topic/hello
in the Publish textbox, and click on Publish to topic
.
Publish to the topic | Source: Me
An echo from the ESP32 on AWS IoT | Source: Me
Serial Console | Source: Me
The code contains the absolute (mostly) minimal code required to perform MQTT Pub/Sub with AWS IoT MQTT endpoints. Other than the MQTT client verifying the server’s identity, AWS also requires that all clients be authenticated with client certificates. Hence, the following lines:
client.setCACert(ca_certificate);
client.setCertificate(iot_certificate);
client.setPrivateKey(iot_privatekey);
are responsible for setting the required certificates for communication.
Publishing is done like so:
mqtt.publish(aws_iot_pub_topic, aws_iot_pub_message);
And subscribing is done like so:
mqtt.subscribe(aws_iot_sub_topic); //subscribe to the topic
Do note that for subscribing, you must provide a callback function with the signature of void callback(const char* topic, byte* payload, unsigned int length)
. This callback will be called by the PubSubClient
library whenever there is a new message from the subscribed topics.
MQTT typically uses port 1883
and 8883
. AWS IoT only uses port 8883
, because it uses MQTT over SSL (MQTTS), hence the line:
mqtt.setServer(aws_iot_hostname, 8883);
Hope you enjoyed the tutorial. In part two of this two-parter tutorial, we will be adding a policy that will pipe whatever our ESP32 publishes to AWS IoT into DynamoDB. Until then,
Happy Coding,
CodingIndex