Advertisement
microrobotics

ESP32 Webserver Automated Plant Watering System

May 23rd, 2023
1,959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here are the main components that you will need:
  3.  
  4. An ESP32 module
  5. A DC water pump (3-6V)
  6. A moisture sensor (e.g., capacitive soil moisture sensor v1.2)
  7. A transistor and a diode (to control the pump)
  8. A resistor (to limit the current flowing into the transistor's base)
  9.  
  10. Just replace "Your-SSID" and "Your-WiFi-Password" with your actual WiFi SSID and password.
  11.  
  12. The code checks the soil moisture level every second and if it's below a certain threshold (in this case, 500, which you may need to adjust based on your soil moisture sensor and plant needs), it turns the pump on, but only if it's not already running. Once the pump is on, it will run for 30 seconds (PUMP_TIME) and then turn off automatically.
  13.  
  14. The web server has three endpoints:
  15.  
  16. http://your-esp32-ip/status which returns the current moisture level.
  17. http://your-esp32-ip/on which turns the pump on if it's not already running.
  18. http://your-esp32-ip/off which turns the pump off if it's currently running.
  19. Each "/on" and "/off" request checks the current pump state to prevent unnecessary operation.
  20. */
  21.  
  22. #include <WiFi.h>
  23. #include <ESPAsyncWebServer.h>
  24.  
  25. #define WIFI_SSID "Your-SSID"
  26. #define WIFI_PASSWORD "Your-WiFi-Password"
  27.  
  28. #define PUMP_PIN 2
  29. #define MOISTURE_SENSOR_PIN 35
  30.  
  31. #define PUMP_TIME 30000 // pump will work for 30 seconds
  32.  
  33. AsyncWebServer server(80);
  34.  
  35. bool pumpRunning = false;
  36. unsigned long pumpStartMillis;
  37.  
  38. void setup() {
  39.   Serial.begin(115200);
  40.  
  41.   pinMode(PUMP_PIN, OUTPUT);
  42.   pinMode(MOISTURE_SENSOR_PIN, INPUT);
  43.  
  44.   connectToWifi();
  45.   setupServer();
  46. }
  47.  
  48. void loop() {
  49.   int sensorValue = analogRead(MOISTURE_SENSOR_PIN);
  50.  
  51.   if (pumpRunning && (millis() - pumpStartMillis >= PUMP_TIME)) {
  52.     digitalWrite(PUMP_PIN, LOW);
  53.     pumpRunning = false;
  54.   } else if (sensorValue < 500 && !pumpRunning) { // change the threshold as needed
  55.     digitalWrite(PUMP_PIN, HIGH);
  56.     pumpRunning = true;
  57.     pumpStartMillis = millis();
  58.   }
  59.  
  60.   delay(1000); // check every 1 second
  61. }
  62.  
  63. void connectToWifi() {
  64.   Serial.print("Connecting to ");
  65.   Serial.println(WIFI_SSID);
  66.  
  67.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  68.  
  69.   while (WiFi.status() != WL_CONNECTED) {
  70.     delay(500);
  71.     Serial.print(".");
  72.   }
  73.  
  74.   Serial.println("");
  75.   Serial.println("WiFi connected.");
  76.   Serial.println("IP address: ");
  77.   Serial.println(WiFi.localIP());
  78. }
  79.  
  80. void setupServer() {
  81.   server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request) {
  82.     int sensorValue = analogRead(MOISTURE_SENSOR_PIN);
  83.     String sensorStr = String(sensorValue);
  84.     request->send(200, "text/plain", sensorStr);
  85.   });
  86.  
  87.   server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request) {
  88.     if (!pumpRunning) {
  89.       digitalWrite(PUMP_PIN, HIGH);
  90.       pumpRunning = true;
  91.       pumpStartMillis = millis();
  92.       request->send(200, "text/plain", "Pump is On");
  93.     } else {
  94.       request->send(200, "text/plain", "Pump is already On");
  95.     }
  96.   });
  97.  
  98.   server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request) {
  99.     if (pumpRunning) {
  100.       digitalWrite(PUMP_PIN, LOW);
  101.       pumpRunning = false;
  102.       request->send(200, "text/plain", "Pump is Off");
  103.     } else {
  104.       request->send(200, "text/plain", "Pump is already Off");
  105.     }
  106.   });
  107.  
  108.   server.begin();
  109. }
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement