Created
October 16, 2012 09:08
-
-
Save biskandar/3898200 to your computer and use it in GitHub Desktop.
Build Webserver with Arduino Leonardo and WiFi Shield 2012101613
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loop() { | |
// verify if there is new client connected | |
WiFiClient client = server.available() ; | |
if ( !client ) return ; | |
// prepare for reading request data | |
boolean isBlankLine = true ; | |
// found new client connected and process the request data | |
Serial.println( "Found new client connected" ) ; | |
while( client.connected() ) { | |
if ( !client.available() ) break ; | |
// read all the client's incoming request data | |
char ch = client.read() ; | |
// write all the request data back into the console | |
Serial.write( ch ) ; | |
// http rules: defined the end of http request when found blank line | |
if ( ( ch == '\n' ) && ( isBlankLine ) ) { | |
client.println( "HTTP/1.1 200 OK" ) ; | |
client.println( "Content-Type: text/html" ) ; | |
client.println( "Connection: close" ) ; | |
client.println() ; | |
client.println( "OK" ) ; | |
} | |
// validation of looking for the blank line | |
if ( ch == '\n' ) { | |
isBlankLine = true ; | |
continue ; | |
} | |
if ( ch == '\r' ) { | |
continue ; | |
} | |
isBlankLine = false ; | |
continue ; | |
} | |
// do disconnect from connected client | |
delay( 10 ) ; | |
client.stop() ; | |
Serial.println( "Client disconnected" ) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment