-->
Page 1 of 2

[SOLVED] How to re-connect Wifi with a non-blocking fashion?

PostPosted: Sat Oct 21, 2017 4:00 pm
by andre_teprom
I am doing a project of a vending machine system on which the main task is obviously to count the coins, but whenever the wireless connection is lost, the system keep blocked in a closed loop. I'm using the standard template present at the Webserver sketch:

Code: Select all  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print (".");
      }


I've thinking of using I/O interrupt in order to provide a quick call to the counting routine, or even exit the loop and return again, skipping a new attempt to reconnect if it is done. I mean: To instantiate the WiFi.status() just once, but seems like wont work.

Does anyone have faced to a similar issue, or have some insight on how to solve that ?

Re: How to re-connect Wifi with a non-blocking fashion ?

PostPosted: Sun Oct 22, 2017 7:29 am
by andre_teprom
Code: Select allvoid _wifiDisconnectHandler(const WiFiEventStationModeDisconnected& event){
// cleanup etc, prepare for re-connect
}


Ok, but the problem is what is within this event handler.
The only way I'm aware to restart wifi connection is by calling the WiFi.status() != WL_CONNECTED) loop.

Re: How to re-connect Wifi with a non-blocking fashion ?

PostPosted: Sun Oct 22, 2017 9:10 am
by rudy
andre_teprom wrote:The only way I'm aware to restart wifi connection is by calling the WiFi.status() != WL_CONNECTED) loop.

If you read the documentation you will find your answer.

http://arduino-esp8266.readthedocs.io/e ... mples.html

The process of connection, disconnection and printing messages is done in background



Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "********";
const char* password = "********";

WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;

bool ledState;


void setup()
{
  Serial.begin(115200);
  Serial.println();

  pinMode(LED_BUILTIN, OUTPUT);

  gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event)
  {
    Serial.print("Station connected, IP: ");
    Serial.println(WiFi.localIP());
  });

  disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
  {
    Serial.println("Station disconnected");
  });

  Serial.printf("Connecting to %s ...\n", ssid);
  WiFi.begin(ssid, password);
}


void loop()
{
  digitalWrite(LED_BUILTIN, ledState);
  ledState = !ledState;
  delay(250);
}

Re: How to re-connect Wifi with a non-blocking fashion ?

PostPosted: Sun Oct 22, 2017 10:47 am
by andre_teprom
Ok, thanks.

In fact, the procedure which effectivelly performs the (re)connecting task is the WiFi.begin().
It is clear now, but didn't notice before.