non blocking wifi client connection
Posted: Sun Aug 06, 2017 5:30 am
I have the following loop function:
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?
Code: Select all
void 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?