Skip to content

Commit 90fb863

Browse files
committed
better Client.stop() handling in WebClientRepeating and WifiWebClientRepeating examples
1 parent c2e9860 commit 90fb863

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

libraries/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
1515
created 19 Apr 2012
1616
by Tom Igoe
17+
modified 21 Jan 2014
18+
by Federico Vanzati
1719
1820
http://arduino.cc/en/Tutorial/WebClientRepeating
1921
This code is in the public domain.
@@ -30,7 +32,7 @@ byte mac[] = {
3032
};
3133
// fill in an available IP address on your network here,
3234
// for manual configuration:
33-
IPAddress ip(10, 0, 0, 20);
35+
IPAddress ip(192, 168, 1, 177);
3436

3537
// fill in your Domain Name Server address here:
3638
IPAddress myDns(1, 1, 1, 1);
@@ -39,15 +41,19 @@ IPAddress myDns(1, 1, 1, 1);
3941
EthernetClient client;
4042

4143
char server[] = "www.arduino.cc";
44+
//IPAddress server(64,131,82,241);
4245

4346
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
44-
boolean lastConnected = false; // state of the connection last time through the main loop
45-
const unsigned long postingInterval = 60L * 1000L; // delay between updates, in milliseconds
47+
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
4648
// the "L" is needed to use long type numbers
4749

4850
void setup() {
4951
// start serial port:
5052
Serial.begin(9600);
53+
while (!Serial) {
54+
; // wait for serial port to connect. Needed for Leonardo only
55+
}
56+
5157
// give the ethernet module time to boot up:
5258
delay(1000);
5359
// start the Ethernet connection using a fixed IP address and DNS server:
@@ -63,29 +69,23 @@ void loop() {
6369
// purposes only:
6470
if (client.available()) {
6571
char c = client.read();
66-
Serial.print(c);
67-
}
68-
69-
// if there's no net connection, but there was one last time
70-
// through the loop, then stop the client:
71-
if (!client.connected() && lastConnected) {
72-
Serial.println();
73-
Serial.println("disconnecting.");
74-
client.stop();
72+
Serial.write(c);
7573
}
7674

77-
// if you're not connected, and ten seconds have passed since
78-
// your last connection, then connect again and send data:
79-
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
75+
// if ten seconds have passed since your last connection,
76+
// then connect again and send data:
77+
if (millis() - lastConnectionTime > postingInterval) {
8078
httpRequest();
8179
}
82-
// store the state of the connection for next time through
83-
// the loop:
84-
lastConnected = client.connected();
80+
8581
}
8682

8783
// this method makes a HTTP connection to the server:
8884
void httpRequest() {
85+
// close any connection before send a new request.
86+
// This will free the socket on the WiFi shield
87+
client.stop();
88+
8989
// if there's a successful connection:
9090
if (client.connect(server, 80)) {
9191
Serial.println("connecting...");
@@ -102,11 +102,7 @@ void httpRequest() {
102102
else {
103103
// if you couldn't make a connection:
104104
Serial.println("connection failed");
105-
Serial.println("disconnecting.");
106-
client.stop();
107105
}
108106
}
109107

110108

111-
112-

libraries/WiFi/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
/*
2-
Repeating Wifi Web client
2+
Repeating Wifi Web Client
33
44
This sketch connects to a a web server and makes a request
55
using an Arduino Wifi shield.
66
77
Circuit:
8-
* Wifi shield attached to pins 10, 11, 12, 13
8+
* WiFi shield attached to pins SPI pins and pin 7
99
1010
created 23 April 2012
11-
modifide 31 May 2012
11+
modified 31 May 2012
1212
by Tom Igoe
13+
modified 13 Jan 2014
14+
by Federico Vanzati
1315
1416
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
1517
This code is in the public domain.
1618
*/
1719

1820
#include <SPI.h>
1921
#include <WiFi.h>
20-
22+
2123
char ssid[] = "yourNetwork"; // your network SSID (name)
2224
char pass[] = "secretPassword"; // your network password
2325
int keyIndex = 0; // your network key Index number (needed only for WEP)
@@ -31,9 +33,8 @@ WiFiClient client;
3133
char server[] = "www.arduino.cc";
3234
//IPAddress server(64,131,82,241);
3335

34-
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
35-
boolean lastConnected = false; // state of the connection last time through the main loop
36-
const unsigned long postingInterval = 10 * 1000; // delay between updates, in milliseconds
36+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
37+
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
3738

3839
void setup() {
3940
//Initialize serial and wait for port to open:
@@ -76,33 +77,27 @@ void loop() {
7677
Serial.write(c);
7778
}
7879

79-
// if there's no net connection, but there was one last time
80-
// through the loop, then stop the client:
81-
if (!client.connected() && lastConnected) {
82-
Serial.println();
83-
Serial.println("disconnecting.");
84-
client.stop();
85-
}
86-
87-
// if you're not connected, and ten seconds have passed since
88-
// your last connection, then connect again and send data:
89-
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
80+
// if ten seconds have passed since your last connection,
81+
// then connect again and send data:
82+
if (millis() - lastConnectionTime > postingInterval) {
9083
httpRequest();
9184
}
92-
// store the state of the connection for next time through
93-
// the loop:
94-
lastConnected = client.connected();
85+
9586
}
9687

9788
// this method makes a HTTP connection to the server:
9889
void httpRequest() {
90+
// close any connection before send a new request.
91+
// This will free the socket on the WiFi shield
92+
client.stop();
93+
9994
// if there's a successful connection:
10095
if (client.connect(server, 80)) {
10196
Serial.println("connecting...");
10297
// send the HTTP PUT request:
10398
client.println("GET /latest.txt HTTP/1.1");
10499
client.println("Host: www.arduino.cc");
105-
client.println("User-Agent: arduino-ethernet");
100+
client.println("User-Agent: ArduinoWiFi/1.1");
106101
client.println("Connection: close");
107102
client.println();
108103

@@ -112,8 +107,6 @@ void httpRequest() {
112107
else {
113108
// if you couldn't make a connection:
114109
Serial.println("connection failed");
115-
Serial.println("disconnecting.");
116-
client.stop();
117110
}
118111
}
119112

@@ -136,7 +129,3 @@ void printWifiStatus() {
136129
}
137130

138131

139-
140-
141-
142-

0 commit comments

Comments
 (0)