Simple Web Server WiFi

Turn on and off an LED accessing this simple Web Server.

In this example, a simple web server lets you blink an LED via the web. This example will print the IP address of your WiFi Shield (once connected) to the Arduino Software (IDE) serial monitor. Once you know the IP address of our board, you can open that address in a web browser to turn on and off the LED on pin 9.

If the IP address of your shield is yourAddress: http://yourAddress/H turns the LED on http://yourAddress/L turns it off

This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly.

Hardware Required

  • Arduino WiFi Shield

  • Shield-compatible Arduino board

  • LED attached to pin 9

Circuit

The WiFi shield uses pins 10, 11, 12, and 13 for the SPI connection to the HDG104 module. Digital pin 4 is used to control the chip select pin on the SD card.

You should have access to a 802.11b/g wireless network that connects to the internet for this example. You will need to change the network settings in the sketch to correspond to your particular networks SSID.

For networks using WPA/WPA2 Personal encryption, you need the SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.

WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need the SSID, the key, and key number.

WiFiShield bb

image developed using Fritzing. For more circuit examples, see the Fritzing project page

In the above image, the Arduino board would be stacked below the WiFi shield.

Code

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
26char ssid[] = "yourNetwork"; // your network SSID (name)
27char pass[] = "secretPassword"; // your network password
28int keyIndex = 0; // your network key Index number (needed only for WEP)
29
30int status = WL_IDLE_STATUS;
31WiFiServer server(80);
32
33void 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 String fv = WiFi.firmwareVersion();
44 if (fv != "1.1.0") {
45 Serial.println("Please upgrade the firmware");
46 }
47
48 // attempt to connect to Wifi network:
49 while (status != WL_CONNECTED) {
50 Serial.print("Attempting to connect to Network named: ");
51 Serial.println(ssid); // print the network name (SSID);
52
53 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
54 status = WiFi.begin(ssid, pass);
55 // wait 10 seconds for connection:
56 delay(10000);
57 }
58 server.begin(); // start the web server on port 80
59 printWifiStatus(); // you're connected now, so print out the status
60}
61
62
63void loop() {
64 WiFiClient client = server.available(); // listen for incoming clients
65
66 if (client) { // if you get a client,
67 Serial.println("new client"); // print a message out the serial port
68 String currentLine = ""; // make a String to hold incoming data from the client
69 while (client.connected()) { // loop while the client's connected
70 if (client.available()) { // if there's bytes to read from the client,
71 char c = client.read(); // read a byte, then
72 Serial.write(c); // print it out the serial monitor
73 if (c == '\n') { // if the byte is a newline character
74
75 // if the current line is blank, you got two newline characters in a row.
76 // that's the end of the client HTTP request, so send a response:
77 if (currentLine.length() == 0) {
78 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
79 // and a content-type so the client knows what's coming, then a blank line:
80 client.println("HTTP/1.1 200 OK");
81 client.println("Content-type:text/html");
82 client.println();
83
84 // the content of the HTTP response follows the header:
85 client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
86 client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
87
88 // The HTTP response ends with another blank line:
89 client.println();
90 // break out of the while loop:
91 break;
92 } else { // if you got a newline, then clear currentLine:
93 currentLine = "";
94 }
95 } else if (c != '\r') { // if you got anything else but a carriage return character,
96 currentLine += c; // add it to the end of the currentLine
97 }
98
99 // Check to see if the client request was "GET /H" or "GET /L":
100 if (currentLine.endsWith("GET /H")) {
101 digitalWrite(9, HIGH); // GET /H turns the LED on
102 }
103 if (currentLine.endsWith("GET /L")) {
104 digitalWrite(9, LOW); // GET /L turns the LED off
105 }
106 }
107 }
108 // close the connection:
109 client.stop();
110 Serial.println("client disconnected");
111 }
112}
113
114void printWifiStatus() {
115 // print the SSID of the network you're attached to:
116 Serial.print("SSID: ");
117 Serial.println(WiFi.SSID());
118
119 // print your WiFi shield's IP address:
120 IPAddress ip = WiFi.localIP();
121 Serial.print("IP Address: ");
122 Serial.println(ip);
123
124 // print the received signal strength:
125 long rssi = WiFi.RSSI();
126 Serial.print("signal strength (RSSI):");
127 Serial.print(rssi);
128 Serial.println(" dBm");
129 // print where to go in a browser:
130 Serial.print("To see this page in action, open a browser to http://");
131 Serial.println(ip);
132}

Last revision 2018/08/23 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.