Programming ESP32 using MQTT with AWS and FreeRTOS
What we’re doing
Set up your ESP-32 running FreeRTOS to subscribe and publish events to AWS IoT via MQTT
Install the development environment (I use a Mac)
I used this excellent guide from Espressive
Get started with the example code
Read the post by Espressive here and use the code here. I don’t have the WROOM-32Se, so I skipped the instructions for “Using an ATECC608A with the ESP-AWS-IoT” and embedded the creds in code.
Using ‘idf.py menuconfig’:
- Set up your WiFi SSID and password
- Set up to embed the AWS certs into the binary
- Set up AWS IoT endpoint
Copy your credentials
Get started on the AWS side by creating, activating, and downloading your credentials as described in my previous post. The demo comes with the AWS CA in the cert directory (main/certs), so you need to only copy your certificate and private key to the same place.
Compile, flash, monitor
Use idf.py flash monitor --port /dev/cu.usbserial-0001
, substituting your connection port accordingly.
You’ll see the trace output in your terminal:
I (75682) subpub: test_topic/esp32 Hello from ESP32 (QOS0) : 112
I (75712) subpub: Subscribe callback
I (75712) subpub: test_topic/esp32 Hello from ESP32 (QOS1) : 113
I (75802) subpub: Stack remaining for task 'aws_iot_task' is 3544 bytes
I (76862) subpub: Subscribe callback
I (76862) subpub: test_topic/esp32 {
"message": "Hello from AWS IoT console"
}
I (76882) subpub: Subscribe callback
I (76882) subpub: test_topic/esp32 Hello from ESP32 (QOS0) : 114
I (76912) subpub: Subscribe callback
I (76912) subpub: test_topic/esp32 Hello from ESP32 (QOS1) : 115
I (77002) subpub: Stack remaining for task 'aws_iot_task' is 3544 bytes
I (78062) subpub: Subscribe callback
Note that ‘Hello from AWS IoT console’ trace above is the result of going to the AWS console and publishing to the topic. The demo program subscribes and publishes to the topic, so anything coming from the simulator on that topic will be picked up and displayed.
You can see the messages sent to AWS:
Next step
Publish meaningful events from sensors? Have a second ESP32 communicate with this one?
References: