[SOLVED] Issues creating a auto/manual pump control

Hi, I’m currently working on a project.
So this project has two mode, auto and manual mode.
Auto mode was fine. But when i go to manual mode, after several tries, blynk disconnected. I have tried used similar code to for LED(replace relay), but it was fine. Help Please.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 7                  // What digital pin we're connected to
#include <SoftwareSerial.h>       //Software Serial on Uno
SoftwareSerial EspSerial(2, 3);   // RX, TX
#define relay1 11
#define ledred 10
#define ledGreen 12
#define relay2 4
char auth[] = "";
char ssid[] = "";
char pass[] = "";
ESP8266 wifi(&EspSerial);


#define DHTTYPE DHT11     
DHT dht(DHTPIN, DHTTYPE);
int i=0;
int button2;
SimpleTimer timer;
int selectmode;
long duration;
int distance;
const int trigPin = 9;
const int echoPin = 10;
int lightpin =0;
float Vout;
int Lux;

void senddata()
{
  
  long x = ultrasonic();     
  int y= lightsensor();
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 

  //  Blynk.virtualWrite(V1, h);
  //  Blynk.virtualWrite(V2, t);
//    Blynk.virtualWrite(V3, x);
 //   Blynk.virtualWrite(V4, y);

  if(h>80)
  Blynk.tweet(String("Warning,Current Humidity is ") + h + ",higher than pre-set 80%");

  if(t>40)
  Blynk.tweet(String("Warning,Current Temperature is ")+ t + ",higher than pre-set 40");

    
} 


long ultrasonic (){

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
// Calculating the distance
  distance= duration*0.034/2;
  delay(10);
  return distance;
  
}

int lightsensor(){
  
  int analoglux=analogRead(lightpin);
  Vout = (analoglux* 0.0048828125);  
  Lux=(2500/Vout-500)/10;      
  return Lux;

  
}*/

void Auto(){ 
 
 
  //int w=lightsensor();
  //if(w >50)
  
  if(i==0&&selectmode==0){
  digitalWrite(relay1,HIGH);
  delay(5000);
  digitalWrite(relay1,LOW);};
  
 /* else{
  digitalWrite(relay1,LOW);
  }  
}*/
}

void Manual(){
  
    
    if(button2==1&&selectmode==1){
    digitalWrite(relay1,HIGH);
    }
    
    else{
    digitalWrite(relay1,,LOW);
    }
  
}

/*void waterlevel(){
  
  long w=ultrasonic();
  if (x >= 50) {                           
                      
    digitalWrite(relay2,HIGH);
  } else {
    
    digitalWrite(relay2,LOW);
  }

  
}*/

void setup()
{
    pinMode(led, OUTPUT);
    pinMode(ledGreen, OUTPUT);
    pinMode(ledred, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(lightpin,OUTPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
  // Debug console
  Serial.begin(9600);
  

  // Set ESP8266 baud rate
  EspSerial.begin(9600);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  dht.begin();
  
  //Setup a function to be called every second
//  timer.setInterval(5000, senddata);
  
 // timer.setInterval(100, waterlevel);
  
 
  
}

void loop(){
  
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  
}


BLYNK_WRITE(V6){
 button2 = param.asInt(); 
}

BLYNK_WRITE(V5){
 selectmode = param.asInt();
 if(selectmode ==1){
    // digitalWrite(ledGreen,LOW);
    // digitalWrite(ledred,HIGH);    
     i=1;
     timer.setInterval(1000L, Manual);   
     
 }

 else{
  // digitalWrite(ledred,LOW);
  // digitalWrite(ledGreen,HIGH);
   i=0; //include led over here red + green
   timer.setInterval(15000, Auto);  
 }
}

BLYNK_CONNECTED(){
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);

}

Please format your code as follows: (I have done this for you… please go back in edit to see how it is done, thanks)

And you have a 5 second delay in auto mode… you can NOT have delay() (as it is a blocking code) when using ESP8266 based devices as it tends to interfere with the WiFi and an app like Blynk that needs to constantly keep in communication with the server.

http://docs.blynk.cc/#troubleshooting-delay

http://docs.blynk.cc/#troubleshooting

Hmm, any idea to replace the delay?
Cause i only want the relay to be on for 5 seconds and idle until the next timer starts.

Use another timer of course :wink: Search here for “relay pulse” and “servo sweep” for examples of using timers within timer loops

And also the main timer documentation: http://playground.arduino.cc/Code/SimpleTimer

1 Like

Cool. Thanks gunner. Is it possible that i send too frequent to the server and caused a flood? Manual mode has 1sec interval for every transmission.

Either too much data or too long a delay causes issues. Finding the balance is done by trial and error first, then experience later :wink:

http://docs.blynk.cc/#troubleshooting-flood-error

1 Like
Void Auto(){
digital.Write(relay1,HIGH);
timer.setInterval(10000,OFF);}

void OFF(){
digital.Write(relay2,LOW);

But it didnt work. Same as before, few clicks of button, Blynk disconnected.
And reconnect attempt result such error:

[1607] Failed to disable Echo

I’m starting to doubt if it was the hardware problem.
5v connected to Relay Vcc and Lm1117.
Lm1117 3.3v to Vcc,CH_ of ESP8266.

Pls help. Thanks

Hi. I’m using USB serial connection with arduino to connect blynk.
Auto mode > manual mode was fine. Auto mode suppose to run 5sec and idle 5sec
But when manual mode switch back to auto, therere no relay pulse. The time frame is not restored…
I’m new to blynk and electronics. Please help :slight_smile:

#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include <DHT.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
#define relay1 11
#define lightpin 0
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
SimpleTimer timer;
int i=0;
int button2;
int selectmode;
int timer1;
int timer2;
int timer3;
int Lux;
char auth[] = "14df01c16b1548518503a6bf1d74681c";
WidgetLED led1(8);
WidgetLED led2(9);

void setup()
{
  // Debug console
  DebugSerial.begin(9600);
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  pinMode(lightpin, OUTPUT);
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  dht.begin();
  Blynk.begin(Serial, auth);
  timer.setInterval(3000,senddata);
}

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

void senddata(){
    int analoglux=analogRead(lightpin); 
    int h = dht.readHumidity();
    int t = dht.readTemperature();
    
    Blynk.virtualWrite(V1, h);
    Blynk.virtualWrite(V2, t);
    Blynk.virtualWrite(V3, analoglux);
}

void Auto(){
  
  if( i==0 && selectmode ==0){
  digitalWrite(relay1, HIGH);
  timer2=timer.setTimeout(5000,OFF);}
}

void OFF(){
  digitalWrite(relay1, LOW);
  timer.disable(timer2);
}

void Manual(){


    if(button2==1&&selectmode==1){
    digitalWrite(relay1,HIGH);}

    else{
    digitalWrite(relay1,LOW);}

  }

BLYNK_WRITE(V6){
  button2 = param.asInt();
  }

  BLYNK_WRITE(V5){
  selectmode = param.asInt();
  if(selectmode ==1){
     i=1;
     led1.off();
     led2.on(); 
     timer3=  timer.setInterval(1000, Manual);
  }

  else{
   i=0;
   led1.on();
   led2.off();
   timer1 = timer.setInterval(10000, Auto);
  }
  }

  BLYNK_CONNECTED(){
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);

  }

Please don’t create multiple topics for similar issues on the same project. I have merged this latest question back into your original.

You need to supply much more detail on what you are trying to do, what you are needing help with, what you have already tried, etc. Not everyone on this forum is able or willing to figure out your whole code based on your own terms of “manual and auto”… I mean, manual and auto what, automotive transmission? :wink:

Ops. I’m Sorry. But im desperate…
So, The auto-mode is suppose to send pulse to relay and turn on the water pump for 5 seconds and idle for the other 5 seconds which is fine at first. Next, when user clicked on manual mode. This allows user to control the relay on/off via button2.

Now, the problem comes. When i switched back to auto mode, the time frame is not restored as the same for auto mode. 5s on and 5s off. The relay on/off almost instantly.
I bet there’s error here. But i just don’t get it.

void Auto(){
  
  if( i==0 && selectmode ==0){
  digitalWrite(relay1, HIGH);
  timer2=timer.setTimeout(5000,OFF);}
}

void OFF(){
  digitalWrite(relay1, LOW);
  timer.disable(timer2);
}

I have tried using timer.disable(timerID). However, the result stays the same. I’m sorry but im super desperate for solution.

I don’t understand what you are doing here… you don’t take a timer function and apply it to a variable.

Build your code in stages… Create two totally separate functions, one that works automatically in a non-blocking loop, using timers as you have tried.

Create a second function that relies on manual control, with it’s own timers if necessary.

Then create a third function that chooses one of the two prior ones based on a button/switch state… no timers involved for that.

Properly name your timers and comment your code. Simple names like timer1, timer2… etc… are too easy to get confused. And no one else will even bother trying to figure it out.

And finally, please understand that your cries of desperation does not guarantee quicker or better assistance, perhaps even the opposite, as others avoid what appears to be a potential hand holding session :wink:

Thank you Gunner. Will work on that.

Actually its perfectly valid, timer functions return an integer that represents that timers specific ID. With it the programmer can delete, enable and disable that specific timer :slight_smile:

2 Likes

@Wesley can you be more specific of how you want your modes to operate?

Should your automode continously toggle at 5 seconds interval or just once?

HI Fett!

  1. Once Powered, auto-mode should be default mode. It toggle at 5 seconds interval.
  2. Once button is pushed(HIGH)(V5), Mode will be manual-mode. Controllable by user using button2(V6).
  3. It sends humidity, Temp, luminance every 5seconds to blynk server. (no problem so far).

Problem facing:

  1. Auto-mode was functional when no button was pressed.
  2. Once pressed. Manual-mode engaged. Seems normal and functional too
  3. Press button again to switch back auto-mode. The interval messed up. It did not toggle at 5 seconds, rather toggle instantly.

just a quick idea:

i think that before changing the timeout with timer2=timer.setTimeout(5000,OFF);, first you have to disable it witth timer.disable(timer2);

and it is usually good practice to use L after the values in simple timer.

so, try it like this:

 if( i == 0 && selectmode == 0) {
  digitalWrite(relay1, HIGH);
  timer.disable(timer2);
  timer2 = timer.setTimeout(5000L, OFF);}
1 Like

Hi thanks for helping. But it doesnt work. After few toggle, The Auto-mode runs forever and button don’t work too.

I’ll take a look after I watch my show :slight_smile:
Just an fyi timer intervals can’t be changed, you must delete the current timer and create a new one in order to “change” intervals

Alrighty. I will work on that. Thanks Fett.

Ill have to get back on this at a later point good luck Wesley, got family matters :slight_smile:

1 Like