Leonardo_02Progetto Sistemi
Published © CC BY-NC-SA

Smart Irrigation

Automatic irrigation of 3 plants of Pothos, through the use of Wemos (ESP8266).

IntermediateFull instructions provided656
Smart Irrigation

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
DFRobot Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
water pump
×1
AC/DC Power Supply, 12 V
AC/DC Power Supply, 12 V
×1
Relay (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Fusion 360
Autodesk Fusion 360
Blynk
Blynk

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

draw

these are the drawing used

draw

these are the drawing used

draw

these are the draw used. these draw is about the division of water.

Schematics

draw

this is the outline of our project

Code

code

Arduino
this is the code of the operation of the project
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <Wire.h>
#define PIN_UPTIME V5 //blynk virtual pin variable (V5)
#define VOLUME V7 //blynk virtual pin variable (V7)
#include <NewPingESP8266.h>
#define TRIGGER_PIN  D2  // esp8266 pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D3  // esp8266 pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPingESP8266 setup of pins and maximum distance.
int distance = 0;
const int AirValue = 1000;   //you need to replace this value with Value_1, air humidity value
const int WaterValue = 0 ;  //you need to replace this value with Value_2, water humidity value
const int SensorPin = A0; //esp8266 pin tied to humidity sensor
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
char auth[] = "xx";  //token used for blynk
char ssid[] = "xx"; //wifi name
char pass[] = "xx"; //wifi password
int h = 0;
BLYNK_READ(PIN_UPTIME)
{
  Blynk.virtualWrite(PIN_UPTIME, h); // This command writes esp8266's uptime in seconds to Virtual Pin (5)
}
BLYNK_READ(VOLUME)
{
  Blynk.virtualWrite(PIN_UPTIME, distance); // This command writes esp8266's uptime in seconds to Virtual Pin (7)
}
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
const char* password = "xx";
#define CHAT_ID "331694920"// Initialize Telegram BOT
#define BOTtoken "1700865436:AAGG8dl_pG1SeuTMQfEZP-cYsR80Zlg8R-o"  // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int botRequestDelay = 1000; //Checks for new messages every 1 second.
unsigned long lastTimeBotRan;
const int relayPin = D1;
bool relayPinstate = LOW;
void setup() {
  pinMode(relayPin, OUTPUT);
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  Wire.begin();
  client.setInsecure();
  WiFi.mode(WIFI_STA); // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println(WiFi.localIP()); // Print Local IP Address
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, relayPinstate);
}

void loop()
{
  Blynk.run();
  soilMoistureValue = analogRead(SensorPin);  //put Sensor insert into soil
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); //mapping the value from the sensor
  if (soilmoisturepercent > 100)
  {
    Serial.println("100 %");
    h = 100;
  }
  else if (soilmoisturepercent < 0)
  {
    Serial.println("0 %");
    h = 0;
  }
  else if (soilmoisturepercent >= 0 && soilmoisturepercent <= 100)
  {
    Serial.print(soilmoisturepercent);
    Serial.println("%");
    h = soilmoisturepercent;
  }
  Blynk.virtualWrite(PIN_UPTIME, h); //write h on the virtual pin
  delay(500);                     // Wait 50ms between pings (about 20 pings/sec)

  distance = sonar.ping_cm();

  Serial.print( "   distance:");
  Serial.println(distance);
  distance = (distance * 1075.21) / 1000; //(Raggio^2*pigreco*h)/1000, turn distance into volume
  distance = (38 - distance); //38 is the total volume of the bucket
  Blynk.virtualWrite(VOLUME, distance); //write VOLUME on the virtual pin
  if (h > 50 && distance >= 5) {
    if (millis() > lastTimeBotRan + botRequestDelay)  {
      int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
      while (numNewMessages) {
        Serial.println("got response");
        handleNewMessages(numNewMessages);
        numNewMessages = bot.getUpdates(bot.last_message_received + 1);
      }
      lastTimeBotRan = millis();
    }
  }
  if (h <= 50 && distance >= 5)
  {
    relayPinstate = HIGH;
    digitalWrite(relayPin, relayPinstate);
    delay (5000);
    relayPinstate = LOW;
    digitalWrite(relayPin, relayPinstate);
    if (soilmoisturepercent > 100)
    {
      Serial.println("100 %");
      h = 100;
    }
    else if (soilmoisturepercent < 0)
    {
      Serial.println("0 %");
      h = 0;
    }
    else if (soilmoisturepercent >= 0 && soilmoisturepercent <= 100)
    {
      Serial.print(soilmoisturepercent);
      Serial.println("%");
      h = soilmoisturepercent;
    }
    distance = sonar.ping_cm();

    Serial.print( "   distance:");
    Serial.println(distance);
    distance = (distance * 1075.21) / 1000; //(Raggio^2*pigreco*h)/1000, turn distance into volume
    distance = (38 - distance); //38 is the total volume of the bucket
    Blynk.virtualWrite(VOLUME, distance); //write VOLUME on the virtual pin
    String data = "WATER " + String(distance) + "L, HUMIDITY" + String(h) + "%";
    bot.sendMessage(CHAT_ID, data);
  }
}
void handleNewMessages(int numNewMessages) { // Handle what happens when you receive new messages
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));
  for (int i = 0; i < numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID) {
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    String text = bot.messages[i].text; // Print the received message
    Serial.println(text);
    String from_name = bot.messages[i].from_name;
    if (text == "/start") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Use the following commands to control your outputs.\n\n";
      welcome += "/relay_on to turn GPIO ON \n";
      welcome += "/relay_off to turn GPIO OFF \n";
      bot.sendMessage(chat_id, welcome, "");
    }
    if (text == "/relay_on") {
      bot.sendMessage(chat_id, "RELAY state set to ON", "");
      relayPinstate = HIGH;
      digitalWrite(relayPin, relayPinstate);
    }
    if (text == "/relay_off") {
      bot.sendMessage(chat_id, "RELAY state set to OFF", "");
      relayPinstate = LOW;
      digitalWrite(relayPin, relayPinstate);
    }
  }
}

Credits

Leonardo_02

Leonardo_02

2 projects • 0 followers
Progetto Sistemi

Progetto Sistemi

1 project • 0 followers

Comments