/* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/arduino/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
WebServer server(80);
#define DHT_PIN 13
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
String response;
void sendHtml() {
response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charshet="utf-8" />
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>Nhiet do </h2>
<p>TEMP_VALUE °C</p>
<h2>Do Am </h2>
<p>HUMIDITY_VALUE %</p>
</div>
</body>
</html>
)";
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
response.replace("TEMP_VALUE", String(temperature));
response.replace("HUMIDITY_VALUE", String(humidity));
server.send(200, "text/html", response);
}
void setup(void) {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", sendHtml);
dht.begin(); // Khởi tạo cảm biến DHT22
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
server.send(200, "text/html", response);
server.handleClient();
delay(2000);
}