From 1daed05d7ad2af35366d931276bb7ea1a6143044 Mon Sep 17 00:00:00 2001 From: TD-er Date: Fri, 19 Oct 2018 22:10:24 +0200 Subject: [PATCH] Proper look for connection. See discussion here: https://github.com/esp8266/Arduino/issues/5257 --- src/_C003.ino | 4 ++-- src/_C012.ino | 4 ++-- src/_CPlugin_Helper.h | 51 +++++++++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/_C003.ino b/src/_C003.ino index 346808b56d..f6c5c6385d 100644 --- a/src/_C003.ino +++ b/src/_C003.ino @@ -78,7 +78,7 @@ bool do_process_c003_delay_queue(int controller_number, const C003_queue_element client.print(" \n"); unsigned long timer = millis() + 200; - while (!client.available() && !timeOutReached(timer)) + while (!client_available(client) && !timeOutReached(timer)) delay(1); timer = millis() + 1000; @@ -102,7 +102,7 @@ bool do_process_c003_delay_queue(int controller_number, const C003_queue_element addLog(LOG_LEVEL_DEBUG, log); client.println(SecuritySettings.ControllerPassword[element.controller_idx]); delay(100); - while (client.available()) + while (client_available(client)) client.read(); strcpy_P(log, PSTR("TELNT: Sending cmd")); diff --git a/src/_C012.ino b/src/_C012.ino index 6db4dfecba..23a4a4727e 100644 --- a/src/_C012.ino +++ b/src/_C012.ino @@ -106,8 +106,8 @@ boolean Blynk_get(const String& command, byte controllerIndex, float *data ) boolean success = !ControllerSettings.MustCheckReply; if (ControllerSettings.MustCheckReply || data) { unsigned long timer = millis() + 200; - while (!client.available() && !timeOutReached(timer)) - yield(); + while (!client_available(client) && !timeOutReached(timer)) + delay(1); char log[80] = {0}; diff --git a/src/_CPlugin_Helper.h b/src/_CPlugin_Helper.h index 1eb75a8a10..1493bd6413 100644 --- a/src/_CPlugin_Helper.h +++ b/src/_CPlugin_Helper.h @@ -402,26 +402,30 @@ bool safeReadStringUntil(Stream &input, String &str, char terminator, unsigned i do { //read character - c = input.read(); - if (c >= 0) { - //found terminator, we're ok - if (c == terminator) { - return(true); - } - //found character, add to string - else{ - str += char(c); - //string at max size? - if (str.length() >= maxSize) { - addLog(LOG_LEVEL_ERROR, F("Not enough bufferspace to read all input data!")); - return(false); - } - } - } - // We must run the backgroundtasks every now and then. - if (timeOutReached(backgroundtasks_timer)) { - backgroundtasks_timer += 10; - backgroundtasks(); + if (input.available()) { + c = input.read(); + if (c >= 0) { + //found terminator, we're ok + if (c == terminator) { + return(true); + } + //found character, add to string + else{ + str += char(c); + //string at max size? + if (str.length() >= maxSize) { + addLog(LOG_LEVEL_ERROR, F("Not enough bufferspace to read all input data!")); + return(false); + } + } + } + // We must run the backgroundtasks every now and then. + if (timeOutReached(backgroundtasks_timer)) { + backgroundtasks_timer += 10; + backgroundtasks(); + } else { + yield(); + } } else { yield(); } @@ -621,6 +625,11 @@ bool try_connect_host(int controller_number, WiFiClient& client, ControllerSetti // See: https://github.com/esp8266/Arduino/pull/5113 // https://github.com/esp8266/Arduino/pull/1829 bool client_available(WiFiClient& client) { + #ifdef ESP32 + yield(); + #else + esp_yield(); // Could be called from events + #endif return client.available() || client.connected(); } @@ -631,7 +640,7 @@ bool send_via_http(const String& logIdentifier, WiFiClient& client, const String if (must_check_reply) { unsigned long timer = millis() + 200; - while (!client.available()) { + while (!client_available(client)) { if (timeOutReached(timer)) return false; delay(1); }