1

I am trying to built an automatic plant watering system with my arduino. I have an LCD display screen that outputs the moisture level, a moisture sensor and a water pump with a relay. I have been trying to get the water pump to read the moisture data and if it reads lower that 60 to turn on the pump for 10 seconds and then turn the pump off until it reads the moisture again to see if it needs more water. However I am unable to get my water pump to turn off. Is it not reading the moisture sensor?

String writeAPIKey = "dCFFuwutXLRoHKQNqbIXrP";  //Put your ThingSpeak Write API key here.
unsigned int interval = 60000;  //Update every Minute 
long int now=0, previous=-50000; //Used to hold time variables.
#define DST_IP "maker.ifttt.com"  //Thingspeak Server

//LCD display
//Load libraries
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

 //Define address for the Serial LCD display.
#define I2C_ADDR  0x27  //If 0x3f doesn't work change this to 0x27
#define BACKLIGHT_PIN  3

//Initialise the Serial LCD.
LiquidCrystal_I2C lcd(I2C_ADDR, 2,1,0,4,5,6,7); //These pin numbers are hard coded in on the serial backpack board.

// Moisture sensor
int moistureSensor = A3; // moisture sensor is plugged into A3 on the arduino
int data; // Used to store data from user
int waitTime = 1000;


// water pump
int pumpPin = 8; // water pump
int waterTime = 10; // 10 seconds watering time



void setup() { // put your setup code here, to run once:
  // Moisture Sensor
    Serial.begin(9600);

// LCD display
lcd.begin (16,2);     //Initalize the LCD.
lcd.setBacklightPin(3,POSITIVE);//Setup the backlight.
lcd.setBacklight(HIGH); //Switch on the backlight.
lcd.clear();
lcd.setCursor(0,0);   //go to first column  and first line (0,0)
lcd.print("Hello - Arnold");     //Print at cursor Location
lcd.setCursor(5,1);   //goto first column and second line 
delay(1000);  

// pump
pinMode(pumpPin, OUTPUT);



 // Wifi 
  Serial.begin(9600);  // Open serial Connection to Computer
  Serial1.begin(115200);  // Open serial Connection to ESP8266 
  Serial.println("IFTTT Maker");
  Serial1.println("AT+RST");  //Issue Reset Command
  Serial.println("AT+RST");
  delay(1000);

  //DEBUG LOOP- display ESP output to serial Monitor.
  while (Serial1.available()) { 
  Serial.println(Serial1.read());
  }
  Serial.println("AT+CWMODE=1"); 
  Serial1.println("AT+CWMODE=1");  //Set single client mode.
  delay(5000);

  Serial.println("AT+CIFSR");
  Serial1.println("AT+CIFSR"); //Display IP Information

  //DEBUG LOOP- display ESP output to serial Monitor.
  while (Serial1.available()) {  
  Serial.println(Serial1.read());
  }
  delay(1000);

  Serial.println("AT+CIPMUX=0");  
  Serial1.println("AT+CIPMUX=0");  //Sets up Single connection mode.
  delay(1000);

  //DEBUG LOOP- display ESP output to serial Monitor.
  while (Serial1.available()) { 
    Serial.write(Serial1.read());
  }
  delay(1000);

}
void loop() { // put your main code here, to run repeatedly:

    // Moisture
    delay(waitTime);
    data = analogRead(moistureSensor);
    data = map(data, 0, 1023, 0, 100);
    Serial.println(data);

    // LCD
    for(int count = 0; count < 15; count++) {
    lcd.clear();
    lcd.setCursor(7,0);
    lcd.print(data);
    delay(500);
      }

    // pumpPin
    if (moistureSensor <= 60) {
    digitalWrite(pumpPin, HIGH);
    delay(waterTime*1000); // on for 10 seconds
    digitalWrite(pumpPin, LOW);
    delay(waitTime);

    } else if (moistureSensor >=65) {
        digitalWrite(pumpPin, LOW);
        delay(waitTime);

    }


// IFTTT
  now=millis(); //Get the current time.
//Check to see if it's time to run.
if(now - previous >= interval){  
  previous = now; 
  Serial.println("Running Update");
  updateThingSpeak();
   } 
}

void updateThingSpeak(){ 

  int moisture = analogRead(moistureSensor); //Read the moisture

   //Builds the connection string for the ESP8266
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += DST_IP;
    cmd += "\",80";
    Serial1.println(cmd);  //Run the command
    Serial.println(cmd);    //Print this to the debug window
    delay(1000);

    //DEBUG LOOP- display ESP output to serial Monitor.
    while (Serial1.available()) {
    Serial.write(Serial1.read());
    } 

   //I NEED TO UPDATE THIS IF STATEMENT TO MAKE SURE CONNECTION WORKED
  //if (client.connect(thingSpeakAddress, 80)){         
    String httpcmd="GET/trigger/arnold_the_plant/with/key/dCFFuwutXLRoHKQNqbIXrP?";
    httpcmd += "value1=";
    httpcmd += moisture;
    httpcmd += " HTTP/1.1\r\n";
    httpcmd += "Host: maker.ifttt.com\n";
    httpcmd += "Connection: close\r\n\r\n";

    Serial.print("AT+CIPSEND=");
    Serial.println(httpcmd.length());

    Serial1.print("AT+CIPSEND=");
    Serial1.println(httpcmd.length());
    delay(1000);

    Serial.print(">");
    Serial1.println(httpcmd);
    Serial.println(httpcmd);
    delay(3000);

    //DEBUG LOOP- display ESP output to serial Monitor.
     while (Serial1.available()) {
      Serial.write(Serial1.read());
      }

    Serial.println("AT+CIPCLOSE");
    Serial1.println("AT+CIPCLOSE"); //Close the Web Connection


   } // end of void loop
  • Add a 1hr timeout after every 10s watering. I haven't read all of your code yet, but from your written description, it sounds like you forgot to add a timeout to allow the moisture level to normalize after each watering. – Robherc KV5ROB Apr 17 '17 at 0:32
  • My absolute lack of ever using Arduinos might make me wrong here, and it is possible with the weird that is their libraries, but assuming your MoistureSensor outputs a voltage and the A3 definition is some sort of pin enumeration, should you not be reading with AnalogRead(moistureSensor)? To my tired eyes it looks like you're just comparing 60 and 65 to the fixed value of "A3". Edit:: In fact, you're comparing the pin, whilst you also read it into the moisture variable. Typo I assume? – Asmyldof Apr 17 '17 at 1:16
  • Probably unrelated to your question, but in httpcmd you need a space after "GET" and a \r after "maker.ifttt.com". – Edgar Bonet Apr 17 '17 at 9:56
1

Invert logical level on the pin that controls water pump.

// pumpPin 
if (moistureSensor <= 60) 
 { digitalWrite(pumpPin, LOW ); 
 delay(waterTime*1000); 
 // on for 10 seconds
 digitalWrite(pumpPin, HIGH);
 delay(waitTime); } 
else if (moistureSensor >=65)
 { digitalWrite(pumpPin, HIGH);
 delay(waitTime); }
| improve this answer | |
  • I'm sorry I am really new to this. What do you mean by this? – Bethany Belbin Apr 17 '17 at 23:08
  • In if condition it says that whenever your moisture sensor detects some desired level means a level when you turn OFF your water pump and then wait for 10 seconds as written and then again turn ON your water pump. – Hasan Apr 18 '17 at 6:00
  • And In else if condition your moisture sensor detects some value more then some another desired level then your motor pump is always ON. – Hasan Apr 18 '17 at 6:02
  • I short, here we set two desired level means 60 and 65.Whenever your moisture sensor detects value which is less than and equal to 60 then your motor pump stop and runs 10 seconds and then motor pump start. And If your moisture sensor detect value which is more then 65 then your motor pump is always ON. – Hasan Apr 18 '17 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.