Skip to content
Prev Previous commit
Next Next commit
Update examples and minor tweaks
  • Loading branch information
k3ldar committed Dec 4, 2025
commit 61b798fbe42b28b61bed8145ee3f1beaacfaa4be
8 changes: 4 additions & 4 deletions libraries/WiFiS3/examples/WiFiWebClient/WiFiWebClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "arduino_secrets.h"

#define MaximumConnections 1
#define MaximumConnections 2

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
Expand All @@ -51,7 +51,7 @@ WiFiClient client;
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Expand Down Expand Up @@ -108,7 +108,7 @@ void loop() {

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

Expand Down Expand Up @@ -149,7 +149,7 @@ void loop() {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
status = WL_CONNECTED;
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.
}
}
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
95 changes: 47 additions & 48 deletions libraries/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

#include "arduino_secrets.h"

#define MaximumConnections 1
// 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)
Expand All @@ -39,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 @@ -57,7 +58,7 @@ void setup() {
}

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

void connectToWifi() {
Expand Down Expand Up @@ -91,56 +92,54 @@ void read_response() {
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
// do some processing
Serial.println("loop processing");

// only allowed to connect n times
if (connectionCount >= MaximumConnections) {
delay(300);
return;
}
Serial.println("loop processing");

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

//connect and wait for connection to be made
connectToWifi();
status = WiFi.isConnected();
// Connect to WiFi if not already connected
connectToWifi();
status = WiFi.isConnected();

if (status == WL_CONNECTING) {
Serial.println("Connecting to wifi");
delay(200);
}
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...");
clientConnected = client.connect(server, 443);

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();
// 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
3 changes: 2 additions & 1 deletion libraries/WiFiS3/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ int CWifi::begin(const char* ssid, const char *passphrase) {
return WL_CONNECTING;
}


/* -------------------------------------------------------------------------- */
int CWifi::isConnected()
/* -------------------------------------------------------------------------- */
{
uint8_t current_status = status();

Expand Down
4 changes: 2 additions & 2 deletions libraries/WiFiS3/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CWifi {
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
void _sortAPlist(uint8_t num);
unsigned long _timeout;
unsigned long _start_connection_time;
unsigned long _start_connection_time = millis();
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.

Initializing _start_connection_time with millis() in the header file is problematic. In C++, non-static member initializers are evaluated at object construction time, not at compile time. This means millis() will be called when the global WiFi object is constructed (before main() runs), which could be before the Arduino framework is initialized, potentially returning 0 or an undefined value.

The constructor in WiFi.cpp correctly initializes this to 0. Remove the = millis() initializer from the header and rely solely on the constructor initialization.

Suggested change
unsigned long _start_connection_time = millis();
unsigned long _start_connection_time;
Copilot uses AI. Check for mistakes.
CAccessPoint access_points[WIFI_MAX_SSID_COUNT];
uint8_t _apsFound = 0;
std::string ssid;
Expand Down Expand Up @@ -449,7 +449,7 @@ class CWifi {

/**
* @brief Sets the timeout value for the WiFi connection.
*
*
* @param `timeout` The timeout value in milliseconds.
*/
void setTimeout(unsigned long timeout);
Expand Down
Loading