Establish wifi connection "in background"

I am using an ESP8266 for my PoolRobot.

The old one had a broken circuit board… nothing we cant fix eh?
It finally works great but there is one problem.

I have to start the cleaning program outside the water because as soon as I put the robot into the water it of course loses wifi connection.

It then repeats some programms like turning pump on, accelerate, drive forward, break, turn around, turn pump off, etc.

I have a function that checks the online status regularly and tries to reconnect if not online or not connected to the blynk server.

It looks like this:

void CheckConnection()
{
  if (!Blynk.connected())
  {
    if (WiFi.status() != WL_CONNECTED)
    {
      Serial.println("Connecting to wifi...");

      WiFi.begin(ssid, pass);

      unsigned int counter = 0;
      while (WiFi.status() != WL_CONNECTED)
      {
        counter++;
        if (counter >= 5000) break;
        delay(1);
      }
      if (WiFi.status() != WL_CONNECTED)
      {
        Serial.println("Wifi connection failed!");
      }  
    }

    if ( WiFi.status() == WL_CONNECTED && !Blynk.connected() )
    {
      Serial.println("Establish SSL encrypted connection to the Blynk server...");
      Blynk.connect();
      if (!Blynk.connected())
      {
        Serial.println("Connecting failed!");
      }
    }
  }
}

While the robot is in water, it has no wifi connection and calls the CheckConnection() every 5 seconds .

This function tires to connect to my wifi for 5 seconds (but it cant find it under water).
So it blocks my main program for doing the right things and causing the robot to drive up the walls instead of turning back.

Is there a method to reconnect to wifi “in background” with interrupts or anything that wont block the rest of my program?

I believe that while connecting to wifi/Blynk blocking in inevitable. You can lessen this time by adding a timeout to the Blynk.connect(); function, but it will still block for that period none the less.

You may be able to use an ESP32, and utilize its dual cores. Separating the critical control commands, and the wifi/blynk connection stuff.

You may also try increasing the interval in which it tries to reconnect to the wifi, say every 30 seconds to 1 minute, instead of every 5 seconds.

Is it critical that your robot re-connects to WiFi and Blynk within a few seconds of it finishing its cycle?
If not, then reduce the frequency of the WiFi connection attempts.

You may also find that you get quicker reconnections if you assign a static IP address to the ESP. This cuts out the DHCP negotiation process, which may be very quick if your router still has a routing table entry for your device, but most routers flush their routing table every few minutes.

Pete.

Thanks for your answers.

@Toro_Blanco:
The Blynk.conntect is not the slowest here. The

  while (WiFi.status() != WL_CONNECTED)
  {
    counter++;
    if (counter >= 5000) break;
    delay(1);
  }

is the problem.

Sadly an ESP32 is no option because the WeMos D1 mini hardly fitted into the robot (will all the other stuff of course).

Thanks also Pete for your suggestion. I will give it a try but it only affects the connection time if wifi is in range.

Also the cycle is non blocking. So it checks all the time during the cycles.
I will look for pauses of the cleaning cycle to only trying to connect to wifi on the non criticyl parts… but its not very good coding practice ^^

Thanks so far!

I Haven’t tried these myself, but they seem to have a similar footprint to the Wemos D1 Mini.

https://www.amazon.com/ACROBOTIC-Arduino-NodeMCU-Raspberry-MiniKit/dp/B01LBOJUI6

I’m still not clear why you need to reconnect to Wi-Fi whilst the robot is in mid-cycle. Surely if the robot is in mid cycle then it won’t be in Wi-Fi range as it will be underwater?

I’m not sure how long it takes for the device to attempt to, and fail to connect, to Wi-Fi but asking it to try this 5000 times before it quits seems to be a significant overkill.

Pete.

@Toro_Blanco: wow nice one! Didnt know this was existing. Its a little different but I will order one and look if it fits

@Pete Knight:
The robot needs about 35s for one cycle.
Its not a must, but its better if the robot is availilble as soon as possible to turn it off or get infor via terminal to see all the debug messages :wink:

It takes about 4000ms to connect to wifi.
Sometimes less than 3000 sometimes close to 5000.
My function just makes sure it doesnt use much more than 5000ms and breaks as soon as its connected.
If you have a better idea feel free to help me out :slight_smile: