Skip to content

Commit 336e578

Browse files
committed
Added SimpleWebServerWiFi
1 parent 94ae9a7 commit 336e578

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
WiFi Web Server LED Blink
3+
4+
A simple web server that lets you blink an LED via the web.
5+
This sketch will print the IP address of your WiFi Shield (once connected)
6+
to the Serial monitor. From there, you can open that address in a web browser
7+
to turn on and off the LED on pin 9.
8+
9+
If the IP address of your shield is yourAddress:
10+
http://yourAddress/H turns the LED on
11+
http://yourAddress/L turns it off
12+
13+
This example is written for a network using WPA encryption. For
14+
WEP or WPA, change the Wifi.begin() call accordingly.
15+
16+
Circuit:
17+
* WiFi shield attached
18+
* LED attached to pin 9
19+
20+
created 25 Nov 2012
21+
by Tom Igoe
22+
*/
23+
#include <SPI.h>
24+
#include <WiFi.h>
25+
26+
char ssid[] = "yourNetwork"; // your network SSID (name)
27+
char pass[] = "secretPassword"; // your network password
28+
int keyIndex = 0; // your network key Index number (needed only for WEP)
29+
30+
int status = WL_IDLE_STATUS;
31+
WiFiServer server(80);
32+
33+
void setup() {
34+
Serial.begin(9600); // initialize serial communication
35+
pinMode(9, OUTPUT); // set the LED pin mode
36+
37+
// check for the presence of the shield:
38+
if (WiFi.status() == WL_NO_SHIELD) {
39+
Serial.println("WiFi shield not present");
40+
while(true); // don't continue
41+
}
42+
43+
// attempt to connect to Wifi network:
44+
while ( status != WL_CONNECTED) {
45+
Serial.print("Attempting to connect to Network named: ");
46+
Serial.println(ssid); // print the network name (SSID);
47+
48+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
49+
status = WiFi.begin(ssid, pass);
50+
// wait 10 seconds for connection:
51+
delay(10000);
52+
}
53+
server.begin(); // start the web server on port 80
54+
printWifiStatus(); // you're connected now, so print out the status
55+
}
56+
57+
58+
void loop() {
59+
WiFiClient client = server.available(); // listen for incoming clients
60+
61+
if (client) { // if you get a client,
62+
Serial.println("new client"); // print a message out the serial port
63+
String currentLine = ""; // make a String to hold incoming data from the client
64+
while (client.connected()) { // loop while the client's connected
65+
if (client.available()) { // if there's bytes to read from the client,
66+
char c = client.read(); // read a byte, then
67+
Serial.write(c); // print it out the serial monitor
68+
if (c == '\n') { // if the byte is a newline character
69+
70+
// if the current line is blank, you got two newline characters in a row.
71+
// that's the end of the client HTTP request, so send a response:
72+
if (currentLine.length() == 0) {
73+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
74+
// and a content-type so the client knows what's coming, then a blank line:
75+
client.println("HTTP/1.1 200 OK");
76+
client.println("Content-type:text/html");
77+
client.println();
78+
79+
// the content of the HTTP response follows the header:
80+
client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
81+
client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
82+
83+
// The HTTP response ends with another blank line:
84+
client.println();
85+
// break out of the while loop:
86+
break;
87+
}
88+
else { // if you got a newline, then clear currentLine:
89+
currentLine = "";
90+
}
91+
}
92+
else if (c != '\r') { // if you got anything else but a carriage return character,
93+
currentLine += c; // add it to the end of the currentLine
94+
}
95+
96+
// Check to see if the client request was "GET /H" or "GET /L":
97+
if (currentLine.endsWith("GET /H")) {
98+
digitalWrite(9, HIGH); // GET /H turns the LED on
99+
}
100+
if (currentLine.endsWith("GET /L")) {
101+
digitalWrite(9, LOW); // GET /L turns the LED off
102+
}
103+
}
104+
}
105+
// close the connection:
106+
client.stop();
107+
Serial.println("client disonnected");
108+
}
109+
}
110+
111+
void printWifiStatus() {
112+
// print the SSID of the network you're attached to:
113+
Serial.print("SSID: ");
114+
Serial.println(WiFi.SSID());
115+
116+
// print your WiFi shield's IP address:
117+
IPAddress ip = WiFi.localIP();
118+
Serial.print("IP Address: ");
119+
Serial.println(ip);
120+
121+
// print the received signal strength:
122+
long rssi = WiFi.RSSI();
123+
Serial.print("signal strength (RSSI):");
124+
Serial.print(rssi);
125+
Serial.println(" dBm");
126+
// print where to go in a browser:
127+
Serial.print("To see this page in action, open a browser to http://");
128+
Serial.println(ip);
129+
}

0 commit comments

Comments
 (0)