-->
Page 1 of 1

HTTP GET response problem[SOLVED]

PostPosted: Sun Nov 08, 2015 7:30 pm
by FOXi
Hi,
I am trying to get response from my web server(Raspberry) via HTTP GET method, but I don't see any reply on my client serial monitor (ESP).
Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "Pi_AP";
const char* password = "Raspberry";

const char* host = "data.sparkfun.com";
const char* streamId   = "1/";
IPAddress server(192, 168, 2, 1);

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8000;
  if (!client.connect(server, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = "/start/";
  url += streamId;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: 192.168.2.1" + "\r\n\r\n");
               //"Host: " + host + "\r\n" + 
               //"Connection: close\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}

My request successfully reach the server because function on my server is executed.
Web server is working correctly I test it from terminal
Code: Select all$ curl -i -X GET http://192.168.2.1:8000/start/1/
HTTP/1.1 200 OK
Server: gunicorn/19.3.0
Date: Sun, 08 Nov 2015 22:59:59 GMT
Connection: close
Transfer-Encoding: chunked
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8


<!DOCTYPE html>
<html lang="en">
<head>
.......

Output from my serial monitor is here:
aa.png


Can you help me? Why I don't see response from server in my serial monitor?

Re: HTTP GET response problem

PostPosted: Mon Nov 09, 2015 10:05 am
by martinayotte
Did you try to simply place a longer delay instead of your delay(10) ?
Normally, it would be better to wait for a response instead of a delay, and check for timeout if no response at all.
Here is what I'm usually doing :

Code: Select all  int timeout = millis() + 5000;
  while (client.available() == 0) {
    if (timeout - millis() < 0) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  while(client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

Re: HTTP GET response problem

PostPosted: Tue Nov 10, 2015 8:37 am
by FOXi
martinayotte wrote:Did you try to simply place a longer delay instead of your delay(10) ?
Normally, it would be better to wait for a response instead of a delay, and check for timeout if no response at all.
Here is what I'm usually doing :

Code: Select all  int timeout = millis() + 5000;
  while (client.available() == 0) {
    if (timeout - millis() < 0) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  while(client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

It's working now. Thanks.

Re: HTTP GET response problem

PostPosted: Sun Nov 15, 2015 7:49 pm
by bubba198
martinayotte wrote:Did you try to simply place a longer delay instead of your delay(10) ? Normally, it would be better to wait for a response instead of a delay, and check for timeout if no response at all. Here is what I'm usually doing :


martinayotte you are a life saver.

Thank you! I was missing characters due to the server responding to the GET request in a very fragmented fashion (who knows why) and so now it does take longer to assemble the response as far as my code is concerned but the response is 100% reliable.
!B