Untitled

 avatar
unknown
plain_text
a month ago
4.0 kB
8
Indexable
#define BLYNK_TEMPLATE_ID "TMPL6NSAOemmn"
#define BLYNK_TEMPLATE_NAME "Smart Irrigation System"
#define BLYNK_AUTH_TOKEN "cYB1cgqDaRpFR55lGX1pepCNHo9BWKKz"

// Include required libraries
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <LiquidCrystal.h>  // Include the LCD library

// Your WiFi credentials
char ssid[] = "Purna Protno";
char pass[] = ".197788.";

// Blynk auth token
char auth[] = BLYNK_AUTH_TOKEN;

// Define the relay and sensor pins
const int relayPins[4] = {D1, D2, D3, D4};  // Relay control pins (GPIOs)
const int moistureSensorPins[4] = {A0};  // Soil moisture sensor pins (Analog)

// LCD pins configuration for 4-bit mode
const int lcdRS = D5;  // GPIO4
const int lcdE  = D6;  // GPIO14
const int lcdD4 = D7;  // GPIO12
const int lcdD5 = D8;  // GPIO13
const int lcdD6 = D9;  // GPIO15
const int lcdD7 = D10;  // GPIO0 (Not used in 4-bit mode, can be left unconnected)

// Initialize the LCD
LiquidCrystal lcd(lcdRS, lcdE, lcdD4, lcdD5, lcdD6, lcdD7);

// Variables for mode
bool isManualMode = false;

// Moisture thresholds for each plant (these are example values and should be calibrated for your sensors)
int moistureThresholds[4] = {300, 300, 400, 250}; // Example thresholds for Tomato, Capsicum, Chili, Cucumber

// Blynk button control for manual mode
BLYNK_WRITE(V1) {
  int buttonState = param.asInt();  // Get button state from app
  if (isManualMode) {
    for (int i = 0; i < 4; i++) {
      if (buttonState == 0) {
        digitalWrite(relayPins[i], HIGH);  // Turn on pump (relay active LOW)
        Serial.print("Pump "); Serial.print(i + 1); Serial.println(": ON");
      } else {
        digitalWrite(relayPins[i], LOW);  // Turn off pump
        Serial.print("Pump "); Serial.print(i + 1); Serial.println(": OFF");
      }
    }
  }
}

// Blynk button control for mode selection
BLYNK_WRITE(V2) {
  isManualMode = param.asInt();  // Get mode state from app
  if (isManualMode) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Mode: MANUAL");
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Mode: AUTO");
  }
}

void setup() {
  // Debug console
  Serial.begin(115200);

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

  // Initialize relay pins
  for (int i = 0; i < 4; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);  // Start with pumps off
  }

  // Initialize the LCD
  lcd.begin(16, 2);

  // Display welcome message
  lcd.setCursor(0, 0);
  lcd.print("IOT Based Smart");
  lcd.setCursor(0, 1);
  lcd.print("Irrigation System");
  delay(2000);  // Display the welcome message for 2 seconds
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Project by");
  lcd.setCursor(0, 1);
  lcd.print("Mushfiqur Rahman Zim");
  delay(2000);  // Display the project owner message for 2 seconds
  lcd.clear();

  // Initial display settings
  lcd.setCursor(0, 0);
  lcd.print("Mode: MANUAL");
  lcd.setCursor(0, 1);
  lcd.print("All Pumps OFF");
}

void loop() {
  Blynk.run();
 
  if (!isManualMode) {
    // Automatic mode
    for (int i = 0; i < 4; i++) {
      int moistureLevel = analogRead(moistureSensorPins[i]);
      Serial.print("Soil Moisture Level (Sensor ");
      Serial.print(i + 1);
      Serial.print("): ");
      Serial.println(moistureLevel);

      // Send the moisture level to the Blynk app for each sensor
      Blynk.virtualWrite(V3 + i, moistureLevel);  // Update the level meter widget with moisture data for each plant

      if (moistureLevel < moistureThresholds[i]) {
        // Soil is dry, turn on the pump
        digitalWrite(relayPins[i], HIGH);  // Turn on pump
        Serial.print("Pump "); Serial.print(i + 1); Serial.println(": ON");
      } else {
        // Soil is wet, turn off the pump
        digitalWrite(relayPins[i], LOW);  // Turn off pump
        Serial.print("Pump "); Serial.print(i + 1); Serial.println(": OFF");
      }
    }

    delay(5000);  // Wait 5 seconds before checking again
  }
}
Leave a Comment