I was wandering why when I turn on (by Blynk console or smart phone) any of four LED’s it doesn’t stay on for ever until I turn it back off by myself, it turns off on its own after a little less than a day, even though the device is remains online.
Basically what I want to do is remotely turn on an LED by pressing the widget switch on the blynk console, and turn it off only when I press the widget to turn it off. (Eventually the LED’s will be replaced by actuators)
I think I must be doing something wrong in the code I don’t think its on BLYNK side.
I’m using an ESP32 dev module. Arduino IDE ver. 2.3.7, Blynk library ver. 1.3.2.
I’m including the Arduino code here below, any help would be greatly appreciated. Hope my info is understandable.
Not sure if I’m attaching the code proper in format
Thank you
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
#define LED1_PIN 16 // pins leds are connected to
#define LED2_PIN 17
#define LED3_PIN 18
#define LED4_PIN 19
// V0 is a datastream used to transfer and store LED switch state.
// Evey time you use the LED switch in the app, this function
// will listen and update the state on device
BLYNK_WRITE(V1)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED1_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED1_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
// :
// : leds 2&3 not shown here to save space
// :
BLYNK_WRITE(V4)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED4_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED4_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
void setup()
{
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, LED1_PIN);
}
void loop()
{
Blynk.run();
timer.run();
}
@Yiannis 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:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
This can’t be the code that you’re currently running for two reasons…
You aren’t defining the BlynkTimer object called “timer”
Your BlynkTimer setInterval instance is trying to call a function called LED1_PIN() once every second. There is no fiction called LED1_PIN(), but you do have alias called LED1_PIN which evaluates to the integer value 16
If you post your actual code that you are running, in its entirety, (with the sensitive data redacted) then I’ll take a look at it.
Thank you Pete,
I see now, the LED1_PIN is a definition of the LED connected to the pin on the MCU. I need to replace it with a function.
I’m attaching the entire code, if you don’t mind to look at it.
What I want, is to turn-on any of four LED’s represented by four widget switches and four LED’s on the Blynk console. So touching switch one turns on or off LED1, switch two LED2 etc.
Corresponding LED’s should remain on indefinitely and turn-off only when I touch the switch again to turn it off.
Basically a toggle on off set of four switches and four LED’s.
Thanks for your help.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
#define LED1_PIN 16 // pins leds are connected to
#define LED2_PIN 17
#define LED3_PIN 18
#define LED4_PIN 19
// V0 is a datastream used to transfer and store LED switch state.
// Evey time you use the LED switch in the app, this function
// will listen and update the state on device
BLYNK_WRITE(V1)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED1_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED1_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
BLYNK_WRITE(V2)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED2_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED2_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
BLYNK_WRITE(V3)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED3_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED3_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
BLYNK_WRITE(V4)
{
// Local variable `value` stores the incoming LED switch state (1 or 0)
// Based on this value, the physical LED on the board will be on or off:
int value = param.asInt();
if (value == 1) {
digitalWrite(LED4_PIN, HIGH);
Serial.print("value =");
Serial.println(value);
} else {
digitalWrite(LED4_PIN, LOW);
Serial.print("value = ");
Serial.println(value);
}
}
void setup()
{
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, LED1_PIN);
}
void loop()
{
Blynk.run();
timer.run();
}
Once again, this can’t be the code that you are currently running when you say…
Because this code won’t compile, for the reasons I stated before.
You’re asking for advice regarding unexplained operational issues, but this code can’t be the code you’re running when you’re experiencing therese operation issued because it doesn’t compile.
Why are you using a BlynkTimer in the non-working code?
You say…
Hi Pete,
Yes I’m sorry I drove you mad.
As I mentioned above all I basically wanted four toggle switches with four indicator LEDs.
I really don’t need to do anything, no need for any function.
So I just removed two lines from the code since I don’t need a timer. I must of copied/pasted some code from other sketches and didn’t realize the timer function.
Thank you for making me realize the timer function, actually not needed.
Sketch compiles well and runs on ESP32 dev board, I will keep it running for a good while, the LEDs should remain on for ever until I turn them off.
Thanks!
John
PS. there may be a better way to compose the code for what I’m doing but I’m not at that level yet.
I’d suggest you take a look at the documentation for BLYNK_CONNECTED()
This special function is called automatically whenever the device connects or re-connects to the Blynk server.
If you place a series of BlynksyncVirtual(VPin) commands within the BLYNK_CONNECTED() function then these will automatically call the corresponding BLYNK_WRITE(Vpin) functions, which will synchronise the LEDs to the app widget status.