1
1
/*
2
- Repeating Wifi Web client
2
+ Repeating Wifi Web Client
3
3
4
4
This sketch connects to a a web server and makes a request
5
5
using an Arduino Wifi shield.
6
6
7
7
Circuit:
8
- * Wifi shield attached to pins 10, 11, 12, 13
8
+ * WiFi shield attached to pins SPI pins and pin 7
9
9
10
10
created 23 April 2012
11
- modifide 31 May 2012
11
+ modified 31 May 2012
12
12
by Tom Igoe
13
+ modified 13 Jan 2014
14
+ by Federico Vanzati
13
15
14
16
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
15
17
This code is in the public domain.
16
18
*/
17
19
18
20
#include < SPI.h>
19
21
#include < WiFi.h>
20
-
22
+
21
23
char ssid[] = " yourNetwork" ; // your network SSID (name)
22
24
char pass[] = " secretPassword" ; // your network password
23
25
int keyIndex = 0 ; // your network key Index number (needed only for WEP)
@@ -31,9 +33,8 @@ WiFiClient client;
31
33
char server[] = " www.arduino.cc" ;
32
34
// IPAddress server(64,131,82,241);
33
35
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
37
38
38
39
void setup () {
39
40
// Initialize serial and wait for port to open:
@@ -76,33 +77,27 @@ void loop() {
76
77
Serial.write (c);
77
78
}
78
79
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) {
90
83
httpRequest ();
91
84
}
92
- // store the state of the connection for next time through
93
- // the loop:
94
- lastConnected = client.connected ();
85
+
95
86
}
96
87
97
88
// this method makes a HTTP connection to the server:
98
89
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
+
99
94
// if there's a successful connection:
100
95
if (client.connect (server, 80 )) {
101
96
Serial.println (" connecting..." );
102
97
// send the HTTP PUT request:
103
98
client.println (" GET /latest.txt HTTP/1.1" );
104
99
client.println (" Host: www.arduino.cc" );
105
- client.println (" User-Agent: arduino-ethernet " );
100
+ client.println (" User-Agent: ArduinoWiFi/1.1 " );
106
101
client.println (" Connection: close" );
107
102
client.println ();
108
103
@@ -112,8 +107,6 @@ void httpRequest() {
112
107
else {
113
108
// if you couldn't make a connection:
114
109
Serial.println (" connection failed" );
115
- Serial.println (" disconnecting." );
116
- client.stop ();
117
110
}
118
111
}
119
112
@@ -136,7 +129,3 @@ void printWifiStatus() {
136
129
}
137
130
138
131
139
-
140
-
141
-
142
-
0 commit comments