/********* Written by Geoff McIntyre View complete project details at: Based on code by Rui Santos https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/ Thank you for checking out my project! *********/ // Import required libraries #include "WiFi.h" #include "ESPAsyncWebServer.h" // Replace with your network credentials const char* ssid = "Network SSID"; const char* password = "Network Password"; #define relay_pin 15 #define soil_moist 36 #define water_level 39 //Variables for Buzzer int freq = 261.63; //Set frequency of the buzzer, this is a C note int channel = 0; int resolution = 8; //Boolean for light status bool light = true; //Set thresholds for sensors (Tweak depending on plant/soil type) float highWater = 1000; float lowWater = 700; float highMoist = 1200; float lowMoist = 500; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); //Returns soil moisture reading String readSoilMoisture() { float m = analogRead(soil_moist); if (isnan(m)) { Serial.println("Failed to read from Soil Moisture sensor!"); return "--"; } else { Serial.println(m); //Sets text depending on values if(m > highMoist){ return String("High"); } else if (m > lowMoist) { return String("Low"); } else { //Beeps the buzzer for 250ms ledcWriteTone(channel, freq); delay(250); ledcWriteTone(channel, 0); Serial.println("Buzzer Activated"); return String("Dry"); } } } //Returns water level reading String readWaterLevel() { float l = analogRead(water_level); if (isnan(l)) { Serial.println("Failed to read from Water Level sensor!"); return "--"; } else { Serial.println(l); //Sets text depending on values if(l > highWater){ return String("High"); } else if (l > lowWater) { return String("Low"); } else { //Beeps the buzzer for 250ms ledcWriteTone(channel, freq); delay(250); ledcWriteTone(channel, 0); Serial.println("Buzzer Activated"); return String("Empty"); } } } //Toggles the light status String toggleLight() { if(light){ light = false; //Activates relay digitalWrite(relay_pin, LOW); return String("OFF"); } else if (!light){ light = true; //Deactivates relay digitalWrite(relay_pin, HIGH); return String("ON"); } } //The HTML code const char index_html[] PROGMEM = R"rawliteral(
Soil Moisture: %SOILMOISTURE%
Water Level: %WATERLEVEL%
)rawliteral"; //Updates variable text with current values String processor(const String& var){ //Serial.println(var); if(var == "SOILMOISTURE"){ return readSoilMoisture(); } else if(var == "WATERLEVEL"){ return readWaterLevel(); } return String(); } //Runs at the start void setup(){ // Serial port for debugging purposes Serial.begin(115200); //Setup the buzzer ledcSetup(channel, freq, resolution); ledcAttachPin(25, channel); //Setup the pinMode for the relay pinMode (relay_pin, OUTPUT); // 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 root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); server.on("/soil-moisture", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", readSoilMoisture().c_str()); }); server.on("/water-level", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", readWaterLevel().c_str()); }); server.on("/toggle-light", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", toggleLight().c_str()); }); // Start server server.begin(); } void loop(){ //Nothing goes here because we are running an Asynchronous Web Server. }