Virtual button to control relay on GPIO 16

The command to ask the Blynk server to send you the latest value for the slider widget attached to pin V7…

Pete.

1 Like

@PeteKnight like this?

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

const int wifists=2;
int slider;
#include "BlynkEdgent.h"
#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)
{
 slider = param.asInt();
}
  
BLYNK_WRITE(V0)
{
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );
  } else {
  digitalWrite(16, HIGH);  
  }
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
}
void loop() {
  BlynkEdgent.run();
  timer.run();
}

Yes :+1:

phew!
but i have a feeling that

 {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}

shouldnt be in the place where it is right now

You didn’t answer this question…

If you had have answered it correctly then you would have said that the device MUST be connected to the Blynk server for BLYNK_WRITE(V0), so putting this test into BLYNK_WRITE(V0) is a total waste of time…

Because Blynk.connected will ALWAYS be true when BLYNK_WRITE(V0) executes.

As I said before, but you chose to ignore, when this piece of code was in your void loop…

Pete.

so this should go under BLYNK_CONNECTED?
i find it hard to figure out how to use a timer to call this functiin

No, we’ve been there already and the answer is still no.

Same answer as before…

Pete.

@PeteKnight I tried reading both posts linked in post #2 again…and i couldn’t find anything that relates to using a timer to

any help would be appreciated :slight_smile:

read this too and this is what i came up with

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

const int wifists=2;
int slider;
#include "BlynkEdgent.h"
BlynkTimer ledtimer;

#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)
{
 slider = param.asInt();
}
  
BLYNK_WRITE(V0)
{
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );
  } else {
  digitalWrite(16, HIGH);  
  }
  
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  ledtimer.setInterval(1000L, senddata);
}
void senddata()
{
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
}
void loop() {
  BlynkEdgent.run();
  timer.run();
  ledtimer.run();
}

Giving your functions names that describe what they do is always a good idea. This function isn’t sending data to Blynk, so calling it sendData isn’t a great idea.

You have one BlynkTimer object declared, called ledtimer

But you are calling this BlynkTimer object and the now non-existent BlynkTimer object called timer in your void loop…

This will cause a compiler error (unless you’re running a very early version of the Edgent example).

If you’d actually bothered to read my timers tutorial you would have seen that each BlynkTimer object can support upto 16 timers - that’s 16 separate timer declarations in void setup like this…

So using a generic name for your timer object - such as timer - makes more sense, as that one BlynkTimer object can be used to call functions which perform very different functions, not just switching the state of an LED.

I think I’ll step back from further comments on your posts, other than ones that require moderator input, as I find your unwillingness to read information that is provided to you at odds with my approach.

Pete.

I am sorry for my not proprly understanding what you meant; im new to the blynk software…

I think im using the newer version(the blynnk library installed is 1.1.0)
the code compiles fine

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

const int wifists=2;
int slider;
#include "BlynkEdgent.h"

#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)
{
 slider = param.asInt();
}
  
BLYNK_WRITE(V0)
{
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );
  } else {
  digitalWrite(16, HIGH);  
  }
  
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  timer.setInterval(1000L, connect);
}
void connect()
{
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
}
void loop() {
  BlynkEdgent.run();
  timer.run();
  
}

This is my code as of now , and i hope ive not done any mistakes :slight_smile:

@Blynk_Coeur
@John93
anyone willing to just check my code
I apologize for the previous bhaviour

The latest version of the C++ Blynk library is 1.2.0

If you go to the settings.h tab, you will notice that pin 2 is already in use.

#define BOARD_LED_PIN               2

If you use the same pin for several porpoises, your software will become unstable. You should change it to something else. For example,

const int wifists=5;

How did you configure the V7 datastream?

Just updated the library an the code compiles fine.

I thought that using pin 2 would b a good idea as the onboard led is connected to pin 2 and it would be easier to see th connection status without the need of hooking up a seperate LED.
is it possible to change

the pin in the settings tab?

You mean in the blynk. cloud dashboard;
data type as integer,
min value= 500,
max value=5000
default value=750

Yes.

should there be a led hoooked to whichever pin i connect the BOARD_LED_PIN to?

i tried only integer and not long int. should i try long int too?
from what i understood,i dont think long int will be needed in this case as im using values from 500 to 5000

It’s optional. You can add one if you like.

If you are working with numbers that are within the range of int, then using int instead of long int should not cause any issues. However, if you are working with numbers that are larger than the range of int, then using int instead of long int could cause overflow issues, where the value stored in the variable exceeds the maximum representable value.

1 Like

Thanks for the help

I checked the settings.h tab but couldnt find this line of code
btw…how do i use this code for multiple buttons and relays (3 buttons and relays while using the same slider as the timeout value for all buttons and relays)
Heres my code

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

const int wifists=2;
int slider;
#include "BlynkEdgent.h"

#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)
{
 slider = param.asInt();
}
  
BLYNK_WRITE(V0)
{
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );
  } else {
  digitalWrite(16, HIGH);  
  }
  
  
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  timer.setInterval(1000L, connect);
}
void connect()
{
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
}
void loop() {
  BlynkEdgent.run();
  timer.run();
  
}
#define BLYNK_TEMPLATE_ID ""

#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION        "1.0"

#define BLYNK_PRINT Serial

//#define BLYNK_DEBUG

#define APP_DEBUG

const int wifists=2;

int slider;

#include "BlynkEdgent.h"

#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)

{

 slider = param.asInt();

}

 

BLYNK_WRITE(V0)

{

  int pin = param.asInt();

  if (param.asInt()){

  digitalWrite(16, LOW);

  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );

  } else {

  digitalWrite(16, HIGH);  

  }

}

  BLYNK_WRITE(V1)

{

  int pin = param.asInt();

  if (param.asInt()){

  digitalWrite(4, LOW);

  timer.setTimeout(slider, [] () {digitalWrite(4, HIGH);Blynk.virtualWrite(V1,LOW);} );

  } else {

  digitalWrite(4, HIGH);  

  }

}

BLYNK_WRITE(V2)

{

  int pin = param.asInt();

  if (param.asInt()){

  digitalWrite(5, LOW);

  timer.setTimeout(slider, [] () {digitalWrite(5, HIGH);Blynk.virtualWrite(V2,LOW);} );

  } else {

  digitalWrite(5, HIGH);  

  }

}

void setup() {

  Serial.begin(115200);

  delay(100);

  pinMode(16, OUTPUT);

  pinMode(4, OUTPUT);

  pinMode(5, OUTPUT);

  pinMode(wifists, OUTPUT);

  digitalWrite(16,HIGH);

  digitalWrite(4,HIGH);

  digitalWrite(5,HIGH);

  BlynkEdgent.begin();

  timer.setInterval(1000L, connect);

}

void connect()

{

  {

 if (Blynk.connected()) {

    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet

  } else {

    digitalWrite(wifists, LOW);

  }

 

}

}

BLYNK_CONNECTED()

{

  Blynk.syncVirtual(V7);

}

void loop() {

  BlynkEdgent.run();

  timer.run();

 

}

Here’s what i came up with
hope there are no errors

It’s line number 15.

Looks ok. Have you tried it?