Skip to content

Commit e8118e2

Browse files
author
Tom Igoe
committed
Added Repeating Web Client example
1 parent 5e9c956 commit e8118e2

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Repeating Wifi Web client
3+
4+
This sketch connects to a a web server and makes a request
5+
using an Arduino Wifi shield.
6+
7+
Circuit:
8+
* Wifi shield attached to pins 10, 11, 12, 13
9+
10+
created 23 Apr 2012
11+
by Tom Igoe
12+
13+
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
14+
This code is in the public domain.
15+
*/
16+
17+
#include <SPI.h>
18+
#include <WiFi.h>
19+
20+
char ssid[] = "yourNetwork"; // your network SSID (name)
21+
char pass[] = "secretPassword"; // your network password
22+
int keyIndex = 0; // your network key Index number (needed only for WEP)
23+
24+
int status = WL_IDLE_STATUS;
25+
26+
// Initialize the Wifi client library
27+
WiFiClient client;
28+
29+
// server address:
30+
char server[] = "www.arduino.cc";
31+
//IPAddress server(64,131,82,241);
32+
33+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
34+
boolean lastConnected = false; // state of the connection last time through the main loop
35+
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
36+
37+
void setup() {
38+
// start serial port:
39+
Serial.begin(9600);
40+
41+
// attempt to connect to Wifi network:
42+
while ( status != WL_CONNECTED) {
43+
Serial.print("Attempting to connect to SSID: ");
44+
Serial.println(ssid);
45+
status = WiFi.begin(ssid, pass);
46+
// wait 10 seconds for connection:
47+
delay(10000);
48+
}
49+
// you're connected now, so print out the status:
50+
printWifiStatus();
51+
// print the Wifi board/shield's IP address:
52+
Serial.print("My IP address: ");
53+
Serial.println(WiFi.localIP());
54+
}
55+
56+
void loop() {
57+
// if there's incoming data from the net connection.
58+
// send it out the serial port. This is for debugging
59+
// purposes only:
60+
while (client.available()) {
61+
char c = client.read();
62+
Serial.write(c);
63+
}
64+
65+
// if there's no net connection, but there was one last time
66+
// through the loop, then stop the client:
67+
if (!client.connected() && lastConnected) {
68+
Serial.println();
69+
Serial.println("disconnecting.");
70+
client.stop();
71+
}
72+
73+
// if you're not connected, and ten seconds have passed since
74+
// your last connection, then connect again and send data:
75+
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
76+
httpRequest();
77+
}
78+
// store the state of the connection for next time through
79+
// the loop:
80+
lastConnected = client.connected();
81+
}
82+
83+
// this method makes a HTTP connection to the server:
84+
void httpRequest() {
85+
// if there's a successful connection:
86+
if (client.connect(server, 80)) {
87+
Serial.println("connecting...");
88+
// send the HTTP PUT request:
89+
client.println("GET /latest.txt HTTP/1.1");
90+
client.println("Host: www.arduino.cc");
91+
client.println("User-Agent: arduino-ethernet");
92+
client.println("Connection: close");
93+
client.println();
94+
95+
// note the time that the connection was made:
96+
lastConnectionTime = millis();
97+
}
98+
else {
99+
// if you couldn't make a connection:
100+
Serial.println("connection failed");
101+
Serial.println("disconnecting.");
102+
client.stop();
103+
}
104+
}
105+
106+
107+
void printWifiStatus() {
108+
// print the SSID of the network you're attached to:
109+
Serial.print("SSID: ");
110+
Serial.println(WiFi.SSID());
111+
112+
// print your WiFi shield's IP address:
113+
IPAddress ip = WiFi.localIP();
114+
Serial.print("IP Address: ");
115+
Serial.println(ip);
116+
117+
// print the received signal strength:
118+
long rssi = WiFi.RSSI();
119+
Serial.print("signal strength (RSSI):");
120+
Serial.print(rssi);
121+
Serial.println(" dBm");
122+
}
123+
124+
125+
126+
127+
128+

0 commit comments

Comments
 (0)