mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-29 05:33:05 +01:00
More robust HTTP connection for data reading
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#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:
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user