Turn on two relays and after 40 minutes turn off automatically

First of all, I would like to apologize if this question is frequent on the forum, but I searched around here and couldn’t find a solution to my problem. In addition, I am new to blynk and arduino codes.

Second, I am trying to connect two relays and have them switch off automatically after 40 minutes. When I use the code below, without time codes, it works.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "XXXXXX";
char ssid[] = "XXXXXX";
char pass[] = "XXXXXXX";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
#define PUMP 23
#define VALVE 24

ESP8266 wifi(&EspSerial);

BLYNK_WRITE(V0) {
  
   if (param.asInt()==1) {
    //PUMP AND VALVE TURN ON
    TEST_ON();
   }else{
    //PUMP AND VALVE TURN OFF
   TEST_OFF();
  }
}

void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200);
  pinMode(13,OUTPUT);
  delay(10);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  
  pinMode (PUMP, OUTPUT);
  pinMode (VALVE, OUTPUT);
  
    }

    BLYNK_CONNECTED()
 {
    Blynk.syncAll();
    Blynk.syncVirtual(V0);
}

void loop()
{
  Blynk.run();
    if ( Serial3.available() )   {  Serial.write( Serial3.read() );  }
    if ( Serial.available() )       {  Serial3.write( Serial.read() );  }
}

void TEST_ON() {
    digitalWrite(VALVE, HIGH);
    digitalWrite(PUMP, HIGH);
    }

void TEST_OFF() {
    digitalWrite(VALVE, LOW);
    digitalWrite(PUMP, LOW);
    }  

When I add the “timer” codes, the relays turn on themselves. I turn off, they turn on again and do not turn off at the indicated time.

The “Timer” and “Eventor” widgets are not suitable for this project because there is no exact time to activate these relays. Can someone help me?

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "xxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxx";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
#define PUMP 23
#define VALVE 24

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

BLYNK_WRITE(V0) {
  
   if (param.asInt()==1) {
    //PUMP AND VALVE TURN ON
    TEST_ON();
    timer.setTimeout(5000, TEST_OFF);
   }else{
    //PUMP AND VALVE TURN OFF
   TEST_OFF();
  }
}

void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200);
  pinMode(13,OUTPUT);
  delay(10);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  
  pinMode (PUMP, OUTPUT);
  pinMode (VALVE, OUTPUT);
  
  timer.setTimeout(5000, TEST_OFF);
    }

    BLYNK_CONNECTED()
 {
    Blynk.syncAll();
    Blynk.syncVirtual(V0);
}

void loop()
{
  Blynk.run();
    if ( Serial3.available() )   {  Serial.write( Serial3.read() );  }
    if ( Serial.available() )       {  Serial3.write( Serial.read() );  }
    timer.run();
}

void TEST_ON() {
    digitalWrite(VALVE, HIGH);
    digitalWrite(PUMP, HIGH);
    }

void TEST_OFF() {
    digitalWrite(VALVE, LOW);
    digitalWrite(PUMP, LOW);
    }  

You seem to be continuing the trend you set in your last post, where you don’t provide the data that you’re asked for…

  1. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version

That’s clear from your code!

I’ll ask these questions once, and if you don’t won’t answer them I’ll bow-out of this topic…

This seems very high. I assume that you are using a Mega + ESP-01 (although you didn’t provide that data) with the ESP-01 attached to pins 14 & 15.
Question - Is this actually connecting to the Blynk server? If so, what do you see in your serial monitor

Question - Why doesn’t your sketch contain any WiFi credentials or Blynk auth code?

Question (repeated from your previous post in a different topic) - Are your relays active HIGH or LOW?

Question - Are your relays on pre-built boards, or are they relays that you have wired-up yourself?

Question - How are you powering your relays?

Question - What is attached to pin 13? …

Question, what is the purpose of this code in your void loop…

Comments regarding your code…

This line should be removed from your void setup:

as it does the same as this line…

This line should also be removed from your void setup:

Blynk.syncAll does what its name suggests - it synchronises ALL digital and virtual pins. I would not recommend using it, as it is overkill in most situations. It is far better to simply synchronise the virtual pins that you are interested in, which is what you do (again, because it’s already been done in the syncAll command) in the next line…
Blynk.syncVirtual(V0);

Pete.

Thanks, Pete. But I thought it best to create another topic about my problem so as not to mix with the other.

And I’m sorry if sometimes I don’t answer what you ask, but sometimes I don’t understand the question because I’m newbie. So, I need to study and research a little more to answer.

But let’s answer your questions …

(1)
• Hardware model + communication type: Arduino Mega+WiFi R3 ATmega2560+ESP8266 32M Memory USB-TTL CH340G ( Arduino Mega with onboard ESP8266)
• Smartphone OS: Android v. 10
• Blynk server
• Blynk Library version 0.6.1

(2)
About “#define ESP8266_BAUD 115200”, if I use 9600, for example, ESP won’t connect.

(3) Question - Is this actually connecting to the Blynk server? If so, what do you see in your serial monitor:
Yes. The following message appears on the serial monitor confirming the connection:
“[8635] Connected to Wifi
[19741] Ready (ping: 12 ms).”

(4) Question - Why doesn’t your sketch contain any WiFi credentials or Blynk auth code?
I remove it for security when I post it here. I don’t know if anyone can access my project with this data.

(5) Question (repeated from your previous post in a different topic) - Are your relays active HIGH or LOW?
They start at “LOW” and when activated (pressing the button in the Blynk app) they are “HIGH”

(6) Question - Are your relays on pre-built boards, or are they relays that you have wired-up yourself?
I use 8 channel Relay (https://uploaddeimagens.com.br/imagens/48r6u10)

(7) Question - How are you powering your relays?
With external power supply connected to the GND + JD-VCC of the relay module.(https://uploaddeimagens.com.br/imagens/TgYnVnM)

(8)Question - What is attached to pin 13?
Arduino internal LED

(9) Question, what is the purpose of this code in your void loop…
I couldn’t connect my onboard ESP8266 to the wi-fi network and it was very difficult to find information about arduino mega with onboard ESP8266 on the internet. I found this code and tested it and it worked.

About “Comments regarding your code…
(1)
If I remove:
Serial3.begin (115200);
EspSerial.begin (ESP8266_BAUD);

The ESP8266 does not connect.

(2) About the code “timer.setTimeout (5000, TEST_OFF);” I put it in an attempt to see if it worked, but so far I couldn’t put it to work properly and I don’t even know if it can really help me.

(3) About the code “Blynk.syncAll”, you recommended me in my first post here (Problem with the Blynk app when the arduino is restarted (all pins start with high)) and it solved my problem in the use of digital buttons.

When I went to use virtual buttons, the same problem occurred and I had to include the code “Blynk.syncVirtual” and the problem was gone.

That way, everything is ok with the digital and virtual pins when I turn on the blynk app.

So, Pete, I hope I have explained better about my project and my attempts to get it right.

If you can help me or someone else in the community, I really appreciate it. In the meantime, in parallel, I will continue to look for a solution.

Oh dear! Probably the biggest pile of :poop: out there.
If you search this forum then there are a few topics about this board and the RobotDyn versions too.

It’s best to redact the credentials rather than totally delete the lines of code.

Okay, you clearly don’t understand the question.
Disconnect the relay module from your board and apply 5v to the VCC and GND pins. Temporarily connect one end of a wire to the IN1 pin and the other end of the wire to VCC. If the red LED attached to relay 1 lights-up and the relay clicks on then your board is active HIGH (relays are energised by connecting them to a HIGH signal).
Then remove the temporary wire from VCC and apply it to GND. If the LED/relay are activated now then the relay is active LOW (relays are energised when a LOW signal is applied).

You have no code that controls this pin within your sketch, so remove the irrelevant pinMode command while you are debugging this issue.

This code is not appropriate and needs to be removed from your sketch. The reason it’s necessary is probably because you have one of the jumpers/switches on your board set to the wrong setting, so the ESP8266 is not connected to COM3 (did I mention that this board is a pile of :poop:)

I didn’t suggest that you should remove both line of code, I said that both of these lines do the same thing, so you should remove the first one.

Remove it.

Remove the Blynk.syncAll from this sketch, as you only need to synchronise one virtual pin.

Pete.

(1) relays are energised by HIGH signal
(2) Removed code about pin 13
(3) If I remove or try change the sketch in the loop it doesn’t connect with Blynk. I already tried to change and error occurs. The switches on the board are right. I found several videos explaining about the switches, including a standard test and they are OK. I tested the standard test (ok), creating a webserver (ok). Using blynk works too, but I need to use this code in the loop.
(4) The same thing. I understand that “Serial3.begin” seems to be redundant, but if I just remove it and try to change the “void loop”, an error occurs.
(5) I need to find a way for this code (timer.setTimeout) to work or look for another one that helps me.
(6) In my Blynk app I have digital buttons too. If I leave only “Blynk.syncVirtual” there is an error in the digital buttons when I start the Blynk app. I need both codes.

Without knowing what this “standard test” sketch looks like it’s difficult to know which scenario has been tested, but this code should not be in your void loop.

I don’t understand what “removing it” and “changing the void loop” means in practice. It sounds like two separate actions, when one is required.

A timeout timer runs the specified function once it has completed. This is attempting to turn the relays off approximately 5 seconds after your board has booted up. I doubt that this is what you want, and it will confuse your testing process considerably.
Calling a timeout timer (ultimately with a 40 minute duration) at the point in the code when it is required is the correct approach. You already have this, although you’ve abandoned the lambda timer that we previously discussed.

This is an interesting time to share this useful piece of information!
Would you care to elaborate?
Blynk.syncAll synchronises all digital and virtual pins. The Blynk.syncVirtual is redundant - unless of course you’re doing something in the app that you haven’t explained, which is affecting the status of the V14 pin.

As far as I’m concerned, you shouldn’t be mixing digital and virtual pin usage in the same project.

Pete.