Arduino + Ethernet shield, A better Webserver

The Arduino Ethernet Shield allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 ethernet chip (datasheet). The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP. It supports up to four simultaneous socket connections.

The Webserver example sketch that comes with the Arduino IDE is is a great starting tutorial and should be at lest reviewed before reading this tutorial. The web server example sketch is  missing a few key features such as, the ability to respond to different GET requests with different response.

This tutorial assumes that you are using Eclipse as your editor for Arduino and you are familiar with both the Arduino and the Ethernet shield. This tutorial was created in response to VHS Hack challenge. You can download the Eclipse project for this tutorial from our website. Respond to different GET requests with different response source code.

When you request a page from the Arduino in your web browser, your web browser create a HTTP GET request. This GET request is sent to the Arduino’s where it gets processed and a response is formed and sent back to your web browser and is displayed to the user.

The GET request will look something like this

Hypertext Transfer Protocol
GET /helloworld.html HTTP/1.1\r\n
Connection: keep-alive\r\n
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/ Safari/532.5\r\n
Cache-Control: max-age=0\r\n
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n
Accept-Encoding: gzip,deflate,sdch\r\n
Accept-Language: en-US,en;q=0.8\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n

The first line in a HTTP GET request contains the requested file, in this example it is /helloworld.html. We can use this line to determine what page the user is requesting. The following function takes the request buffer and extracts the request string and stores it in a CWebserverRequest structure for reference.

bool CWebserver::ProcessHeader( char * requestBuffer, CWebserverRequest * request )
if( request == NULL || request == NULL ) {
return false;
sprintf( request->request_string, "/" );
// Get the request path.
char * get = strstr( requestBuffer, "GET " );
if( get != NULL ) {
get += 4 ;
char * eol = strstr( get, " " );
if( eol != NULL ) {
eol[ 0 ] = 0 ;
unsigned short length = strlen( get );
if( length > HTTP_MAX_REQUEST_LENGTH ) {
strncpy( request->request_string, get, length );
request->request_string[ length ] = 0 ; // NULL termanate the string.
return true;

We can user the request string later in our code to determine what page the user was requesting and respond accordingly.

More information