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();
}