Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By FOXi
#33504 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?
You do not have the required permissions to view the files attached to this post.
Last edited by FOXi on Tue Nov 10, 2015 8:38 am, edited 1 time in total.
User avatar
By martinayotte
#33548 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);
  }
User avatar
By FOXi
#33619
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.
User avatar
By bubba198
#34136
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