Skip to content

WifiS3 - Non blocking Wifi connect #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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);
}

// 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
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;
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);
}

// 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