Relay water pump not pump out water but I got notification already pump

Ok, this board need a 5V signal trigger input , so it can’t work with the ESP8266 !

How do I uncomment it? Means that in the void loop onle have Blynk.run() and timer.run()?

That’s right ! you have to use timer in setup

1 Like

Really? Because before this I tried the water pump command only in 1 file. just water pump. and it works. But when I want to combine with rain sensor, dht11, soil sensor and pir sensor, the relay cannot open. :sweat_smile: do you know how I’m supposed to re-arrange my code?

The /* is traeted as a block comment in the Arduino IDE.
Use // instead for a single like comment.

That’s correct. If you want to continuously monitor the PIR GPIO then you need to poll it with a timer running at maybe a 100ms interval.
Also, the DHT11 doesn’t like being sampled more tanh once every 10 seconds.

Pete.

read this good tutorial from @PeteKnight

1 Like

Pete.

1 Like

:scream: :scream:

1 Like

But if I put only blynk.run() and timer.run() in void loop, it will have other error. :sweat_smile:

I’m sorry for asking, can you teach me on how I am supposed to connect it with OneWire bus, Pete?

No, not if you use timers correctly, as explained in the “keep your void loop clean” document.

You aren’t, it’s not a OneWire relay. Simply connect it to a GPIO and remove the OneWire command for D2.

Pete.

1 Like

First of all, I’m sorry I dont know how to put my new code in the grey field like this ‘’ ‘’. Okay I already clean my void loop. But the water pump will turn on non-stop even I not push button on. same goes to my pir motion sensor. The pir sensor will turn on buzzer even I not touch it. Do you have any idea or solution? This is my code.

#define BLYNK_PRINT Serial   
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

String apiKey = "";                    //API Key from ThingSpeak channel
char auth[]   = "";    //Authentication code sent by Blynk
char ssid[]   = "";                        //WiFi SSID
char pass[]   = "";                            //WiFi Password


//PIR
#define pirPin D1            
int pirValue;                   
int pinValue;
const int buzzerPin = D7;
                   
//SOIL MOISTURE
#define sensorPin D6 

//RAIN 
#define rainPin D5
int enable2 = D8; 
int sensorState = 0;
int rainState = 0;
int lastState = 0;
int lastRainState = 0;

//DHT11 
#define DHTPIN 2    
#define DHTTYPE DHT11     
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

//WATER PUMP RELAY
#define WATER_PUMP D2
boolean state = false;


//INPUT_OUTPUT
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   pinMode(sensorPin, INPUT);
   pinMode(rainPin, INPUT);
   pinMode(pirPin, INPUT);
   pinMode(buzzerPin, OUTPUT);
   pinMode(WATER_PUMP, OUTPUT);
   pinMode(enable2, OUTPUT);
   dht.begin();

   timer.setInterval(1000L, sendSensor);
   timer.setInterval(1000L, sendTemps);
   timer.setInterval(1000L, getPirValue);
   Serial.begin(9600);
   Blynk.begin(auth, ssid, pass);
   sensors.begin();
}


//WATER_PUMP
void loop()
{
   timer.run();
   Blynk.run(); 
  
}
BLYNK_WRITE(V1)
{
 if (state == false) {
 state == true;
 digitalWrite(WATER_PUMP,HIGH);
 Serial.println("You just watered your plant");
 Blynk.notify("You just watered your plant.");
 delay(1000);
 }
 else {
 state = false;
 digitalWrite(WATER_PUMP,LOW);
 }
}

//DHT11
void sendSensor()
{
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   
   Blynk.virtualWrite(V6, t);  //V6 is for Temperature
   Serial.print("Current temperature = ");
   Serial.print(t);
   Serial.print(" ");
   Blynk.virtualWrite(V5, h);  //V5 is for Humidity
   Serial.print("Current humidity = ");
   Serial.print(h);
}


//SOIL
int sensor=0;
void sendTemps()
{
  sensor=analogRead(A0);
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0); 
  Serial.println(temp);
  Serial.print("Soil Moisture is : ");
  Serial.println(sensor);
  if (sensor > 900) 
  {
      digitalWrite (WATER_PUMP, HIGH);
  }
  if (sensor > 900 && sensor < 950)
  {
    digitalWrite(WATER_PUMP, HIGH);
  }
  if (sensor < 700)
  {
    digitalWrite(WATER_PUMP, LOW);
  }
    Blynk.virtualWrite(V1, temp);
    Blynk.virtualWrite(V2,sensor);
    delay(1000);
}




//MOTION
void getPirValue(void)        
{
   pirValue = digitalRead(pirPin);
   Serial.println(pirValue);
    if (pirValue == 0) 
     { 
       Serial.print("Motion detected");
       Serial.print(pirValue); 
       Blynk.notify("Motion detected"); 
       digitalWrite(buzzerPin, HIGH);
       Blynk.virtualWrite(V0, pirValue);  //V0 is for Motion
     }
     else 
     {
      digitalWrite (buzzerPin, LOW);
     }
     delay(100);


//RAIN
rainState = digitalRead(rainPin);
Serial.println(rainState);

  if (rainState == 0 && lastRainState == 0) {
      Serial.println("Its Raining!");
      Blynk.notify("Its Raining!");
      digitalWrite(enable2, HIGH);
      lastRainState = 1;
      delay(100); 
  } else {
      Serial.println("No Rains");
      digitalWrite(enable2, LOW); 
      lastRainState = 0;
      delay(100);
  }
}

Unreadable :face_with_monocle:

1 Like

@kakngah please edit your post and put triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these, if you can’t find the correct character on your keyboard.

Pete.

1 Like

alright… I already fix my post. Thank u pete. My problem is, I already clear my loop, but the water pump relay keeps turning on even if I not push the button. same goes to PIR sensor that detect motion, which it keeps turning on even I didn’t block it. Do you know how to fix this?

It seems that you aren’t interested in listening to the advice that you’re given.

Your code is littered with delays, even though the “keep your void loop clean” document explains that you shouldn’t do this.

You’re mixing GPIO numbers and “D” numbers in your code, even though @Blynk_Coeur advised against it

You’re taking DHT readings every second, despite my advice to the contrary…

And you’re also polling your PIR once every second despite my advice to do this every 100ms…

In addition, your code is a mess, and as @Blynk_Coeur said…

It would be far better to group all of your variable declarations together at the top of the code and add comments to explain what each variable is for.
The variables that declare pin aliases should use GOPI numbers, not D numbers, with a comment saying what the corresponding D number is, and what type of sensor/device is connected to each of these pins.

The comments like…

Are confusing and make it extremely difficult to track-down your void setup and void loop in the mess of code.

Pete.

2 Likes

@PeteKnight @Blynk_Coeur Is it like this? I’m sorry, I try to understand and follow your advice. I’m very new to this. My problem is the water pump still running non-stop. and for PIR motion sensor, is it right I do like that?

#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 16                            //GPIO16 or D0 is for OneWireBus
#define BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer;                                       //Timer  object


String apiKey = "";                    //API Key from ThingSpeak channel
char auth[]   = "";                      //Authentication code sent by Blynk
char ssid[]   = "";                      //WiFi SSID
char pass[]   = "";                     //WiFi Password




#define DHTPIN 2             //GPIO2  or D4 is for DHT11 sensor
#define pirPin 5             //GPIO5  or D1 is for PIR Motion sensor
#define WATER_PUMP 4         //GPIO4  or D2 is for Water Pump relay
#define sensorPin 12         //GPIO12 or D6 is for Soil moisture sensor
#define rainPin 14           //GPIO14 or D5 is for Rain sensor
#define DHTTYPE DHT11        //DHT11  temperature & Humidity sensor
DHT dht(DHTPIN, DHTTYPE);    //DHT11  temperature & Humidity sensor
int pirValue;                //For PIR Motion sensor value    
int pinValue;
const int buzzerPin = 13;    //GPIO13 or D7 is for buzzer connected with PIR Motion sensor
int enable2 = 15;            //GPIO15 or D8 is for LED connected with Rain sensor
int sensorState = 0;         //For soil sensor value
int rainState = 0;           //For rain sensor value
int lastState = 0;           //For rain sensor value
int lastRainState = 0;       //For rain sensor value
boolean state = false;       //For water pump to turn on or off


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   pinMode(sensorPin,  INPUT);
   pinMode(rainPin,    INPUT);
   pinMode(pirPin,     INPUT);
   pinMode(buzzerPin,  OUTPUT);
   pinMode(WATER_PUMP, OUTPUT);
   pinMode(enable2,    OUTPUT);
   dht.begin();

   timer.setInterval(10000L, sendSensor);    //main function for DHT11 is called here 
   timer.setInterval(10000L, sendTemps);     //main function for Soil  is called here 
   timer.setInterval(100L, getPirValue);       //main function for PIR   is called here 
   Serial.begin(9600);
   Blynk.begin(auth, ssid, pass);
   sensors.begin();
}

void loop()              
{
   timer.run();
   Blynk.run(); 
  
}

void sendSensor()                               //main function for DHT11 sensor
{
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   
   Blynk.virtualWrite(V6, t);                   //V6 is for Temperature in Blynk App
   Serial.print("Current temperature = ");      //Value is printed on serial monitor
   Serial.print(t);                         
   Serial.print(" ");
   Blynk.virtualWrite(V5, h);                   //V5 is for Humidity in Blynk App
   Serial.print("Current humidity = ");         //Value is printed on serial monitor
   Serial.print(h);
}


int sensor=0;
void sendTemps()                                //main function for Soil sensor
{ 
  sensor=analogRead(A0);                        //Get the soil sensor value
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0); 
  Serial.println(temp);
  Serial.print("Soil Moisture is : ");
  Serial.println(sensor);
  if (sensor > 900) 
  {
      digitalWrite (WATER_PUMP, HIGH);
  }
  if (sensor > 900 && sensor < 950)
  {
    digitalWrite(WATER_PUMP, HIGH);
  }
  if (sensor < 700)
  {
    digitalWrite(WATER_PUMP, LOW);
  }
    Blynk.virtualWrite(V1, temp);               //Value is sent to the Blynk App
    Blynk.virtualWrite(V2,sensor);              //V2 for Soil sensor in Blynk App. 
}


void getPirValue(void)                          //main function for Motion sensor   
{
   pirValue = digitalRead(pirPin);              //Get the PIR motion sensor value 
   Serial.println(pirValue);
    if (pirValue == 0) 
     { 
       Serial.print("Motion detected");         //Value is printed on serial monitor
       Serial.print(pirValue);                  //Value is printed on serial monitor
       Blynk.notify("Motion detected");         //Send notification to Blynk App
       digitalWrite(buzzerPin, HIGH);           //Buzzer on when detect motion
       Blynk.virtualWrite(V0, pirValue);        //V0 for PIR Motion sensor in Blynk 
     }
     else 
     {
       digitalWrite(buzzerPin, LOW);            //Buzzer off when not detect motion
     }


rainState = digitalRead(rainPin);                //Get the Rain sensor value   
Serial.println(rainState);

  if (rainState == 0 && lastRainState == 0) {    //if detect any rain
      Serial.println("Its Raining!");            //Value printed on serial monitor
      Blynk.notify("Its Raining!");              //Send notification to Blynk App
      digitalWrite(enable2, HIGH);               //LED is on when detect rain
      lastRainState = 1;
      delay(100); 
  } else {
      Serial.println("No Rains");                //if not detect any rain
      digitalWrite(enable2, LOW);                //LED off when not detect any rain
      lastRainState = 0;
  }
}

BLYNK_WRITE(V1)                                  //V1 for Water Pump button in Blynk 
{
 if (state == false) {                           
 state = true;                                  
 digitalWrite(WATER_PUMP,HIGH);                  //Water pump will turn On 
 Serial.println("You just watered your plant");  //Value is printed on serial monitor
 Blynk.notify("You just watered your plant.");   //Send notification to Blynk App
 }
 else {
 state = false;                                
 digitalWrite(WATER_PUMP,LOW);                   //Water pump will turn Off
 }
}

One issue I see…

The first use of the double == is correct, as a comparator

The second use is not correct, it should be a single = to assign a value

Another issue is that all these timers call their respective functions at the same time (the 3rd one every 10th time the others run). These are not multitasking devices :slight_smile:

And then there are all the delay() commands in your code… not good :frowning: I would recommend timeout timers and flags to avoid too frequent use of notifications. But then that is more programming you need to learn and you need to fix what you have. I would suggest avoiding the notifications altogether until you have the rest figured out.

@PeteKnight has already provided a topic on timers and the link was posted above. You NEED to read it.

2 Likes

Got it. I did not realize it. Thank u so much @Gunner :blush:

Meaning that all the delay() part must be removed isn’t?

Does this mean I have to use timer.timeout instead of timer.setInterval? or combine it? I already read it but I quite confused :confused:

Yes , you can’t use delays with Blynk

Your code must be modified like this :

   Serial.begin(9600);
   Blynk.begin(auth, ssid, pass);
   sensors.begin();
   dht.begin();

   timer.setInterval(10000L, sendSensor);    //main function for DHT11 is called here 
   timer.setInterval(10250L, sendTemps);     //main function for Soil  is called here 
   timer.setInterval(500L, getPirValue);     //main function for PIR   is called here 

//*****
 digitalWrite(enable2, HIGH);               //LED is on when detect rain
      lastRainState = 1;
      //delay(100); ----> have to be removed
1 Like