From eab7d74aef6d697b3f1aaac5768fc3c4f1f65b20 Mon Sep 17 00:00:00 2001 From: norbert-walter Date: Fri, 5 Dec 2025 12:27:47 +0100 Subject: [PATCH] More robust HTTP connection for data reading --- lib/obp60task/NetworkClient.cpp | 21 +++++++++++++++++---- lib/obp60task/NetworkClient.h | 8 +++++--- lib/obp60task/OBP60Extensions.h | 2 ++ lib/obp60task/PageNavigation.cpp | 8 ++++---- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/obp60task/NetworkClient.cpp b/lib/obp60task/NetworkClient.cpp index 9e75ae5..66b973d 100644 --- a/lib/obp60task/NetworkClient.cpp +++ b/lib/obp60task/NetworkClient.cpp @@ -48,23 +48,33 @@ int NetworkClient::skipGzipHeader(const uint8_t* data, size_t len) { // HTTP GET + GZIP Decompression (reading in chunks) bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& outLen) { - const size_t capacity = READLIMIT; // limit (can be adjusted in NetworkClient.h) + const size_t capacity = READLIMIT; // Read limit for data (can be adjusted in NetworkClient.h) uint8_t* buffer = (uint8_t*)malloc(capacity); if (!buffer) { - if (DEBUG) {Serial.println("Malloc failed (buffer)");} + if (DEBUG) {Serial.println("Malloc failed (buffer");} return false; } HTTPClient http; + + // Timeouts to prevent hanging connections + http.setConnectTimeout(CONNECTIONTIMEOUT); // Connect timeout in ms (can be adjusted in NetworkClient.h) + http.setTimeout(TCPREADTIMEOUT); // Read timeout in ms (can be adjusted in NetworkClient.h) + http.begin(url); http.addHeader("Accept-Encoding", "gzip"); int code = http.GET(); if (code != HTTP_CODE_OK) { Serial.printf("HTTP ERROR: %d\n", code); - free(buffer); + + // Hard reset HTTP + socket + WiFiClient* tmp = http.getStreamPtr(); + if (tmp) tmp->stop(); // Force close TCP socket http.end(); + + free(buffer); return false; } @@ -72,7 +82,7 @@ bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& ou size_t len = 0; uint32_t lastData = millis(); - const uint32_t READ_TIMEOUT = NETWORKTIMEOUT; // Network timeout for reading data (can be adjusted in NetworkClient.h) + const uint32_t READ_TIMEOUT = READDATATIMEOUT; // Timeout for reading data (can be adjusted in NetworkClient.h) bool complete = false; @@ -122,6 +132,9 @@ bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& ou free(test); } + // --- Added: Force-close connection in all cases to avoid stuck TCP sockets --- + if (stream) stream->stop(); + http.end(); free(buffer); diff --git a/lib/obp60task/NetworkClient.h b/lib/obp60task/NetworkClient.h index 1a29178..84d7a87 100644 --- a/lib/obp60task/NetworkClient.h +++ b/lib/obp60task/NetworkClient.h @@ -3,9 +3,11 @@ #include #include -#define DEBUG false // Debug flag for NetworkClient for more live information -#define READLIMIT 200000 // HTTP read limit in byte for gzip content (can be adjusted) -#define NETWORKTIMEOUT 8000 // 8s Network timeout +#define DEBUG false // Debug flag for NetworkClient for more live information +#define READLIMIT 200000 // HTTP read limit in byte for gzip content (can be adjusted) +#define CONNECTIONTIMEOUT 3000 // Timeout in ms for HTTP connection +#define TCPREADTIMEOUT 2000 // Timeout in ms for read HTTP client stack +#define READDATATIMEOUT 2000 // Timeout in ms for read data class NetworkClient { public: diff --git a/lib/obp60task/OBP60Extensions.h b/lib/obp60task/OBP60Extensions.h index 15d2396..33b9218 100644 --- a/lib/obp60task/OBP60Extensions.h +++ b/lib/obp60task/OBP60Extensions.h @@ -30,6 +30,8 @@ #define FRAM_BAROGRAPH_START 0x0400 #define FRAM_BAROGRAPH_END 0x13FF +#define PI 3.1415926535897932384626433832795 + extern Adafruit_FRAM_I2C fram; extern bool hasFRAM; extern bool hasSDCard; diff --git a/lib/obp60task/PageNavigation.cpp b/lib/obp60task/PageNavigation.cpp index 61fae93..43803fa 100644 --- a/lib/obp60task/PageNavigation.cpp +++ b/lib/obp60task/PageNavigation.cpp @@ -64,7 +64,7 @@ public: static double value2old = 0; static String svalue2old = ""; static String unit2old = ""; - static double value3old = 0; + static double value3old = 0; // Deg static String svalue3old = ""; static String unit3old = ""; static double value4old = 0; @@ -162,8 +162,8 @@ public: } // COG value (Course Over Ground) if(valid3){ - courseOverGround = value3; - value3old = value3; + courseOverGround = (value3 * 360) / (2 * PI); + value3old = courseOverGround; } else{ courseOverGround = value3old; @@ -196,7 +196,7 @@ public: "zoom=" + zoom + // Zoom level: 15 "&lat=" + String(latitude, 6) + // Latitude "&lon=" + String(longitude, 6) + // Longitude - "&mrot=" + int(courseOverGround) + // Rotation angle navigation map + "&mrot=" + int(courseOverGround) + // Rotation angle navigation map in degree "&mtype=1" + // Open Street Map "&dtype=4" + // Dithering type: Atkinson dithering "&width=400" + // With navigation map