Sending different Post request to a Webserver on an Arduino

Hi guys,
i was trying to control a LED by clicking a submit button to send a form with a hidden input to my arduino server. However i was not able to submit 2 different forms each with a different LED to control. I tryed out changing the html code in case i have some syntax error but i couldnt figure out why my code wouldnt work for the 2nd LED. So maybe if you know what im doing wrong please tell me.
the Library i used was the WiflyHQ library http://arduino.cc/forum/index.php/topic,99362.0.html

oid loop()
{
  if (wifly.available() > 0) {

    /* See if there is a request */
    if (wifly.gets(buf, sizeof(buf))) {
      if (strncmp_P(buf, PSTR("GET / "), 6) == 0) {
        /* GET request */
        Serial.println(F("Got GET request"));
        while (wifly.gets(buf, sizeof(buf)) > 0) {
          /* Skip rest of request */
        }
        sendIndex();
        Serial.println(F("Sent index page"));
      } 
      else if (strncmp_P(buf, PSTR("POST"), 4) == 0) {
        /* Form POST */
        char intensity[16];
        Serial.println(F("Got POST"));

        /* Get posted field value */
        if (wifly.available() > 0) {
          int match = wifly.multiMatch_P(100, 3,F("intensity="), F("led"));
          switch (match) {
          case 0: 
            wifly.gets(intensity, sizeof(intensity));
            wifly.flushRx();		// discard rest of input
            sendGreeting(intensity);
            Serial.println(F("Sent greeting page"));
            break;
          case 1:                
            if (LEDval == 0)
            {
              LEDval =1;
            }
            else
            {
              LEDval=0;
            }
            digitalWrite(LEDpin, LEDval);
            break;


          default :
            /* Unexpected request */
            Serial.print(F("Unexpected: "));
            Serial.println(buf);
            wifly.flushRx();		// discard rest of input
            Serial.println(F("Sending 404"));
            send404();
            break;
          }
        }
      }
    }
  }
}
/** Send an index HTML page with an input box for a username */
void sendIndex()
{
  /* Send the header direclty with print */
  wifly.println(F("HTTP/1.1 200 OK"));
  wifly.println(F("Content-Type: text/html"));
  wifly.println(F("Transfer-Encoding: chunked"));
  wifly.println();

  /* Send the body using the chunked protocol so the client knows when
   * the message is finished.
   * Note: we're not simply doing a close() because in version 2.32
   * firmware the close() does not work for client TCP streams.
   */
  wifly.sendChunkln(F("<html>"));
  wifly.sendChunkln(F("<title>WiFly HTTP Server Example</title>"));
  wifly.sendChunkln(F("<h1>"));
  wifly.sendChunkln(F("<p>Hello</p>"));
  wifly.sendChunkln(F("</h1>"));
  wifly.sendChunkln(F("<body"));
  wifly.sendChunkln(F("bgcolor=\"#CCFFFF\">"));
  wifly.sendChunkln(F("</body>"));
  wifly.sendChunkln(F("<form name=\"input\" action=\"/\" method=\"post\">"));
  wifly.sendChunkln(F("Intensity:"));
  wifly.sendChunkln(F("<input type=\"text\" name=\"intensity\" />"));
  wifly.sendChunkln(F("<input type=\"submit\" value=\"Submit\" />"));
  wifly.sendChunkln(F("</form>"));
  wifly.sendChunkln(F("<form name = \"input\" action=\"/\" method=\"post\" >"));
  wifly.sendChunkln(F("<input type=\"hidden\" name=\"led\" />"));
  wifly.sendChunkln(F("<input type=\"submit\" value=\"LED\""));
  wifly.sendChunkln(F("</form>")); 
  wifly.sendChunkln(F("</html>"));
  wifly.sendChunkln();
}

/** Send an greeting HTML page with the user's name and an analog reading */
void sendGreeting(char *intensity)
{
  /* Send the header direclty with print */
  wifly.println(F("HTTP/1.1 200 OK"));
  wifly.println(F("Content-Type: text/html"));
  wifly.println(F("Transfer-Encoding: chunked"));
  wifly.println();

  /* Send the body using the chunked protocol so the client knows when
   * the message is finished.
   */
  wifly.sendChunkln(F("<html>"));
  wifly.sendChunkln(F("<title>WiFly HTTP Server Example</title>"));
  /* No newlines on the next parts */
  wifly.sendChunk(F("<h1><p>Value of Intensity "));
  wifly.sendChunk(intensity);
  /* Finish the paragraph and heading */
  wifly.sendChunkln(F("</p></h1>"));

  /* Include a reading from Analog pin 0 */
  snprintf_P(buf, sizeof(buf), PSTR("<p>Analog0=%d</p>"), analogRead(A0));
  wifly.sendChunkln(buf);
  wifly.sendChunkln(F("</html>"));
  int intensityVal=atoi(intensity);
  analogWrite(ledPin,intensityVal);
  wifly.sendChunkln();
}

If one works, then logically the other should work the same if you just change the parts that relate to which LED is being switched.

What's the difference between the two HTTP Post requests your forms are supposed to submit? How have you specified the LED in each request?

the difference between the 2 requests is that the 1st one submits a form reading the value from an input and then changes the brightness of the LED according to the value entered over PWM. The 2nd form submits a form including a hidden input and should then should turn on/off a LED by clicking the submit button if the hidden text is being send. However the Post request seems to not even being send because it wouldnt show a loading animation when i try to click the button. The sketch searches for a POST request and then checks for the name of the input field. in case the name is "LED" it will switch the pin high or low depending on its previous state.

It sounds as if the two cases are significantly different and not just doing the same thing to a different LED.

Is your web page actually submitting a URL request when you click the submit button? Obviously without that nothing will work. Are you using similar HTML for both forms? If one works the other should too, as long as you are submitting the right form and not mis-spelled the form name or anything like that.

wifly.sendChunkln(F("<form name="input" action="/" method="post">"));
wifly.sendChunkln(F("Intensity:"));
wifly.sendChunkln(F("<input type="text" name="intensity" />"));
wifly.sendChunkln(F("<input type="submit" value="Submit" />"));
wifly.sendChunkln(F(""));
wifly.sendChunkln(F("<form name = "input" action="/" method="post" >"));
wifly.sendChunkln(F("<input type="hidden" name="led" />"));
wifly.sendChunkln(F("<input type="submit" value="LED""));
wifly.sendChunkln(F(""));
This are both my forms. the first one works the 2nd doesnt. The submit button is located within the form so i suppose it should submit the form correctly.

PeterH:
Is your web page actually submitting a URL request when you click the submit button?

I dont really know what u mean with this. Its not changing the URL with the request because its a POST request. However if this means that its "loading" to process the submitted form it does that with the first not with my 2nd form.
Also this line it checking for the form that is submitted int match = wifly.multiMatch_P(100, 3,F("intensity="), F("led="));

Read characters from the WiFly and match them against the set of
progmem strings. Ignore any leading characters that don't match. Keep
reading, discarding the input, until one of the strings is matched
or until no characters are received for the timeout duration.

joe-hannes:
I dont really know what u mean with this.

In that case there's a good chance that the page is incorrect. I suggest you load the page in a browser, use your browser's 'view page source' option to show the raw HTML and post that here (inside [ code ] [ /code ] tags).

wifly.sendChunkln(F("Intensity:"));

What is this supposed to do in the middle of a form?

PeterH:
In that case there's a good chance that the page is incorrect. I suggest you load the page in a browser, use your browser's 'view page source' option to show the raw HTML and post that here (inside [ code ] [ /code ] tags).

good way, the problem was probably caused by the html code.

<html>
<title>WiFly HTTP Server Example</title>
<h1>
<p>Hello</p>
</h1>
<body
bgcolor="#CCFFFF">
</body>
<form name="input" action="/" method="post">
Intensity:
<input type="text" name="intensity" />
<input type="submit" value="Submit" />
</form>
<form name = "input" action="/" method="post" >
<input type="hidden" name="led" />
<input type="submit" value="LED"
</form>
</html>

Ok so this is the html code from the webpage.

wifly.sendChunkln(F("Intensity:"));

Uhm it just prints "Intensity:" on the webpage. Probably not on the right location but the example sketch i derivated my sketch from had it like that and i am not really good at HTML.

OK so i changed the line with the submit button and inserted it right before the hidden input and for some for me not visible reason it suddenly works. so thanks for your answers though :slight_smile:

In the first form you have:

<input type="submit" value="Submit" />

In the second form you have:

<input type="submit" value="LED"

The submit input element is not closed. You could fix that by putting a /> on the end of the line to close it.

ok seems im too dumb to copy code '-'. thank you for helping me ^^