[SOLVED] Bad usage of control structure using WiFi library

Am not sure if it’s my control structure that fails or whether i’m not using the WiFi library correctly. ( #include <ESP8266WiFi.h> )

i started off by using the Example sketch “WiFiWebServer.ino”
and then tried to customize the http-GET commands to pass parameters to control some servos.

i have succeeded in converting the example code but cannot get it to continually loop moving the servos, using the last command sent by the client.

the general flow of the code is asf;

the ORIGINAL example code.

...
...

void loop() {

WiFiClient client = server.available();
if (!client) {
  return;
}

...

// process client request

...

// send response to client

...
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed

}   // == END loop() ==

i then edited the ‘process client request’ section to set parameters for the servo movements and placed the function to move the servos at the end of the sketch, just before the end of loop()

this works okay, but the servo only does one cycle of movement and stops until the client sends a new command/request.

what i want to do is for the servo movements to keep continuing using the last command/request.

so i changed the structure like this;

...
...

void loop() {

WiFiClient client = server.available();

do {
  DOservo();
} while (!client)


...

// process client request

...

// send response to client

...
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed

}   // == END loop() ==

but then, a client request (client=true) will NOT break out of the do..while loop - i’m not sure why ?

another question is,
What does the return in the original if(!client) {} loop do ?
If it “does nothing” - wouldn’t just an empty line work the same ?
Am guessing it passes control to just before the if loop, which might be the difference compared to the do…while loop.

What does the return in the original if(!client) {} loop do ?
If it "does nothing" - wouldn't just an empty line work the same ?
Am guessing it passes control to just before the if loop, which might be the difference compared to the do..while loop.

Yes, the return is to the start of loop().

The doWhile loop never updates the status of client.

Place DOservo() as the first thing in loop().

cattledog:
Yes, the return is to the start of loop().

ahh, i see - even that far back then - thanks for that !

cattledog:
The doWhile loop never updates the status of client.

i kind of figured the program flow had to get back to the WiFiClient client = server.available(); but wasn’t sure how to send the program flow back there from inside the do…while loop.

cattledog:
Place DOservo() as the first thing in loop().

but wouldn’t that still only execute once while the flow loops waiting for (client) to be true ?

What is DOservo() doing/supposed to do? We can't (politely) tell you where to put it unless we know what it is supposed to do.

but wouldn't that still only execute once while the flow loops waiting for (client) to be true ?

if (!client) {
  return;
}

The return from this is to the start of loop(). If DOservo() is placed there, it will be executed repeatedly while there is no new data from the client.

It would take less time to try the placement, then to wait for an answer from the forum. :slight_smile:

cattledog:

if (!client) {

return;
}




The **return from this is to the start** of loop(). If DOservo() is placed there, it will be executed repeatedly while there is no new data from the client.

yeah, you already said it, and it finally sank in to my brain just now !

cattledog:
It would take less time to try the placement, then to wait for an answer from the forum. :slight_smile:

indeed - infact i had tried several (even more convoluted) options with do..while in the if(!client) loop which obviously failed.

the key was understanding what return did !

Thanks again !!