Yarana Iot Guru
Published © MIT

DHT22 Real-Time Data to MySQL Build an IoT Dashboard

Monitor real-time temperature and humidity using DHT22, ESP32, and MySQL with a smart IoT dashboard. Simple IoT project by Yarana IoT Guru

BeginnerFull instructions provided8 hours34
DHT22 Real-Time Data to MySQL Build an IoT Dashboard

Things used in this project

Hardware components

DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

Schematic ESP32 with DHT22

Code

Program ESP32 with DHT22

C/C++
#include 
#include 
#include "DHT.h"

#define DHTPIN 4       // DHT sensor pin
#define DHTTYPE DHT22  // or DHT11 if using DHT11

const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* serverName = "https://yaranaiotguru.in/tutorial/DHT/insert_data.php";

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  dht.begin();

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

  if (isnan(temp) || isnan(hum)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    String postData = "temperature=" + String(temp) + "&humidity=" + String(hum);
    int httpResponseCode = http.POST(postData);

    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    Serial.println("Server response: " + http.getString());

    http.end();
  } else {
    Serial.println("WiFi disconnected");
  }
  delay(5000);
}

Credits

Yarana Iot Guru
35 projects • 0 followers
Yarana Iot Guru Yarana IoT Guru: Arduino, ESP32, GSM, NodeMCU & more. Projects, Tutorials & App Development. Innovate with us!
Thanks to Yarana Iot Guru.

Comments