/********* https://circuits4you.com/2018/11/20/web-server-on-esp32-how-to-update-and-display-sensor-values/ *********/ // ---------------------------------------------------------------------------------------------------------------- // Webserver // s. a.: https://lastminuteengineers.com/creating-esp32-web-server-arduino-ide/ // https://randomnerdtutorials.com/esp32-async-web-server-espasyncwebserver-library/ // https://github.com/me-no-dev/ESPAsyncWebServer ESPAsyncWebServer-master.zip // Installieren über Sketch-Install ZIP Library ... // NICHT die aus der IDE-Library nehmen! // Load Wi-Fi library #include #include //#include #include #include #include #include #include #include "webPageSource.h" /** * VAR1 accessPointMode = true; ESP32 ist selber ein AccessPoint * VAR2 accessPointMode = false; Verbindung mit dem Heimnetz */ bool accessPointMode = false; // VAR1 AccessPointMode const char *ssid = SECRET_AP_SSID; const char *password = SECRET_AP_PASSWD; /* Put IP Address details */ IPAddress local_ip(192, 168, 3, 1); IPAddress gateway(192, 168, 3, 1); IPAddress subnet(255, 255, 255, 0); // VAR2 Wifi-Client const char *ssid2 = SECRET_SSID; const char *password2 = SECRET_PASSWD; // Set web server port number to 80 AsyncWebServer server(80); // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; JsonObject root; void (*resetFunc)(void) = 0; //declare reset function @ address 0 void setupSalatserver() { Serial.begin(9600); Serial.println(); Serial.println("Booting Sketch..."); if (accessPointMode == true) { // ESP32 as AccessPoint WiFi.softAP(ssid, password); WiFi.softAPConfig(local_ip, gateway, subnet); delay(100); Serial.println("Connecting to "); Serial.print(ssid); } else { // ESP32 connects to your wifi ----------------------------------- WiFi.mode(WIFI_STA); // Connect to your wifi WiFi.begin(ssid2, password2); // Wait for WiFi to connect while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.print("waitForConnectResult..."); //delay(3000); } // If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(ssid2); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // IP address assigned to your ESP } //---------------------------------------------------------------- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { int paramsNr = request->params(); //Serial.println(paramsNr); String s = webPageSource; // Read HTML contents if (paramsNr <= 0) { request->send(200, "text/html", s); } else { for (int i = 0; i < paramsNr; i++) { AsyncWebParameter const *p = request->getParam(i); //Serial.print("Param name: "); //Serial.println(p->name()); //Serial.print("Param value: "); //Serial.println(p->value()); //Serial.println("------"); } if (request->hasParam("waterNow")) { buttonsWaterNow(); Serial.println("Webserver: waterNow"); //request->send(200, "text/plain", "waterNow received"); request->send(200, "text/html", s); } else if (request->hasParam("resetAll")) { Serial.println("Webserver: resetAll"); buttonsResetAll(); //request->send(200, "text/plain", "resetAll received"); request->send(200, "text/html", s); } else if (request->hasParam("waterLess")) { buttonsWaterLess(); Serial.println("Webserver: waterLess"); //request->send(200, "text/plain", "waterLess received"); request->send(200, "text/html", s); } else if (request->hasParam("waterMore")) { buttonsWaterMore(); Serial.println("Webserver: waterMore"); request->send(200, "text/html", s); //request->send(200, "text/plain", "waterMore received"); } else if (request->hasParam("intLess")) { buttonsIntLess(); Serial.println("Webserver: intLess"); request->send(200, "text/html", s); // request->send(200, "text/plain", "intLess received"); } else if (request->hasParam("intMore")) { buttonsIntMore(); Serial.println("Webserver: intMore"); request->send(200, "text/html", s); // request->send(200, "text/plain", "intMore received"); } else if (request->hasParam("readData")) { AsyncJsonResponse *response = new AsyncJsonResponse(); response->addHeader("Server", "ESP Async Web Server"); root = response->getRoot(); root["version"] = version; root["uptime"] = uptime; root["trigger"] = trigger; root["duration_interval"] = duration_interval; root["dry_time_left"] = dry_time_left; root["max_water"] = max_water; root["wet_time_left"] = wet_time_left; root["dry_since"] = dry_since; root["min_moisture"] = minMoisture; root["sensor_moisture_value"] = sensor_moisture_value; root["temp0"] = tempSensor[0]; root["message"] = message; response->setLength(); request->send(response); // request->send(200, "text/plain", "data"); //Send value only to client ajax request } else if (request->hasParam("resetArduino")) { resetFunc(); } }; }); server.begin(); } void loopSalatserver() { }