Client read timeout issue
Posted: Tue Sep 22, 2015 3:43 pm
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
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);
}