// Blynk template details
#define BLYNK_TEMPLATE_ID "TMPL3S5-pLOS7"
#define BLYNK_TEMPLATE_NAME "Smart soil nutrition monitoring system"
#define BLYNK_AUTH_TOKEN "B0yuziY1dIDtDy8fo4TmdDRgV2QharG5"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// WiFi credentials
char ssid[] = "Redmi note 9 ";      // Replace with your WiFi SSID
char pass[] = "1234567890";  // Replace with your WiFi Password

#define DHTPIN 21 // DHT22 data pin
#define DHTTYPE DHT22

#define BUZZER_PIN 18
#define MOTOR_PIN 19
#define WIND_SENSOR_PIN 34

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address and dimensions

void setup() {
  // Start serial communication
  Serial.begin(115200);

  // Initialize DHT sensor
  dht.begin();

  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Set pin modes
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(MOTOR_PIN, OUTPUT);
  pinMode(WIND_SENSOR_PIN, INPUT);

  // Print a test message on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Soil Health");

  // Connect to WiFi
  WiFi.begin(ssid, pass);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");

  // Connect to Blynk
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  // Test message to ensure setup runs
  Serial.println("Setup complete");
}

void loop() {
  // Run Blynk
  Blynk.run();

  // Read temperature and humidity from DHT22
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reads failed
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read wind speed from potentiometer
  int windSpeedRaw = analogRead(WIND_SENSOR_PIN);
  float windSpeed = (windSpeedRaw / 4095.0) * 100; // Example conversion, adjust as needed

  // Print raw analog value to Serial Monitor
  Serial.print("Raw Wind Sensor Value: ");
  Serial.println(windSpeedRaw);

  // Display data on LCD
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print("C ");

  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print("%");

  lcd.setCursor(8, 1);
  lcd.print("Wind: ");
  lcd.print(windSpeed);  // Display converted wind speed value
  lcd.print("m/s");

  // Print data to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Wind Speed: ");
  Serial.print(windSpeed);  // Print converted wind speed value
  Serial.println(" m/s");

  // Send data to Blynk
  Blynk.virtualWrite(V1, temperature);
  Blynk.virtualWrite(V2, humidity);
  Blynk.virtualWrite(V3, windSpeed);

  // Control buzzer and motor based on sensor data
  if (temperature > 30.0) { // Example condition for high temperature
    digitalWrite(BUZZER_PIN, HIGH);
  } else {
    digitalWrite(BUZZER_PIN, LOW);
  }

  if (humidity < 20.0) { // Example condition for low humidity
    digitalWrite(MOTOR_PIN, HIGH);
  } else {
    digitalWrite(MOTOR_PIN, LOW);
  }

  delay(2000); // Delay between readings
}