/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
*********/
// Import required libraries
#ifdef ESP32
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#else
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#endif
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// HTML content as a string
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.highcharts.com/highcharts.js"></script>
<style>
body {
min-width: 310px;
max-width: 800px;
height: 400px;
margin: 0 auto;
}
h2 {
font-family: Arial;
font-size: 2.5rem;
text-align: center;
}
</style>
</head>
<body>
<h2>ESP Weather Station</h2>
<div id="chart-temperature" class="container"></div>
<div id="chart-humidity" class="container"></div>
</body>
<script>
var chartT = new Highcharts.Chart({
chart:{ renderTo : 'chart-temperature' },
title: { text: 'DHT22 Temperature' },
series: [{ showInLegend: false, data: [] }],
plotOptions: {
line: { animation: false, dataLabels: { enabled: true } },
series: { color: '#059e8a' }
},
xAxis: { type: 'datetime', dateTimeLabelFormats: { second: '%H:%M:%S' } },
yAxis: { title: { text: 'Temperature (Celsius)' } },
credits: { enabled: false }
});
setInterval(function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
if(chartT.series[0].data.length > 40) {
chartT.series[0].addPoint([x, y], true, true, true);
} else {
chartT.series[0].addPoint([x, y], true, false, true);
}
}
};
xhttp.open("GET", "/temperature", true);
xhttp.send();
}, 30000);
var chartH = new Highcharts.Chart({
chart:{ renderTo:'chart-humidity' },
title: { text: 'DHT22 Humidity' },
series: [{ showInLegend: false, data: [] }],
plotOptions: {
line: { animation: false, dataLabels: { enabled: true } }
},
xAxis: { type: 'datetime', dateTimeLabelFormats: { second: '%H:%M:%S' } },
yAxis: { title: { text: 'Humidity (%)' } },
credits: { enabled: false }
});
setInterval(function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
if(chartH.series[0].data.length > 40) {
chartH.series[0].addPoint([x, y], true, true, true);
} else {
chartH.series[0].addPoint([x, y], true, false, true);
}
}
};
xhttp.open("GET", "/humidity", true);
xhttp.send();
}, 30000);
</script>
</html>
)rawliteral";
String readDHT22Temperature() {
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read from DHT22 sensor!");
return "";
} else {
return String(t);
}
}
String readDHT22Humidity() {
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read from DHT22 sensor!");
return "";
} else {
return String(h);
}
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
Serial.println("IP Address: " + WiFi.localIP().toString());
// Define server routes
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readDHT22Temperature().c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readDHT22Humidity().c_str());
});
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Empty loop as we use AsyncWebServer
}