Hallo ihr Lieben,
ich programmiere mir gerade eine eigene Wetterstation für den Balkon. Diese soll mittels Webserver erreichbar sein.
Nun habe ich das Problem, dass der Webserver zwar erstellt wird und auch erreichbar ist, aber aus irgendeinem Grund die Seite weiß bleibt. Der HTML Teil ist auch ersichtlich. Allerdings wird mittlerweile folgende Fehlermeldung auf dem Webserver ausgegeben: setInterval(function () { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { // This code searches for the component with ID "dht" and replaces the component content with the returned content document.getElementById("dht").innerHTML = this.responseText; } }; // Request "/dht" via "GET" xhttp.open("GET", "/dht", true); xhttp.send(); }, 1000)
Folgenden Code nutze ich:
#include <Arduino.h>
#include "DHT.h"
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <analogWrite.h>
#include <ESP32_Servo.h>
#define DHTPIN 14
#define LEDPIN 33
#define LIGHTPIN 2
#define STEAMPIN 18
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char *SSID = "xxx";
const char *PASS = "xxx";
static int A = 0;
static int B = 0;
static int C = 0;
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<title>Wetterstation Kaulsdorf-Nord</title>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="btn">
<div id="dht"></div>
</div>
</body>
setInterval(function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// This code searches for the component with ID "dht" and replaces the component content with the returned content
document.getElementById("dht").innerHTML = this.responseText;
}
};
// Request "/dht" via "GET"
xhttp.open("GET", "/dht", true);
xhttp.send();
}, 1000)
</script>
<style>
/*Web page*/
html,body{margin: 0;width: 100%;height: 100%;}
body{display: flex;justify-content: center;align-items: center;}
#dht{text-align: center;width: 100%;height: 100%;color: #fff;background-color: #47a047;font-size: 48px;}
.btn button{width: 100%;height: 100%;border: none;font-size: 30px;color: #fff;position: relative;}
button{color: #ffff;background-color: #89e689;margin-top: 20px;}
.btn button:active{top: 2px;}
</style>
</html>
)rawliteral";
String Merge_Data(void)
{
float Temperature = dht.readTemperature();
float Humidity = dht.readHumidity();
String dataBuffer;
String Steam;
String Light;
Steam = String(analogRead(STEAMPIN);
//Photoresistor
Light = String(analogRead(LIGHTPIN));
dataBuffer += "<p>";
dataBuffer += "<h1>Sensoren Daten</h1>";
dataBuffer += "<b>Temperatur:</b><b>";
dataBuffer += Temperature;
dataBuffer += "</b><b>℃</b><br/>";
dataBuffer += "<b>Luftfeuchte:</b><b>";
dataBuffer += Humidity;
dataBuffer += "</b><b>%</b><br/>";
dataBuffer += "<b>Regen:</b><b>";
dataBuffer += Steam;
dataBuffer += "</b><b>%</b><br/>";
dataBuffer += "<b>Helligkeit:</b><b>";
dataBuffer += Light;
dataBuffer += "</b><b></b><br/>";
return dataBuffer;
}
void Config_Callback(AsyncWebServerRequest *request)
{
if (request->hasParam("value"))
{
String HTTP_Payload = request->getParam("value")->value();
Serial.printf("[%lu]%s\r\n", millis(), HTTP_Payload.c_str());
}
request->send(200, "text/plain", "OK");
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setup()
{
dht.begin(9600);
Serial.begin(9600);
WiFi.begin(SSID, PASS);
while (!WiFi.isConnected())
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Set pins modes
pinMode(LEDPIN,OUTPUT);
pinMode(STEAMPIN,INPUT);
pinMode(LIGHTPIN,INPUT);
delay(1000);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/html", index_html); });
server.on("/dht", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", Merge_Data().c_str()); });
server.on("/set", HTTP_GET, Config_Callback);
server.begin();
;
}
void loop(){
}
tippe oder füge den Code hier ein
Werden die Daten aus der DHT Abfrage auch in den databuffer geladen, um auf dem Webserver angezeigt zu werden?
Wo ist mein Denkfehler?