How to Connect an ESP32 to Microsoft Azure IoT Hub for Real-Time Data Monitoring

Connecting inexpensive edge hardware to robust cloud architecture is the foundation of modern engineering projects. The ESP32 microcontroller has become a staple for developers due to its integrated Wi-Fi capability and low price point. However, capturing data locally on your workbench is vastly different from streaming data securely to an enterprise cloud system.
While this tutorial will help you build a functional prototype on your workbench, companies looking to securely deploy thousands of these IoT nodes across a smart factory often team up with a Microsoft solutions partner to architect a highly scalable and secure Azure environment. For our purposes today, we will focus on bridging the gap between physical hardware and the cloud by setting up a reliable telemetry pipeline.
Prerequisites and Components Needed
To build this setup, you will need a few basic hardware components and an active Microsoft Azure account. The free tier of Azure is perfectly fine for running this tutorial without incurring unexpected costs.
| Table 1 | |
|---|---|
| Component / Tool | Purpose |
| ESP32 Development Board | Microcontroller handling the edge data and Wi-Fi connection. |
| DHT22 Sensor | Generates real-time temperature and humidity telemetry data. |
| Breadboard and Jumper Wires | Used for prototyping the physical circuit connections. |
| Arduino IDE | The software environment used to write and upload the C++ firmware. |
| Active Azure Subscription | Hosts the IoT Hub resource that receives our sensor data. |
| Thanks !!! | |
Step 1: Configuring the Microsoft Azure IoT Hub
First, log in to the Azure Portal and create an IoT Hub resource. This hub acts as the central cloud gateway that manages communication between your physical hardware and your cloud services.
- When creating the hub, select your resource group and choose the Free (F1) tier to keep the project budget-friendly.
- Once the hub is active, navigate to the Devices menu on the left sidebar and select the option to add a new device.
- Name your device something easily recognizable, such as ESP32_Sensor_Node.
- Keep the authentication type set to Symmetric Key.
After saving, click on your newly created device profile and copy the Primary Connection String. This unique string contains the authorization keys your microcontroller will need to verify its identity with the cloud.
Step 2: Hardware Wiring
Prototyping the physical circuit is straightforward. We are using a DHT22 sensor to generate real-time temperature and humidity data for our stream.
Connect the VCC pin of the DHT22 to the 3.3V pin on the ESP32, and connect the GND pin to a ground pin on the board. Then, connect the data pin of the sensor to GPIO 23 on your microcontroller.
Hardware Note: If you are using a bare DHT22 sensor rather than a pre-assembled module breakout board, remember to place a 10k-ohm pull-up resistor between the VCC line and the data line to ensure stable data transmission.
Step 3: Programming the ESP32
Open the Arduino IDE to write the firmware. You will need to install a few libraries through the built-in Library Manager, specifically WiFi.h for network access and the standard DHT sensor library from Adafruit. Because Azure requires secure connections, we will use an MQTT client library capable of handling TLS encryption.
In your code configuration, define your local Wi-Fi credentials and paste the connection string you copied from the Azure portal.
C++
// Example configuration block for the ESP32 code
const char* ssid = "Your_WiFi_Name";
const char* password = "Your_WiFi_Password";
const char* connectionString = "HostName=YourHub.azure-devices.net;DeviceId=ESP32_Sensor_Node;SharedAccessKey=...";
The setup function will initialize the serial monitor, connect to your local network, and start the sensor. Inside the main loop, write the logic to read the temperature and humidity every five seconds. Format these readings into a standard JSON payload string. The ESP32 will then publish this JSON payload directly to the MQTT topic provided by your Azure IoT Hub.
Step 4: Verifying Real-Time Data
Once your code is uploaded, open the Arduino serial monitor to ensure the ESP32 successfully connects to your local network. To verify that the data is reaching the cloud, return to your Azure Portal dashboard.
The metrics tab on your IoT Hub page will show a visible spike in incoming messages. For a detailed view of the raw data stream, you can use the Azure IoT Tools extension in Visual Studio Code. This tool allows you to monitor the live telemetry string as it hits the cloud in real time.
Expanding Project Horizons
Building this pipeline opens up incredible opportunities for further development. With your telemetry data now resting securely in Azure, you can easily route it to other cloud services. For example, you could feed the incoming sensor values into Power BI to create live visual graphs, or set up automated alerts that flag an operator if a temperature threshold is crossed. This basic workbench setup forms the core framework for complex industrial monitoring systems.































































