Advertisement
microrobotics

ESP32 WebServer DIY Irrigation Kit

May 18th, 2023 (edited)
4,632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <time.h>
  3. #include <ESPAsyncWebServer.h>
  4.  
  5. // Define your pins
  6. #define MOISTURE_SENSOR_PIN 34
  7. #define RELAY_PIN 13
  8.  
  9. // Initial thresholds
  10. int MOISTURE_MIN = 300;
  11. int MOISTURE_MAX = 800;
  12.  
  13. AsyncWebServer server(80);
  14.  
  15. unsigned long lastWateringTime = 0;
  16. time_t pumpActivationTime = 0;
  17.  
  18. const char* ntpServer = "pool.ntp.org";
  19. const long  gmtOffset_sec = 0;
  20. const int   daylightOffset_sec = 3600;
  21.  
  22. const char* ssid = "your_SSID";
  23. const char* password = "your_PASSWORD";
  24.  
  25. void setup(){
  26.   Serial.begin(115200);
  27.   pinMode(MOISTURE_SENSOR_PIN, INPUT);
  28.   pinMode(RELAY_PIN, OUTPUT);
  29.   digitalWrite(RELAY_PIN, HIGH);
  30.  
  31.   connectToWiFi();
  32.   setupNTP();
  33.   setupServer();
  34. }
  35.  
  36. void loop(){
  37.   int soilMoisture = getSoilMoisture();
  38.  
  39.   time_t now;
  40.   time(&now);
  41.  
  42.   if(soilMoisture < 50 || now >= pumpActivationTime) {
  43.     digitalWrite(RELAY_PIN, LOW);
  44.     lastWateringTime = millis();
  45.     pumpActivationTime = 0;
  46.   } else {
  47.     digitalWrite(RELAY_PIN, HIGH);
  48.   }
  49. }
  50.  
  51. void connectToWiFi() {
  52.   Serial.print("Connecting to WiFi...");
  53.   WiFi.begin(ssid, password);
  54.   while (WiFi.status() != WL_CONNECTED) {
  55.     delay(1000);
  56.     Serial.print(".");
  57.   }
  58.   Serial.println("\nConnected to WiFi");
  59.   Serial.print("IP Address: ");
  60.   Serial.println(WiFi.localIP());
  61. }
  62.  
  63. void setupNTP() {
  64.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  65. }
  66.  
  67. void setupServer() {
  68.   server.on("/status", HTTP_GET, handleGetStatus);
  69.   server.on("/setMinMoisture", HTTP_GET, handleSetMinMoisture);
  70.   server.on("/setMaxMoisture", HTTP_GET, handleSetMaxMoisture);
  71.   server.on("/setPumpTime", HTTP_GET, handleSetPumpTime);
  72.  
  73.   server.begin();
  74. }
  75.  
  76. void handleGetStatus(AsyncWebServerRequest *request) {
  77.   String message = "Soil moisture: ";
  78.   message += String(getSoilMoisture());
  79.   message += "%\nLast watering: ";
  80.   message += String((millis() - lastWateringTime)/60000);
  81.   message += " minutes ago";
  82.   request->send(200, "text/plain", message);
  83. }
  84.  
  85. void handleSetMinMoisture(AsyncWebServerRequest *request){
  86.   handleSetParameter(request, MOISTURE_MIN, "value", "New minimum moisture set to ", "Minimum moisture must be less than maximum moisture.");
  87. }
  88.  
  89. void handleSetMaxMoisture(AsyncWebServerRequest *request){
  90.   handleSetParameter(request, MOISTURE_MAX, "value", "New maximum moisture set to ", "Maximum moisture must be greater than minimum moisture.");
  91. }
  92.  
  93. void handleSetPumpTime(AsyncWebServerRequest *request){
  94.   handleSetParameter(request, pumpActivationTime, "timestamp", "Pump set to activate at ", "Invalid timestamp.");
  95. }
  96.  
  97. void handleSetParameter(AsyncWebServerRequest *request, int &parameter, const char* paramName, String successMessage, String errorMessage) {
  98.   String message;
  99.   if(request->hasParam(paramName)) {
  100.     int newValue = request->getParam(paramName)->value().toInt();
  101.     if(newValue > 0) {
  102.       parameter = newValue;
  103.       message = successMessage;
  104.       message += String(parameter);
  105.     } else {
  106.       message = errorMessage;
  107.     }
  108.   } else {
  109.     message = "No value provided.";
  110.   }
  111.   request->send(200, "text/plain", message);
  112. }
  113.  
  114. int getSoilMoisture() {
  115.   return map(analogRead(MOISTURE_SENSOR_PIN), MOISTURE_MIN, MOISTURE_MAX, 0, 100);
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement