A step-by-step guide to securely transmit sensor data from an ESP32 microcontroller to Microsoft Azure IoT Hub using MQTT.
This project demonstrates how to send telemetry data (e.g., mock temperature, humidity) from an ESP32 to Azure IoT Hub. It includes:
- Secure communication with Azure IoT Hub (TLS 1.2).
- JSON-formatted telemetry data.
- Integration with PlatformIO (VS Code) for streamlined development.
- ESP32 board (e.g., Seeed Studio XIAO ESP32-C6).
- USB cable for programming.
- Visual Studio Code with PlatformIO extension.
- Azure account (free tier available).
- Wi-Fi SSID and password.
- Azure IoT Hub connection details:
- Hostname
- Device ID
- Primary Key
- Create an IoT Hub in the Azure Portal.
- Register a new device in your IoT Hub and note its Device ID and Primary Key.
git clone https://github.com/your-repo/esp32-azure-iot.git
- Open the project in VS Code with PlatformIO.
- Update
lib/iot_configs/iot_configs.h
with your credentials:
// Wi-Fi
#define WIFI_SSID "your-ssid"
#define WIFI_PASSWORD "your-password"
// Azure IoT Hub
#define HOSTNAME "your-iot-hub-hostname"
#define DEVICE_ID "your-device-id"
#define PRIMARY_KEY "your-primary-key"
- Build the Project
- Upload to ESP32
- Use monitor output to debug
- Navigate to your IoT Hub in the Azure Portal.
- Use the Azure CLI to monitor incoming data:
az iot hub monitor-events --hub-name YourHubName --device-id YourDeviceID
Example output:
{"temperature": 25.5, "humidity": 60}
-
Wi-Fi Connection Failed
- Cause: Incorrect SSID or password.
- Fix: Update
WIFI_SSID
andWIFI_PASSWORD
inlib/iot_configs/iot_configs.h
.
-
Azure Authentication Error
- Cause: Invalid IoT Hub credentials.
- Fix: Verify
HOSTNAME
,DEVICE_ID
, andPRIMARY_KEY
iniot_configs.h
.
-
MQTT Protocol Errors
- Cause: TLS version mismatch.
- Fix: Ensure Azure IoT Hub uses TLS 1.2 (configured during hub creation).
-
Serial Monitor Shows No Output
- Cause: Incorrect baud rate or USB port issues.
- Fix:
- Set Serial Monitor baud rate to
115200
. - Reconnect the ESP32 USB cable.
- Set Serial Monitor baud rate to
-
Data Not Visible in Azure IoT Hub
- Cause: Device not registered or firewall restrictions.
- Fix:
- Re-register the device in Azure IoT Hub.
- Allow outbound traffic on port
8883
(MQTT over TLS).