Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Minimize blocking during multi-wifi reconnect #6

Closed
jjskaife opened this issue Jul 15, 2021 · 4 comments
Closed

Minimize blocking during multi-wifi reconnect #6

jjskaife opened this issue Jul 15, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@jjskaife
Copy link

jjskaife commented Jul 15, 2021

Is your feature request related to a problem? Please describe.

ESP8266

If wifi is lost, the device will wait CONFIG_TIMEOUT until attempting a reconnect. The reconnect (connectMultiWifi) attempts to reconnect a hard coded 10 times (with ~3 second delays).

The problem that I have for a remote IOT device (PID temperature controller, 1 second sampling frequency) is that the reconnect attempts are blocking. the amount of time stuck in reconnect can be significant (at least 60 seconds for minimum 2 reconnect attempts + 30second connect attempt after reset).

Describe the solution you'd like

I'd like the reconnect attempts (connectMultiWifi) to be adjustable (or less) to limit time spent blocking main loop. Currently this is hardcoded.

Describe alternatives you've considered

changing specific hardcoded attempts to a define.

Additional context

Add any other context or screenshots about the feature request here.

@khoih-prog
Copy link
Owner

khoih-prog commented Jul 15, 2021

Hi @jjskaife

Thanks for your interest in the library and your good research to understand the internal working of it.

I'll certainly make those parameters configurable in the next release, even the configurable parameters are already too many for normal users to handle ;-)

I also suggest that you redesign your code to prevent possible issue with any blocking code in the loop(), for example either

  1. Using ESP8266TimerInterrupt to read from IoT device or
  2. Using higher priority task to read from from IoT device and lower priority for Blynk.run() task.

Good Luck,

@khoih-prog khoih-prog added the enhancement New feature or request label Jul 15, 2021
@khoih-prog
Copy link
Owner

In the mean time, you can modify the library code as follows to test

#ifndef WIFI_MULTI_CONNECT_WAITING_MS
  // For ESP8266, this better be 3000 to enable connect the 1st time
  #define WIFI_MULTI_CONNECT_WAITING_MS       3000L  
#endif


#ifndef WIFI_MULTI_CONNECT_WAITING_TIMES
  #define WIFI_MULTI_CONNECT_WAITING_TIMES    10 
#endif
    

    uint8_t connectMultiWiFi()
    {
     
      uint8_t status;
      BLYNK_LOG1(BLYNK_F("Connecting MultiWifi..."));

      WiFi.mode(WIFI_STA);
      
      setHostname();
           
      int i = 0;
      status = wifiMulti.run();
      delay(WIFI_MULTI_CONNECT_WAITING_MS);

      while ( ( i++ < WIFI_MULTI_CONNECT_WAITING_TIMES ) && ( status != WL_CONNECTED ) )
      {
        status = wifiMulti.run();

        if ( status == WL_CONNECTED )
          break;
        else
          delay(WIFI_MULTI_CONNECT_WAITING_MS);
      }

      if ( status == WL_CONNECTED )
      {
        BLYNK_LOG2(BLYNK_F("WiFi connected after time: "), i);
        BLYNK_LOG4(BLYNK_F("SSID="), WiFi.SSID(), BLYNK_F(",RSSI="), WiFi.RSSI());
        BLYNK_LOG4(BLYNK_F("Channel="), WiFi.channel(), BLYNK_F(",IP="), WiFi.localIP() );
      }
      else
        BLYNK_LOG1(BLYNK_F("WiFi not connected"));

      return status;
    }

then specify somewhere in your code, for example

#define WIFI_MULTI_CONNECT_WAITING_MS       1000L
#define WIFI_MULTI_CONNECT_WAITING_TIMES    3

khoih-prog added a commit that referenced this issue Jul 15, 2021
### Releases v1.6.1

1. Add configurable connectMultiWiFi parameters. Check [Minimize blocking during multi-wifi reconnect #6](#6)
2. Update ESP8266_CORE_VERSION for ESP8266 core v3.0.1+
khoih-prog added a commit that referenced this issue Jul 15, 2021
### Releases v1.6.1

1. Add configurable connectMultiWiFi parameters. Check [Minimize blocking during multi-wifi reconnect #6](#6)
2. Update ESP8266_CORE_VERSION for ESP8266 core v3.0.1+
@khoih-prog
Copy link
Owner

Your contribution has been noted in Contributions and Thanks


Releases v1.6.1

  1. Add configurable connectMultiWiFi parameters. Check Minimize blocking during multi-wifi reconnect #6
  2. Update ESP8266_CORE_VERSION for ESP8266 core v3.0.1+

@jjskaife
Copy link
Author

thanks for the link to your ESP8266TimerInterrupt code. That looks like a great solution. I've been using the blynk timers and will try yours.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants