Partial reconnection with Blynk after disconnection of WiFi

Hi, I have made a project of home automation that works both online as well as offline. So I can control things with Blynk which works only with internet and I can control through an IR remote which controls it in both online and offline mode, and the changes also get reflected in the app when online.

I have tweaked the enterError() function in ConfigMode.h file so that it runs my IR remote codes when there is no internet, and also try to reconnects with wifi without interrupting the IR remote functions.

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);
  enterConnectNet();
}

With these changes it works perfectly fine for past 1 year but recently some updates in Blynk library make it not properly working.

When my wifi goes off everything works perfectly fine with my IR remote, but when wifi gets on, it do reconnects with Blynk and also updates the changes made by IR remote in the app, but I cannot control it through Blynk app anymore until I power on and off manually.

Only one way connection is going on after reconnection as my ESP8266 can send things to Blynk server and updates the value but Blynk server cannot send things back to ESP8266. How can I fix this?? I have to manually power off and on everyday after turning on WiFi to make it work properly again.

A good solution is very much appreciated!! Thanks in advance

@BeamCruzer when you post code to the forum you need to copy/paste the text and put triple backticks at the beginning and end so that it displays correctly.
Triple backticks look like this…
```

Posting screenshots of code, compiler error messages or serial output doesn’t really work well.

Also, if you’re using the Blynk Edgent example and you want to add your own code into the sketch then it’s a bad idea to put this in the .h files, but it’s not really clear what changes you’ve made based just on your screenshot.

Do you need the Edgent dynamic provisioning functionality? If not then it would be far easier to start with a simpler static provisioning sketch.

Pete.

Firstly, I made the changes in code as you specified.

Secondly, I don’t want static or dynamic provisioning. I want to make my nodemcu perfectly connect with blynk after my wifi goes off and on, and also carry on some functions that works when offline

But there are no comments to indicate how this code snippet has been modified from the original Edgent example.

You have to have one or the other, otherwise the sketch won’t know your WiFi credentials or your Blynk Auth token.
If you don’t mind hard-coding these into your sketch (static provisioning) then you don’t need to use the Edgent example, so the process will be much easier, because the codebase is much simpler.

Pete.

Ok sorry for that, I am new in forum so I beg your pardon.

The code given below is the by default code in the edgent

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();
}

And this one is the code with changes made by me

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);
  enterConnectNet();
}

I only changed the restartMCU command to enterConnectNet so that instead of restarting it again tries to connect to blynk.

Actually I also initially coded without any provisioning and hard coded the credentials of wifi through code. But the thing is that it does not make my IR remote work properly when my there is no internet and it also fails to make connection with blynk after wifi gets turned on. So i changes it to static provisioning.

If you know a better way to do it without any provisioning, i would be happy to hear that

In that case you probably used Blynk.begin() which is a blocking function, so all code execution will stop at that point if the sketch can’t connect to either WiFi or the Blynk server.

For a non-Blocking Blynk connection you need to use Blynk.config() and Blynk.connect(), manage your WiFi connection manually, and have a re-connection routine for both WiFi and Blynk. Here’s an example…

I think you mean “so I changed to dynamic provisioning using Edgent”

As I explained earlier, you have to provision your device with the WiFi credentials and Blynk Auth token, otherwise it can’t authenticate to these systems. You should be looking at static provisioning, where these credentials are hard-coded into your sketch, rather than the complexity of dynamic provisioning via Edgent.

Pete.

True, I did the same thing.

Yeah, i meant that you got it right!

I am looking forward to it, i will give it a try hope it will work.
If not I will specify the issues.
Thank you for you help!!

The example is for the ESP32, so you’ll need to make some tweaks to the Blynk and WiFi libraries that are included.

Pete.

In the following example, if my connection is interrupted it will try to do a reconnection attempt 15 times every 30 seconds, and a successful reconnection will take around 5-6 seconds.

So, won’t my offline operation will be interrupted in the main code because there is a high probability that I will use my IR remote (offline operation) when the nodemcu is trying to connect to the internet and blynk?

Yes, of course, but this was just an example of how to correctly use Blynk.config() with a optional timeout, and Blynk.connect(), and a routine that periodically checks the WiFi and Blynk connections.

If you read the whole of the topic you’ll see that it was intended to prove that there wasn’t a bug in the Blynk library, and it would make no sense to wait for a longer period between the reconnection attempts.

You can incorporate this into you sketch in whatever format you wish, with whichever timing parameters work best for you. If you wish you could add some additional code which extends the reconnection timer if you have multiple consecutive failed connection attempts, if this is something that frequently occurs.

Pete.

Ok, thanks for your help!