How to reconnect Wifi on ESP8266 without restarting when WiFi is lost

I guys I need help with my project. I am currently using a ESP8266 and a relay to activate a mechanical switch. When the WiFi from the provider stops working or loses connection, the ESP will restart itself to search for the connected WiFi. When this happens it also activates the relay and causes a false signal (HIGH).
I am using the Blynk Legacy and it uses a token generated on the Blynk Dashboard website.

My question is: how do I prevent it from restarting? I would like for it to search for the WiFi connection and reconnect to the same SSID that it lost without restarting itself to end up in the loop to search for a connection. Is there somewhere in the code that I could add that it monitors and reconnects to the WiFi?

Thank you very much I really appreciate the input !

So you’re running your own Blynk legacy local server. Is this on the same network as the ESP8266?

Why is your device restarting when it loses the WiFi connection?

Are you using virtual or digital pins?

Pete.

Hi Pete thanks for the fast feedback! I am using the Blynk Legacy servers and the ESP connects to the WiFi here at home. I do have virtual pins.
The code I’ve gotten online. It is NodeMCU ESP8266 using Blynk 2.0.
In this program instead of pressing a button for a LED, I use it to activate a virtual pin for a relay.

Hope this helps

The Blynk Legacy cloud servers were de-commissioned at the end of 2022.
The only way to use Blynk Legacy is to either run your own Legacy server, or to connect to someone else’s unofficial Legacy server.

The YouTube video you’ve linked to is about Blynk IoT (otherwise know as Blynk 2.0 or “New Blynk”), not Blynk Legacy.
I assume therefore that you are using Blynk IoT, not the discontinued Blynk Legacy.

The Edgent sketch used in the YouTube video does indeed restart the ESP device if it is unable to connect to the stored WiFi after a certain amount of time.
Unfortunately, the program files for the YouTube project haven’t been updated for two years, so are using extremely outdated versions of the Edgent example sketch. They also make the mistake of not un-commenting one of the board types in the .ino file, so using the custom board configuration, which isn’t appropriate for any ESP8266 board that I’m aware of.

I’d suggest that you ensure you have the latest Blynk library (version 1.2.0) installed, and create a new project from the File > Examples > Blynk > Edgent menu, then update this with the custom pieces of code from the YouTube example. You’ll need to un-comment the most appropriate board type and add your two line of firmware configuration from the Template you created.

You can then solve your relay issue by adding this line to your void setup, immediately after the pinMode statement…

Virtualwrite(2, LOW);

You may also want to read the documentation about the use of BLYNK_CONNECTED() and Blynk.syncVirtual(vPin) to synchronise your relay with the widget in your dashboard.

Pete.

Thank you so much Pete I really appreciate it. I will test it and update my Blynk to the newest version and you are right, I am using Blynk IoT and not Blynk Legacy like I’ve mentioned. Sorry for that mistake. I really hope I can get this running without it restarting the board to re-connect to the WiFi network .
Thanks again,

Josh

Hi Pete I’ve updated the Blynk to the latest version but it still has to code where it’ll restart the ESP when it encounters an error (which is when it loses connection to the wifi

[Unformatted code removed by moderator]

This is in the ConfigMode.h tab at the bottom.

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.

Pete.

BlynkState::set(MODE_CONNECTING_NET);
}

void enterError() {
BlynkState::set(MODE_ERROR);

unsigned long timeoutMs = millis() + 10000;
while (timeoutMs > millis() || g_buttonPressed)
{
delay(10);
app_loop();
if (!BlynkState::is(MODE_ERROR)) {
return;
}
}
DEBUG_PRINT(“Restarting after error.”);
delay(10);

restartMCU();
}

Yes, as I said before, that’s what the Edgent sketch does…

If you’ve read ther rest of what I said in that post, and made the appropriate changes, then restarting won’t be an issue.

Pete.

Ok Pete I’m pretty new at this so that’s why I’m just clarifying and making sure. I appreciate the patience !
So I need to add the change after the void as you mentioned ?

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

  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
}

You need to…

Then…

But judging by the snippet of code that you’ve posted, it seems that you didn’t do this bit either…

Pete.

1 Like