Continuing loop while reconnecting

Hi.
I dont think this is a bug, hence i placed it in this category.
I use ESP8266.
As far as i can see, the sketch stalls if it looses connection to blynk server, while trying to reconnect. Is this correct or is it just me?
If it is the intention, is it possible to avoid it somehow, or are you aware of it to further updates.

I’m trying to create at Blynk Controlled PID (for Sous vide) and so far the last hurdle is to keep it running if connection should be lost.
I created a simple test sketch, and could see that it runs the loop every few seconds. But if loose the connection for longer period, it would be critical.

Just in case I have attached the sketch. It uses some of the same functionalities as in my PID sketch.

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "7bce3902e10e44cb94e65084b143e194"; // Put your Auth Token here. (see Step 3 above)

int ledPin = 14;
boolean ledState = true;

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, "Alcatraz24", "dubbIdubbI");  // Here your Arduino connects to the Blynk Cloud.
  timer.setInterval(500, ledBlink);
  pinMode(ledPin, OUTPUT);
  BLYNK_LOG("LED ON SENT FROM SETUP")
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run();

}

void ledBlink(){
  
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
  BLYNK_LOG("LED TOGGLE SENT FROM ledBlink function")
}

And here is a small piece of the log.

[285559] LED TOGGLE SENT FROM ledBlink function
[286059] LED TOGGLE SENT FROM ledBlink function
[286559] LED TOGGLE SENT FROM ledBlink function
[287059] LED TOGGLE SENT FROM ledBlink function
[287559] LED TOGGLE SENT FROM ledBlink function
[288059] LED TOGGLE SENT FROM ledBlink function
[292081] Sent 0/5
[292099] LED TOGGLE SENT FROM ledBlink function
[292150] Connecting to cloud.blynk.cc:8442
[303731] LED TOGGLE SENT FROM ledBlink function
[303781] Connecting to cloud.blynk.cc:8442
[314855] LED TOGGLE SENT FROM ledBlink function
[314904] Connecting to cloud.blynk.cc:8442
[326929] LED TOGGLE SENT FROM ledBlink function
[326978] Connecting to cloud.blynk.cc:8442
[338104] LED TOGGLE SENT FROM ledBlink function
[338153] Connecting to cloud.blynk.cc:8442
[349353] LED TOGGLE SENT FROM ledBlink function
[349404] Connecting to cloud.blynk.cc:8442
[361577] LED TOGGLE SENT FROM ledBlink function
[361626] Connecting to cloud.blynk.cc:8442
[372826] LED TOGGLE SENT FROM ledBlink function
[372875] Connecting to cloud.blynk.cc:8442
[384051] LED TOGGLE SENT FROM ledBlink function
[384101] Connecting to cloud.blynk.cc:8442
[395250] LED TOGGLE SENT FROM ledBlink function
[395299] Connecting to cloud.blynk.cc:8442
[406424] LED TOGGLE SENT FROM ledBlink function
[406474] Connecting to cloud.blynk.cc:8442
[406869] Ready!
[406885] LED TOGGLE SENT FROM ledBlink function
[406936] LED TOGGLE SENT FROM ledBlink function
[406987] LED TOGGLE SENT FROM ledBlink function

Hey, I see your idea.

Currently we don’t have such option in our API.
Reconnection attempt always uses some time.
You could try something like this to make reconnection attempts once a minute:

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  timer.setInterval(60000, reconnectBlynk);
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

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

Please note that you will have to use the latest blynk from master branch (not the released one). We will release a new version soon.

Hope this helps… Thanks!

That looks to be perfect. (will try later). That would be the perfect tradeoff to adjust the interval.

Maybe a dumb question, but “Blynk.connect()” will it force a connection attempt and return a boolean value?
And is there anywhere yet to find information of “hidden functions” like this :smile:

Again thanks for the help.

bool connect() {

Should be :slight_smile:

@vshymanskyy

Thanks for the help. Worked, almost perfectly. There is a small issue with the code trying to catch up with lost actions while trying to reconnect.
I have the timer for the reconnect, and also a timer running every 1000ms blinking the LED. When connection is lost, at some point it tries to reconnect, and it takes a few seconds. When continuing, it rapidly blinks the LED until caught up on time. (see the log below. The timestamps after reconnect is aprox. 50ms instead of 1000. )

I tried to counter this by disabling the timer while reconnecting en reenable it after. But this had no effect. It’s not the biggest issue, but i still think it will unnecessary affect the sketch. Can you see if i’m doing something wrong or why its not working by disabling the timer.

void setup()

{
  Serial.begin(9600);
  Blynk.begin(auth, "Alcatraz24", "dubbIdubbI");
  reconnectTimer = timer.setInterval(20000, reconnectBlynk);
  ledTimer = timer.setInterval(1000, ledBlink);
  pinMode(ledPin, OUTPUT);
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    timer.disable(ledTimer);
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
    timer.enable(ledTimer);
  }
}

void ledBlink(){
  
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
  BLYNK_LOG("LED TOGGLE SENT FROM ledBlink function")
}

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

log:
[189878] LED TOGGLE SENT FROM ledBlink function
[190878] LED TOGGLE SENT FROM ledBlink function
[191878] LED TOGGLE SENT FROM ledBlink function
[192878] Connecting to cloud.blynk.cc:8442
[204479] Not reconnected
[204504] LED TOGGLE SENT FROM ledBlink function
[204557] LED TOGGLE SENT FROM ledBlink function
[204608] LED TOGGLE SENT FROM ledBlink function
[204658] LED TOGGLE SENT FROM ledBlink function
[204709] LED TOGGLE SENT FROM ledBlink function
[204761] LED TOGGLE SENT FROM ledBlink function
[204812] LED TOGGLE SENT FROM ledBlink function
[204862] LED TOGGLE SENT FROM ledBlink function
[204913] LED TOGGLE SENT FROM ledBlink function
[204965] LED TOGGLE SENT FROM ledBlink function
[205016] LED TOGGLE SENT FROM ledBlink function
[205066] LED TOGGLE SENT FROM ledBlink function
[205117] LED TOGGLE SENT FROM ledBlink function
[205878] LED TOGGLE SENT FROM ledBlink function
[206878] LED TOGGLE SENT FROM ledBlink function
[207878] LED TOGGLE SENT FROM ledBlink function
[208878] LED TOGGLE SENT FROM ledBlink function
[209878] LED TOGGLE SENT FROM ledBlink function
[210878] LED TOGGLE SENT FROM ledBlink function
[211878] LED TOGGLE SENT FROM ledBlink function

As you are working with timer and LED I have the following question:

can you start (restart) a timer inside the main loop with a interval defined by a slider on the Bynk app.
This timer should change the frequency of the LED on board of the Aruino pin 13.

@Geab

This Code changes the interval of the ledBlink loop when the slider is changed.
I used ESP8266 board, so just alter it to fit yours. I think it will cover what you are looking for.
Also take a look at the Simple Timer library documentation http://playground.arduino.cc/Code/SimpleTimer

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

boolean ledState = false;
const int ledPin = 13;
long ledInterval;
int ledTimer;

SimpleTimer timer;

char auth[] = "";


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "", "");
  pinMode(ledPin, OUTPUT);
}


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


BLYNK_WRITE(4) {
  if (timer.isEnabled(ledTimer)) {                          // Check if timer is enabled
    timer.deleteTimer(ledTimer);                            // Delete timer if enabled
}    
    ledInterval = param.asInt() * 1000;                     // Set Input from slider to millis
    ledTimer = timer.setInterval(ledInterval, ledBlink);    // Create timer with new input
}


void ledBlink() {
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
1 Like

@iclimb works smoothly; just adapted some code to fit it to the arduino uno; really nice and now I understand the delete timer command - there was no example in the http://playground.arduino.cc/Code/SimpleTimer ][1]

//#include <SoftwareSerial.h>
//SoftwareSerial SwSerial(10, 11);
//#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

SimpleTimer timer;

boolean ledState = false;
const int ledPin = 13;
long ledInterval;
int ledTimer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(ledPin, OUTPUT);
}

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

BLYNK_WRITE(3) {
  if (timer.isEnabled(ledTimer)) {                          // Check if timer is enabled
    timer.deleteTimer(ledTimer);                            // Delete timer if enabled
}    
    ledInterval = param.asInt();                     // Set Input from slider to millis
    ledTimer = timer.setInterval(ledInterval/10, ledBlink);    // Create timer with new input
}

void ledBlink() {
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
 }

Good to hear. I worked a lot with it to a few days ago :slight_smile: no need for more people to stumble around with it.

:slight_smile: you made my day!