Salve a tutti, navigando sul sito ho trovato proprio uno sketch che fa al caso mio da cui ho preso spunto
http://arduino.cc/playground/Code/WebServer
Ho modificato qua e la il codice rendendolo a me più leggibile ma non riesco più a caricare la pagina
#include <Ethernet.h>
#include <SPI.h>
#include <String.h>
#include <SD.h>
/**********************************************************************************************************************
* MAC address and IP address.
***********************************************************************************************************************/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x66, 0x5F };
byte ip[] = { 192, 168, 0, 180 };
/**********************************************************************************************************************
* Memory space for string management and setup WebServer service
***********************************************************************************************************************/
// For sending HTML to the 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 transmitting
***********************************************************************************************************************/
#define NUM_PAGES 4
// Page 1
char http_uri1[] = "/";
char file1[] = "index.html";
// Page 2
char http_uri2[] = "/jquery.js";
char file2[] = "jquery.js";
// Page 3
char http_uri3[] = "/flot.js";
char file3[] = "flot.js";
// Page 4
char http_uri4[] = "/croshair.js";
char file4[] = "croshair.js";
// declare tables for the pages
char* files[] = { file1, file2, file3, file4 }; // files
char* http_uris[] = { http_uri1, http_uri2, http_uri3, http_uri4 }; // URIs
/**********************************************************************************************************************
* define HTTP return structure ID for parsing HTTP header request
***********************************************************************************************************************/
struct HTTP_DEF {
int pages;
char vars[20];
} ;
/**********************************************************************************************************************
* Shared variable
***********************************************************************************************************************/
Server server(80);
void setup() {
Ethernet.begin(mac, ip);
server.begin();
if (!SD.begin(4)) { return; }
Serial.begin(9600); // DEBUG
}
/**********************************************************************************************************************
* Main 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 comparison (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_PAGES; i++) {
strcpy_P(load, http_uris[i]);
// GET
strcpy(compare,"GET ");
strcat(compare,load);
strcat(compare," ");
Serial.print("GET compare: "); // DEBUG
Serial.println(compare); // DEBUG
if (strncmp(buffer,compare, strlen(load)+5)==0) {
http_def.pages = i+1;
break;
}
// POST
strcpy(compare,"POST ");
strcat(compare,load);
strcat(compare," ");
Serial.print("POST compare: "); // DEBUG
Serial.println(compare); // DEBUG
if (strncmp(buffer,compare, strlen(load)+6)==0) {
http_def.pages = i+1;
break;
}
}
// select the page from the buffer (GET and POST) [stop]
// read other stuff (for POST requests) [start]
if (strncmp(buffer, "POST /", 5)==0) {
processRequest:
loadindex = 2; // reset load
memset(load,0,STRING_LOAD_SIZE);
load[0] = client.read();
load[1] = client.read();
while (load[loadindex-2] != '\r' && load[loadindex-1] != '\n') {
c = client.read();
if (loadindex<STRING_BUFFER_SIZE) load[loadindex] = c;
loadindex++;
}
if (strncmp(load, "Content-Length: ",16)==0) {
loadindex = 16;
for(i = loadindex; i< strlen(load) ; i++) {
if (load[i] != ' ' && load[i] != '\r' && load[i] != '\n') {
vars[i-loadindex] = load[i];
} else {
break;
}
}
contentLength = atoi(vars);
memset(vars,0,STRING_VARS_SIZE);
client.read(); client.read(); // read null line
i = 0;
while (i<contentLength) {
c = client.read();
vars[i] = c;
++i;
}
} else {
goto processRequest;
}
}
// read other stuff (for POST requests) [stop]
// clean buffer for next row
bufindex = 0;
}
Serial.print("Grepped page: "); // DEBUG
Serial.println(http_def.pages); // DEBUG
strncpy(http_def.vars,vars,STRING_VARS_SIZE);
return http_def;
}
/**********************************************************************************************************************
* Send Pages
***********************************************************************************************************************/
File htmlFile;
void sendPage(Client client,struct HTTP_DEF http_def) {
htmlFile = SD.open(files[http_def.pages-1]);
if (htmlFile) {
while (htmlFile.available()) {
client.print(htmlFile.read(), BYTE);
}
}
client.print("
");
// send POST variables
client.print(vars);
}
gran parte del codice lo comprendo ma non del tutto e adesso non capisco proprio dove trovare l'errore....