Mega+ESP will freeze if there is a connection problem

Hello,
I have a problem that is similar to furby_goulag.
I have an Arduino Mega connected to an ESP-01 module. I have the wifi connection and Blynk working fine. Non-technical people will be using my device in various locations, on various wifi networks. I have a touch screen interface on the Arduino that allows the user to enter the ssid and password for whatever network they intend to use. This system works fine assuming:

  1. The user enters the ssid correctly
  2. The user enters the password correctly
  3. The wifi signal is sufficiently strong
  4. There are no firewalls blocking the exchange with the Blynk server

The problem I have is that the Mega+ESP will freeze if there is a problem with any of the conditions above. I do not get a timeout. For example, when the wifi setup is correct, everything works fine, and my serial monitor looks like this:

Blynk.begin starting
[4210] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on Arduino Mega

[4719] Connecting to VR Guest
[7768] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
[12999] +CIFSR:STAIP,"192.168.31.29"
+CIFSR:STAMAC,"5c:cf:7f:56:40:ef"
[13000] Connected to WiFi
[23180] Ready (ping: 11ms).
Blynk.begin finished 

BUT… if I enter the incorrect password, the Mega+ESP seems to freeze, and my serial monitor looks like this:

Blynk.begin starting
[610] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on Arduino Mega

[1119] Connecting to VR Guest
[4169] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
[14210] Failed to connect WiFi

No matter how long I wait, I do not get a timeout, and in this state, the Arduino does not act on any interrupts; the timers in the Arduino code do not run. Everything just stops. What I need is for the Arduino to bail out of the Blynk.begin routine so that I can send a message to the user saying, “Wifi connection failed, check your wifi settings”.

I have read through a lot of threads on this board that deal with timeout issues. I have tried a lot of the suggested coding solutions. Nothing seems to do the trick.

About 10 months ago, furby_goulag seemed to be having the same problem. The discussion here seemed to indicate there is an inherent problem with timeouts when we use the Mega+ESP on wifi. Is that still the case? Is there any workaround?

Hello. We try not to reopen old topics here… things keep changing and even old issues may have new solutions.

I have moved your issue into a new topic.

Blynk.begin() is a blocking command… no connection, no further action. Period.

Normally that was the only option for an Arduino & shield combo, but now Blynk.config() seems to work with such combos and is non-blocking and will allow the rest of the sketch to keep running without Blynk connection… But you do need to figure out the Wifi connection/reconnection routines for your needs.

I use this startup and reconnection routine with my Mega/ESP-01 combo… seems to work OK.

In pre setup

#include <ESP8266_Lib.h>  // ESP-01 Link
#include <BlynkSimpleShieldEsp8266.h>  // ESP-01 Link

In my void setup()

  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);
  if (Blynk.connectWiFi(ssid, pass)) {
    Blynk.connect();
  }

In void loop()

void loop() {
  timer.run();

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already tring to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
      Blynk.config(wifi, auth, server, port);
      Blynk.connect();  // Try to reconnect to the server
      if (Blynk.connectWiFi(ssid, pass)) {
        Blynk.connect();
      }
    });  // END Timer Function
  }
}

You will have to look through all that and figure out the bits you want/need.

2 Likes

Gunner, you’re awesome. Your example code helped a lot, and I have this working now.
Thanks for the speedy response.
Just in case some other Blynkers want to see my code, here is the important bit:

void startBlynk(){
  if(LinkState==1){               // Ah! Someone pressed the wifi connect button
    LinkState=2;                  // Let's start Blynk
    Serial.println("LinkState went from 1 to 2");
    wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
    Serial.println("Blynk.config starting");
    Blynk.config(wifi, auth, "blynk-cloud.com", 8442);
    if (Blynk.connectWiFi(ssid, pass)) {
      Blynk.connect();
    }
    //LinkState=3;                  // Blynk startup done
    Serial.println("startBlynk finished");
  }
}

A post was split to a new topic: Blynk starts up OK but restarts about 10 times an hour