ok gracias curro92, pero el mayor problema es donde poner el codigo..
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(ledPin, OUTPUT);
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);
}
else
{
contentPrinter(client,(char*)pgm_read_word(&(page_404[0])));
}
}
// 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 attenction!
int bufindex = 0; // reset buffer
int loadindex = 0; // reset load
int contentLenght = 0; // reset POST content lenght
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_PAGES; i++)
{
strcpy_P(load, (char*)pgm_read_word(&(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;
}
}
contentLenght = atoi(vars);
memset(vars,0,STRING_VARS_SIZE);
client.read(); client.read(); // read null line
i = 0;
while (i<contentLenght)
{
c = client.read();
vars[i] = c;
++i;
}
}
else
{
goto processRequest;
}
}
// read other stuff (for POST requests) [stop]
// clean buffer for next row
bufindex = 0;
}
// delay(10); // removing this nothing work... if you understand why mail me a bebbo [at] bebbo [dot] it
Serial.print("Grepped page: "); // DEBUG
Serial.println(http_def.pages); // DEBUG
strncpy(http_def.vars,vars,STRING_VARS_SIZE);
return http_def;
}
ese es el código q tengo..
y le tengo q agregar este
Client client = server.available();
if (client)
{
while(client.connected())
{
if(client.available())
{
char c = client.read();
if(readString.length() < 100)
{
readString.append(c);
}
if(c == '\n')
{
if(readString.contains("acc1=ON"))
{
digitalWrite(Acc1, HIGH);
}
if(readString.contains("acc1=OFF"))
{
digitalWrite(Acc1, LOW);
}
el problema es q si lo agrego en el void loop()
no me carga la pagina, no se q es lo que estoy haciendo mal
