Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Energia to program TI's CC3200 MCU. Since Energia is based off the Arduino IDE, I'm hoping someone can help me here.

I'm trying to get the CC3200 to act as a Webserver which when successfully starts up serves up a page with two fields for the username and password that the user needs to fill. Now I'm able to see that the two fields are being successfully POST'd (through Serial.write but only when the code to get it into buffer is not present) but I was hoping someone can help me get this content into a character array (called buffer declared before the setup loop begins).

The code in the section //Here is where the POST data is does not seem to be working as I see garbage values in buffer and in the Serial.write. How do I get these POST values into buffer OR even better how do I split the two field values and store them into two separate character arrays?

void loop()
{
  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    //Serial.println("Client connected");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;

    while (client.connected()) {
      while(client.available()) {
        char c = client.read();
        // 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) {

            // Here is where the POST data is.
            Serial.println("Post data: ");
            int ctr = 0;
            while(client.available())
            {
            Serial.write(client.read());
            buffer[ctr] = client.read();
            ctr++;
        }
        Serial.println("Buffer: ");
        Serial.write(buffer);
        Serial.println();

        //Serial.println("Sending response");
        // send a standard http response header
        client.println("HTTP/1.0 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: keep-alive");
        client.println();

        //form added to send data from browser and view received data in serial monitor
        client.println();
        client.println("<!DOCTYPE html>");
        client.println("<html lang=\"en\">");
        client.println("<body>");
        client.println(" <FORM action=\"\" method=\"POST\">");
        client.println("    <P>");
        client.println("    <LABEL for=\"username\">Username:</LABEL>");
        client.println("    <INPUT type=\"text\" name=\"uname\"><BR><BR>");
        client.println("    <LABEL for=\"password\">Password:</LABEL>");
        client.println("    <INPUT type=\"text\" name=\"pwd\"><BR><BR>");
        client.println("    <INPUT type=\"submit\" value=\"Submit\">");
        client.println("    </P>");
        client.println(" </FORM>");
        client.println("</body>");
        client.println("</html>");

        client.println();

        client.stop();
        }
        else 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;
        }
      }
    }
    //Serial.println("Disconnected");
    Serial.println();
  }
}
share|improve this question
    
What sort of garbage data are you seeing? – Majenko Jun 12 at 20:20
up vote 1 down vote accepted

In this loop

while(client.available()) { 
    Serial.write(client.read());
    buffer[ctr] = client.read();

you're using client.read() twice, so the buffer gets only every second character.

A solution could be:

while(client.available()) { 
    int c = client.read();
    Serial.write(c);
    buffer[ctr] = c;
share|improve this answer
    
Thanks! This code worked great! int ctr = 0; while(client.available()) { buffer[ctr] = client.read(); ctr++; } – hpb Jun 12 at 21:26
    
Any idea why this loop runs twice? I see two outputs of the buffer. Is it once when I enter the URL and hit Enter and the second when I hit Submit? – hpb Jun 13 at 11:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.