-->
Page 1 of 1

non blocking wifi client connection

PostPosted: Sun Aug 06, 2017 5:30 am
by Warthog
I have the following loop function:

Code: Select allvoid loop()
{
  // check if WLAN is connected
  if (WiFi.status() != WL_CONNECTED)
  {
    WiFiStart();
  }

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  unsigned long ultimeout = millis() + 250;
while (!client.available() && (millis() < ultimeout) )
  {
    delay(1);
  }
  if (millis() > ultimeout)
  {
    Serial.println("client connection time-out!");
    return;
  }

  // Read the first line of the request
  String sRequest = client.readStringUntil('\r');
  client.flush();

  if (sRequest == "")
  {
    Serial.println("empty request! - stopping client");
    client.stop();
    return;
  }
}


The loop waits for an incoming message over wifi. But I want to do something more in the meantime, e.g. getting data from a sensor every minute. And I need an interrupt pin for a wind sensor (reed relais).

The problem seems to be the while loop:
while (!client.available() && (millis() < ultimeout) )
{
delay(1);
}

How can I rewrite this section, that this is not blocking the main loop but also reading in the wifi-message and the sensor data?

Re: non blocking wifi client connection

PostPosted: Sun Aug 06, 2017 6:21 am
by andre_teprom
Just replace while condition by if condition.