1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-29 13:33:06 +01:00

More robust HTTP connection for data reading

This commit is contained in:
norbert-walter
2025-12-05 12:27:47 +01:00
parent 0972f12b9e
commit eab7d74aef
4 changed files with 28 additions and 11 deletions

View File

@@ -48,23 +48,33 @@ int NetworkClient::skipGzipHeader(const uint8_t* data, size_t len) {
// HTTP GET + GZIP Decompression (reading in chunks) // HTTP GET + GZIP Decompression (reading in chunks)
bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& outLen) { 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); uint8_t* buffer = (uint8_t*)malloc(capacity);
if (!buffer) { if (!buffer) {
if (DEBUG) {Serial.println("Malloc failed (buffer)");} if (DEBUG) {Serial.println("Malloc failed (buffer");}
return false; return false;
} }
HTTPClient http; 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.begin(url);
http.addHeader("Accept-Encoding", "gzip"); http.addHeader("Accept-Encoding", "gzip");
int code = http.GET(); int code = http.GET();
if (code != HTTP_CODE_OK) { if (code != HTTP_CODE_OK) {
Serial.printf("HTTP ERROR: %d\n", code); 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(); http.end();
free(buffer);
return false; return false;
} }
@@ -72,7 +82,7 @@ bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& ou
size_t len = 0; size_t len = 0;
uint32_t lastData = millis(); 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; bool complete = false;
@@ -122,6 +132,9 @@ bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& ou
free(test); free(test);
} }
// --- Added: Force-close connection in all cases to avoid stuck TCP sockets ---
if (stream) stream->stop();
http.end(); http.end();
free(buffer); free(buffer);

View File

@@ -3,9 +3,11 @@
#include <WiFi.h> #include <WiFi.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#define DEBUG false // Debug flag for NetworkClient for more live information #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 READLIMIT 200000 // HTTP read limit in byte for gzip content (can be adjusted)
#define NETWORKTIMEOUT 8000 // 8s Network timeout #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 { class NetworkClient {
public: public:

View File

@@ -30,6 +30,8 @@
#define FRAM_BAROGRAPH_START 0x0400 #define FRAM_BAROGRAPH_START 0x0400
#define FRAM_BAROGRAPH_END 0x13FF #define FRAM_BAROGRAPH_END 0x13FF
#define PI 3.1415926535897932384626433832795
extern Adafruit_FRAM_I2C fram; extern Adafruit_FRAM_I2C fram;
extern bool hasFRAM; extern bool hasFRAM;
extern bool hasSDCard; extern bool hasSDCard;

View File

@@ -64,7 +64,7 @@ public:
static double value2old = 0; static double value2old = 0;
static String svalue2old = ""; static String svalue2old = "";
static String unit2old = ""; static String unit2old = "";
static double value3old = 0; static double value3old = 0; // Deg
static String svalue3old = ""; static String svalue3old = "";
static String unit3old = ""; static String unit3old = "";
static double value4old = 0; static double value4old = 0;
@@ -162,8 +162,8 @@ public:
} }
// COG value (Course Over Ground) // COG value (Course Over Ground)
if(valid3){ if(valid3){
courseOverGround = value3; courseOverGround = (value3 * 360) / (2 * PI);
value3old = value3; value3old = courseOverGround;
} }
else{ else{
courseOverGround = value3old; courseOverGround = value3old;
@@ -196,7 +196,7 @@ public:
"zoom=" + zoom + // Zoom level: 15 "zoom=" + zoom + // Zoom level: 15
"&lat=" + String(latitude, 6) + // Latitude "&lat=" + String(latitude, 6) + // Latitude
"&lon=" + String(longitude, 6) + // Longitude "&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 "&mtype=1" + // Open Street Map
"&dtype=4" + // Dithering type: Atkinson dithering "&dtype=4" + // Dithering type: Atkinson dithering
"&width=400" + // With navigation map "&width=400" + // With navigation map