Skip to content

Commit 78b5e3c

Browse files
author
Tom Igoe
committed
Updated all Wifi web client and server examples to attempt continual reconnects to the SSID
1 parent e8118e2 commit 78b5e3c

File tree

7 files changed

+114
-102
lines changed

7 files changed

+114
-102
lines changed

WiFi/examples/ScanNetworks/ScanNetworks.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
void setup() {
2222
// initialize serial and wait for the port to open:
2323
Serial.begin(9600);
24-
while(!Serial) ;
2524

2625
// attempt to connect using WEP encryption:
2726
Serial.println("Initializing Wifi...");

WiFi/examples/WifiChatServer/WifiChatServer.ino

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
created 18 Dec 2009
1616
by David A. Mellis
17-
modified 12 Mar 2012
17+
modified 23 Apr 2012
1818
by Tom Igoe
1919
2020
*/
@@ -34,28 +34,32 @@ WiFiServer server(23);
3434
boolean alreadyConnected = false; // whether or not the client was connected previously
3535

3636
void setup() {
37-
// initialize serial:
37+
// start serial port:
3838
Serial.begin(9600);
39-
Serial.println("Attempting to connect to Wifi network...");
40-
Serial.print("SSID: ");
41-
Serial.println(ssid);
4239

43-
status = WiFi.begin(ssid, pass);
44-
if ( status != WL_CONNECTED) {
45-
Serial.println("Couldn't get a wifi connection");
46-
while(true);
40+
// attempt to connect to Wifi network:
41+
while ( status != WL_CONNECTED) {
42+
Serial.print("Attempting to connect to SSID: ");
43+
Serial.println(ssid);
44+
status = WiFi.begin(ssid, pass);
45+
// wait 10 seconds for connection:
46+
delay(10000);
4747
}
48-
else {
49-
server.begin();
50-
Serial.print("Connected to wifi.");
51-
printWifiStatus();
52-
}
48+
// start the server:
49+
server.begin();
50+
// you're connected now, so print out the status:
51+
printWifiStatus();
52+
// print the Wifi board/shield's IP address:
53+
Serial.print("My IP address: ");
54+
Serial.println(WiFi.localIP());
5355
}
56+
57+
5458
void loop() {
5559
// wait for a new client:
5660
WiFiClient client = server.available();
5761

58-
62+
5963
// when the client sends the first byte, say hello:
6064
if (client) {
6165
if (!alreadyConnected) {
@@ -95,3 +99,4 @@ void printWifiStatus() {
9599
Serial.println(" dBm");
96100
}
97101

102+

WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* Analog sensor attached to analog in 0
1616
* Wifi shield attached to pins 10, 11, 12, 13
1717
18-
created 13 March 2012
18+
created 13 Mar 2012
19+
modified 23 Apr 2012
1920
by Tom Igoe
2021
2122
This code is in the public domain.
@@ -45,21 +46,22 @@ boolean lastConnected = false; // state of the connection last t
4546
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
4647

4748
void setup() {
49+
// start serial port:
4850
Serial.begin(9600);
49-
Serial.println("Attempting to connect to Wifi network...");
50-
Serial.print("SSID: ");
51-
Serial.println(ssid);
52-
53-
status = WiFi.begin(ssid, pass);
54-
if ( status != WL_CONNECTED) {
55-
Serial.println("Couldn't get a wifi connection");
56-
// stop here and do nothing:
57-
while(true);
51+
52+
// attempt to connect to Wifi network:
53+
while ( status != WL_CONNECTED) {
54+
Serial.print("Attempting to connect to SSID: ");
55+
Serial.println(ssid);
56+
status = WiFi.begin(ssid, pass);
57+
// wait 10 seconds for connection:
58+
delay(10000);
5859
}
59-
else {
60-
Serial.println("Connected to wifi");
61-
printWifiStatus();
62-
}
60+
// you're connected now, so print out the status:
61+
printWifiStatus();
62+
// print the Wifi board/shield's IP address:
63+
Serial.print("My IP address: ");
64+
Serial.println(WiFi.localIP());
6365
}
6466

6567

WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
* Analog sensor attached to analog in 0
1919
* Wifi shield attached to pins 10, 11, 12, 13
2020
21-
created 16 March 2012
21+
created 16 Mar 2012
22+
modified 23 Apr 2012
2223
by Tom Igoe
2324
2425
This code is in the public domain.
@@ -50,21 +51,22 @@ boolean lastConnected = false; // state of the connection last t
5051
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
5152

5253
void setup() {
54+
// start serial port:
5355
Serial.begin(9600);
54-
Serial.println("Attempting to connect to Wifi network...");
55-
Serial.print("SSID: ");
56-
Serial.println(ssid);
5756

58-
status = WiFi.begin(ssid, pass);
59-
if ( status != WL_CONNECTED) {
60-
Serial.println("Couldn't get a wifi connection");
61-
// stop here and do nothing:
62-
while(true);
57+
// attempt to connect to Wifi network:
58+
while ( status != WL_CONNECTED) {
59+
Serial.print("Attempting to connect to SSID: ");
60+
Serial.println(ssid);
61+
status = WiFi.begin(ssid, pass);
62+
// wait 10 seconds for connection:
63+
delay(10000);
6364
}
64-
else {
65-
Serial.println("Connected to wifi");
66-
printWifiStatus();
67-
}
65+
// you're connected now, so print out the status:
66+
printWifiStatus();
67+
// print the Wifi board/shield's IP address:
68+
Serial.print("My IP address: ");
69+
Serial.println(WiFi.localIP());
6870
}
6971

7072
void loop() {
@@ -161,3 +163,4 @@ void printWifiStatus() {
161163
Serial.println(" dBm");
162164
}
163165

166+

WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Circuit:
1414
* WiFi shield attached to pins 10, 11, 12, 13
1515
16-
created 15 Mar 2012
16+
created 23 apr 2012
1717
by Tom Igoe
1818
1919
This code is in the public domain.
@@ -24,8 +24,8 @@
2424

2525
char ssid[] = "YourNetwork"; // your network SSID (name)
2626
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
27-
2827
int keyIndex = 0; // your network key Index number (needed only for WEP)
28+
2929
int status = WL_IDLE_STATUS; // status of the wifi connection
3030

3131
// initialize the library instance:
@@ -49,22 +49,25 @@ void setup() {
4949
// reserve space for the strings:
5050
currentLine.reserve(256);
5151
tweet.reserve(150);
52-
// initialize serial:
52+
// start serial port:
5353
Serial.begin(9600);
54-
Serial.println("Attempting to connect to WPA network...");
55-
56-
status = WiFi.begin(ssid, pass);
57-
if ( status != WL_CONNECTED) {
58-
Serial.println("Couldn't get a wifi connection");
59-
// stop here and do nothing:
60-
while(true);
54+
55+
// attempt to connect to Wifi network:
56+
while ( status != WL_CONNECTED) {
57+
Serial.print("Attempting to connect to SSID: ");
58+
Serial.println(ssid);
59+
status = WiFi.begin(ssid, pass);
60+
// wait 10 seconds for connection:
61+
delay(10000);
6162
}
62-
else {
63-
Serial.println("Connected to wifi");
64-
printWifiStatus();
65-
connectToServer();
66-
}
63+
// you're connected now, so print out the status:
64+
printWifiStatus();
65+
// print the Wifi board/shield's IP address:
66+
Serial.print("My IP address: ");
67+
Serial.println(WiFi.localIP());
68+
connectToServer();
6769
}
70+
6871
void loop()
6972
{
7073
if (client.connected()) {
@@ -147,3 +150,4 @@ void printWifiStatus() {
147150

148151

149152

153+

WiFi/examples/WifiWebClient/WifiWebClient.ino

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
This sketch connects to a website (http://www.google.com)
66
using a WiFi shield.
77
8-
This example is written for a network using WPA encryption. For
8+
This example is written for a network using WPA encryption. For
99
WEP or WPA, change the Wifi.begin() call accordingly.
10-
10+
1111
This example is written for a network using WPA encryption. For
1212
WEP or WPA, change the Wifi.begin() call accordingly.
1313
@@ -16,7 +16,7 @@
1616
1717
created 13 July 2010
1818
by dlf (Metodo2 srl)
19-
modified 13 Mar 2012
19+
modified 23 Apr 2012
2020
by Tom Igoe
2121
*/
2222

@@ -41,38 +41,37 @@ WiFiClient client;
4141

4242
void setup() {
4343
Serial.begin(9600);
44-
Serial.println("Attempting to connect to Wifi network...");
45-
Serial.print("SSID: ");
46-
Serial.println(ssid);
4744

48-
status = WiFi.begin(ssid, pass);
49-
if ( status != WL_CONNECTED) {
50-
Serial.println("Couldn't get a wifi connection");
51-
// stop here and do nothing:
52-
while(true);
45+
// attempt to connect to Wifi network:
46+
while ( status != WL_CONNECTED) {
47+
Serial.print("Attempting to connect to SSID: ");
48+
Serial.println(ssid);
49+
status = WiFi.begin(ssid, pass);
50+
// wait 10 seconds for connection:
51+
delay(10000);
5352
}
54-
else {
55-
Serial.println("Connected to wifi");
56-
printWifiStatus();
57-
Serial.println("\nStarting connection to server...");
58-
// if you get a connection, report back via serial:
59-
if (client.connect(server, 80)) {
60-
Serial.println("connected to server");
61-
// Make a HTTP request:
62-
client.println("GET /search?q=arduino HTTP/1.1");
63-
client.println("Host:www.google.com");
64-
client.println("Connection: close");
65-
client.println();
66-
}
53+
Serial.println("Connected to wifi");
54+
printWifiStatus();
55+
56+
Serial.println("\nStarting connection to server...");
57+
// if you get a connection, report back via serial:
58+
if (client.connect(server, 80)) {
59+
Serial.println("connected to server");
60+
// Make a HTTP request:
61+
client.println("GET /search?q=arduino HTTP/1.1");
62+
client.println("Host:www.google.com");
63+
client.println("Connection: close");
64+
client.println();
65+
6766
}
6867
}
6968

7069
void loop() {
7170
// if there are incoming bytes available
7271
// from the server, read them and print them:
73-
if (client.available()>0) {
72+
while (client.available()) {
7473
char c = client.read();
75-
Serial.print(c);
74+
Serial.write(c);
7675
}
7776

7877
// if the server's disconnected, stop the client:
@@ -94,7 +93,7 @@ void printWifiStatus() {
9493

9594
// print your WiFi shield's IP address:
9695
IPAddress ip = WiFi.localIP();
97-
Serial.print("IP Address: ");
96+
Serial.print("IP Address: ");
9897
Serial.println(ip);
9998

10099
// print the received signal strength:
@@ -107,3 +106,4 @@ void printWifiStatus() {
107106

108107

109108

109+

WiFi/examples/WifiWebServer/WifiWebServer.ino

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
1414
created 13 July 2010
1515
by dlf (Metodo2 srl)
16-
modified 20 Mar 2012
16+
modified 23 Apr 2012
1717
by Tom Igoe
1818
*/
19-
20-
2119
#include <SPI.h>
2220
#include <WiFi.h>
2321

@@ -31,22 +29,23 @@ int status = WL_IDLE_STATUS;
3129
WiFiServer server(80);
3230

3331
void setup() {
34-
// initialize serial:
32+
// start serial port:
3533
Serial.begin(9600);
36-
Serial.println("Attempting to connect to Wifi network...");
37-
Serial.print("SSID: ");
38-
Serial.println(ssid);
39-
40-
status = WiFi.begin(ssid, pass);
41-
if ( status != WL_CONNECTED) {
42-
Serial.println("Couldn't get a wifi connection");
43-
while(true);
34+
35+
// attempt to connect to Wifi network:
36+
while ( status != WL_CONNECTED) {
37+
Serial.print("Attempting to connect to SSID: ");
38+
Serial.println(ssid);
39+
status = WiFi.begin(ssid, pass);
40+
// wait 10 seconds for connection:
41+
delay(10000);
4442
}
45-
else {
46-
server.begin();
47-
Serial.print("Connected to wifi.");
48-
printWifiStatus();
49-
}
43+
server.begin();
44+
// you're connected now, so print out the status:
45+
printWifiStatus();
46+
// print the Wifi board/shield's IP address:
47+
Serial.print("My IP address: ");
48+
Serial.println(WiFi.localIP());
5049
}
5150

5251

0 commit comments

Comments
 (0)