Having trouble getting this to function correctly. I have an Arduino with an Ethernet Shield hosting a web server that has two buttons controlling the high or low state of an LED. The page loads fine with the "On" button and the "Off" button using POST, but whenever I press either they don't work. Here's my code, anyone happen see the problem?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x50, 0x9A };
byte ip[] = { 10,0,1,8 };
const int MAX_PAGENAME_LEN = 8; //Max number of characters for page name
char buffer[MAX_PAGENAME_LEN+1]; //Max number of characters + terminating null is the buffer
EthernetServer server(8010);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
delay(2000);
}
void loop() {
EthernetClient client = server.available();
if (client) {
int type = 0;
while (client.connected()) {
if (client.available()) {
memset(buffer, 0, sizeof(buffer)); //clear the buffer
if (client.find("/")) {
if (client.readBytesUntil('/', buffer, sizeof(buffer))) {
Serial.println(buffer);
if (strcmp(buffer, "POST ") == 0) { //find the start of the posted form
client.find("\n\r"); //skip to the body
while(client.findUntil("pinD", "\n\r")) { //find string starting with "pin", stop on first blank line
int pin = client.parseInt(); //the number after "pinD" is the pin number
int val = client.parseInt(); //the number after the pin number is its value (1 or 0)
pinMode(pin, OUTPUT);
digitalWrite(pin, val);
}
}
sendHeader(client, "Post example"); //send a standard header, set up both buttons with POST
client.println("<h2>Press button to control LED</h2>");
client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
client.println(" value='1'><input type='submit' value='On'/></form>");
client.print("<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
client.println(" value='0'><input type='submit' value='Off'/></form>");
client.println("</body></html>");
client.stop();
}
break;
}
}
delay(1);
client.stop();
}
}
}
void sendHeader(EthernetClient client, char *title) { //standard HTTP header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>");
client.print(title);
client.println("</title><body>");
}