#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "RTClib.h"

// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";

// RTC settings (you may need to adjust these according to your RTC module)
RTC_DS3231 rtc;

// Pin definitions for the RTC module (SDA, SCL), buttons, water pumps, and pH sensor
#define SDA_PIN 21
#define SCL_PIN 22
#define BUTTON_PIN_1 14
#define BUTTON_PIN_2 15
#define WATER_PUMP_PH2_PIN 26 // Example pin for water pump pH 2 control
#define WATER_PUMP_PH4_PIN 27 // Example pin for water pump pH 4 control
#define WATER_PUMP_PH6_PIN 33 // Example pin for water pump pH 6 control
#define MAIN_WATER_PUMP_PIN 32 // Example pin for main water pump control
#define PH_SENSOR_PIN A0 // Example pin for pH sensor (analog input)

// Define fertilizer schedules (day number, pH value, pump duration)
const int schedule1[][3] = {
  {1, 2, 5},   // Day 1: pH 2, pump duration 5 seconds
  {7, 2, 5},   // Day 7: pH 2, pump duration 5 seconds
  {14, 4, 5},  // Day 14: pH 4, pump duration 5 seconds
  {21, 4, 5},  // Day 21: pH 4, pump duration 5 seconds
  {28, 4, 5},  // Day 28: pH 4, pump duration 5 seconds
  {35, 6, 5}   // Day 35: pH 6, pump duration 5 seconds
};

const int schedule2[][3] = {
  {1, 2, 5},   // Day 1: pH 2, pump duration 5 seconds
  {7, 2, 5},   // Day 7: pH 2, pump duration 5 seconds
  {14, 4, 5},  // Day 14: pH 4, pump duration 5 seconds
  {21, 4, 5},  // Day 21: pH 4, pump duration 5 seconds
  {28, 4, 5},  // Day 28: pH 4, pump duration 5 seconds
  {35, 6, 5}   // Day 35: pH 6, pump duration 5 seconds
};

// Sleep duration in seconds (e.g., 24 hours = 86400 seconds)
#define SLEEP_DURATION 86400

void setup() {
  Serial.begin(115200);

  // Initialize the RTC module
  Wire.begin(SDA_PIN, SCL_PIN);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC is NOT running!");
    // Uncomment the following line to set the RTC to the date & time the sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Set button pins as inputs
  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);

  // Set water pump pins as outputs
  pinMode(WATER_PUMP_PH2_PIN, OUTPUT);
  pinMode(WATER_PUMP_PH4_PIN, OUTPUT);
  pinMode(WATER_PUMP_PH6_PIN, OUTPUT);
  pinMode(MAIN_WATER_PUMP_PIN, OUTPUT);

  // Connect to Wi-Fi
  connectToWiFi();

  // Handle button press and execute corresponding action
  if (digitalRead(BUTTON_PIN_1) == LOW) {
    executeFertilizerSchedule(schedule1);
  } else if (digitalRead(BUTTON_PIN_2) == LOW) {
    executeFertilizerSchedule(schedule2);
  } else {
    // If neither button is pressed, print the current date & time and sleep
    printDateTime();
    enterDeepSleep();
  }
}

void loop() {
  // This function will not be called as the microcontroller will sleep instead
}

void connectToWiFi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
}

void printDateTime() {
  DateTime now = rtc.now();
  Serial.print("Current date & time: ");
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}

void executeFertilizerSchedule(const int schedule[][3]) {
  DateTime now = rtc.now();
  int day = now.day();
  bool scheduleMatched = false;
  
  for (int i = 0; i < 6; i++) {
    if (day == schedule[i][0]) {
      scheduleMatched = true;
      // Execute fertilizer action
      Serial.print("Day ");
      Serial.print(day);
      Serial.print(": pH ");
      Serial.print(schedule[i][1]);
      Serial.print(", Pump duration ");
      Serial.print(schedule[i][2]);
      Serial.println(" seconds");
      // Activate water pump based on pH value
      activateWaterPump(schedule[i][1]);
      // Record pH sensor data
      float pHValue = readPHSensor();
      recordPHData(day, pHValue);
      // Perform action here (e.g., record pH)
      // Replace this with your actual fertilizer system control code
      // For demonstration purposes, we'll just delay for the pump duration
      delay(schedule[i][2] * 1000); // Convert seconds to milliseconds
      // Deactivate water pump
      deactivateWaterPump();
      // After completing the watering operation, activate main water pump
      activateMainWaterPump();
      delay(5000); // Example: Keep main water pump on for 5 seconds
      deactivateMainWaterPump(); // Deactivate main water pump after 5 seconds
      break;
    }
  }
  
  if (!scheduleMatched) {
    Serial.println("No schedule matched for today.");
  }

  // After completing the watering operation, deactivate main water pump
  deactivateMainWaterPump();
  enterDeepSleep();
}

float readPHSensor() {
  // Placeholder function to read pH sensor data
  // Implement your actual pH sensor reading code here
  // For demonstration purposes, let's return a dummy value
  return 7.0; // Example: pH value of 7.0
}

void recordPHData(int day, float pHValue) {
  // Placeholder function to record pH sensor data
  // Implement your actual data recording code here
  Serial.print("Recorded pH value for day ");
  Serial.print(day);
  Serial.print(": ");
  Serial.println(pHValue);
}


void activateWaterPump(int pH) {
  // Activate water pump based on pH value
  switch (pH) {
    case 2:
      digitalWrite(WATER_PUMP_PH2_PIN, HIGH);
      break;
    case 4:
      digitalWrite(WATER_PUMP_PH4_PIN, HIGH);
      break;
    case 6:
      digitalWrite(WATER_PUMP_PH6_PIN, HIGH);
      break;
    default:
      break;
  }
}

void deactivateWaterPump() {
  // Deactivate all water pumps
  digitalWrite(WATER_PUMP_PH2_PIN, LOW);
  digitalWrite(WATER_PUMP_PH4_PIN, LOW);
  digitalWrite(WATER_PUMP_PH6_PIN, LOW);
}

void activateMainWaterPump() {
  // Activate main water pump
  digitalWrite(MAIN_WATER_PUMP_PIN, HIGH);
}

void deactivateMainWaterPump() {
  // Deactivate main water pump
  digitalWrite(MAIN_WATER_PUMP_PIN, LOW);
}

void enterDeepSleep() {
  Serial.println("Entering deep sleep mode...");
  esp_deep_sleep(SLEEP_DURATION * 1000000); // Convert seconds to microseconds
}
Loading
esp32-devkit-c-v4
GND5VSDASCLSQWRTCDS1307+