WiFi Server
#include <>
/*H*******************************************************
WiFi Web Server
A simple web Srv that shows the value of the analog input pins.
using a WiFi shield.
This example is written for a network using WPA encryption. For
WEP or WPA, change the 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 ************************************
//************************* PROTOTYPES ************************************
//************************* VARIABLES ************************************
char ssid[] = "yourNetwork"; // NETWORK SSID (NAME)
char pass[] = "secretPassword"; // NETWORK PASSWORD
int KeyNdx = 0; // NETWORK KEY iNDEX NUMBER (ONLY FOR WEP)
int status = WL_IDLE_STATUS;
WiFiServer Srv( 80 );
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( 9600 ); // INIT SERIAL AND WAIT FOR PORT TO OPEN
while( !Serial )
{
;// WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR 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 the firmware" );
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO WiFi NETWORK
{
Serial.print( "Attempting to connect to SSID: ");
Serial.println( ssid );
// CONNECT TO WPA/WPA2 NETWORK. CHANGE THIS LINE IF USING OPEN OR WEP NETWORK
status = WiFi.begin( ssid, pass );
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
Srv.begin();
printWifiStatus(); // YOU'RE CONNECTED NOW, SO PRINT OUT STATUS
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
WiFiClient client = Srv.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 END OF LINE (NEWLINE CHAR) AND LINE IS BLANK, HTTP REQUEST ENDED, SEND REPLY
if( c == '\n' && currentLineIsBlank )
{ // SEND A STANDARD HTTP RESPONSE HEADER
client.println( "HTTP/1.1 200 OK" );
client.println( "Content-Type: text/html");
// CONN WILL BE CLOSED AFTER COMPLETION OF RESPONSE
client.println( "Connection: close");
// REFRESH PAGE AUTOMATICALLY EVERY 5 SEC
client.println( "Refresh: 5");
client.println();
client.println( "");
client.println( "");
// OUTPUT VALUE OF EACH ANALOG INPUT PIN
for( int nanChnl = 0; nanChnl < 6 ; nanChnl++)
{
int sensorReading = analogRead( nanChnl );
client.print( "analog input ");
client.print( nanChnl );
client.print( " is " );
client.print( sensorReading );
client.println( "
" );
}
client.println( "" );
break;
}
if( c == '\n' )
currentLineIsBlank = true; // STARTING A NEW LINE
else if( c != '\r')
currentLineIsBlank = false; // RCVD CHAR ON CURR LINE
}
}
delay( 1 ); // GIVE WEB BROWSER TIME TO RECEIVE DATA
client.stop(); // CLOSE CONNECTION
Serial.println( "client disconnected" );
}
}
/*F********************************************************************
*
**********************************************************************/
void
printWifiStatus()
{
Serial.print( "SSID: " );
Serial.println( WiFi.SSID() ); // PRINT SSID OF NETWORK ATTACHED TO
IPAddress ip = WiFi.localIP();
Serial.print( "IP Address: " );
Serial.println( ip );
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");// PRINT RECEIVED SIGNAL STRENGTH
Serial.print( rssi );
Serial.println( " dBm" );
}