Hi,
I have managed to modify the code above and produce my own version. It's a single page web server and with the ability to send commands back to the Arduino.
Not sure of the protocol for updating other people's code....so let me know if I've done wrong.
I'll update this post as I further modify the code (I'm not a programmer really!......just like to hack away at code!). Certainly it needs tidying up.
Ian.
/***********************************************************************************************
* WEB SERVER SELL STRUCTURED
* Requires an Ethernet Shield & Arduino.
* Originally by Alessandro Calzavara & Alberto Capponi
* Modified by Ian Johnston Jan 2010
* I have totally stripped this program apart, it's now just a single page utility and with ability
* of having buttons on the web page to command the Arduino
* 22/01/2010
************************************************************************************************/
#include <Ethernet.h>
#include <avr/pgmspace.h>
#include <string.h>
// Setup TCP/IP
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
// Setup Global Vars
int cmd = 0; // Init. command var
char Domain[ ] = "www.yourdomain.com:1234/";
// Memory space for string management and setup WebServer service
// for send html to client
#define STRING_BUFFER_SIZE 128
char buffer[STRING_BUFFER_SIZE];
// to store data from http request
#define STRING_LOAD_SIZE 128
char load[STRING_LOAD_SIZE];
// POST and GET variables
#define STRING_VARS_SIZE 128
char vars[STRING_VARS_SIZE];
// Strings stored in flash of the HTML we will be xmitting
#define NUM_ID 16
// Page 1 ID
PROGMEM prog_char http_id1[] = "/";
// Command ID 2 to 16
PROGMEM prog_char http_id2[] = "/id2";
PROGMEM prog_char http_id3[] = "/id3";
PROGMEM prog_char http_id4[] = "/id4";
PROGMEM prog_char http_id5[] = "/id5";
PROGMEM prog_char http_id6[] = "/id6";
PROGMEM prog_char http_id7[] = "/id7";
PROGMEM prog_char http_id8[] = "/id8";
PROGMEM prog_char http_id9[] = "/id9";
PROGMEM prog_char http_id10[] = "/id10";
PROGMEM prog_char http_id11[] = "/id11";
PROGMEM prog_char http_id12[] = "/id12";
PROGMEM prog_char http_id13[] = "/id13";
PROGMEM prog_char http_id14[] = "/id14";
PROGMEM prog_char http_id15[] = "/id15";
PROGMEM prog_char http_id16[] = "/id16";
// declare tables for the ID's
PGM_P http_id[] PROGMEM = { http_id1, http_id2, http_id3, http_id4, http_id5, http_id6, http_id7, http_id8, http_id9, http_id10, http_id11, http_id12, http_id13, http_id14, http_id15, http_id16 }; // URIs
// define HTTP return structure ID for parsing HTTP header request ******************************
struct HTTP_DEF {
int pages;
char vars[20];
} ;
Server server(80);
/***********************************************************************************************
* VOID SETUP
************************************************************************************************/
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
/***********************************************************************************************
* VOID LOOP
************************************************************************************************/
void loop() {
Client client = server.available();
if (client) { // now client is connected to arduino
// read HTTP header request... so select what page client are looking for
HTTP_DEF http_def = readHTTPRequest(client);
if (http_def.pages > 0) {
sendPage(client,http_def);
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
/***********************************************************************************************
* Method for read HTTP Header Request from web client
************************************************************************************************/
struct HTTP_DEF readHTTPRequest(Client client) {
char c;
int i;
// use buffer, pay attention!
int bufindex = 0; // reset buffer
int loadindex = 0; // reset load
int contentLength = 0; // reset POST content Length
char compare[50]; // page comparation (uri selection)
HTTP_DEF http_def; // use the structure for multiple returns
http_def.pages = 0; // default page selection... error
// reading all rows of header
if (client.connected() && client.available()) { // read a row
buffer[0] = client.read();
buffer[1] = client.read();
bufindex = 2;
// read the first line to determinate the request page
while (buffer[bufindex-2] != '\r' && buffer[bufindex-1] != '\n') { // read full row and save it in buffer
c = client.read();
if (bufindex<STRING_BUFFER_SIZE) buffer[bufindex] = c;
bufindex++;
}
// select the page from the buffer (GET and POST) [start]
for(i = 0; i < NUM_ID; i++) {
strcpy_P(load, (char*)pgm_read_word(&(http_id[i])));
// GET
strcpy(compare,"GET ");
strcat(compare,load);
strcat(compare," ");
if (strncmp(buffer,compare, strlen(load)+5)==0) {
http_def.pages = i+1;
if (http_def.pages > 1) { // If greater than 4 then it's not a page being requested, rather a command via POST. So, page11=cmd1, page12=cmd2 etc.
cmd = http_def.pages; // command from webpage to action
http_def.pages = 1; // This sets the page requested to default 1.
}
break;
}
// POST
strcpy(compare,"POST ");
strcat(compare,load);
strcat(compare," ");
if (strncmp(buffer,compare, strlen(load)+6)==0) {
http_def.pages = i+1;
if (http_def.pages > 1) { // If greater than 4 then it's not a page being requested, rather a command via POST. So, page11=cmd1, page12=cmd2 etc.
cmd = http_def.pages; // command from webpage to action
http_def.pages = 1; // This sets the page requested to default 1.
}
break;
}
}
// clean buffer for next row
bufindex = 0;
}
return http_def;
}
/***********************************************************************************************
* SEND WEB PAGE & PROCESS ARDUINO CODE
************************************************************************************************/
void sendPage(Client client,struct HTTP_DEF http_def) {
// Put Arduino commands in this IF after cmd=0
if (cmd > 0) { // Commands will be 11, 12, 13, 14, 15 or 16
cmd = 0; // reset cmd back to zero
// Start Arduino commands
// End Arduino commands
}
// Send web page to browser
if (http_def.pages==1) {
client.print("<html><head><title>Arduino Web Server</title></head>");
client.print("<b>Arduino Web Server</b>");
client.print("\n<p>\n");
client.print("<form method=\"post\" action=\"http://");
client.print(Domain);
client.print("id13\" NAME=\"CMD1\"><BUTTON name=\"submit\" value=\"button\" type=\"submit\">Sample CMD 13</button></form>");
client.print("\n<p>\n");
client.print("<form method=\"post\" action=\"http://");
client.print(Domain);
client.print("id14\" NAME=\"CMD1\"><BUTTON name=\"submit\" value=\"button\" type=\"submit\">Sample CMD 14</button></form>");
client.print("\n<p>\n");
client.print("<form method=\"post\" action=\"http://");
client.print(Domain);
client.print("id15\" NAME=\"CMD1\"><BUTTON name=\"submit\" value=\"button\" type=\"submit\">Sample CMD 15</button></form>");
client.print("</head><html>");
}
}