ESP32 with MySQL Database Management: Read & Write

ESP32 With MySQL Database Management: Read & Write Operations Tutorial ( Part 1 ) IOT Projects

ESP32 Projects IoT Projects MySQL Database

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

ComponentQuantityDescription
ESP32 Board1Wi-Fi and Bluetooth enabled microcontroller
DHT22 Sensor1For measuring temperature and humidity
Jumper WiresAs neededFor connections
Breadboard1For circuit assembly
MySQL Server1Local or online database
USB Cable1For programming ESP32

🔌 Circuit Diagram

📷 [Insert your circuit diagram image here]
Connections:

  • DHT22 → ESP32 GPIO 4
  • VCC → 3.3V
  • GND → GND

💾 Setting Up MySQL Database

  1. Install XAMPP or use Remote MySQL Hosting (like 000webhost / InfinityFree).
  2. Create a database named esp32_data.
  3. Inside it, create a table sensor with columns:
    • id (INT, AUTO_INCREMENT, PRIMARY KEY)
    • temperature (FLOAT)
    • humidity (FLOAT)
    • timestamp (DATETIME DEFAULT CURRENT_TIMESTAMP)
  4. Upload your PHP API files (insert.php, read.php) to your hosting folder (htdocs or /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

  1. ESP32 connects to your Wi-Fi network.
  2. Reads data from DHT22 sensor.
  3. Sends data to MySQL through the REST API (insert.php).
  4. 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

Leave a Reply

Your email address will not be published. Required fields are marked *