... als Text
Completion requirements
#include <NTPClient.h> /********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-websocket-server-arduino/ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ // Import required libraries #include <WiFi.h> #include <AsyncTCP.h> #include <ESPAsyncWebServer.h> #include "soc/soc.h" #include "soc/rtc_cntl_reg.h" //for disabling brownout detector #include "time.h" #include <WiFiUdp.h> #include "sntp.h" // aktuelle Zeit holen #include <DHT.h> #define DHT_SENSOR_PIN 12 // ESP32 pin GPIO21 connected to DHT11 sensor #define DHT_SENSOR_TYPE DHT22 DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE); // Replace with your network credentials const char* ssid = "ESP32net"; const char* password = ""; const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 1*3600; // GMT+1 (2 Stunde Zeitunterschied), 1 im Winter const int daylightOffset_sec = 0; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, ntpServer, gmtOffset_sec); WiFiServer server(80); const char index_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML><html> <head> <title>ESP-Temperatur & Luftfeuchtigkeit-Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style> html { font-family: Arial, Helvetica, sans-serif; text-align: center; } h1 { font-size: 0.8rem; color: blue; } table, th, td { padding: 10px; border: 1px solid black; border-collapse: collapse; } h2{ font-size: 1.5rem; font-weight: bold; color: #143642; } .topnav { overflow: hidden; background-color: #143642; } body { margin: 0; } .content { padding: 30px; max-width: 600px; margin: 0 auto; } .card { background-color: #F8F7F9;; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); padding-top:10px; padding-bottom:20px; } .button { padding: 15px 50px; font-size: 24px; text-align: center; outline: none; color: #fff; background-color: #0f8b8d; border: none; border-radius: 5px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-tap-highlight-color: rgba(0,0,0,0); } /*.button:hover {background-color: #0f8b8d}*/ .button:active { background-color: #0f8b8d; box-shadow: 2 2px #CDCDCD; transform: translateY(2px); } .state { font-size: 1.5rem; color:#8c8c8c; font-weight: bold; } </style> </head> )rawliteral"; void setup(){ WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector // Serial port for debugging purposes Serial.begin(9600); Serial.println("Monitor an"); dht_sensor.begin(); // initialize the DHT sensor // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Connecting to WiFi.."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Zeitübertragung über NTP konfigurieren configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); server.begin(); Serial.println("Read from DHT sensor:"); } void loop() { // read humidity float humi = dht_sensor.readHumidity(); // read temperature in Celsius float tempC = dht_sensor.readTemperature(); // read temperature in Fahrenheit float tempF = dht_sensor.readTemperature(true); // check whether the reading is successful or not if ( isnan(tempC) || isnan(tempF) || isnan(humi)) { //Serial.println("Failed to read from DHT sensor!"); //Serial.print("."); } else { Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); delay(3000); } struct tm timeinfo; if (!getLocalTime(&timeinfo)) { Serial.println("Keine Zeit verfügbar (noch nicht)"); return; } // Die Zeit in einem String speichern char timeString[50]; strftime(timeString, sizeof(timeString), "%A, %B %d %Y %H:%M:%S", &timeinfo); // Hier können Sie die Zeit auf der seriellen Schnittstelle ausgeben Serial.println(timeString); WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("New Client."); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); client.println(index_html); // Tabelle formatieren // the content of the HTTP response follows the header: client.println("<body>"); client.print("<h2><table><tr><td>Temperatur: </td><td></td><td>Luftfeuchtigkeit: </td></tr><tr><td>T = "); client.print(tempC); client.print(" °C</td>"); client.print("<td></td><td>H = "); client.print(humi); client.print(" %</td></tr></tr></table></h2>"); client.print("<h1><table><tr><td>Aktuelle Zeit: "); client.println(timeString); client.print("</td></tr></table></h1>"); // Fügen Sie den Reload-Button hinzu client.println("<form><input type='button' value='Seite aktualisieren' onClick='window.location.reload()'></form>"); client.println("</body>"); client.println("</html>"); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } delay(2000); }
Last modified: Monday, 9 December 2024, 9:25 AM