What you'll build
A temperature and humidity sensor connected to the internet that sends data to your computer every 5 seconds.
When finished you'll have:
- An ESP32 reading real temperature and humidity from your room
- Data traveling over WiFi to a public MQTT broker
- A script on your computer showing real-time data
This is your first real IoT project. Hardware + Software + Internet.
Shopping list (exact)
You need these specific components:
| Component | Exact model | Approx price | Where to buy |
|---|---|---|---|
| Microcontroller | ESP32 DevKit V1 (30 pins) | $5-8 USD | Amazon, AliExpress |
| Sensor | DHT22 (blue) or DHT11 (cheaper blue) | $2-5 USD | Amazon, AliExpress |
| Cables | Jumper wires female-female (3 cables) | $2 USD | Any electronics store |
| USB Cable | Micro USB (to program ESP32) | $2 USD | You probably have one |
Total: ~$12-15 USD
Note: DHT22 is more accurate than DHT11. If you can, buy the DHT22.
Wiring diagram
Connect the DHT22 sensor to the ESP32 like this:
ESP32 DevKit V1 DHT22
+--------------+ +-------+
| | | |
| 3.3V |------ wire ------| VCC | (pin 1 - left)
| | | |
| GPIO4 |------ wire ------| DATA | (pin 2)
| | | |
| GND |------ wire ------| GND | (pin 4 - right)
| | | |
+--------------+ +-------+
Note: DHT22 pin 3 is not used (leave disconnected)
Connection table:
| ESP32 Pin | Wire | DHT22 Pin | Description |
|---|---|---|---|
| 3.3V | Red | Pin 1 (VCC) | Sensor power |
| GPIO4 | Yellow | Pin 2 (DATA) | Sensor data |
| GND | Black | Pin 4 (GND) | Ground |
Important: Use 3.3V, NOT 5V. The ESP32 works at 3.3V.
Step 1: Install Arduino IDE
- Go to arduino.cc/en/software
- Download Arduino IDE 2.x for your operating system
- Install and open Arduino IDE
Step 2: Configure ESP32 in Arduino IDE
ESP32 doesn't come pre-installed. We need to add it:
- Open Arduino IDE
- Go to File โ Preferences (or Arduino IDE โ Settings on Mac)
- In "Additional boards manager URLs" add:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - Click OK
- Go to Tools โ Board โ Boards Manager
- Search "esp32"
- Install "esp32 by Espressif Systems" (takes 2-3 minutes)
Step 3: Install libraries
You need 2 libraries:
- Go to Tools โ Manage Libraries
- Search "DHT sensor library" by Adafruit โ Install
- Search "PubSubClient" by Nick O'Leary โ Install
If asked about dependencies, accept to install them.
Step 4: Connect the ESP32
- Connect ESP32 to your computer with USB cable
- Go to Tools โ Board and select "ESP32 Dev Module"
- Go to Tools โ Port and select the port (COM3, COM4 on Windows, or /dev/cu.usbserial on Mac)
Port not showing? You need the CP210x driver:
- Windows/Mac: silabs.com/developers/usb-to-uart-bridge-vcp-drivers
Step 5: The complete code
Create a new sketch and paste this code:
// ============================================
// IoT Sensor: ESP32 + DHT22 + MQTT
// luxIA.us - Complete tutorial
// ============================================
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// --- CONFIGURATION: EDIT THESE VALUES ---
const char* WIFI_SSID = "YOUR_WIFI_NAME"; // Your WiFi name
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD"; // Your WiFi password
const char* MQTT_SERVER = "broker.hivemq.com"; // Public MQTT broker (free)
const int MQTT_PORT = 1883;
const char* MQTT_TOPIC = "luxia/sensor/yourName"; // Change "yourName" to something unique
// ----------------------------------------
// Sensor configuration
#define DHT_PIN 4 // GPIO4 - where you connected DATA
#define DHT_TYPE DHT22 // Change to DHT11 if using that model
DHT dht(DHT_PIN, DHT_TYPE);
WiFiClient espClient;
PubSubClient mqtt(espClient);
void setup() {
// Start serial monitor (to see messages on your computer)
Serial.begin(115200);
Serial.println();
Serial.println("=== IoT Sensor starting ===");
// Start sensor
dht.begin();
Serial.println("DHT sensor started");
// Connect WiFi
Serial.print("Connecting to WiFi: ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi connected! IP: ");
Serial.println(WiFi.localIP());
// Configure MQTT
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
Serial.println("MQTT configured");
}
void connectMQTT() {
while (!mqtt.connected()) {
Serial.print("Connecting to MQTT...");
// Create unique ID for this device
String clientId = "ESP32-" + String(random(0xffff), HEX);
if (mqtt.connect(clientId.c_str())) {
Serial.println(" connected!");
} else {
Serial.print(" failed, code: ");
Serial.print(mqtt.state());
Serial.println(" - retrying in 5 seconds");
delay(5000);
}
}
}
void loop() {
// Ensure MQTT connection
if (!mqtt.connected()) {
connectMQTT();
}
mqtt.loop();
// Read sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check valid reading
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Error reading sensor!");
delay(2000);
return;
}
// Create JSON message
String message = "{\"temp\":" + String(temperature, 1) +
",\"hum\":" + String(humidity, 1) + "}";
// Send via MQTT
mqtt.publish(MQTT_TOPIC, message.c_str());
// Show in Serial Monitor
Serial.print("Sent: ");
Serial.println(message);
// Wait 5 seconds
delay(5000);
}
Step 6: Configure YOUR WiFi
In the code, change these lines with your data:
const char* WIFI_SSID = "YOUR_WIFI_NAME"; // Your WiFi network name
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD"; // Your WiFi password
const char* MQTT_TOPIC = "luxia/sensor/yourName"; // Change yourName
Step 7: Upload the code
- Click the Upload button (right arrow) in Arduino IDE
- Wait for compilation (30-60 seconds)
- Wait for upload to ESP32 (shows "Connecting..." then percentages)
- When done, open Tools โ Serial Monitor
- Select 115200 baud in the bottom right corner
Step 8: Verify it works
In Serial Monitor you should see:
=== IoT Sensor starting ===
DHT sensor started
Connecting to WiFi: YOUR_WIFI
....
WiFi connected! IP: 192.168.1.105
MQTT configured
Connecting to MQTT... connected!
Sent: {"temp":23.5,"hum":45.2}
Sent: {"temp":23.6,"hum":45.1}
Sent: {"temp":23.5,"hum":45.3}
If you see this, it works! Your sensor is sending data to the internet.
Step 9: View data from your computer
Now let's receive that data on your computer.
Install an MQTT client:
npm install -g mqtt
Subscribe to your topic:
mqtt sub -h broker.hivemq.com -t "luxia/sensor/yourName"
You should see data arriving:
{"temp":23.5,"hum":45.2}
{"temp":23.6,"hum":45.1}
Troubleshooting
| Problem | Solution |
|---|---|
| COM port not showing | Install CP210x driver (link above) |
| "Error reading sensor" | Check wires: 3.3V to VCC, GPIO4 to DATA, GND to GND |
| Won't connect to WiFi | Verify exact name and password (case-sensitive) |
| Won't connect to MQTT | Check internet connection, try restarting ESP32 |
| Wrong temperature | Wait 2 minutes for sensor to stabilize |
Next step
โ Analytics Dashboard - Display this data in charts