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[] = ""; // your network SSID (name)
char pass[] = ""; // 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):
[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[] = ""; // your network SSID (name)
char pass[] = ""; // 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):
[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.