AWS IoT Core & ESP32: Secure Connection and Shadow Setup

This comprehensive guide combines practical steps, security strategies, and real-world examples to connect ESP32 microcontrollersArquitetura ESP32: SoC dual-core, subsistemas RF integradosArquitetura ESP32: SoC dual-core, subsistemas RF integradosDiscover the ESP32’s dual-core prowess and integrated RF subsystems for efficient, innovative IoT applications—from smart homes to industrial sensors. to AWS IoT Core. Learn how to implement secure authentication with X.509 certificates, synchronize device states using Device Shadows, and follow best practices for scalable IoT deployments.

Table of Contents🔗

Introduction to AWS IoT Core & ESP32🔗

AWS IoT CoreConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. enables secure, bidirectional communication between IoT devices and the cloud. When paired with the ESP32, you can build scalable solutions for industrial automation, smart homes, and remote monitoring. Key features include:

  • Secure Authentication: X.509 certificates for device identity verification.
  • State Management: Device Shadows for real-time and offline synchronization.
  • Scalability: Support for millions of devices and integration with AWS services like Lambda and DynamoDB.

X.509 Certificates: Secure Authentication🔗

X.509 certificates provide mutual TLS (mTLS) authentication, ensuring only authorized devices connect to AWS IoT CoreConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies..

Key Features:

Generating and Managing Certificates🔗

Method 1: Using OpenSSL

Generate a certificate and private key:

openssl req -newkey rsa:2048 -nodes -keyout device.key -x509 -days 365 -out device.pem

Method 2: AWS IoT Console

1. Create a Thing in AWS IoT CoreConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies..

2. Generate and download certificates, keys, and the root CA.

3. Attach an IoTSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsLearn efficient data encoding techniques for Sigfox's constrained 12-byte payloads. Discover bitwise operations, structured encoding & CBOR strategies. policy granting permissions like iotSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsLearn efficient data encoding techniques for Sigfox's constrained 12-byte payloads. Discover bitwise operations, structured encoding & CBOR strategies.:Connect and iotSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsLearn efficient data encoding techniques for Sigfox's constrained 12-byte payloads. Discover bitwise operations, structured encoding & CBOR strategies.:Publish.

Policy Example:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["iot:Connect", "iot:Publish", "iot:Subscribe"],
    "Resource": ["arn:aws:iot:region:account:client/esp32"]
  }]
}

Embedding Certificates in ESP32 Firmware🔗

Store certificates in the ESP32’sCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsLearn how to integrate Wi-Fi and deep sleep on ESP32 to maximize battery life in IoT devices. This guide offers practical tips and step-by-step instructions. flash memory and configure a secure MQTT client.

Example (Arduino Framework):

const char AWS_CERT_CA[] = "-----BEGIN CERTIFICATE-----\n...";
const char AWS_CERT_CRT[] = "-----BEGIN CERTIFICATE-----\n...";
const char AWS_CERT_PRIVATE[] = "-----BEGIN RSA PRIVATE KEY-----\n...";
WiFiClientSecure net;
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
MQTTClient client(256);
client.begin("your-ats.iot.region.amazonaws.com", 8883, net);

Secure Storage Tip: Use ESP32’sCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsLearn how to integrate Wi-Fi and deep sleep on ESP32 to maximize battery life in IoT devices. This guide offers practical tips and step-by-step instructions. NVS (Non-Volatile Storage) for private keys.

Device Shadows: State Synchronization🔗

Device Shadows are JSON documents storing device states. Use them to:

Shadow Document Example:

{
  "state": {
    "reported": {"temperature": 25.3},
    "desired": {"led_status": 1}
  }
}

Publishing a Shadow Update from ESP32:

void publishShadowUpdate() {
  String payload = "{\"state\":{\"reported\":{\"temperature\":" + String(readTemp()) + "}}}";
  client.publish("$aws/things/esp32/shadow/update", payload);
}

Subscribing to Desired State Changes:

client.subscribe("$aws/things/esp32/shadow/update/delta");
void messageHandler(String &topic, String &payload) {
  if (topic.endsWith("/delta")) {
    DynamicJsonDocument doc(256);
    deserializeJson(doc, payload);
    bool ledStatus = doc["state"]["led_status"];
    digitalWrite(LED_PIN, ledStatus);
  }
}

Step-by-Step Setup Guide🔗

1. AWS IoT CoreConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. Configuration:

2. ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. Firmware Setup:

3. Complete Code Example:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
WiFiClientSecure net;
MQTTClient client;
void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PASSWORD");
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);
  client.begin("AWS_ENDPOINT", net);
  client.onMessage(messageHandler);
}
void loop() {
  if (!client.connected()) connectAWS();
  client.loop();
  publishShadowUpdate();
  delay(5000);
}

Security Best Practices🔗

1. Certificate Rotation: Use AWS IoTConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. Jobs to programmatically rotate credentials.

2. Least Privilege Policies: Restrict device actions to only what’s necessary.

3. Secure Storage: Store keys in NVS or hardware security modules (HSMs).

4. OTA UpdatesImplementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Implementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Learn how to implement secure and reliable OTA updates on ESP32 for enhanced IoT performance, easy updates, and rollback capability without physical access.: Deploy firmware updates securely to patch vulnerabilities.

Error Handling and Troubleshooting🔗

  • Connection Issues:
void connectAWS() {
  while (!client.connect("esp32")) {
    Serial.print(".");
    delay(1000);
  }
}

Conclusion🔗

By leveraging X.509 certificates for authentication and Device Shadows for state synchronization, the ESP32 becomes a robust, scalable edge device in AWS IoTConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. ecosystems. Implement certificate rotation, granular policies, and secure storage to ensure production-grade security and reliability.

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles