Button Widget + simple timer

Hello Guys,

I’ve already searched for similar topics, but couldn’t find what I was looking for.
Im looking for this:
Button widget on my phone, when pressed a relay (wired to my nodemcu 8266) switches on for a period of lets say 10minutes, then switches of until I press the button again on my phone.

This isn’t a hard code, right? I tested with simple timer and blynk timer but nothing works out.
Anyone that might give it a try?

Greetings,
Jozef10

Hi Jozef, for Blynk Legacy or Blynk IOT?

You can post your sketch so we can take a look at it.

IoT

Working on it, I deleted al the other crap that did not work.
If you have some ideas, always welcome :slight_smile:

You can try this example

Did you use a BlynkTimer in Timeout mode, preferably as part of a Lambda function?

Read the “Timeout Timers” section of this document, and don’t forget to declare your BlynkTimer object and add timer.run(); to your void loop.

Once you have this working, if you want to add a slider or time input widget to your app to change the amount of time that the relay stays on for with each button press then you can use the technique described in the "Using variables instead of hard-coded numbers2 section of that document.

You may also need to learn about how to retrieve values from virtual pins, and how to sync the current values when the device starts up. That’s covered here…

Pete.

1 Like

Thanks for you answer.
This is the code I now have plotted. This is without the basic #define & #include blah blah :wink:
Now I need to setup the blynktimer or simpletimer, to start (10 minute) timer when I have pushed the blynk push button widget on my phone. But I do not know how to include it in my sketch. Any ideas?

Greetings,
Jozef

   void setup()
{

 pinMode(2, OUTPUT); //digital pin 2 (D4) as output.
}

 BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
 if(param.asInt() == 1)
 {
   // execute this code if the switch widget is now ON
   digitalWrite(2,HIGH);  // Set digital pin 2 HIGH
 {
 else
 {
   // execute this code if the switch widget is now OFF
   digitalWrite(2,LOW);  // Set digital pin 2 LOW    
 }
 
 
 // Debug console
 Serial.begin(9600);

 Blynk.begin(auth, ssid, pass);
 // You can also specify server:
 //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
 //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

 // Setup a function to be called every 10s
 timer.setInterval(10000L);
}

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

Please edit your post and add triple backticks ( ``` ) before and after your sketch.

@Joepl10 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
\ ```

Pete.

sorry, done now :slight_smile:

You can use timeout timer, or you can try automation which is the easiest way.

It’s always best to post the whole sketch.

So clearly my “Using Blynk Timer or SimpleTimer” guide isn’t as easy to follow as it should be. Maybe if you can explain which part(s) of that guide you find confusing then I can improve it.

Pete.

Hello Guys,

I again tried to look for answers in the Blynk community forums and online.
@PeteKnight: I understand the guide completely, I just don’t know how to put it together in my case.
I just want to make a LED burn or relay switch on for a certain amount of time once I’ve pushed the button widget on the blynk app on my iphone.

I came across some piece of coding. Mabye I should give it a try.
So this is what I made out of it. Need to test it, but this is what i’ve made out of it.

#define BLYNK_TEMPLATE_ID "H"
#define BLYNK_DEVICE_NAME "r" 
#define BLYNK_AUTH_TOKEN "E"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>  
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "M";  // Enter your Wifi Username
char pass[] = "2";  // Enter your Wifi password

BlynkTimer timer;

int virtual_pin value = param.asInt();

void setup()

{
  
  pinMode(2, OUTPUT); //digital pin 2 (D4) as output.
}

  BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if(param.asInt() == 1)
  digitalWrite(2,HIGH);  // Set digital pin 2 HIGH
  timer.setTimeout(10000L, []() //After 10 seconds the relay is turned off
  {
   digitalWrite(2, LOW);
  }); 
    
  {
  else
  {
    digitalWrite(2,LOW);  // Set digital pin 2 LOW    
  }
  
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);


  // Setup a function to be called every 10s
  timer.setInterval(10000L);
}

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

Hello so this is my code now.
I want to make a button (PUSH) and when I press it the relay or LED or whatever is connected will be powered via NodeMCU ESP8266 for 10seconds and then switches of until I again press the button.

  • What you think about the code?
  • I get an error at the ‘’ else digitalwrite low’’ (before void loop) and the error message says:
    expected primary-expression before ‘,’ token. I never got this message before and don’t know how to fix it.

Could anyone help and say if this code is good for my ideas about switching on/off?
Greetings,
Jozef

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME "r"
#define BLYNK_AUTH_TOKEN "Lc"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define relay

int value = 0; //Variable that will store the value of the button that will trigger the relay


BlynkTimer timer;

BLYNK_WRITE(V1)//Button that activates the relay
{
   value = param.asInt();
}

void setup() {

if(value == 1){ //If the button that triggers the relay is pressed
    digitalWrite(relay, HIGH);
    timer.setTimeout(10000L, []() //After 10 seconds the relay is turned off
  {  
    digitalWrite(relay, LOW);
  }); 
  }else
    digitalWrite(relay, LOW);
  }
  

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

If you used indents better, an placed opening curly brackets on a line of their own, instead of appending them to the end of the previous line, your code would look like this…


void setup()
{
  if(value == 1)
  { 
    //If the button that triggers the relay is pressed
    digitalWrite(relay, HIGH);
    timer.setTimeout(10000L, []() //After 10 seconds the relay is turned off
    {  
    digitalWrite(relay, LOW);
    }); 
  }
  else
    digitalWrite(relay, LOW);

// missing Blynk connection code in here…

}

You’d then be able to see that you are missing an opening and closing curly bracket around your digitalWrite(relay, LOW); line of code.

You are also missing pinMode commands in your void setup to allow your digitalWrite commands to work, along with Blynk connection code, Serial.begin commands etc.

Pete.

Hello,

Thanks @PeteKnight for replying.

I now made my code like this, like you told me to :slight_smile:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "";
char pass[] = "";

#define relay D2

int pinValue = 0; //Variable that will save the button's value

BlynkTimer timer;

BLYNK_WRITE(V1)                         //Button that activates the relay
{
   pinValue = param.asInt();
}

void setup() 
{
  // put your setup code here, to run once:
digitalWrite(4, OUTPUT);

Serial.begin(9600);
  Blynk.begin(auth, "'', "");
}

void loop() {
  // put your main code here, to run repeatedly:
if(pinValue == 1) //If the relay is activated by the button
{             
    digitalWrite(relay, HIGH);
    timer.setTimeout(10000L, []()   //After 10 seconds the relay is turned off
    {
      digitalWrite(relay, LOW);
     }); 
  }
  else if (pinValue == 0)         //If the relay is not activated by the button
    digitalWrite(relay, LOW);         // assuming relay is active HIGH
}

And look at the attached screenshot, this is my setup at the Blynk2.0 webpage.
I made a pushbutton with datastream ‘‘relay’’ and set to PUSH mode.
I uploaded the sketch and it is online at Blynk.
But when I press the button nothing happens.
Is there any chance you can spot the fault ???

I hope I’m almost there :slight_smile:

Greetings,
Jozef

Hmmm, it appears that we are speaking different languages!

I didn’t tell you to do with your void loop, instead I was explaining what changes you needed to make to your void setup to fix the error message you were getting, and also pointing-out other pieces of code that were missing from your void setup to enable your sketch to work correctly.

I’d suggest that you re-read what I wrote!

For 10 seconds maybe, but no longer now that you’ve removed Blynk.run() from your void loop.

Instead of making random changes to code that you clearly don’t understand, you should either go back to your original code. Or, if you don’t understand your original code either, you should learn more about C++ programming in general.

Pete.
Pete.

Hello,

I am sorry for my last code sketch I was working of 2-3 different sketches and mixed them a little up.
I have basic knowhow on writing codes but I’m just not familiar with connecting it to Blynk.
I normally work with physical buttons and such.

I now made my code complete, so no random changes :wink:
But clearly there is something wrong in the connection between Blynk and my NodeMCU

The code uploads fine and Blynk says my device is online, and stays online now.
But once I press the button widget I’ve made, nothing happens.

Is there anyone who could tell me what I’m doing wrong, and could explain it to me with using my code, so not by referring me to manuals and examples that are not related to my problem/situation.

Here is my full code, I would like to hear what you think about it! :slight_smile:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "";
char pass[] = "";

#define relay D1

int pinValue = 0; //Variable that will save the button's value

BlynkTimer timer;

BLYNK_WRITE(V1)                         //Button that activates the relay
{
   pinValue = param.asInt();
}

void setup() 
{
  // put your setup code here, to run once:
digitalWrite(5, OUTPUT);

Serial.begin(9600);
  Blynk.begin(auth, "", "");
}

void loop() {
  Blynk.run();
  // put your main code here, to run repeatedly:
if(pinValue == 1) //If the relay is activated by the button
{             
    digitalWrite(relay, HIGH);
    timer.setTimeout(10000L, []()   //After 10 seconds the relay is turned off
    {
      digitalWrite(relay, LOW);
     }); 
  }
  else if (pinValue == 0)         //If the relay is not activated by the button
    digitalWrite(relay, LOW);         // assuming relay is active HIGH
}

Iassume that you intended this to be a pinMode statement?

pinMode is not specific to Blynk.

Once again, you appear to have difficulty distinguishing between your void * setup and you void loop because as I said earlier…

Pete.