Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 66 additions & 32 deletions libraries/WiFiS3/examples/WiFiWebClient/WiFiWebClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@

#include "arduino_secrets.h"

#define MaximumConnections 2

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)

int connectionCount = 0;
bool clientConnected = false;
int status = WL_IDLE_STATUS;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
Expand Down Expand Up @@ -62,30 +67,19 @@ void setup() {
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

printWifiStatus();

Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}

// 3 second wait for connection
client.setTimeout(3000);
}

void connectToWifi() {
if (status != WL_IDLE_STATUS)
return;

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
}

/* just wrap the received data up to 80 columns in the serial print*/
Expand All @@ -109,16 +103,56 @@ void read_response() {
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
read_response();
// do some processing
Serial.println("loop processing");

// only allowed to connect n times
if (connectionCount >= MaximumConnections) {
delay(2000);
return;
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
//connect and wait for connection to be made
connectToWifi();
status = WiFi.isConnected();

// do nothing forevermore:
while (true);
if (status == WL_CONNECTING) {
Serial.println("Connecting to wifi");
delay(200);
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WL_CONNECTING state is checked but there's no return statement after the delay. This means the code will continue to execute the if (status == WL_CONNECTED) block on line 125 in the same loop iteration, even when still connecting. Add a return statement after the delay on line 121 to prevent continuing execution when in the connecting state.

Suggested change
delay(200);
delay(200);
return;

Copilot uses AI. Check for mistakes.
}

Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WL_CONNECT_FAILED state is not handled. If the WiFi connection fails (either immediately or after timeout), the code doesn't reset the status to WL_IDLE_STATUS to allow retry. Consider adding a check: if (status == WL_CONNECT_FAILED) { Serial.println("WiFi connection failed!"); status = WL_IDLE_STATUS; return; } before the WL_CONNECTED check.

Suggested change
// Handle WiFi connection failure
if (status == WL_CONNECT_FAILED) {
Serial.println("WiFi connection failed!");
status = WL_IDLE_STATUS;
return;
}

Copilot uses AI. Check for mistakes.
// If connected to Wifi then send a request to a server
if (status == WL_CONNECTED) {
Serial.println("Connected to WiFi");
printWifiStatus();

Serial.println("\nStarting connection to server...");
clientConnected = client.connect(server, 80);

if (clientConnected) {
connectionCount++;

// if you get a connection, report back via serial:
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();

Serial.println("Reading response");
read_response();

if (clientConnected) {
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
status = WL_IDLE_STATUS;
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line uses tabs for indentation while the rest of the file uses spaces. This creates inconsistent formatting. Please use spaces to match the surrounding code style.

Suggested change
status = WL_IDLE_STATUS;
status = WL_IDLE_STATUS;

Copilot uses AI. Check for mistakes.
}
Comment on lines +146 to +153
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition if (clientConnected) on line 146 is redundant. At this point, we're already inside the if (clientConnected) block from line 132. This condition will always be true here. This redundant check should be removed, and the code inside should be dedented accordingly.

Suggested change
if (clientConnected) {
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
status = WL_IDLE_STATUS;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
status = WL_IDLE_STATUS;

Copilot uses AI. Check for mistakes.
}
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the server connection fails, the status is not reset. This means the code will stay in the WL_CONNECTED block and keep trying to connect to the server on every loop iteration. While the if (clientConnected) block won't execute, the loop will still continuously attempt to connect without the proper state management. Consider adding an else clause that handles the connection failure and potentially resets the status or adds a delay.

Suggested change
}
}
} else {
// Handle server connection failure
Serial.println("Failed to connect to server.");
status = WL_IDLE_STATUS;
delay(1000); // Add a delay to avoid rapid retries

Copilot uses AI. Check for mistakes.
}
}
}

Expand Down
96 changes: 64 additions & 32 deletions libraries/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@

#include "arduino_secrets.h"

// maximum number of times the uri will be checked
#define MaximumConnections 2

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)


int connectionCount = 0;
bool clientConnected = false;
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable clientConnected is declared but never used in this example. Consider removing it to keep the code clean.

Suggested change
bool clientConnected = false;

Copilot uses AI. Check for mistakes.
int status = WL_IDLE_STATUS;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
Expand All @@ -33,7 +40,7 @@ WiFiSSLClient client;
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.begin(9600);
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The serial baud rate has been changed from 115200 to 9600. This change appears unrelated to the non-blocking WiFi connection feature and slows down serial communication significantly. Unless there's a specific reason for this change, consider reverting to 115200 to maintain consistency with other examples and provide faster serial debugging.

Suggested change
Serial.begin(9600);
Serial.begin(115200);

Copilot uses AI. Check for mistakes.
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Expand All @@ -50,30 +57,18 @@ void setup() {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network.
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

printWifiStatus();
// 3 second wait for connection
client.setTimeout(3000);
}

Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
void connectToWifi() {
if (status != WL_IDLE_STATUS)
return;

if (client.connect(server, 443)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET / HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
}

/* just wrap the received data up to 80 columns in the serial print*/
Expand All @@ -97,17 +92,54 @@ void read_response() {
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
read_response();
Serial.println("loop processing");

// only allowed to connect n times
if (connectionCount >= MaximumConnections) {
delay(2000);
return;
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// Connect to WiFi if not already connected
connectToWifi();
status = WiFi.isConnected();

// do nothing forevermore:
while (true);
}
if (status == WL_CONNECTING) {
Serial.println("Connecting to wifi");
delay(200);
return;
}

Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WL_CONNECT_FAILED state is not handled. If the WiFi connection fails (either immediately or after timeout), the code doesn't reset the status to WL_IDLE_STATUS to allow retry. Consider adding a check: if (status == WL_CONNECT_FAILED) { Serial.println("WiFi connection failed!"); status = WL_IDLE_STATUS; return; } before the WL_CONNECTED check.

Suggested change
if (status == WL_CONNECT_FAILED) {
Serial.println("WiFi connection failed!");
status = WL_IDLE_STATUS;
return;
}

Copilot uses AI. Check for mistakes.
// If connected to Wifi then send a request to a server
if (status == WL_CONNECTED) {
Serial.println("Connected to WiFi");
printWifiStatus();

Serial.println("\nStarting connection to server...");

if (client.connect(server, 443)) {
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WiFiSSLClient connects to server over TLS without any server certificate validation or hostname verification. An on-path attacker (e.g., rogue AP on local network) can perform a MITM and intercept/modify traffic because the client does not verify the peer; fix by loading and enforcing a trusted CA or server certificate/fingerprint before client.connect, e.g., by calling the appropriate API to set a root CA or pinned certificate and validating the hostname.

Copilot uses AI. Check for mistakes.
connectionCount++;

Serial.println("connected to server");

// Make HTTP request
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();

Serial.println("Reading response");
read_response();

Serial.println("disconnecting from server.");
client.stop();

// Reset status so we don't immediately reconnect
status = WL_IDLE_STATUS;
} else {
Serial.println("Connection to server failed!");
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the server connection fails, the status is not reset to WL_IDLE_STATUS. This means the code will continuously stay in the WL_CONNECTED block and keep trying to connect to the server on every loop iteration without any delay, potentially causing rapid repeated connection attempts. Add status = WL_IDLE_STATUS; after the error message to reset the state.

Suggested change
Serial.println("Connection to server failed!");
Serial.println("Connection to server failed!");
status = WL_IDLE_STATUS;

Copilot uses AI. Check for mistakes.
}
}
}

/* -------------------------------------------------------------------------- */
Expand Down
24 changes: 13 additions & 11 deletions libraries/WiFiS3/src/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ void ModemClass::begin(int badurate, int retry){
_serial->begin(badurate);
string res = "";
_serial->flush();

unsigned long modemTimeout = _timeout;
modem.timeout(500);
while(!beginned && retry > 0) {
beginned = modem.write(string(PROMPT(_SOFTRESETWIFI)),res, "%s" , CMD(_SOFTRESETWIFI));
retry -= 1;
}
modem.timeout(MODEM_TIMEOUT);
modem.timeout(modemTimeout);
}
}

Expand Down Expand Up @@ -186,7 +188,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
unsigned long start_time = millis();
while(state != at_parse_state_t::Completed) {

if(millis() - start_time > _timeout) {
if(millis() - start_time > _readTimeout) {
res = Timeout;
break;
}
Expand Down Expand Up @@ -293,15 +295,15 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
* in data_res contains the length of the next token
*/

if(c == '|') { // sized read, the previous parameter is the length
sized_read_size = atoi(data_res.c_str());
data_res.clear();
if (sized_read_size != 0) {
state = at_parse_state_t::Sized;
} else {
state = at_parse_state_t::Res;
}
} else if(c == '\r') {
if(c == '|') { // sized read, the previous parameter is the length
sized_read_size = atoi(data_res.c_str());
data_res.clear();
if (sized_read_size != 0) {
state = at_parse_state_t::Sized;
} else {
state = at_parse_state_t::Res;
}
} else if(c == '\r') {
state = at_parse_state_t::ResWaitLF;
} else if(c == '\n') {
state = at_parse_state_t::Res;
Expand Down
Loading
Loading