BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

ESP8266-01 with arduino Mega disconnect when router restart

Hi,
i have problem with project that is my Mega doesn’t reconnect when my router restart by any reason.

thanks

Not really a Blynk specific issue, but search this forum for similar issues and solutions.

E.g. you will need to use a combination of Blynk connection management and Arduino WDT reset.

Hi Gunner,
I tried the Arduino WDT reset but it didn’t work with my project. i have idea and i need your help, my idea can we connect the reset pin of ESP-01 to one of pins in arduino mega and give them if statement to reset ESP-01 through that?

How did you run it, and in what way did it not work… I mean, all it does is reset the arduino if something locks up… how can that not work?

Good point… I forgot to mention that I DO have the reset pins on both the ESP-01 and Mega tied together, so when the Mega resets (RST pulled to ground), so does the ESP. No need for connecting the resets to any other IO pins.

Hi Gunner,
thanks about your advice, i have get the WDT working, but there is one problem how to use this sentence “Failed to connect WiFi” inside IF STATEMENT code to let WDT reset the MEGA when there is failed to connect with Wifi???

thanks

Sorry, I don’t quite understand your question.

Basically, once you run this (Arduino only?) command in setup()…

wdt_enable(WDTO_8S);  // maximum setting is 8S

you have 8 seconds to send this command to reset the countdown (usually in the main void loop() )…

wdt_reset();

…or the Mega reboots.

So you could put this basic structure in the void loop()… Tweek to account for different timing/reconnection routines, heartbeat indicators, etc.

void loop()
{
  if (Blynk.connected()) {
    Blynk.run();
    wdt_reset();  // make sure this gets called at least once every 8 seconds!
  } else {
    Blynk.connect();  // try to reconnect before reset
  }
  timer.run();
}

Sorry Gunner, my problem is when I have my arduino connected with blynk and everything Ok than if my router get reset and that will take maybe 2 minute to run normally again, my arduino will not be able to connect again, on serial monitor it will freeze with this words “Failed to connect WiFi”, so at this point the WDT will not be able to reset the Mega, so i need your help to but conditional statement inside the code to let the Mega keep reset until the router be ready and the arduino can connect.

Normal procedure is to use Blynk.config() instead of Blynk.begin()… but unfortunately I don’t think that works with the library required for ESP as shield. @Dmitriy is this option able to be made available?

http://docs.blynk.cc/#blynk-firmware-configuration

Everything is possible. But this setup is complex and I’m not the right person to ask :slight_smile:.

I didn’t think so… but I know, you know, the right one :stuck_out_tongue_winking_eye: Can you stick a bug in their ear for us poor souls that like to merge ESP/Arduino.

Guys,
Please don’t fight :disappointed::disappointed::disappointed:, l just need to know how to get benefit from these words “Failed to connect WiFi” will show up on serial moniter screen and use them when they will show up to reset the Arduino.

Thanks

?? No fighting is happening ??

As for your issue… that is exactly what we are discussing… right now I don’t think there is anything else you can really do about that error, except to manually restart the Mega, AFTER the router.

I was joking :wink::wink::wink:. What about using like serial.find or serial.read to indicate something will be show up on serial port?

Like
If ( serial.find(“Failed to connect WiFi”))
{ //do reset code
}

The problem is that your sketch is NOT running, until connected, when using Blynk.begin() Which is why all standalone ESP’s should be using the Blynk.config() method… which cannot be done (with ESP as shield) as it throws a compile error with #include <BlynkSimpleShieldEsp8266.h>.

Short of a 2nd MCU monitoring your debug serial, and thus tripping an external reset… well, I don’t see any other way of doing it on one MCU when the sketch is halted.

if (wifi->joinAP(ssid, pass)) {
            String my_ip = wifi->getLocalIP();
            BLYNK_LOG1(my_ip);
        } else {
            BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
            return false;
        }
        BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
        return true;
    }

this is from BlynkSimpleShieldEsp8266.h

this is the source of Failed to connect WiFi.

That is the source of the error message… not the root of the issue.

Keep digging into it :+1:. Maybe you can come up with a solution??

I got it :blush::blush::blush::blush::blush::blush::grinning:.
To let any ESP8266 work automatically if any wifi issues will happen we have to update the BlynkSimpleShieldEsp8266.h library as following:
1- add #include <avr/wdt.h> library inside the BlynkSimpleShieldEsp8266.h library.
2- add wdt_enable(WDTO_1S); and wdt_reset(); to connectWiFi lines as shown down:

 bool connectWiFi(const char* ssid, const char* pass)
    {
        ::delay(500);
        BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
        /*if (!wifi->restart()) {
            BLYNK_LOG1(BLYNK_F("Failed to restart"));
            return false;
        }*/
        if (!wifi->kick()) {
             BLYNK_LOG1(BLYNK_F("ESP is not responding"));
			wdt_enable(WDTO_1S);
                        wdt_reset();
             //TODO: BLYNK_LOG_TROUBLE(BLYNK_F("esp8266-not-responding"));
             return false;
        }
        if (!wifi->setEcho(0)) {
            BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
            return false;
        }
        String ver = wifi->ESP8266::getVersion();
        BLYNK_LOG1(ver);
        if (!wifi->enableMUX()) {
            BLYNK_LOG1(BLYNK_F("Failed to enable MUX"));
        }
        if (!wifi->setOprToStation()) {
            BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
            return false;
        }
        if (wifi->joinAP(ssid, pass)) {
            String my_ip = wifi->getLocalIP();
            BLYNK_LOG1(my_ip);
        } else {
            BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
			wdt_enable(WDTO_1S);
			wdt_reset();
            return false;
        }
1 Like

Interesting approach…I will have to test that.

But why add the wdt_reset(); if the purpose is to activate the WDT when that message is called.

Both of them work together the time for reset and active the reset.

But wdt_reset();doesn’t “activate” anything, it resets the timeout… is only needed if for some reason you DON’T want the reboot to happen after the 1 second you have allotted… in fact all that it does is reset the WDT a millisecond after it is activated :stuck_out_tongue: then since it is never called again… 1 second later… reboot!