„esp32_temp_monitor.ino“ hinzufügen

This commit is contained in:
Michael Clemens // DK1MI 2023-05-29 10:58:40 +00:00
parent ef566f8ed5
commit 8a50c8b6aa
1 changed files with 109 additions and 0 deletions

109
esp32_temp_monitor.ino Normal file
View File

@ -0,0 +1,109 @@
#include <WiFi.h>
#include <WebServer.h>
#include "DHT.h"
// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
/*Put your SSID & Password*/
const char* ssid = "xxxxxxxxxxxxxxxxxxxx"; // Enter SSID here
const char* password = "xxxxxxxxxxxxxxxxxxxx"; //Enter Password here
WebServer server(80);
// DHT Sensor
uint8_t DHTPinRack = 4;
uint8_t DHTPinOutside = 2;
// Initialize DHT sensor.
DHT dht_rack(DHTPinRack, DHTTYPE);
DHT dht_outside(DHTPinOutside, DHTTYPE);
float temp_rack, temp_outside;
float hum_rack, hum_outside;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(DHTPinRack, INPUT);
pinMode(DHTPinOutside, INPUT);
dht_rack.begin();
dht_outside.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
temp_rack = dht_rack.readTemperature(); // Gets the values of the temperature
hum_rack = dht_rack.readHumidity(); // Gets the values of the humidity
temp_outside = dht_outside.readTemperature(); // Gets the values of the temperature
hum_outside = dht_outside.readHumidity(); // Gets the values of the humidity
server.send(200, "text/html", SendHTML(temp_rack,hum_rack,temp_outside,hum_outside));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(float temp_rack, float hum_rack, float temp_outside, float hum_outside){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>ESP32 Gartenhaus</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<h1>Outside</h1>\n";
ptr +="<p>Temperature: ";
ptr +=String(temp_outside,1);
ptr +="C</p>";
ptr +="<p>Humidity: ";
ptr +=String(hum_outside,1);
ptr +="%</p>";
ptr +="<h1>Rack</h1>\n";
ptr +="<p>Temperature: ";
ptr +=String(temp_rack,1);
ptr +="C</p>";
ptr +="<p>Humidity: ";
ptr +=String(hum_rack,2);
ptr +="%</p>";
ptr +="</div>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}