Introduction
In this tutorial, we will walk through the process of connecting an ESP32 microcontroller to AWS IoT Core using MQTT to publish temperature sensor data securely with TLS encryption. This setup will allow you to remotely monitor temperature readings from your ESP32 device.
Prerequisites
- Basic knowledge of programming with Arduino IDE
- ESP32 development board
- Temperature sensor (e.g., DHT11 or DS18B20)
- AWS account with access to AWS IoT Core
- Arduino IDE installed with ESP32 board support
- Wi-Fi network for the ESP32 device
Parts/Tools
- ESP32 development board
- Temperature sensor (DHT11 or DS18B20)
- Jumper wires
- Computer with Arduino IDE
- AWS IoT Core access
Steps
- Set Up AWS IoT Core
- Log in to your AWS Management Console.
- Navigate to the IoT Core service.
- Create a new IoT Thing:
- Click on “Manage” and then “Things”.
- Click “Create a thing”.
- Choose “Create a single thing”.
- Provide a name for your Thing and click “Next”.
- Create a new certificate for authentication:
- Choose “Create certificate”.
- Download the certificate, private key, and Amazon Root CA. Store these securely.
- Attach a policy to the certificate that allows publishing to your topic:
allow-iot-policy { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Connect", "iot:Publish", "iot:Subscribe", "iot:Receive" ], "Resource": "*" } ] }
- Connect the Temperature Sensor to the ESP32
- Wiring:
- For DHT11: Connect VCC to 3V3, GND to GND, and Data pin to GPIO 4.
- For DS18B20: Connect VCC to 3V3, GND to GND, and Data pin to GPIO 4 with a 4.7k ohm resistor between VCC and Data pin.
- Wiring:
- Install Required Libraries
- In Arduino IDE, go to Sketch -> Include Library -> Manage Libraries.
- Search and install the following libraries:
- DHT sensor library (if using DHT11)
- OneWire (if using DS18B20)
- PubSubClient for MQTT
- Write the Arduino Code
- Open a new sketch in Arduino IDE and include the necessary libraries:
#include <WiFi.h> #include <PubSubClient.h> #include <DHT.h> // or <OneWire.h> if using DS18B20 - Define your Wi-Fi and AWS IoT credentials:
const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* mqtt_server = "YOUR_AWS_IOT_ENDPOINT"; const char* mqtt_topic = "temperature/data"; const char* aws_root_ca = "-----BEGIN CERTIFICATE-----n..."; // Your Root CA const char* device_cert = "-----BEGIN CERTIFICATE-----n..."; // Your device certificate const char* device_key = "-----BEGIN PRIVATE KEY-----n..."; // Your private key - Setup Wi-Fi and MQTT:
WiFiClientSecure net; net.setCACert(aws_root_ca); net.setCertificate(device_cert); net.setPrivateKey(device_key); PubSubClient client(net); client.setServer(mqtt_server, 8883); - Implement the loop to read temperature and publish:
void loop() { if (!client.connected()) { reconnect(); } client.loop(); float temperature = dht.readTemperature(); // Replace with appropriate read function String tempString = String(temperature); client.publish(mqtt_topic, tempString.c_str()); delay(60000); // Publish every minute }
- Open a new sketch in Arduino IDE and include the necessary libraries:
- Upload the Code to the ESP32
- Select the correct board and port in the Arduino IDE.
- Upload the code to your ESP32.
- Monitor the Data on AWS IoT Core
- In the AWS IoT Console, navigate to “Test”.
- Subscribe to your topic:
temperature/data - Check for incoming data from the ESP32.
Troubleshooting
- If you can’t connect to Wi-Fi, check your SSID and password.
- If MQTT connection fails, verify your endpoint, certificates, and policy permissions.
- Ensure the temperature sensor is correctly wired and functioning.
- Check the AWS IoT Console for any error messages related to your Thing.
Conclusion
By following this tutorial, you have successfully set up your ESP32 to publish temperature sensor data to AWS IoT Core securely using MQTT and TLS encryption. This foundational setup can be expanded with additional sensors or features as you grow your IoT projects.


