Secure ESP32 IoT: HTTPS, MQTT, and Network Hardening

Table of Contents🔗

Introduction🔗

The ESP32 is a powerful microcontroller with built-in Wi-Fi capabilitiesConnecting 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., making it a popular choice for IoT projects. However, with connectivity comes the responsibility of securing data transmission. This guide explores how to implement secure communicationConnecting 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. over Wi-Fi on the ESP32, focusing on HTTPS, secure MQTT, and network hardening techniques. By the end, you'll have the tools to build robust, secure IoT applicationsConnecting 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..

Why Secure Communication Matters🔗

In 2023, 63% of 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. attacks targeted unencrypted network traffic (Source: Palo Alto Networks). 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. Wi-Fi stack exposes two critical attack surfaces:

1. Data in Transit: Sensor readings, credentials, firmware updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.

2. Network Identity: Device spoofing, rogue APSetting 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. attacks

By implementing secure communicationConnecting 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. protocols, you can ensure data integrity, confidentiality, and authentication, safeguarding your 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. ecosystem.

HTTPS: Encrypted Web Communication🔗

HTTPS (Hypertext Transfer Protocol Secure) is the foundation of secure web communication. It encrypts data using TLS/SSLConnecting 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., preventing eavesdropping and tampering. Here’s how to implement HTTPS on the 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.:

Step 1: Generate a Certificate

To use HTTPS, you need a TLS certificate. You can either:

  • Use a self-signed certificate for testing.
  • Obtain a CA-signed certificate for production.

For self-signed certificates, you can use OpenSSLAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices.:

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

Step 2: Load the Certificate on ESP32

Convert the certificate and key into a format compatible with the 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.:

openssl x509 -outform DER -in cert.pem -out cert.der
openssl rsa -outform DER -in key.pem -out key.der

Then, embed them in your 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. code:

#include <WiFiClientSecure.h>
WiFiClientSecure client;
void setup() {
  client.setCACert(cert_der, sizeof(cert_der));
  client.setPrivateKey(key_der, sizeof(key_der));
}

Step 3: Make HTTPS Requests

Use the WiFiClientSecure object to make secure requests:

void loop() {
  if (client.connect("example.com", 443)) {
    client.println("GET /path HTTP/1.1");
    client.println("Host: example.com");
    client.println("Connection: close");
    client.println();
  }
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }
}

Key Settings for HTTPS:

ParameterRecommended ValueRationale
TLS Version1.2 or 1.3PCI-DSS compliance
Certificate ValidationFull Chain (not All)Prevents self-signed cert bypass
Cipher SuitesECDHE-ECDSA-AES128-GCM-SHA256Balance of speed & security

MQTT with TLS: Securing IoT Messaging🔗

MQTT (Message Queuing Telemetry TransportProtocol Bridging: Translating MQTT to CoAP for 6LoWPAN NetworksProtocol Bridging: Translating MQTT to CoAP for 6LoWPAN NetworksExplore seamless integration of MQTT and CoAP to optimize IoT by bridging cloud platforms with resource-sensitive 6LoWPAN networks via the versatile ESP32.) is widely used in IoT for lightweight messaging. However, without encryption, it’s vulnerable to attacks. Here’s how to secure MQTT on ESP32:

Step 1: Use TLS/SSL for MQTT

Enable TLS encryption for MQTT communication. Most MQTT brokers (e.g., Mosquitto, 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.) support TLS.

Step 2: Configure the ESP32 MQTT Client

Use the PubSubClient library with TLS:

#include <WiFiClientSecure.h>
#include <PubSubClient.h>
WiFiClientSecure wifiClient;
PubSubClient mqttClient(wifiClient);
void setup() {
  wifiClient.setCACert(cert_der, sizeof(cert_der));
  mqttClient.setServer("mqtt.example.com", 8883);
}
void loop() {
  if (!mqttClient.connected()) {
    mqttClient.connect("ESP32Client", "username", "password");
    mqttClient.subscribe("topic");
  }
  mqttClient.loop();
}

Step 3: Authenticate with Certificates

For enhanced security, use client certificates for mutual TLS (mTLS):

wifiClient.setCertificate(client_cert_der, sizeof(client_cert_der));
wifiClient.setPrivateKey(client_key_der, sizeof(client_key_der));

Certificate Management Strategies🔗

Embedding Certificates:

1. Direct in Code: Simple but insecure

const char* root_ca = "-----BEGIN CERTIFICATE-----...";

2. SPIFFSCreating a Wi-Fi Configuration Web Panel for ESP32Creating a Wi-Fi Configuration Web Panel for ESP32Discover how to create a secure ESP32 Wi-Fi configuration web panel for dynamic IoT deployments. Enjoy easy network setups and improved device management. Storage:

File caFile = SPIFFS.open("/ca.crt");
client.loadCACert(caFile, caFile.size());

3. Hardware Security (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.-WROOM-S2/S3):

esp_cert_store_set_cert(CA_CERT, ca_cert, ca_cert_len);

Network Hardening Techniques🔗

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. Firewall Rules:

// Block all incoming connections by default
esp_wifi_set_default_wifi_sta_handlers();
esp_wifi_set_sta_ip(IP, GW, NM);
esp_netif_dhcpc_stop(netif);

Service Hardening Table:

ServiceActionCommand/Code Snippet
mDNSDisablemdns_free()
SNTPRestrict to 3 NTP poolsconfigTzTime()
TelnetDisablemake menuconfig → Disable
Debug LogsObfuscateesp_log_level_set("", ESP_LOG_ERROR);

End-to-End Example: Secure Weather Station🔗

Scenario: Temperature/Humidity sensor sending 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. and local dashboard.

#include <AWS_IOT.h>
#include <WiFiClientSecure.h>
AWS_IOT awsClient;
WiFiClientSecure net;
void setup() {
  // 1. Secure Wi-Fi Connection
  WiFi.begin(ssid, password, WIFI_CHANNEL_6, bssid, true);
  // 2. Certificate Setup
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);
  // 3. MQTT Connection
  awsClient.begin("a3qj89xzf7a8oi-ats.iot.us-west-2.amazonaws.com", 8883, net);
  // 4. Publish Secure Data
  char payload[256];
  sprintf(payload, "{\"temp\":%.2f,\"hum\":%.2f}", readTemp(), readHumidity());
  awsClient.publish("iot/weather", payload);
}
void loop() {
  // Implement keep-alive and error recovery
}

Security Checklist:

Best Practices and Conclusion🔗

To wrap things up, here are some best practicesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications.:

By combining WPA2-secured Wi‑Fi, HTTPS for secure server connections, and MQTT over SSL/TLS for reliable messaging, you build a solid foundation for any IoT application. This layered approach not only protects your data but also increases user trust and system resilienceConnecting 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..

Implementing these techniques on the 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. may seem challenging at first, but with a practical approach and continuous learning, you can master these protocols and build truly secure, connected devices.

Happy coding and stay secure!

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