In this project, we’ll learn how to connect the ESP32 microcontroller to a MySQL database to read and write real-time data such as temperature and humidity.
We’ll also use a simple REST API to send and retrieve data, making this project perfect for IoT dashboard creation.
By the end of this tutorial, you’ll be able to:
- Send sensor data from ESP32 to MySQL database
- Retrieve live readings from your database
- Build a real-time IoT dashboard
🧩 Components Required
| Component | Quantity | Description |
|---|---|---|
| ESP32 Board | 1 | Wi-Fi and Bluetooth enabled microcontroller |
| DHT22 Sensor | 1 | For measuring temperature and humidity |
| Jumper Wires | As needed | For connections |
| Breadboard | 1 | For circuit assembly |
| MySQL Server | 1 | Local or online database |
| USB Cable | 1 | For programming ESP32 |
🔌 Circuit Diagram
📷 [Insert your circuit diagram image here]
Connections:
- DHT22 → ESP32 GPIO 4
- VCC → 3.3V
- GND → GND
💾 Setting Up MySQL Database
- Install XAMPP or use Remote MySQL Hosting (like 000webhost / InfinityFree).
- Create a database named
esp32_data. - Inside it, create a table
sensorwith columns:id(INT, AUTO_INCREMENT, PRIMARY KEY)temperature(FLOAT)humidity(FLOAT)timestamp(DATETIME DEFAULT CURRENT_TIMESTAMP)
- Upload your PHP API files (
insert.php,read.php) to your hosting folder (htdocsor/public_html/).
🧠 PHP API Code Example
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "esp32_data";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET["temperature"]) && isset($_GET["humidity"])) {
$temperature = $_GET["temperature"];
$humidity = $_GET["humidity"];
$sql = "INSERT INTO sensor (temperature, humidity) VALUES ('$temperature', '$humidity')";
if ($conn->query($sql) === TRUE) {
echo "Data inserted successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
⚙️ ESP32 Arduino Code
#include <WiFi.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
const char* server = "http://yourwebsite.com/insert.php"; // Replace with your link
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String url = String(server) + "?temperature=" + String(t) + "&humidity=" + String(h);
WiFiClient client;
if (client.connect("yourwebsite.com", 80)) {
client.println("GET " + url + " HTTP/1.1");
client.println("Host: yourwebsite.com");
client.println("Connection: close");
client.println();
}
Serial.print("Temperature: "); Serial.println(t);
Serial.print("Humidity: "); Serial.println(h);
delay(10000);
}
📊 How It Works
- ESP32 connects to your Wi-Fi network.
- Reads data from DHT22 sensor.
- Sends data to MySQL through the REST API (insert.php).
- You can view or plot this data in any dashboard (like ThingSpeak or your custom site).
📺 Video Tutorial
🎥 [Embed your YouTube video here]
Use the “YouTube” block in WordPress → paste the video link.
🏁 Conclusion
With this setup, you can log sensor data to a MySQL database and visualize it in real-time.
This method is perfect for:
- Smart Home monitoring
- Environmental sensing
- Industrial IoT systems