ESP32 S3 - AWS IoT

This tutorial instructs you how to use the ESP32 S3 with AWS IoT Core. By following along, you will learn how to configure AWS IoT Core for secure device communication and how to program the ESP32 S3 to exchange data with the cloud using the MQTT protocol.

What you'll build:

  1. A secure connection from the ESP32 S3 to AWS IoT Core using TLS certificates
  2. A publisher that sends sensor data from the ESP32 S3 to an AWS IoT topic every four seconds
  3. A subscriber that receives messages from AWS IoT Core on the ESP32 S3
  4. A foundation for integrating the ESP32 S3 with other AWS services via IoT Rules
ESP32 S3 AWS IoT Core - connect to Amazon Web Services and send MQTT messages

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of ESP32 S3 and AWS IoT

The ESP32 S3 connects to AWS IoT Core using the MQTT protocol over a secure TLS connection. Unlike a local MQTT broker, AWS IoT Core enforces strict authentication, meaning you must first obtain valid certificates and configure an authorization policy before integrating those credentials into your ESP32 S3 sketch. The overall process breaks down into two main phases: configuring AWS IoT Core to generate credentials, and then writing the ESP32 S3 code that uses those credentials to connect and communicate.

Key Specifications

  • Protocol: MQTT over TLS (port 8883)
  • Authentication: X.509 certificates issued by AWS IoT Core
  • Authorization: IoT Policy attached to the device certificate
  • Directions: Bidirectional — ESP32 S3 can publish to and subscribe from AWS IoT topics

Configuring AWS IoT Core for use with ESP32 S3

The objectives of this step include:

  • Creating a representation of the ESP32 S3 device on AWS IoT Core, referred to as a "Thing."
  • Configuring the necessary authorization to allow the ESP32 S3 device to connect, publish, and subscribe to/from AWS IoT Core, known as a "Policy."
  • Generating the AWS credentials required for authentication, known as "Certificates." These credentials will be downloaded and integrated into the Arduino ESP32 S3 code.

Below are instructions on configuring AWS IoT Core for use with ESP32 S3 via the AWS IoT Console. Please note that while the User Interface may change over time, the process should remain similar to the instructions provided below:

  • Sign in to the AWS IoT Console
  • Create a Thing by going to Manage All devices Things
AWS IoT creates Things
  • Click the Create things button.
  • Select Create single things and click the Next button.
AWS IoT Core creates Things
  • Specify the Thing name, for example, ESP32-S3-thing and click the Next button at the bottom of the page.
AWS IoT Core Thing name
  • Generate the credentials by selecting the Auto-generate a new certificate option, and click the Next button.
AWS IoT Core generates certificate
  • Now, a Certificate is created and linked to the Thing.
  • Create a policy by clicking the Create policy button.
AWS IoT Core Create policy
  • A new tab will be opened
AWS IoT Core Create policy ESP32 S3
  • Specify the Policy name, for example, ESP32-S3-policy and click the JSON button.
  • Copy the below JSON policy content and paste it to the Policy document area:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "*", "Resource": "*" } ] }
  • Click the Create button at the bottom of the page to create the policy.
  • Now, a Policy is created and attached to the Certificate. Close that page and back to the Thing page.
  • Check the ESP32-S3-policy and Click the Create thing button to create the Thing.
AWS IoT Core creates Thing ESP32 S3
  • A popup window appears that allows you to download the credentials files. Download all files and store them in a safe location on your PC and keep them confidentially.
AWS IoT Core credentials file
  • Then, click the Done button.

Among downloaded files, there are three files will be used in the ESP32 S3 code in the next steps:

  • AmazonRootCA1.pem
  • Xxxxxxxxxx-certificate.pem.crt
  • Xxxxxxxxxx-private.pem.key

These files can be opened by any text editor such as Notepad, or Notepad++

Writing the ESP32 S3 Code to Connect to AWS IoT Core

The following code demonstrates how to connect the ESP32 S3 to AWS IoT Core, publish sensor readings to a cloud topic, and subscribe to incoming messages from AWS. The sketch uses the MQTT and ArduinoJson libraries, and loads the TLS certificates from a companion secrets.h file that you will create in the next step.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-aws-iot */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-aws-iot */ #include "secrets.h" #include <WiFiClientSecure.h> #include <MQTTClient.h> #include <ArduinoJson.h> #include "WiFi.h" // The MQTT topics that this device should publish/subscribe #define AWS_IOT_PUBLISH_TOPIC "esp32/esp32-to-aws" #define AWS_IOT_SUBSCRIBE_TOPIC "esp32/aws-to-esp32" #define PUBLISH_INTERVAL 4000 // 4 seconds WiFiClientSecure net = WiFiClientSecure(); MQTTClient client = MQTTClient(256); unsigned long lastPublishTime = 0; void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("ESP32 S3 connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); connectToAWS(); } void loop() { client.loop(); if (millis() - lastPublishTime > PUBLISH_INTERVAL) { sendToAWS(); lastPublishTime = millis(); } } void connectToAWS() { // Configure WiFiClientSecure to use the AWS IoT device credentials net.setCACert(AWS_CERT_CA); net.setCertificate(AWS_CERT_CRT); net.setPrivateKey(AWS_CERT_PRIVATE); // Connect to the MQTT broker on the AWS endpoint we defined earlier client.begin(AWS_IOT_ENDPOINT, 8883, net); // Create a handler for incoming messages client.onMessage(messageHandler); Serial.print("ESP32 S3 connecting to AWS IOT"); while (!client.connect(THINGNAME)) { Serial.print("."); delay(100); } Serial.println(); if (!client.connected()) { Serial.println("ESP32 S3 - AWS IoT Timeout!"); return; } // Subscribe to a topic, the incoming messages are processed by messageHandler() function client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC); Serial.println("ESP32 S3 - AWS IoT Connected!"); } void sendToAWS() { StaticJsonDocument<200> message; message["timestamp"] = millis(); message["data"] = analogRead(1); // read ESP32-S3 pin GPIO1 (ADC1). Or you can read data from other sensors char messageBuffer[512]; serializeJson(message, messageBuffer); // print to client client.publish(AWS_IOT_PUBLISH_TOPIC, messageBuffer); Serial.println("sent:"); Serial.print("- topic: "); Serial.println(AWS_IOT_PUBLISH_TOPIC); Serial.print("- payload:"); Serial.println(messageBuffer); } void messageHandler(String &topic, String &payload) { Serial.println("received:"); Serial.println("- topic: " + topic); Serial.println("- payload:"); Serial.println(payload); // You can process the incoming data as json object, then control something /* StaticJsonDocument<200> doc; deserializeJson(doc, payload); const char* message = doc["message"]; */ }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE
  • Type MQTT on the search box, then look for the MQTT library by Joel Gaehwiler.
  • Click Install button to install MQTT library.
  • Search for MQTT created by Joel Gaehwiler and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
MQTT by Joel Gaehwiler
This library bundles the lwmqtt client and adds a thin wrapper to get an Arduino like API. More info
2.5.2
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  • Type ArduinoJson on the search box, then look for the ArduinoJson library by Benoit Blanchon.
  • Click Install button to install ArduinoJson library.
  • Search for ArduinoJson created by Benoit Blanchon and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
ArduinoJson by Benoit Blanchon
⭐ 6953 stars on GitHub! Supports serialization, deserialization, MessagePack, streams, filtering, and more. Fully tested and documented. More info
7.4.2
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  • Copy the above code and paste it to Arduino IDE.
  • Create the secrets.h file On Arduino IDE by:
    • Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
    Arduino IDE 2 adds file
    • Give file's name secrets.h and click OK button
    Arduino IDE 2 adds file secrets.h
    • Copy the below code and paste it to the created secrets.h file.
    #include <pgmspace.h> #define SECRET #define THINGNAME "ESP32-S3-thing" const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // CHANGE IT const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // CHANGE IT const char AWS_IOT_ENDPOINT[] = "xxxxx.amazonaws.com"; // Amazon Root CA 1 static const char AWS_CERT_CA[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- PASTE YOUR AMAZON ROOT CA 1 HERE -----END CERTIFICATE----- )EOF"; // Device Certificate static const char AWS_CERT_CRT[] PROGMEM = R"KEY( -----BEGIN CERTIFICATE----- PASTE YOUR DEVICE CERTIFICATE HERE -----END CERTIFICATE----- )KEY"; // Device Private Key static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( -----BEGIN RSA PRIVATE KEY----- PASTE YOUR DEVICE PRIVATE KEY HERE -----END RSA PRIVATE KEY----- )KEY";
    • Update the following information in the secrets.h
      • The WIFI_SSID and WIFI_PASSWORD of your WiFi network
      • The AWS_CERT_CA, AWS_CERT_CRT, and AWS_CERT_PRIVATE. These information are in the files you downloaded in the previous step.
      • The AWS_IOT_ENDPOINT. This information can be found on AWS IoT Console by going to Setting as below image:
      AWS IoT endpoint
      • Compile and upload code to ESP32 S3 board by clicking Upload button on Arduino IDE
      • Pro Tip: Keep your secrets.h file out of version control by adding it to .gitignore. This prevents accidentally exposing your AWS certificates in a public repository.

Sending Data from ESP32 S3 to AWS IoT

The ESP32 S3 code periodically reads data from an analog pin and sends it to AWS IoT Core every four seconds over MQTT. Opening the Serial Monitor in Arduino IDE will display a connection log followed by a publishing confirmation for each message sent.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
ESP32 S3 connecting to AWS IOT. ESP32 S3 - AWS IoT Connected! sent: - topic: esp32/esp32-to-aws - payload:{"timestamp":1718500001,"data":0} sent: - topic: esp32/esp32-to-aws - payload:{"timestamp":1718500005,"data":130} sent: - topic: esp32/esp32-to-aws - payload:{"timestamp":1718500009,"data":247} sent: - topic: esp32/esp32-to-aws - payload:{"timestamp":1718500013,"data":85}
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

To check if the data are received by AWS IoT or not, do the following steps:

  • In AWS IoT Console, navigate to Test MQTT Test Client
AWS IoT MQTT Test Client ESP32 S3
  • Click the Subcribe to a topic button.
  • Type esp32/esp32-to-aws to the Topic filter. You can change the topic but MUST be matched the topic on the ESP32 S3 code.
  • Click the Subcribe button.
  • You will be able to see the data sent from ESP32 S3 on the AWS IoT Console.

Sending Data from AWS IoT to ESP32 S3

You are able to send the data from AWS IoT Console to the ESP32 S3 by doing the following steps:

  • On Arduino IDE, Open the Serial Monitor
  • On AWS IoT Console, navigate to Test MQTT Test Client
AWS IoT MQTT Test Client ESP32 S3
  • Click the Publish to a topic button.
  • Type esp32/aws-to-esp32 to the Topic name. You can change the topic but MUST be matched the topic on the ESP32 S3 code.
  • Optionally, you can change the message payload, or just keep it as default.
  • Click the Publish button.
  • Check out the Serial Monitor on Arduino IDE, you will see the message sent from AWS IoT Console.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
received: - topic: esp32/aws-to-esp32 - payload: { "message": "Hello from AWS IoT console" }
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications

The ESP32 S3 paired with AWS IoT Core opens the door to a wide range of real-world IoT solutions that benefit from cloud-scale processing, storage, and automation.

  1. Remote Environmental Monitoring: Deploy the ESP32 S3 in the field to stream temperature, humidity, or air-quality readings to AWS IoT, then visualize trends in Amazon QuickSight.
  2. Smart Agriculture: Collect soil moisture and weather data from ESP32 S3 nodes and trigger irrigation relays automatically using AWS IoT Rules and Lambda.
  3. Industrial Asset Tracking: Monitor machine vibration or power consumption on the factory floor, storing time-series data in Amazon Timestream for predictive maintenance.
  4. Home Automation Hub: Control lights, locks, and HVAC equipment remotely by publishing commands from a web dashboard to the ESP32 S3 via AWS IoT topics.
  5. Security Alerting: Have the ESP32 S3 publish motion-sensor or door-sensor events to AWS IoT, then route alerts to SNS for instant SMS or email notifications.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenges

Now that your ESP32 S3 is connected to AWS IoT Core, push the project further with the following challenges.

  1. Beginner: Modify the sketch to publish a fixed JSON payload containing your name and the current uptime, then verify receipt in the MQTT Test Client.
  2. Intermediate: Add a DHT22 temperature and humidity sensor to the ESP32 S3, publish readings every ten seconds, and create an AWS IoT Rule that stores the data in a DynamoDB table.
  3. Advanced: Build a two-way control dashboard using AWS Amplify that reads telemetry from DynamoDB and publishes relay-control commands back to the ESP32 S3 through AWS IoT Core.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!