-->
Page 1 of 3

Client read timeout issue

PostPosted: Tue Sep 22, 2015 3:43 pm
by hakha4
I'm connecting to a server (MLServer - Cinemar) sending sensordata at intervals on port 23 from an ESP client and that works ok. I also wan't to send commands to the ESP client(not as a respond to incoming data) and I can do so but program hangs waiting for incoming data preventing the sensordata to be sent until some data has been revieved, I've tried several approches to timeout and close connection in the reading part but without success. Can someone enlight me how to do this? It's probably a smarter way to code but if I can manage to timeout it will serve my purposes.

Thank's in advance

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "*******";
const char* password = "********";
const int Port = 23;
const char* host = "192.168.1.30";
String InputString;
// Use WiFiClient class to create TCP connections
WiFiClient client;


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

  // 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());


}



void loop() {

  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    return;
  }





  Serial.println("Sending ...");
  client.print("TEST DATA TO MLServer\r\n");
  delay(10);
  // yield();

  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    return;
  }

  // Read cmd:s FROM MLServer
  //*********************************************************
  //Connected to MLServer waiting for incoming data
  //How to timeout and exit if no data avaliable ???????
  //*********************************************************
  while (client.connected()) {
    while (client.available()) {
      String line = client.readStringUntil('\r');
      Serial.println(line);
      return;
    }
  }


  delay(3000);

}

Re: Client read timeout issue

PostPosted: Tue Sep 22, 2015 4:08 pm
by martinayotte
You code is using "return" in many places in the loop().
Your should better adding "else" statements and continue to the next task.
Same thing apply for the readStringUntil(), you should not simply return, but you should empty all received data and when no more data is received, you can either relying that server has disconnect (the client.available() will return false) or issue a client.stop() and break outside the outer while().

Re: Client read timeout issue

PostPosted: Tue Sep 22, 2015 5:54 pm
by hakha4
martinayotte wrote:You code is using "return" in many places in the loop().
Your should better adding "else" statements and continue to the next task.
Same thing apply for the readStringUntil(), you should not simply return, but you should empty all received data and when no more data is received, you can either relying that server has disconnect (the client.available() will return false) or issue a client.stop() and break outside the outer while().


Thank's for reply! Have I understood your advices correct? Still stops until data is sent. Is it the right way to send code if (!client.connect(host, Port)) {............. } twice as I do or is another approach better for switching betwwen sending/reading ?

Regards HÃ¥kan


Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "********";
const char* password = "************";
const int Port = 23;
const char* host = "192.168.1.30";
String InputString;
// Use WiFiClient class to create TCP connections
WiFiClient client;


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

  // 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());


}



void loop() {

  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    // return;
  }
  else {

    Serial.println("Sending ...");
    client.print("TEST DATA TO MLServer\r\n");
    delay(10);
    // yield();
  }
  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    // return;
  }

  else {
    // Read all the lines of the reply from server and print them to Serial
    //*********************************************************
    //Connected to MLServer waiting for incoming data
    //How to timeout and exit if no data avaliable ???????
    //*********************************************************

    while (client.connected()) {


      while (client.available()) {
        String line = client.readStringUntil('\r');
        delay(10);
        Serial.println(line);
        line = "";

      }

      // client.stop();
    }
    client.stop();

    delay(3000);
  }



}

Re: Client read timeout issue

PostPosted: Tue Sep 22, 2015 7:35 pm
by martinayotte
It is a bit better, but I'm not sure when reading the code what is the scenario ...

Is the client.readStringUntil('\r') is reading the answer of the first client.print() ?
If yes, then you don't need to do a second connect, especially that the reply will be send to the first, nothing will be received on the second since you don't do a client.print() on the second.

If not, and you just wish to received a connection request later from the server, let says an hour later, then you need to implement a server on ESP side too, which is the goal of client.available(). But I don't see any server code here.