In the code below, the WiFiClient
object changes behavior depending on whether it is assigned to a locally scoped or non-locally scoped variable.
Locally scoped:
#include <WiFi.h>
char ssid[] = "[redacted]"; // your network SSID (name)
char pass[] = "[redacted]"; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(5000);
}
server.begin();
}
void loop()
{
WiFiClient client = server.available();
if (client) {
Serial.println("new client connected");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
//do something
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("client disconnected");
}
}
Output (when client connects):
// execution resumes here from blocking server.available() after a client connects ...
[INFO] A client connected to this server :
[PORT]: 58620
[IP]:192.168.2.36
new client connected
GET / HTTP/1.1
Host: 192.168.2.172
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,fil;q=0.8
Cookie: __guid=259319114.1308697201864418800.1719734660203.9114
Non-locally scoped:
#include <WiFi.h>
char ssid[] = "[redacted]"; // your network SSID (name)
char pass[] = "[redacted]"; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient client;
void setup()
{
Serial.begin(115200);
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(5000);
}
server.begin();
}
void loop()
{
client = server.available();
if (client) {
Serial.println("new client connected");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
//do something
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("client disconnected");
}
}
Output (when client connects):
// execution resumes here from blocking server.available() after a client connects ...
[INFO] A client connected to this server :
[PORT]: 51193
[IP]:192.168.2.36
new client connected
client disconnected
[INFO] A client connected to this server :
[PORT]: 51192
[IP]:192.168.2.36
new client connected
client disconnected
[INFO] A client connected to this server :
[PORT]: 51194
[IP]:192.168.2.36
new client connected
client disconnected
// ...continues indefinitely
According to Arduino's documentation for server.available()
, the behavior observed when WiFiClient
is not locally scoped isn't normal, and I've already reported the problem to the developers of the SDK I'm using. However, I'd like to understand, in general, why assigning an object within different scopes in C/C++ can cause such a change in behavior.
Aside from putting WiFiClient
in a globally scoped variable, as above, I also tried making it a class member. I also tried various things to try to get it to work, such as storing a pointer or reference to the WiFiClient
object, none of which worked.
I've mainly used interpreter-based languages and haven't yet encountered an issue like this. My previous experience conditioned me to believe that an object should behave relatively the same in different scopes (I'll admit that it took an embarrassing amount of time to narrow the issue down to changes in variable scope because of that expectation), so this is really new and fascinating to me.
Obviously, a more specific discussion of why what I experienced above occurred would require intimate knowledge of the Arduino framework and the SDK I'm using, so that's not what I'm asking. I'd really like to know more conceptually why such an issue can occur in the first place.
Additional detail: I forgot to mention that server.available()
is blocking — although there is a non-blocking mode, it isn't enabled here to further rule out the issue being caused by that mode. The issues occur on the very first trip through loop()
. Execution halts at WiFiClient client = server.available()
/client = server.available()
until a client connects. The Serial Monitor outputs I include start immediately after a client connects. I've also edited those outputs to make that clearer.
Best Answer
There are at least 2 differences (which are related) between the versions:
In the local version the following line:
calls the constructor of
WiFiClient
. But in the global version this line:calls the assignment operator (move/copy) which is potentially different.
The
loop
function is called in a loop by the Arduino system code.Every time is it called a new instance of by this line:
Then every time the
loop
function returns the scope ofclient
will also end and it will be destructed.This is different from the global version where the same instance of
client
is always used (but as mentioned above it is assigned every call ofloop
). Note that in the global case a temporary object is constructed for every assignment (and then destructed).Both differences (construction vs assignment, and multiple destructions of
client
) are actually aspects of the same cause - the local/global scope.