Does any know how to override control automatic relay?

My project is an automated plant watering system. when it is below 20% it will turn on the pump and when it is above 80% it will turn off the pump. Now, I want the automated system to turn on and off manually using blynk. The system will turn on when I turn it on and off when I turn it off.

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include <ESP8266WiFi.h>
#include<DHT.h>

#include "BlynkEdgent.h"
    
    #define DHTTYPE DHT22
    #define DHTPIN 3 //RX
    #define S0 D3                            
    #define S1 D4                             
    #define S2 D5
    #define S3 D6 
    #define SIG A0                            

    #define pump1 13//D7
    #define pump2 15//D8
    

DHT dht(DHTPIN, DHTTYPE);    
BlynkTimer timer;

               
    int sensor0;                           
    int sensor1;  

  BLYNK_WRITE(V8){
       if(param.asInt() == 1) {
              
              digitalWrite(pump1,HIGH);
              Blynk.virtualWrite(V1,1);
               
                } else {
             
            digitalWrite(pump1, LOW);
            Blynk.virtualWrite(V1,0);
             
          }
  }                       

void sensors(){

     float humid = dht.readHumidity();
     float temp = dht.readTemperature(); 
     Blynk.virtualWrite(V6,humid);
     Blynk.virtualWrite(V7,temp);
    
          // 00
              digitalWrite(S0,LOW); 
              digitalWrite(S1,LOW); 
              sensor0 = analogRead(SIG);
              sensor0 = map(sensor0, 1024, 578 ,0 ,100);
                     if (sensor0 < 20)
                  {
                    digitalWrite(pump1, LOW); //on
                  }
                  else if(sensor0 >85){
                    digitalWrite(pump1,HIGH); //off
                  }
                   Blynk.virtualWrite(V1,sensor0);   
        
         
    

  
    // 10
    digitalWrite(S0,HIGH);
    digitalWrite(S1,LOW);
    sensor1 = analogRead(SIG);
   sensor1 = map(sensor1,1024, 610, 0, 100);
   
    if (sensor1 < 20)
  {
    digitalWrite(pump2, LOW); //on
  }
  else if(sensor1 >85){
    digitalWrite(pump2,HIGH); //off
  }
    
    Blynk.virtualWrite(V2,sensor1);
    
    /*Serial.print("sensor0: ");
    Serial.println(sensor0);
    Serial.print("sensor1: ");
    Serial.println(sensor1);*/
}
  
void setup() {                                

    
    Serial.begin(115200);
    BlynkEdgent.begin();
    pinMode(pump1, OUTPUT);
    pinMode(pump2, OUTPUT);
    pinMode(S0,OUTPUT);                              
    pinMode(S1,OUTPUT); 
    pinMode(S2,OUTPUT);
    pinMode(S3,OUTPUT);          
    pinMode(SIG, INPUT);                   
    
    dht.begin();
    
    
    timer.setInterval(1000L,sensors);
}

void loop() { 
    BlynkEdgent.run();
    timer.run();

}

Set a flag inside your sensor function.
And create one more BLYNK_WRITE(V10)

Inside this control the flag to be 1 or 0.

So accordingly your sensor() function will be executed or stopped.

1 Like

The neatest way is described in the “Overriding a Timer (auto/manual mode)” of this tutorial:

The relevant section says this:

Overriding a Timer (auto/manual mode)

It is possible to enable, disable and delete timers, and I’ll cover that in the next section, but in most situations it’s not necessary to do this.

Consider a situation where we are using one timer to take a reading from a temperature sensor every 5 seconds, then another timer which runs every 30 seconds and checks whether the temperature is below the target temperature and turns on a heater if it is, and off if it isn’t.

In addition, there is a manual override, which can be used to turn the heater on regardless of the temperature reading.

One approach to tackling this is to disable the 30 second timer when we are in manual mode, but the other is to leave the timer running all the time - but only execute the code in the temperature comparison function if we are in automatic mode.

That 30 second timed function to turn the heater on/off might look like this:

void control_heater()
{
  If(auto_mode == true)
  {
    // compare temperature and switch relay accordingly
  }
}

This code structure allows us to keep calling the control_heater() function with a timer, but only process the code within the function if our auto_mode variable is set to true. This variable would be switched between true and false elsewhere within our sketch.

Pete.

1 Like

There’s no need to add this line because it’s already included in the ConfigMode.h file, so you can delete it.

I don’t recommend using the RX pin as an input or output. I’d suggest that you avoid it completely.

You didn’t specify your board type
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
so custom configuration will be in use, pin 0 is used as BOARD_BUTTON_PIN. Check the settings.h file.

also, the sampling rate of the DHT22 is 0.5Hz (one reading every two seconds), so you might see "failed to read from DHT sensor” or the DHT readings return “Nan” when you read it every 1 second.

I’d suggest using esp32 board instead of nodemcu with multiplexer, the esp32 has more GPIO pins.

2 Likes