BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Another stupid noob question


#1

Good morning all,

I’m trying to operate a start relay that needs to stay engaged for around 10 seconds.
I’m using NodeMCU board with Blynk.
I’m using V20 to start.(which controls D3) and V21 to stop (which controls D4).
In Blynk I have V20 as push, not switch; so that when I get the timer delay it will only activate the start relay for the 10 second delay.
My problem is if I put in a delay, V20 doesn’t work at all. I read that I should use a blynk.timer command: but I’m not understanding the syntax and where to put it…
Could some one please explain the syntax of the command and tell me where to put it in my sketch?

Thank you in advance.



#define BLYNK_PRINT Serial


#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// #include <LiquidCrystal.h>
// #include <LiquidCrystal_I2C.h>
#include <Wire.h> 
#define N_DIMMERS 3
#define led_pin D0
int dimmer_pin[] = {14, 5, 15};


char auth[] = "**";

char ssid[] = "**";
char pass[] = "**";
char host[] = "**";


// Select your pin with physical button
const int btnPin = D3;
const int Stop = D4;

WidgetLED led3(V3);
WidgetLED led4(V4);

BlynkTimer timer;

BLYNK_CONNECTED() {
  Blynk.syncAll();
}


void LedWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin
    
    led3.on();
  
  } else {
   
    led3.off ();

   
 }  
if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of Stop  
  led4.on();

} else {
  
  led4.off ();

  }
}

void ButtonWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V20, HIGH);
    } else {

   Blynk.virtualWrite(V20, LOW);
  }
  if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of Stop
    Blynk.virtualWrite(V21, HIGH);
  } else {

    Blynk.virtualWrite(V21, LOW);
  }
}

void setup()
{
  
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
  pinMode(btnPin, OUTPUT);
  pinMode(Stop, OUTPUT);
  timer.setInterval(100L, LedWidget);
  timer.setInterval(100L, ButtonWidget);
  Serial.println("Booting");

  WiFi.mode(WIFI_STA);



  Blynk.begin(auth, ssid, pass);
  


  while (WiFi.waitForConnectResult() != WL_CONNECTED) {

    Blynk.begin(auth, ssid, pass);
 
    Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
    Serial.println("Retrying connection...");

  }

  /* switch off led */

  digitalWrite(led_pin, HIGH);



  /* configure dimmers, and OTA server events */

  analogWriteRange(1000);

  analogWrite(led_pin, 990);



  for (int i = 0; i < N_DIMMERS; i++) {

    pinMode(dimmer_pin[i], OUTPUT);

    analogWrite(dimmer_pin[i], 50);

  }



  ArduinoOTA.setHostname(host);

  ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade

    for (int i = 0; i < N_DIMMERS; i++) {

      analogWrite(dimmer_pin[i], 0);

    }

    analogWrite(led_pin, 0);

  });



  ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end

    for (int i = 0; i < 30; i++) {

      analogWrite(led_pin, (i * 100) % 1001);

      delay(50);

    }

  });



  ArduinoOTA.onError([](ota_error_t error) {

    (void)error;

    ESP.restart();

  });



  /* setup the OTA server */

  ArduinoOTA.begin();

  Serial.println("Ready");


}

BLYNK_WRITE(V20) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V21) {
  digitalWrite(Stop, param.asInt());  // Sets Stop HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V22) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V23) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V24) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

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

Turn on a relay only for 5 seconds
#2

Check the post here: Timer widget
It’ll give you a start.


#3

Thank you ericsson; but that is not really what I’m trying to do.


#4

@MyBluOx
you could try lambda function like that where ‘x’ is your button

BLYNK_WRITE(Vx) {
  BTNx = param.asInt();
  if (BTNx == true) {
    Serial.println("BT x on");

    timer.setTimeout(10000L, []() { // 10 seconds
     //do something like run

    });  // END Timer Function

 Blynk.virtualWrite(Vx, LOW);
 BTNx = false;
  //stop running
  }
  else {
    Serial.println("BT x off");
       //do something like stop
  
    BTNx = false;
  }
}

#5

Alexis,
Where would I put the code?

Thanks


#6

after end of setup, but not in the loop

I think it is V21 ?
if it is, add my code


#7

@MyBluOx
why do you use ButtonWidget timer?

it will be better to attach interrupt , no ?


#8

Sorry,

I don’t understand you question.

I am using a button widget. but no time is attached. I’m just needing a delay when button V20 is pressed to operate the relay. it needs to be energized for 10 seconds then go off.


#9

@Alexis_Cabrera,

This is how I added the code:


BLYNK_WRITE(V20) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V21) {
  digitalWrite(Stop, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V22) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V23) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V24) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
BLYNK_WRITE(V20) {
(V20) = param.asInt();
  if (V20 == true) {
    Serial.println("BT x on");

    timer.setTimeout(10000L, []() { // 10 seconds
     //do something like run

    });  // END Timer Function

 Blynk.virtualWrite(V20, LOW);
 V20 = false;
  //stop running
  }
  else {
    Serial.println("BT x off");
       //do something like stop
  
    V20 = false;
  }
}

This is the error I get:

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (3M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkApi.h:18:0,

                 from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/BlynkApiArduino.h:14,

                 from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/BlynkSimpleEsp8266.h:18,

                 from C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:9:

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino: In function 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)':

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:152:10: error: redefinition of 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)'

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:201:1: note: in expansion of macro 'BLYNK_WRITE'

 BLYNK_WRITE(V20) {

 ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:152:10: error: 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)' previously defined here

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:182:1: note: in expansion of macro 'BLYNK_WRITE'

 BLYNK_WRITE(V20) {

 ^

Virtual_Pin_LCD_Test_1:202: error: lvalue required as left operand of assignment

 (V20) = param.asInt();

       ^

Virtual_Pin_LCD_Test_1:212: error: lvalue required as left operand of assignment

  V20 = false;

      ^

Virtual_Pin_LCD_Test_1:219: error: lvalue required as left operand of assignment

     V20 = false;

         ^

exit status 1
lvalue required as left operand of assignment

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

What did I do wrong?


#10

don’t put that to format
use ```


#11

Thank you.


#12

you have redefined blynk button and you have to name V20 button , do not use (V20).

like this :

bool buttonV20 = false;
or bool BTN20=false;


#13

@MyBluOx

replace that :

BLYNK_WRITE(V21) {
  digitalWrite(Stop, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

with something like that

bool BTN20=false;




BLYNK_WRITE(V20) {
BTN20 = param.asInt();
  if (BTN20 == true) {
    Serial.println("BT 20 on");

    timer.setTimeout(10000L, []() { // 10 seconds
     //do something like run

    });  // END Timer Function

 Blynk.virtualWrite(V20, LOW);
BTN20 = false;
  //stop running
  }
  else {
    Serial.println("BT 20 off");
       //do something like stop
  
    BTN20 = false;
  }
}

#14

I tried that and I get this error:


Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (3M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkApi.h:18:0,

                 from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/BlynkApiArduino.h:14,

                 from C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/BlynkSimpleEsp8266.h:18,

                 from C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:9:

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino: In function 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)':

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:152:10: error: redefinition of 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)'

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:191:1: note: in expansion of macro 'BLYNK_WRITE'

 BLYNK_WRITE(V20) {

 ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:152:10: error: 'void BlynkWidgetWrite20(BlynkReq&, const BlynkParam&)' previously defined here

     void BlynkWidgetWrite ## pin (BlynkReq BLYNK_UNUSED &request, const BlynkParam BLYNK_UNUSED &param)

          ^

C:\Users\Jeff\Documents\Arduino\libraries\Blynk\src/Blynk/BlynkHandlers.h:160:31: note: in expansion of macro 'BLYNK_WRITE_2'

 #define BLYNK_WRITE(pin)      BLYNK_WRITE_2(pin)

                               ^

C:\Users\Jeff\Documents\Arduino\Virtual_Pin_LCD_Test_1\Virtual_Pin_LCD_Test_1.ino:182:1: note: in expansion of macro 'BLYNK_WRITE'

 BLYNK_WRITE(V20) {

 ^

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


#15

that is to say: you use twice


#16

I uploader the sketch to test and V20 is not timing out. It stays engaged. I have the V20 button set to push, not switch.

I tried it in switch mode and it didn’t work.

Here is the sketch I uploaded.



#define BLYNK_PRINT Serial


#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// #include <LiquidCrystal.h>
// #include <LiquidCrystal_I2C.h>
#include <Wire.h> 
#define N_DIMMERS 3
#define led_pin D0
int dimmer_pin[] = {14, 5, 15};


char auth[] = "**";

char ssid[] = "**";
char pass[] = "**";
char host[] = "**";


// Select your pin with physical button
const int btnPin = D3;
const int Stop = D4;

WidgetLED led3(V3);
WidgetLED led4(V4);

BlynkTimer timer;

BLYNK_CONNECTED() {
Blynk.syncAll();
}


void LedWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin
    
    led3.on();
  
  } else {
   
    led3.off ();

   
 }  
if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin
  
  led4.on();

} else {
  
  led4.off ();

  }
}

void ButtonWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V20, HIGH);
    } else {

   Blynk.virtualWrite(V20, LOW);
  }
  if  (digitalRead(Stop) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V21, HIGH);
  } else {

    Blynk.virtualWrite(V21, LOW);
  }
}

void setup()
{
  
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
  pinMode(btnPin, OUTPUT);
  pinMode(Stop, OUTPUT);
  timer.setInterval(100L, LedWidget);
  timer.setInterval(100L, ButtonWidget);
 
  Serial.println("Booting");

  WiFi.mode(WIFI_STA);



  Blynk.begin(auth, ssid, pass);
  


  while (WiFi.waitForConnectResult() != WL_CONNECTED) {

    Blynk.begin(auth, ssid, pass);
 
    Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
    Serial.println("Retrying connection...");

  }

  /* switch off led */

  digitalWrite(led_pin, HIGH);



  /* configure dimmers, and OTA server events */

  analogWriteRange(1000);

  analogWrite(led_pin, 990);



  for (int i = 0; i < N_DIMMERS; i++) {

    pinMode(dimmer_pin[i], OUTPUT);

    analogWrite(dimmer_pin[i], 50);

  }



  ArduinoOTA.setHostname(host);

  ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade

    for (int i = 0; i < N_DIMMERS; i++) {

      analogWrite(dimmer_pin[i], 0);

    }

    analogWrite(led_pin, 0);

  });



  ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end

    for (int i = 0; i < 30; i++) {

      analogWrite(led_pin, (i * 100) % 1001);

      delay(50);

    }

  });



  ArduinoOTA.onError([](ota_error_t error) {

    (void)error;

    ESP.restart();

  });



  /* setup the OTA server */

  ArduinoOTA.begin();

  Serial.println("Ready");


}

BLYNK_WRITE(V20) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V21) {
  digitalWrite(Stop, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V22) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V23) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}

BLYNK_WRITE(V24) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}
bool BTN20=false;




BLYNK_WRITE(V25) {
BTN20 = param.asInt();
  if (BTN20 == true) {
    Serial.println("BT 20 on");

    timer.setTimeout(10000L, []() { // 10 seconds
     //do something like run

    });  // END Timer Function

 Blynk.virtualWrite(V20, LOW);
BTN20 = false;
  //stop running
  }
  else {
    Serial.println("BT 20 off");
       //do something like stop
  
    BTN20 = false;
  }
}
void loop()
{
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}  


#17

1/ bool BTN20=false; would be after const int Stop = D4;
2/ you mix V25 and V20, it’ll never work.

I think you have to understand how Blynk works first…


#18

Is btnPin attached to a physical button or switch? Is the Relay Active HIGH or LOW? Are you trying to control the relay with both a physical switch/button and BLYNK?

I think you code needs a bit of work to get it to function as you expect.


#19

To answer your questions btnPin goes straight to the relay. The relay is activated HIGH. I’m only trying to control the relay with Blynk.

Thanks for asking


How to make a button initialize after 10 seconds
#20

try this

BLYNK_WRITE(V20) {
  int value = param.asInt();
  if (value == 1) {
    Serial.println("BT x on");
     digitalWrite(led_pin, HIGH);
    timer.setTimeout(10000L, []() { // 10 seconds
     digitalWrite(led_pin, LOW);
     Blynk.virtualWrite(V20, LOW);

    });  // END Timer Function


  //stop running
  }
 }

Button widget should be linked to Virtual pin 20, and your code should not contain any other BLYNK_WRITE(V20) functions. Only this one. Additionally it should be set to switch.

Aslo, if it is to only be controlled by BLYNK, get rid of the

if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V20, HIGH);
    } else {

   Blynk.virtualWrite(V20, LOW);
  }

in your void ButtonWidget() function, as it serves no purpose, and may cause issues.