Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
99 changes: 66 additions & 33 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 1

///////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 All @@ -46,7 +51,7 @@ WiFiClient client;
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Expand All @@ -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.setConnectionTimeout(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,55 @@ void read_response() {
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
read_response();
// do some processing
Serial.println("loop processing");

// only allowed to connect n times
if (connectionCount >= MaximumConnections) {
delay(300);
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();
}
}
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
93 changes: 63 additions & 30 deletions libraries/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

#include "arduino_secrets.h"

#define MaximumConnections 1

///////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 Down Expand Up @@ -50,30 +56,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
modem.readTimeout(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,16 +91,55 @@ void read_response() {
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
read_response();
// do some processing
Serial.println("loop processing");

// only allowed to connect n times
if (connectionCount >= MaximumConnections) {
delay(300);
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_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, 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();
}
}
}
}
}

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