Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <time.h>
- #include <ESPAsyncWebServer.h>
- // Define your pins
- #define MOISTURE_SENSOR_PIN 34
- #define RELAY_PIN 13
- // Initial thresholds
- int MOISTURE_MIN = 300;
- int MOISTURE_MAX = 800;
- AsyncWebServer server(80);
- unsigned long lastWateringTime = 0;
- time_t pumpActivationTime = 0;
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 0;
- const int daylightOffset_sec = 3600;
- const char* ssid = "your_SSID";
- const char* password = "your_PASSWORD";
- void setup(){
- Serial.begin(115200);
- pinMode(MOISTURE_SENSOR_PIN, INPUT);
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, HIGH);
- connectToWiFi();
- setupNTP();
- setupServer();
- }
- void loop(){
- int soilMoisture = getSoilMoisture();
- time_t now;
- time(&now);
- if(soilMoisture < 50 || now >= pumpActivationTime) {
- digitalWrite(RELAY_PIN, LOW);
- lastWateringTime = millis();
- pumpActivationTime = 0;
- } else {
- digitalWrite(RELAY_PIN, HIGH);
- }
- }
- void connectToWiFi() {
- Serial.print("Connecting to WiFi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("\nConnected to WiFi");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- }
- void setupNTP() {
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- }
- void setupServer() {
- server.on("/status", HTTP_GET, handleGetStatus);
- server.on("/setMinMoisture", HTTP_GET, handleSetMinMoisture);
- server.on("/setMaxMoisture", HTTP_GET, handleSetMaxMoisture);
- server.on("/setPumpTime", HTTP_GET, handleSetPumpTime);
- server.begin();
- }
- void handleGetStatus(AsyncWebServerRequest *request) {
- String message = "Soil moisture: ";
- message += String(getSoilMoisture());
- message += "%\nLast watering: ";
- message += String((millis() - lastWateringTime)/60000);
- message += " minutes ago";
- request->send(200, "text/plain", message);
- }
- void handleSetMinMoisture(AsyncWebServerRequest *request){
- handleSetParameter(request, MOISTURE_MIN, "value", "New minimum moisture set to ", "Minimum moisture must be less than maximum moisture.");
- }
- void handleSetMaxMoisture(AsyncWebServerRequest *request){
- handleSetParameter(request, MOISTURE_MAX, "value", "New maximum moisture set to ", "Maximum moisture must be greater than minimum moisture.");
- }
- void handleSetPumpTime(AsyncWebServerRequest *request){
- handleSetParameter(request, pumpActivationTime, "timestamp", "Pump set to activate at ", "Invalid timestamp.");
- }
- void handleSetParameter(AsyncWebServerRequest *request, int ¶meter, const char* paramName, String successMessage, String errorMessage) {
- String message;
- if(request->hasParam(paramName)) {
- int newValue = request->getParam(paramName)->value().toInt();
- if(newValue > 0) {
- parameter = newValue;
- message = successMessage;
- message += String(parameter);
- } else {
- message = errorMessage;
- }
- } else {
- message = "No value provided.";
- }
- request->send(200, "text/plain", message);
- }
- int getSoilMoisture() {
- return map(analogRead(MOISTURE_SENSOR_PIN), MOISTURE_MIN, MOISTURE_MAX, 0, 100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement