WiFI Server
#include <>
/*H**************************************************
WiFi Web Server LED Blink
A simple web Srv that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 5.
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.
Circuit:
* WiFi shield attached
* LED attached to pin 5
created for arduino 25 Nov 2012
by Tom Igoe
ported for sparkfun esp32
31.01.2017 by Jan Hendrik Berlin
***************************************************/
#include
//************************* DEFINES ************************************
#define BAUD 9600
const char *Ssid = "yourSsid";
const char *Pwd = "yourpasswd";
//************************* PROTOTYPES ************************************
//************************* VARIABLES ************************************
WiFiServer Srv( 80 );
WiFiClient Clnt;
int value = 0;
char *LedMsg[] = { { "HTTP/1.1 200 OK" }, { "Content-type:text/html" },
{ "Click here to turn LED on pin 5 on.
" },
{ "Click here to turn LED on pin 5 off.
" }};
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD );
pinMode( 5, OUTPUT ); // set the LED pin mode
delay( 10 );
Serial.println();
Serial.println();
Serial.print( "Connecting to " );
Serial.println( Ssid );
WiFi.begin( Ssid, Pwd ); // START BY CONNECTING TO A WiFi NETWORK
while( WiFi.status() != WL_CONNECTED )
{
delay( 500 );
Serial.print( "." );
}
Serial.println( "" );
Serial.println( "WiFi connected." );
Serial.println( "IP address: " );
Serial.println( WiFi.localIP() );
Srv.begin();
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
if( Clnt = Srv.available()) // LISTEN FOR INCOMING CLNTS
{ // IF GET A Clnt,
Serial.println( "New Client." ); // PRINT MESSAGE TO SERIAL PORT
String currentLine = ""; // INCOMING DATA String from CLNT
while( Clnt.connected() )
{ // LOOP WHILE CLNT'S CONNECTED
if( Clnt.available() )
{ // IF THERE'S BYTES TO READ FROM CLNT,
char c = Clnt.read(); // READ A BYTE, THEN
Serial.write( c ); // PRINT IT
if( c == '\n' )
{ // BYTE IS NL CHARACTER
// IF CURRENT LINE BLANK, TWO NL CHARACTERS IN ROW
// END OF CLIENT HTTP REQUEST, RESPOND
if( currentLine.length() == 0 )
{
// HTTP HEADERS ALWAYS START WITH A RESPONSE CODE ( e.g. HTTP/1.1 200 OK )
// AND A CONTENT-TYPE SO CLIENT KNOWS WHAT'S COMING, THEN A BLANK LINE
// HTTP RESPONSE ENDS WITH ANOTHER BLANK LINE
for( ndx =0; ndx < 4; ndx++ )
Clnt.println( LedMsg[ndx] ); // SEND RESPONSE
Clnt.println(); // HTTP END BLANK LINE
break; // BREAK OUT OF WHILE LOOP
}
else // IF GOT NEWLINE, THEN CLEAR CURRENTlINE
currentLine = "";
}
else if( c != '\r' ) // IF NOT A CR CHAR
currentLine += c; // ADD TO END OF CURRENTlINE
if( currentLine.endsWith( "GET /H" ) )
digitalWrite( 5, HIGH ); // GET /H TURNS LED ON
if( currentLine.endsWith( "GET /L" ) )
digitalWrite( 5, LOW ); // GET /L TURNS LED OFF
}
}
Clnt.stop(); // CLOSE CONNECTION
Serial.println( "Client Disconnected." );
}
}