#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ThingSpeak.h>
#include <BlynkSimpleEsp32.h>

#define WIFI_SSID "Mark"
#define WIFI_PASSWORD "pathsword"

#define ONE_WIRE_BUS_1 22   // Digital pin connected to the first DS18B20 sensor
#define ONE_WIRE_BUS_2 24   // Digital pin connected to the second DS18B20 sensor
OneWire oneWire1(ONE_WIRE_BUS_1);
OneWire oneWire2(ONE_WIRE_BUS_2);
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);

#define SOILPIN_1 34        // Analog pin connected to the first soil moisture sensor
#define SOILPIN_2 35        // Analog pin connected to the second soil moisture sensor
#define SOIL_MOISTURE_THRESHOLD 500
int soil_moisture1 = 0;
int soil_moisture2 = 0;


#define TRIGPIN 17        // Digital pin connected to the HC-SR04 trigger
#define ECHOPIN 16        // Digital pin connected to the HC-SR04 echo

#define THINGSPEAK_CHANNEL_ID 2087905         // Replace with your ThingSpeak channel ID
#define THINGSPEAK_API_KEY "SX12PTS0ZFYSU3Z8"   // Replace with your ThingSpeak API key

#define PUMP_PIN 27        // Digital pin connected to the relay module for the water pump
#define VALVE 25           // Digital pin connected to the relay module for the valve

char auth[] = "ZMQfWgeMgMauw1ElRKS4bqDw4tvxx1fv";      // Replace with your Blynk auth token

WiFiClient client;

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

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Blynk.begin(auth, WIFI_SSID, WIFI_PASSWORD);  // Initialize Blynk client
  ThingSpeak.begin(client);                    // Initialize ThingSpeak client
  sensors1.begin();                            // Initialize first DS18B20 sensor
  sensors2.begin();                            // Initialize second DS18B20 sensor
  pinMode(SOILPIN_1, INPUT);                   // Initialize first soil moisture sensor pin
  pinMode(SOILPIN_2, INPUT);                   // Initialize second soil moisture sensor pin
  pinMode(TRIGPIN, OUTPUT);                    // Initialize HC-SR04 trigger pin
  pinMode(ECHOPIN, INPUT);                     // Initialize HC-SR04 echo pin
  pinMode(PUMP_PIN, OUTPUT);                   // Initialize water pump relay pin
  pinMode(VALVE, OUTPUT);                      // Initialize valve relay pin
}

void loop() {
  sensors1.requestTemperatures();     // Read temperature from first DS18B20 sensor
  float temperature1 = sensors1.getTempCByIndex(0);
  sensors2.requestTemperatures();     // Read temperature from second DS18B20 sensor
  float temperature2 = sensors2.getTempCByIndex(0);
  Serial.print("Temperature 1: ");
  Serial.print(temperature1);
  Serial.print(" °C");
  Serial.print(" - Temperature 2: ");
  Serial.print(temperature2);
  Serial.println(" °C");
  int soil_moisture1 = analogRead(SOILPIN_1);
  int soil_moisture2 = analogRead(SOILPIN_2);
  float distance_cm = getDistance();    // Read distance from HC-SR04 sensor

  if (WiFi.status() == WL_CONNECTED) {
    // Update ThingSpeak channel with sensor data
    ThingSpeak.setField(1, temperature1);
    ThingSpeak.setField(2, temperature2);
    ThingSpeak.setField(3, soil_moisture1);
    ThingSpeak.setField(4, soil_moisture2);
    ThingSpeak.setField(5, distance_cm);

    int water_level = distance_cm;  // Calculate water level based on distance
    ThingSpeak.setField(6, water_level);
    ThingSpeak.writeFields(THINGSPEAK_CHANNEL_ID, THINGSPEAK_API_KEY);
    Serial.println("Data sent to ThingSpeak!");

    // Display water level on Blynk console
    Blynk.virtualWrite(V1, water_level);
    Serial.println("Water Level sent to Blynk!");

    // Turn on water pump if water level is less than or equal 40cm
    if (water_level =< 40) 
    {
      digitalWrite(PUMP_PIN, HIGH);
      Serial.println("Water pump turned on!");
      delay(100000);
      digitalWrite(PUMP_PIN, LOW);
      delay(50000);
      digitalWrite(PUMP_PIN, HIGH); 
    } 
    else 
    {
      digitalWrite(PUMP_PIN, LOW);
    }
  } 
  else 
  {
    Serial.println("WiFi not connected");
  }

  delay(60000);  // Wait for 1 minute before sending data again
}

if (water_level < 40) // whawn water level is low
 { 
    if (soil_moisture1 >= SOIL_MOISTURE_THRESHOLD || soil_moisture2 >= SOIL_MOISTURE_THRESHOLD) // if soil moisture is HIGH
    { 
      digitalWrite(VALVE, LOW); // close water tank
      Serial.println("Valve off");
    } 
    else if(water_level >=50)
    {
      digitalWrite(VALVE, HIGH); // open water tank
      Serial.println("Valve on");
    }
 } 
  else 
  {
    digitalWrite(VALVE, LOW); // close water tank
    Serial.println("Valve off");
  }
  
float getDistance() {
  // Send a pulse to trigger the HC-SR04 sensor
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);

  // Measure the duration of the echo signal
  long duration = pulseIn(ECHOPIN, HIGH);

  // Calculate the water level based on the speed of sound
  float water_level = duration * 0.034 / 2;

  return water_level;
}
$abcdeabcde151015202530354045505560fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module