MQTT AT Examples for Cloud
This document mainly describes how to connect your ESP32 to AWS IoT with MQTT AT commands.
Please follow the steps below to connect your ESP32 to AWS IoT with ESP-AT.
Sign in to your AWS IoT Console account and switch to the IoT Core services.
Create an AWS IoT policy, thing, and certificates following the instructions in Create AWS IoT Resources.
Make sure you have got the following certificate and key files:
device.pem.crt (Device certificate)
private.pem.key (Private key)
Amazon-root-CA-1.pem (Root CA certificate)
Get the endpoint and bind the thing to the policy through the certificate according to the documentation Set up the policy.
The endpoint value has the format of xxx-ats.iot.us-east-2.amazonaws.com.
It is strongly recommended to familiarize yourself with the AWS IoT Developer Guide. Below are some key points from this Guide that are worth noting.
All devices must have a device certificate, private key, and root CA certificate installed in order to communicate with AWS IoT.
Information on how to activate certificates.
Select Ohio as your region.
Open your local ESP-AT project and do the following:
Replace customized_partitions/raw_data/mqtt_ca/mqtt_ca.crt with
Replace customized_partitions/raw_data/mqtt_cert/mqtt_client.crt with
Replace customized_partitions/raw_data/mqtt_key/mqtt_client.key with
Compile and flash the AT firmware
Compile the ESP-AT project to build the AT firmware, and flash the firmware to your ESP32. For more information, please refer to Compile ESP-AT Project Locally.
If you do not want to compile the ESP-AT project to replace certificates, you can directly use the AT command to replace certificates in the firmware. For more information, please refer to How to Update PKI Configuration.
Use AT commands to connect to AWS IoT
Set the Wi-Fi mode to station.
Connect to the AP.
Set the SNTP server.
Query the SNTP time.
+CIPSNTPTIME:<asctime style time> OK
The <asctime style time> obtained at this time must be the real-time time of the set time zone, otherwise the connection will fail due to the validity period of the certificate.
Set MQTT user properties.
If the second parameter of AT+MQTTUSERCFG is 5, it is authenticated by both sides and cannot be changed.
Connect to AWS IoT.
Please fill in your endpoint value in the <endpoint> parameter.
The port 8883 cannot be changed.
Subscribe to messages.
Publish a message.
+MQTTSUBRECV:0,"topic/esp32at",10,hello aws! OK
Log for normal interaction is as follows:
Log on the ESP32 side
Log on the AWS side