Search

arduino html POST data

arduino html POST data

Here is where I stand so far with my arduino development.

I am trying to get an arduino to read and parse html POST data.

I can get the arduino to see the data (it is packed as an extra line at the end of the header).
I just need to write something to convert an example into meaningful values.

To read the POST information:

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // it is after the double cr-lf that the variables are
          // read another line!
          String POST = "";
          while(client.available())
          {
            c = client.read();
            // save the variables somewhere
            POST += c;
          }
          Serial.println(POST);
          // Split POST
          if(POST != "")
          {
            // C0=471&C1=431&C2=388&C3=350&C4=387&C5=321
            
          }
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          //client.println("<meta http-equiv=\"refresh\" content=\"5\"/>");
          // output the value of each analog input pin
          client.print("<form method='post'>");
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            String str;
            str  = String("<input type='text' value = '");
            str = str + sensorReading + String("' name='C")  + analogChannel + String("'>");
            client.print(str);

            client.println("<br />");       
          }
          client.print("<input type='submit'/></form>");

          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

A standard POST line looks like

C0=471&C1=431&C2=388&C3=350&C4=387&C5=321
Separation is using the & character

Now I just need to split the POST line so it can be worked on.

Something will come to me no doubt. It is not difficult.

I think I will take each name=value and send it to a function to work on each one at a time.

Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.