Engr Khan
Published © GPL3+

Automatic Plant Watering and Irrigation System

Build your own automatic plant watering system.

IntermediateProtip2 hours1,632
Automatic Plant Watering and Irrigation System

Things used in this project

Story

Read more

Schematics

block_diagram_of_automatic_plant_watering_system_h4w97lICxl.png

Code

Untitled file

Arduino
#include <dht.h>
#define dht_dpin A0
dht DHT;
//———————–
#include <LiquidCrystal.h>
LiquidCrystal lcd (8, 9, 10, 11, 12, 13);
//———————————-
int plantPotMoisture[3] = {A1, A2, A3};
//———————
#include <GSM.h>
#define PINNUMBER “”
GSM gsmAccess; // include a ‘true’ parameter for debug enabled
GSM_SMS sms;
char remoteNumber[] = “0899506304”;
String moistureMessage = “Moisture is Low on sensor: “;
String SMS_Alert = “Sending SMS!”;
String humidityMsg = “Humidity is High. Open All Solenoids”;
String tempMsg = “Temperature is too HIGH!..Open ALl Solenoids “;
String messageBuffer = “”;
char senderNumber[20];
String stringOne = “Opens1”;
String stringTwo = “Opens2”;
String stringThree = “Opens3”;
String stringFour = “OpenAll”;
//—————
#define solenoidData 5
#define solenoidClockster 4
#define solenoidLatch 6
//—————
const int master = 0;
const int slave1 = 1;
const int slave2 = 2;
const int slave3 = 3;
boolean takeReadings = true;
int serialSolenoidOutput = 0;
void setup()
{
pinMode(solenoidData, OUTPUT);
pinMode(solenoidClockster, OUTPUT);
pinMode(solenoidLatch, OUTPUT);
digitalWrite(solenoidLatch, HIGH);
digitalWrite(solenoidLatch, LOW);
shiftOut(solenoidData, solenoidClockster, MSBFIRST, 0);
digitalWrite(solenoidLatch, HIGH);
//————————-
Serial.begin(9600);
lcd.begin (16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Wait Until”);
lcd.setCursor(0, 1);
lcd.print(“GSM Initialized!”);
boolean notConnected = true;
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println(“Not connected”);
delay(1000);
}
}
}
void loop()
{
if (takeReadings)
{
moistureSensor();
TempAndHumidity ();
if (DHT.humidity > 50 || DHT.temperature > 25 && takeReadings )
{
takeReadings = false;
if (DHT.humidity > 50)
{
sendSMS(humidityMsg);
}
else if (DHT.temperature > 25)
{
sendSMS(tempMsg);
}
while (!takeReadings)
recieveSMS();
}
if (plantPotMoisture[0] > 30 || plantPotMoisture[1] > 30 || plantPotMoisture[2] > 30 && takeReadings)
{
takeReadings = false;
if (plantPotMoisture[0] > 30)
{
sendSMS(moistureMessage + “1”);
}
else if (plantPotMoisture[1] > 30)
{
sendSMS(moistureMessage + “2”);
}
else
{
sendSMS(moistureMessage + “3”);
}
while (!takeReadings)
recieveSMS();
}
}
}
void moistureSensor()
{
for (int i = 0 ; i < 3; i++)
{
lcd.clear();
plantPotMoisture[i] = analogRead(i);
plantPotMoisture[i] = map(plantPotMoisture[i], 550, 0, 0, 100);
Serial.print(“Mositure” + i );
lcd.print(“Mositure” + i);
Serial.print(plantPotMoisture[i]);
lcd.print(plantPotMoisture[i]);
Serial.println(“%”);
lcd.print(“%”);
delay(1000);
}
}
void TempAndHumidity ()
{
DHT.read11(dht_dpin);
lcd.setCursor(0, 0);
lcd.print(“Humidity=);
Serial.print(“Current humidity =);
Serial.print(DHT.humidity);
lcd.print(DHT.humidity);
lcd.print(“%”);
Serial.print(“%”);
Serial.print(“temperature =);
Serial.print(DHT.temperature);
Serial.println(“C”);
lcd.setCursor(0, 1);
lcd.print(“temp=);
lcd.print(DHT.temperature);
lcd.print(“C “);
delay(1000);
lcd.clear();
}
void sendSMS(String messageToSend)
{
Serial.print(“Sending a message to mobile number: “);
Serial.println(remoteNumber);
Serial.println(“SENDING”);
lcd.print(SMS_Alert);
Serial.println();
Serial.println(“Message:”);
Serial.println(messageToSend);
sms.beginSMS(remoteNumber);
sms.print(messageToSend);
sms.endSMS();
Serial.println(\nCOMPLETE!\n);
lcd.clear();
lcd.print(“Completed!!!”);
}
void recieveSMS()
{
char c;
if (sms.available())
{
lcd.clear();
lcd.print(“Message received from:”);
delay(800);
lcd.clear();
sms.remoteNumber(senderNumber, 20);
lcd.print(senderNumber);
while (c = sms.read())
{
Serial.println(c);
messageBuffer += c;
}
Serial.println(messageBuffer);
if (messageBuffer == stringOne)
{
toggleSolenoid1();
takeReadings = true;
}
else if (messageBuffer == stringTwo)
{
toggleSolenoid2();
takeReadings = true;
}
else if (messageBuffer == stringThree)
{
toggleSolenoid3();
takeReadings = true;
}
else if (messageBuffer == stringFour)
{
toggleAll();
takeReadings = true;
}
else
{
takeReadings = true;
}
messageBuffer = “”;
Serial.println(\nEND OF MESSAGE”);
// Delete message from modem memory
sms.flush();
Serial.println(“MESSAGE DELETED”);
}
delay(1000);
}
void toggleSolenoid1()
{
solenoidWrite(master, HIGH);
delay(1000);
solenoidWrite(slave1, HIGH);
delay(1000);
solenoidWrite(slave1, LOW);
delay(1000);
solenoidWrite(master, LOW);
delay(1000);
}
void toggleSolenoid2()
{
solenoidWrite(master, HIGH);
delay(1000);
solenoidWrite(slave2, HIGH);
delay(1000);
solenoidWrite(slave2, LOW);
delay(1000);
solenoidWrite(master, LOW);
delay(1000);
}
void toggleSolenoid3()
{
solenoidWrite(master, HIGH);
delay(1000);
solenoidWrite(slave3, HIGH);
delay(1000);
solenoidWrite(slave3, LOW);
delay(1000);
solenoidWrite(master, LOW);
delay(1000);
}
void toggleAll()
{
solenoidWrite(master, HIGH);
delay(1000);
solenoidWrite(slave1, HIGH);
delay(1000);
solenoidWrite(slave2, HIGH);
delay(1000);
solenoidWrite(slave3, HIGH);
delay(1000);
solenoidWrite(slave1, LOW);
delay(1000);
solenoidWrite(slave2, LOW);
delay(1000);
solenoidWrite(slave3, LOW);
delay(1000);
solenoidWrite(master, LOW);
delay(1000);
}
void solenoidWrite(int pin, bool state)
{
if ( pin >= 0 && pin < 8)
{
if (state)
serialSolenoidOutput |= (1 << pin);
else
serialSolenoidOutput &= ~(1 << pin);
}
digitalWrite(solenoidLatch, LOW);
shiftOut(solenoidData, solenoidClockster, MSBFIRST, serialSolenoidOutput);
digitalWrite(solenoidLatch, HIGH);
}

Credits

Engr Khan

Engr Khan

1 project • 1 follower
Electrical & Electronics Engineer

Comments