ESP8266-12E WiFi WebServer - execute instructions while idle

Hello there, I'm using 2 ESP8266-12E's to handle my small system.

First of them - WiFi Server and the second one - WiFi Client. Both are connected to the same router in my home. I am programming ESP's with Arduino IDE, using ESP8266WiFi.h library. My server program is based on WiFiServer sample sketch.

The communication works like a charm, but Server receives messages from second ESP8266 very rarely. I'd like my Server make some instructions not only after it got some connection but also while it is waiting for the next one. Instructions are very simple so I didn't put them here.

I know it waits all the time for connection and then starts to execute rest of the code but I have to make some operation during it is idle and of course do not mis new connections. I was thinking that I could force it to skip first few instructions: if(!client) and "while(!client.available())" but I wonder if it will work properly later. Is there any other way to make it work?

Here is main loop. Sory for my Could you help me to make ESP work while idle and be able to accept any new connection?

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

// Wait until the client sends some data

Serial.println("new client"); while(!client.available()){ delay(1); }

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

Serial.print("Client disconnected");

// The client will actually be disconnected // when the function returns and 'client' object is detroyed }

Is it gonna be ok to put my instructions here?

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

HERE ARE MY INSTRUCTIONS WHILE IDLE :) return; } [....]

I know it waits all the time for connection and then starts to execute rest of the code but I have to make some operation during it is idle and of course do not mis new connections.

“It” does NOT wait. It simply, normally, has nothing to do until there is a client to process data for.

Is it gonna be ok to put my instructions here?

Yes, but that is not the normal way.

Usually, you structure the code to do something when there is something to do. You do NOT normally just return if there is nothing to do.

void loop()
{
    WiFiClient client = server.available();
    if(client)
    {
       // Bow to the client's demands
    }
    else
    {
       // No client making outrageous demands. Can do other stuff...
    }
}