Hallo zusammen,
Ich bin gerade dabei mein erstes Projekt mit dem NodeMCU zu verwirklichen. Dazu habe ich mir den DHT22 dazu geholt und gebe dessen Daten über einen Webserver auf dem NodeMCU im XML Format aus, die ich dann wiederum mit Icinga2 auslese und mir mit Grafana darstellen lasse. Ähnlich wie in diesem Tutorial: https://daniel-ziegler.com/monitorin...afana-esp8266/
Nun habe ich das Gefühl, dass sich der Webserver ab und zu aufhängt. Ich komme einfach nicht mehr drauf und auch Grafana zeigt mir, dass in dieser Zeit keine Daten empfangen werden konnten. Nach einer Weile fängt er sich dann wohl wieder.
Hat jemand gleiche Erfahrungen damit gemacht, oder liegt es vielleicht an meinem Code? Habe ich etwas vergessen oder falsch gemacht? Falsche Bibliotheken geladen?
Viele GrüßeCode:// DHT22 Struct struct Humidity { int humidity; unsigned int humidityFrac; int temperature; unsigned int temperatureFrac; }; #include <ESP8266WiFi.h> #include "DHT.h" const char* ssid = "<WlanSSID>"; const char* password = "<MeinWlanPasswort>"; #define DHTTYPE DHT22 const int DHTPin = D1; DHT dht(DHTPin, DHTTYPE); //Static IP IPAddress ip(192, 168, 1, x); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); dht.begin(); // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); //Disable WiFi AP WiFi.mode(WIFI_STA); //Set Static IP WiFi.config(ip, gateway, subnet); //Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); Humidity humidity = getHumidityAndTemperature(); Serial.print("Humidity: "); Serial.print(humidity.humidity); Serial.print("."); Serial.print(humidity.humidityFrac); Serial.println("%"); Serial.print("Temperature: "); Serial.print(humidity.temperature); Serial.print("."); Serial.print(humidity.temperatureFrac); Serial.println("*C"); // Prepare the response String response = "HTTP/1.1 200 OK\r\nContent-Type: application/xml\r\n\r\n<?xml version='1.0'?><document>"; response += "<humidity>"; response += humidity.humidity; response += "."; response += humidity.humidityFrac; response += "</humidity>"; response += "<temperature>"; response += humidity.temperature; response += "."; response += humidity.temperatureFrac; response += "</temperature>"; response += "<freeheap>"; response += ESP.getFreeHeap(); response += "</freeheap>"; response += "<cpufreqmhz>"; response += ESP.getCpuFreqMHz(); response += "</cpufreqmhz>"; response += "</document>\n"; // Send the response to the client client.print(response); delay(1); Serial.println("Client disonnected"); Serial.println(server.status()); // The client will actually be disconnected // when the function returns and 'client' object is destroyed } Humidity getHumidityAndTemperature() { struct Humidity humidityStruct; float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); humidityStruct.humidity = int(humidity); humidityStruct.humidityFrac = (humidity - int(humidity)) * 100; humidityStruct.temperature = int(temperature); humidityStruct.temperatureFrac = (temperature - int(temperature)) * 100; return humidityStruct; }
B4DschK4Pp







Zitieren


Lesezeichen