/********* Code based on: Rui Santos Complete project details at https://randomnerdtutorials.com Modified by Marie Christine Klanten Details on https://wiki.eolab.de/doku.php?id=amc2020:group_f:start&do= *********/ // Import required libraries #include "WiFi.h" #include "ESPAsyncWebServer.h" #include #include // Replace with your network credentials const char* ssid = "your SSID"; const char* password = "your password"; #define DHTPIN 27 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11 // type of sensor used DHT dht(DHTPIN, DHTTYPE); //define input pins const int waterLevelAnalogInPin = 33; const int lightAnalogInPin = 25; const int moistureAnalogInPin = 35; // Create asynchronous webserver object on port 80 AsyncWebServer server(80); //Read Data //Readings take a while, it is a slow sensor String readDHTTemperature() { // Temperature given by default in °C float t = dht.readTemperature(); // Check if any readings failed and exit early to try again if (isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return "--"; } else { Serial.println(t); return String(t); } } String readDHTHumidity() { float h = dht.readHumidity(); if (isnan(h)) { Serial.println("Failed to read from DHT sensor!"); return "--"; } else { Serial.println(h); return String(h); } } String readSoilMoisture() { float s = analogRead(moistureAnalogInPin ); float Moisture = map(s, 1680, 3519, 100, 0); //map values to soil moisture % Serial.println(Moisture); return String(Moisture); } String readWaterLevel() { float w = analogRead(waterLevelAnalogInPin); if (w<=2700){ //if... else structure used to calibrate readings to avoid deviations due to the irregular slope Serial.println(0); return String(0); } else if (w>2700 && w<=2870){ Serial.println(5); return String(5); } else if (w>2870 && w<=2930){ Serial.println(10); return String(10); } else if (w>2930 && w<=3020){ Serial.println(15); return String(15); } else if (w>3020 && w<=3100){ Serial.println(20); return String(20); } else if (w>3100 && w<=3210){ Serial.println(25); return String(25); } else if (w>3210 && w<=3350){ Serial.println(30); return String(30); } else if (w>3350 && w<=3420){ Serial.println(35); return String(35); } else if (w>3420){ Serial.println(40); return String(40); } } String readLuminosity() { float l = analogRead(lightAnalogInPin); float Lum = map(l, 501, 4095, 0, 30000); //calibrate to get lux (30.000 lux for bright sunlight, 0 for darkness) Serial.println(Lum); return String(Lum); } //Building a web page const char index_html[] PROGMEM = R"rawliteral(

ESP32 - meteorological measurement server

Luminosity %LUMINOSITY% lux

Temperature %TEMPERATURE% °C

Precipitation %WATERLEVEL% mm

Humidity %HUMIDITY% %

Soilmoisture %SOILMOISTURE% %

//automatical updates )rawliteral" ; //assign placeholders String processor(const String& var) { if (var == "TEMPERATURE") { return readDHTTemperature(); } else if (var == "HUMIDITY") { return readDHTHumidity(); } else if (var == "SOILMOISTURE") { return readSoilMoisture(); } else if (var == "WATERLEVEL") { return readWaterLevel(); } else if (var == "LUMINOSITY") { return readLuminosity() ; } return String(); } void setup() { // Serial port for debugging purposes Serial.begin(115200); dht.begin(); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP32 Local IP Address Serial.println(WiFi.localIP()); // Route for web page server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { //store as index_html request->send_P(200, "text/html", index_html, processor); //replace placeholders }); // Start server server.begin(); } // asynchronous webserver- nothing in loop void loop() { }