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

Recovering Blynk connection to server

#1

Hi,
I’m running Blynk on an ESP32 and works fine as long there’s internet. When the power goes off at the router, the Blynk won’t recover however, the ESP still runs fine with the rest of its code. I tried several Blynk functions but none of them seem to help in my case.

The latest code I used in the loop is this

  if (Blynk.connected())
  {
    Blynk.run();
  }
  else {
    if (DEBUG) Serial.println("Connecting to Blynk again.");
    Blynk.disconnect();
    Blynk.connect();
  }

I’d appreciate some direction on this.
TIA

0 Likes

#2

Search for recent posts with “Connection Management”, Blynk.config() and Blynk.connect() in place of the blocking Blynk.begin().

0 Likes

#3

@Costas this helped, thank you.

1 Like

#4

@Emilio glad it helped but when I said recent I didn’t mean 12 months old.
The default forum search is relevance which makes sense but most recent is equally important on a fast moving site about IOT i.e. Blynk’s system constantly changes.

Pretty sure I posted better code in the last 6 weeks but I’ll leave you to iron out the bugs in my original code :slight_smile:

0 Likes

#5

@Costas Thanks for getting back to me. There’re many notes on this issue here and to find a safe and sound code is a challenge. As I said, the code did help me in recover the lost wifi connection however, didn’t like much the 11 second timer. Will keep searching as per your suggestion.

0 Likes

#6

The code is easily modified to change the number of iterations and the duration of the timeout etc.

I certainly posted cleaner code recently to do the same thing for Ethernet and can probably be found with the term NoBlynkBlock.

I have never really been convinced that Blynk timeouts work and I have looked at the code. So in recent versions I just use Blynk.connect() and not Blynk.connect(x) where x was supposed to be so many ms and () is the system default time out. All the break is doing is letting you define your own timeout to bypass the beginners use of the Blynk.begin() blocking routine.

0 Likes

#7

@Costas Understood.
However, the code below didn’t work in my case. It ran ok at startup then, when I turned the wifi off-on, it did not recover.
Why the V11?

0 Likes

#8

It doesn’t do anything, it was just to display something in the app and V11 was spare in my project when I was testing the code.

Pretty sure I tested all eventualities like router off to start and router going off after connecting to the server etc. Did you test the example exactly as it’s written?

0 Likes

#9

@Costas Yes, I used the same test code. The only change I made was to match it to my ESP32 board. Will look into the ESP32 WiFi.h to see if it’s the same as for the ESP8266.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char ssid[]   = "PHONE_HOME";
char pass[]   = "01234";
// Blynk
char auth[] = "8d7102";
char server[] = "blynk-cloud.com";

BlynkTimer timer;
unsigned int myServerTimeout  =  3500;  //  3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout    =  3200;  //  3.2s WiFi connection timeout   (WCT)
unsigned int functionInterval =  7500;  //  7.5s function call frequency   (FCF)
unsigned int blynkInterval    = 25000;  // 25.0s check server frequency    (CSF)

void setup()
{
  Serial.begin(115200);
  Serial.println();
  if (WiFi.status() == 6) {
    Serial.println("\tWiFi not connected yet.");
  }
  timer.setInterval(functionInterval, myfunction);// run some function at intervals per functionInterval
  timer.setInterval(blynkInterval, checkBlynk);   // check connection to server per blynkInterval
  unsigned long startWiFi = millis();
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (millis() > startWiFi + myWiFiTimeout) {
      Serial.println("\tCheck the WiFi router. ");
      break;
    }
  }
  Blynk.config(auth, server);
  checkBlynk();
}

void myfunction() {
  Serial.println("\tLook, no Blynk  block.");
  if (WiFi.status() == 3) {
    Serial.println("\tWiFi still  connected.");
  }
  if (Blynk.connected()) {
    //Blynk.virtualWrite(V11, millis() / 1000);
  }
}

void checkBlynk() {
  if (WiFi.status() == WL_CONNECTED)
  {
    unsigned long startConnecting = millis();
    while (!Blynk.connected()) {
      Blynk.connect();
      if (millis() > startConnecting + myServerTimeout) {
        Serial.print("Unable to connect to server. ");
        break;
      }
    }
  }
  if (WiFi.status() != 3) {
    Serial.print("\tNo WiFi. ");
  }
  Serial.printf("\tChecking again in %is.\n", blynkInterval / 1000);
  Serial.println();
}

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

#10

ESP32 is still short of some of the ESP8266 features, that’s why I don’t use it.

0 Likes

#11

Ok, so you’re suggesting to use

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

instead of

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

for the ESP32?

EDIT: the ESP8266 libraries do not compile for the ESP32 board

0 Likes

#12

No, I’m suggesting you use an ESP8266 :slight_smile:

You will have to Google for a fix relating to ESP32 but along the lines of the code for ESP8266.

0 Likes

#13

Ok thanks. Maybe I keep your older code since it’s working with the ESP32 :wink:

1 Like

#14

Sounds like a plan until we all know more about the ESP32.

0 Likes

#15

Hi @Costas, I slightly changed your code to fit the ESP WROOM 32 dev board and it seems to work every time. Here’s what I did…

#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space
#define BLYNK_DEBUG         // Optional, this enables lots of prints
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = "mySSID";
char pass[] = "myPass";
char auth[] = "myAuth";

BlynkTimer timer;
bool Connected2Blynk = false;

void connectWifi(void);
void CheckConnection(void);

void setup() {
  Serial.begin(115200);
  delay(10);

  connectWifi();

  timer.setInterval(60000L, CheckConnection); //60.0s check Blynk server frequency
}

void loop() {

  if (Connected2Blynk) {
    Blynk.run();  //only process Blyk.run() function if we are connected to Blynk server
  }

  timer.run();
}

void CheckConnection() {
  Connected2Blynk = Blynk.connected();
  if (!Connected2Blynk) {
    Serial.println("Not connected to Blynk server");
    connectWifi();
  }
  else {
    Serial.println("Still connected to Blynk server");
  }
}

void connectWifi() {
  Blynk.disconnect(); //reset Blynk connection
  delay(100);
  
  Serial.println("Starting Wifi/Blynk");

  Blynk.begin(auth, ssid, pass);
  delay(100);

  if (Blynk.connected()) {
    Connected2Blynk = true;
    Serial.println("Wifi/Blynk started");
  }
  else {
    Serial.println("Check Router");
  }
}

Results…

[23] Disconnected
Starting Wifi/Blynk
[123] Connecting to mySSID
[3156] Connected to WiFi
[3156] IP: 192.168.0.37
[3156] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP32 <---------------

[5001] Connecting to blynk-cloud.com:8442
[5110] <[02|00|01|00] myAuth
[5191] >[00|00|01|00|C8]
[5191] Ready (ping: 81ms).
Wifi/Blynk started
[5295] >[00|00|02|00|C8]
[15196] <[06|00|03|00|00]
[15260] >[00|00|03|00|C8]
[25197] <[06|00|04|00|00]
[25267] >[00|00|04|00|C8]
[35198] <[06|00|05|00|00]
[35267] >[00|00|05|00|C8]
[45199] <[06|00|06|00|00]
[45265] >[00|00|06|00|C8]
[55200] <[06|00|07|00|00]
[56340] >[00|00|07|00|C8]
[65201] <[06|00|08|00|00]
[65267] >[00|00|08|00|C8]
Still connected to Blynk server <------------
[75202] <[06|00|09|00|00]
[75267] >[00|00|09|00|C8]
[85203] <[06|00|0A|00|00]
[87204] <[06|00|0B|00|00]

============ reclycled router power ===============

[90802] Cmd error		
[92205] Connecting to blynk-cloud.com:8442
[97206] Connecting to blynk-cloud.com:8442
[102207] Connecting to blynk-cloud.com:8442
[107208] Connecting to blynk-cloud.com:8442
[112209] Connecting to blynk-cloud.com:8442
[117210] Connecting to blynk-cloud.com:8442
[122211] Connecting to blynk-cloud.com:8442
Not connected to Blynk server <--------------
[125295] Disconnected
Starting Wifi/Blynk
[125395] Connecting to mySSID
[126895] Connected to WiFi
[126895] IP: 192.168.0.37
[126895] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP32

=========== FULL RECOVERY =======================

[127212] Connecting to blynk-cloud.com:8442	
[127282] <[02|00|01|00] myAuth
[127353] >[00|00|01|00|C8]
[127353] Ready (ping: 70ms).
Wifi/Blynk started
[127458] >[00|00|02|00|C8]
[137359] <[06|00|03|00|00]
[137423] >[00|00|03|00|C8]
[147360] <[06|00|04|00|00]
[147428] >[00|00|04|00|C8]
[157361] <[06|00|05|00|00]
[157428] >[00|00|05|00|C8]
[167362] <[06|00|06|00|00]
[167426] >[00|00|06|00|C8]
[177363] <[06|00|07|00|00]
[177429] >[00|00|07|00|C8]
Still connected to Blynk server <---------------
[187364] <[06|00|08|00|00]
[187429] >[00|00|08|00|C8]
[197365] <[06|00|09|00|00]
[197430] >[00|00|09|00|C8]
[207366] <[06|00|0A|00|00]
[207484] >[00|00|0A|00|C8]
1 Like

#16

You still have Blynk.begin() which is causing the entries above. Many projects need to keep processing when the internet is down and that can only be done with Blynk.config() and Blynk.connect().

0 Likes

#17

:face_with_raised_eyebrow:My project is still running locally. I’ve it running a clock and monitoring several signals with no immediate need for internet. I thought “[…] Connecting to blynk-cloud.com:8442” meant that the code is trying to connect to Blynk’s server.

0 Likes

#18

Yes every 5s and I would like it to look like this:

[92205] Connecting to blynk-cloud.com:8442
Sketch still running ......
[97206] Connecting to blynk-cloud.com:8442
Sketch still running ......
[102207] Connecting to blynk-cloud.com:8442
Sketch still running ......
[107208] Connecting to blynk-cloud.com:8442
Sketch still running ......
[112209] Connecting to blynk-cloud.com:8442
Sketch still running ......
[117210] Connecting to blynk-cloud.com:8442
Sketch still running ......
[122211] Connecting to blynk-cloud.com:8442

but I’m sure it will not do this with Blynk.begin(). You have cover when the router is struck by lightning, cat chews the cable and the kids unplug to plug in their iPhone :slight_smile:

0 Likes