harlequin-tech / WiFlyHQ

WiFly RN-XV Arduino Library
Other
110 stars 68 forks source link

Problem pulling down webpage via GET #21

Closed xabierlegasa closed 11 years ago

xabierlegasa commented 11 years ago

I'm able to make successfully a GET request with this code:

void HTTPrequestGET(){
  if (wifly.open(site, 80)){  
    wifly.println("GET http://example.com/something.php HTTP/1.0");
    // Some NECESSARY headers
    wifly.println("Host: example.com");
    wifly.println("User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/20100101 Firefox/17.0");
    wifly.println("Content-type: text/html");
    wifly.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    wifly.println("Accept-Language: es,en-us;q=0.8,en;q=0.6,eu;q=0.4,ca;q=0.2");
    wifly.println("Accept-Encoding: gzip, deflate");
    wifly.println("Connection: close");
    wifly.println();
  } else {
    scrollingText("FAILED TO CONNECT");
  } 
}

Im sure the request arrives to the server because I create a record on a certain database each time it happens. BUT after my request, I'm getting only 62 characters back!. Exactly I get this:

HTTP/1.1 200 Ok
Date: Wed, 03 Apr 2013 09:30:17 GMT
Server: A

While if I go to the same URL with my browser, it gives me the full headers and page.

HTTP/1.1 200 OK
Date: Wed, 03 Apr 2013 14:21:48 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Keep-Alive: timeout=2, max=200
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/xml
<something>hello</something>

My problem is that I don't really know where to look at to solve this. Could it be a memory issue (RN module?) Maybe the memory of the RN-XV is full? How can I check this? I thought wifly.read() would make free the memory. Do I have to do flush() somewhere or it has nothing to do with this?

Any idea? Thanks!

I'm taking the characters in my loop() function this way:

void loop() {
    if (wifly.available() > 0) {
      ch = wifly.read(); 

      charValue = ch;
      String stringTwo = "C:";
      stringTwo += charValue;
      char charBufferTwo[10];
      stringTwo.toCharArray(charBufferTwo, 10);
      printString(charBufferTwo); delay(300);
    }else{
      printString("NOTHING"); delay(1000);
      panel.clear();
    }
}

I tryed to debug and added this line in each loop:

bytesToRead = wifly.available();

This tells me that first time bytesToRead is 62 and then it goes decreasing in each loop (because I do wifly.read() ) and it finally arrives to 0.

harlequin-tech commented 11 years ago

I'd suggest not using delay() in your code. When delay() is called it stops the Arduino from doing anything for that period of time, so the serial buffer overflows and you miss out on the rest of the data.

The String type should also be avoided. It's a memory hog and will eat up the RAM.

xabierlegasa commented 11 years ago

Solved!

You were totally right. delay() is not a good idea here... After deleting all the delay() calls (and the String), now I get all the page. So now I'm using at my loop() function the code from WiflyHQ Library/examples/ htttpclient.ino file:

void loop() {
    if (wifly.available() > 0) {
      ch = wifly.read(); 
      Serial.write(ch);
      if (ch == '\n') {
        /* add a carriage return */ 
        Serial.write('\r');
      }
    }
}

NOTE: Using Serial.println(ch) instead of Serial.write(ch) was giving me incorrect letters on my Serial Monitor (only in the body!, headers were ok) so check this if you experiment same problem.

Thank you!!