Controlling LED With Web Server Buttons

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>");

}

Hi there,

Where do you specify the pin number??

When I open the serial monitor, I only see 'HTTP' transmitted..

Greetz

Robbert

Why do you have multiple forms? Both buttons should be on the same form. If you give the buttons names, you don't need the hidden fields with names.

Why do you want to POST the reply, rather than use GET?

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?

Simple web control page code.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

IF I wan Turn On / OFF led should I do with Routers???
Please give me project turn on LED with WEB .