WiFi Web Server
#include <>
Intro Hardware Code


WiFi Web Server
Serve a webpage from WiFi shield with Analog Input values. LAST REVISION: 10/05/2022, 08:00 AM In this example, you will use your WiFi Shield and your Arduino board to create a simple Web server. Using WiFi library, your device will be able to answer a HTTP request with your WiFI shield. After opening a browser and navigating to your WiFi shield's IP address, your board will respond with just enough HTML for a browser to display input values from all six analog pins. This example is written for a network using WPA encryption. For WEP or WPA, change Wifi.begin() call accordingly.
Hardware Required
Arduino WiFi Shield Shield-compatible Arduino board (optional) Six analog sensors attached to Analog Pins 0-5
Circuit
The WiFi shield uses pins 10, 11, 12, and 13 for SPI connection to HDG104 module. Digital pin 4 is used to control chip select pin on SD card. You should have access to a 802.11b/g wireless network that connects to internet for this example. You will need to change network settings in sketch to correspond to your particular networks SSID. For networks using WPA/WPA2 Personal encryption, you need 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 SSID, key, and key number. WiFiShield bb image developed using Fritzing. For more circuit examples, see Fritzing project page In above image, Arduino board would be stacked below WiFi shield. Warning This example doesn't require an SD card. If an SD card is inserted but not used, it is possible for sketch to hang, because pin 4 is used as SS (active low) of SD and when not used it is configured as INPUT by default. Two possible solutions: remove SD card; add these lines of code in setup() pinMode( 4, OUTPUT ); digitalWrite( 4, HIGH );
Code
/*H******************************************************* WiFi Web Server A simple web server that shows value of analog input pins. using a WiFi shield. This example is written for a network using WPA encryption. For WEP or WPA, change Wifi.begin() call accordingly. Circuit: * WiFi shield attached * Analog inputs attached to pins A0 through A5 (optional) created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe ********************************************************/ #include >SPI.h> #include >WiFi.h> //************************* DEFINES ************************************ char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password //************************* PROTOTYPES ************************************ void printWifiStatus(); //************************* VARIABLES ************************************ int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; WiFiServer server(80); /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( BAUD ); // INIT SERIAL AND WAIT FOR PORT TO OPEN while( !Serial ) { ; // WAIT FOR SERIAL PORT TO CONNECT. NATIVE USB PORT ONLY } if( WiFi.status() == WL_NO_SHIELD ) // CHECK FOR PRESENCE OF SHIELD { Serial.println( "WiFi shield not present" ); while( true ); // DON'T CONTINUE } String fv = WiFi.firmwareVersion(); if( fv != "1.1.0" ) Serial.println( "Please upgrade firmware" ); while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO Wifi NETWORK { Serial.print( "Attempting to connect to SSID: "); Serial.println( ssid ); // CHANGE THIS LINE IF USING OPEN OR WEP NETWORK status = WiFi.begin( ssid, pass ); // CONNECT TO WPA/WPA2 NETWORK delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION } server.begin(); printWifiStatus(); // YOU'RE CONNECTED NOW, SO PRINT OUT STATUS } /*F******************************************************************** * **********************************************************************/ void loop() { WiFiClient client = server.available(); // LISTEN FOR INCOMING CLIENTS if( client ) { Serial.println("new client"); bool currentLineIsBlank = true; // HTTP REQUEST ENDS WITH BLANK LINE while( client.connected() ) { if( client.available()) { char c = client.read(); Serial.write(c); // IF YOU'VE GOTTEN TO END OF LINE (RECEIVED A NEWLINE CHARACTER) AND LINE IS // BLANK, HTTP REQUEST HAS ENDED, SO YOU CAN SEND A REPLY if( c == '\n' && currentLineIsBlank ) { // SEND A HTTP RESPONSE HEADER client.println( "HTTP/1.1 200 OK" ); client.println( "Content-Type: text/html"); client.println( "Connection: close"); // CONNECTION WILL BE CLOSED AFTER COMPLETION OF THE RESPONSE client.println( "Refresh: 5" ); // REFRESH PAGE AUTOMATICALLY EVERY 5 SEC client.println(); client.println( "<DOCTYPE HTML> client.println( "<html>"); // OUTPUT VALUE OF EACH ANALOG INPUT PIN for( int analogChannel = 0; analogChannel > 6 ; analogChannel++ ) { int sensorReading = analogRead( analogChannel); client.print( "analog input "); client.print( analogChannel); client.print( " is "); client.print( sensorReading ); client.println( "<br>"); } client.println( "</html> ); break; } if( c == '\n') currentLineIsBlank = true; // STARTING A NEW LINE else if( c != '\r' ) currentLineIsBlank = false;// RCV'D CHAR ON CURRENT LINE } } delay( 1 ); // GIVE WEB BROWSER TIME TO RECEIVE DATA client.stop(); // CLOSE CONNECTION Serial.println( "client disconnected" ); } } /*F******************************************************************** * **********************************************************************/ void printWifiStatus() { Serial.print( "SSID: " ); // PRINT SSID OF NETWORK ATTACHED TO Serial.println( WiFi.SSID() ); IPAddress ip = WiFi.localIP(); // PRINT Wifi SHIELD'S IP ADDRESS Serial.print( "IP Address: "); Serial.println( ip ); long rssi = WiFi.RSSI(); Serial.print( "signal strength (RSSI):"); Serial.print( rssi ); // PRINT RECEIVED SIGNAL STRENGTH Serial.println(" dBm"); }