Skip to content

Commit 7350bbb

Browse files
committed
Update client samples to allow normal processing in loop whilst waiting for connection to wifi
1 parent 99df703 commit 7350bbb

File tree

2 files changed

+129
-63
lines changed

2 files changed

+129
-63
lines changed

libraries/WiFiS3/examples/WiFiWebClient/WiFiWebClient.ino

+66-33
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626

2727
#include "arduino_secrets.h"
2828

29+
#define MaximumConnections 1
30+
2931
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
3032
char ssid[] = SECRET_SSID; // your network SSID (name)
3133
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
3234
int keyIndex = 0; // your network key index number (needed only for WEP)
3335

36+
int connectionCount = 0;
37+
bool clientConnected = false;
3438
int status = WL_IDLE_STATUS;
39+
3540
// if you don't want to use DNS (and reduce your sketch size)
3641
// use the numeric IP instead of the name for the server:
3742
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
@@ -46,7 +51,7 @@ WiFiClient client;
4651
void setup() {
4752
/* -------------------------------------------------------------------------- */
4853
//Initialize serial and wait for port to open:
49-
Serial.begin(9600);
54+
Serial.begin(115200);
5055
while (!Serial) {
5156
; // wait for serial port to connect. Needed for native USB port only
5257
}
@@ -62,30 +67,19 @@ void setup() {
6267
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
6368
Serial.println("Please upgrade the firmware");
6469
}
65-
66-
// attempt to connect to WiFi network:
67-
while (status != WL_CONNECTED) {
68-
Serial.print("Attempting to connect to SSID: ");
69-
Serial.println(ssid);
70-
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
71-
status = WiFi.begin(ssid, pass);
72-
73-
// wait 10 seconds for connection:
74-
delay(10000);
75-
}
76-
77-
printWifiStatus();
78-
79-
Serial.println("\nStarting connection to server...");
80-
// if you get a connection, report back via serial:
81-
if (client.connect(server, 80)) {
82-
Serial.println("connected to server");
83-
// Make a HTTP request:
84-
client.println("GET /search?q=arduino HTTP/1.1");
85-
client.println("Host: www.google.com");
86-
client.println("Connection: close");
87-
client.println();
88-
}
70+
71+
// 3 second wait for connection
72+
client.setConnectionTimeout(3000);
73+
}
74+
75+
void connectToWifi() {
76+
if (status != WL_IDLE_STATUS)
77+
return;
78+
79+
Serial.print("Attempting to connect to SSID: ");
80+
Serial.println(ssid);
81+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
82+
status = WiFi.begin(ssid, pass);
8983
}
9084

9185
/* just wrap the received data up to 80 columns in the serial print*/
@@ -109,16 +103,55 @@ void read_response() {
109103
/* -------------------------------------------------------------------------- */
110104
void loop() {
111105
/* -------------------------------------------------------------------------- */
112-
read_response();
106+
// do some processing
107+
Serial.println("loop processing");
108+
109+
// only allowed to connect n times
110+
if (connectionCount >= MaximumConnections) {
111+
delay(300);
112+
return;
113+
}
113114

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

120-
// do nothing forevermore:
121-
while (true);
119+
if (status == WL_CONNECTING) {
120+
Serial.println("Connecting to wifi");
121+
delay(200);
122+
}
123+
124+
// If connected to Wifi then send a request to a server
125+
if (status == WL_CONNECTED) {
126+
Serial.println("Connected to WiFi");
127+
printWifiStatus();
128+
129+
Serial.println("\nStarting connection to server...");
130+
clientConnected = client.connect(server, 80);
131+
132+
if (clientConnected) {
133+
connectionCount++;
134+
135+
// if you get a connection, report back via serial:
136+
Serial.println("connected to server");
137+
// Make a HTTP request:
138+
client.println("GET /search?q=arduino HTTP/1.1");
139+
client.println("Host: www.google.com");
140+
client.println("Connection: close");
141+
client.println();
142+
143+
Serial.println("Reading response");
144+
read_response();
145+
146+
if (clientConnected) {
147+
// if the server's disconnected, stop the client:
148+
if (!client.connected()) {
149+
Serial.println();
150+
Serial.println("disconnecting from server.");
151+
client.stop();
152+
}
153+
}
154+
}
122155
}
123156
}
124157

libraries/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino

+63-30
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414

1515
#include "arduino_secrets.h"
1616

17+
#define MaximumConnections 1
18+
1719
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
1820
char ssid[] = SECRET_SSID; // your network SSID (name)
1921
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
2022

23+
24+
int connectionCount = 0;
25+
bool clientConnected = false;
2126
int status = WL_IDLE_STATUS;
27+
2228
// if you don't want to use DNS (and reduce your sketch size)
2329
// use the numeric IP instead of the name for the server:
2430
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
@@ -50,30 +56,18 @@ void setup() {
5056
Serial.println("Please upgrade the firmware");
5157
}
5258

53-
// attempt to connect to WiFi network:
54-
while (status != WL_CONNECTED) {
55-
Serial.print("Attempting to connect to SSID: ");
56-
Serial.println(ssid);
57-
// Connect to WPA/WPA2 network.
58-
status = WiFi.begin(ssid, pass);
59-
60-
// wait 10 seconds for connection:
61-
delay(10000);
62-
}
63-
64-
printWifiStatus();
59+
// 3 second wait for connection
60+
modem.readTimeout(3000);
61+
}
6562

66-
Serial.println("\nStarting connection to server...");
67-
// if you get a connection, report back via serial:
63+
void connectToWifi() {
64+
if (status != WL_IDLE_STATUS)
65+
return;
6866

69-
if (client.connect(server, 443)) {
70-
Serial.println("connected to server");
71-
// Make a HTTP request:
72-
client.println("GET / HTTP/1.1");
73-
client.println("Host: www.google.com");
74-
client.println("Connection: close");
75-
client.println();
76-
}
67+
Serial.print("Attempting to connect to SSID: ");
68+
Serial.println(ssid);
69+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
70+
status = WiFi.begin(ssid, pass);
7771
}
7872

7973
/* just wrap the received data up to 80 columns in the serial print*/
@@ -97,16 +91,55 @@ void read_response() {
9791
/* -------------------------------------------------------------------------- */
9892
void loop() {
9993
/* -------------------------------------------------------------------------- */
100-
read_response();
94+
// do some processing
95+
Serial.println("loop processing");
96+
97+
// only allowed to connect n times
98+
if (connectionCount >= MaximumConnections) {
99+
delay(300);
100+
return;
101+
}
101102

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

108-
// do nothing forevermore:
109-
while (true);
107+
if (status == WL_CONNECTING) {
108+
Serial.println("Connecting to wifi");
109+
delay(200);
110+
}
111+
112+
// If connected to Wifi then send a request to a server
113+
if (status == WL_CONNECTED) {
114+
Serial.println("Connected to WiFi");
115+
printWifiStatus();
116+
117+
Serial.println("\nStarting connection to server...");
118+
clientConnected = client.connect(server, 80);
119+
120+
if (clientConnected) {
121+
connectionCount++;
122+
123+
// if you get a connection, report back via serial:
124+
Serial.println("connected to server");
125+
// Make a HTTP request:
126+
client.println("GET /search?q=arduino HTTP/1.1");
127+
client.println("Host: www.google.com");
128+
client.println("Connection: close");
129+
client.println();
130+
131+
Serial.println("Reading response");
132+
read_response();
133+
134+
if (clientConnected) {
135+
// if the server's disconnected, stop the client:
136+
if (!client.connected()) {
137+
Serial.println();
138+
Serial.println("disconnecting from server.");
139+
client.stop();
140+
}
141+
}
142+
}
110143
}
111144
}
112145

0 commit comments

Comments
 (0)