Skip to content

Commit ccd5010

Browse files
committed
Improve memory handling and analog resolution setup
Set analogReadResolution to 12 bits in the client to match sensor divisor. On the server, allocate DynamicJsonDocument on the heap to prevent stack overflow and increase buffer size for client config serialization to 4096 bytes. Added clarifying comments for RPM calculation and memory handling.
1 parent 9015680 commit ccd5010

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

TankAlarm-112025-Client-BluesOpta/TankAlarm-112025-Client-BluesOpta.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ void setup() {
327327
Serial.println();
328328
Serial.println(F("Tank Alarm Client 112025 starting"));
329329

330+
// Set analog resolution to 12-bit to match the /4095.0f divisor used in readTankSensor
331+
analogReadResolution(12);
332+
330333
initializeStorage();
331334
ensureConfigLoaded();
332335
printHardwareRequirements(gConfig);
@@ -1268,6 +1271,7 @@ static float readTankSensor(uint8_t idx) {
12681271
// Calculate RPM from pulse count
12691272
// Formula: RPM = (pulses / sample_duration_seconds) * 60 seconds/minute / pulses_per_revolution
12701273
// Simplified: RPM = (pulses * 60000) / (sample_duration_ms * pulses_per_rev)
1274+
// Note: Max RPM is limited by the polling loop speed (approx 30,000 RPM at 1 pulse/rev), which is sufficient.
12711275
const float MS_PER_MINUTE = 60000.0f;
12721276
uint8_t pulsesPerRev = (cfg.pulsesPerRevolution > 0) ? cfg.pulsesPerRevolution : 1;
12731277
float rpm = ((float)pulseCount * MS_PER_MINUTE) / ((float)RPM_SAMPLE_DURATION_MS * (float)pulsesPerRev);

TankAlarm-112025-Server-BluesOpta/TankAlarm-112025-Server-BluesOpta.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,7 +3308,13 @@ static void sendTankJson(EthernetClient &client) {
33083308
}
33093309

33103310
static void sendClientDataJson(EthernetClient &client) {
3311-
DynamicJsonDocument doc(CLIENT_JSON_CAPACITY);
3311+
// Allocate large JSON document on heap instead of stack to prevent overflow
3312+
DynamicJsonDocument *docPtr = new DynamicJsonDocument(CLIENT_JSON_CAPACITY);
3313+
if (!docPtr) {
3314+
respondStatus(client, 500, F("Server Out of Memory"));
3315+
return;
3316+
}
3317+
DynamicJsonDocument &doc = *docPtr;
33123318

33133319
JsonObject serverObj = doc.createNestedObject("server");
33143320
serverObj["name"] = gConfig.serverName;
@@ -3407,9 +3413,11 @@ static void sendClientDataJson(EthernetClient &client) {
34073413
String json;
34083414
if (serializeJson(doc, json) == 0) {
34093415
respondStatus(client, 500, F("Failed to encode client data"));
3416+
delete docPtr;
34103417
return;
34113418
}
34123419
respondJson(client, json);
3420+
delete docPtr;
34133421
}
34143422

34153423
static void handleConfigPost(EthernetClient &client, const String &body) {
@@ -3605,7 +3613,7 @@ static bool sendRelayCommand(const char *clientUid, uint8_t relayNum, bool state
36053613
}
36063614

36073615
static ConfigDispatchStatus dispatchClientConfig(const char *clientUid, JsonVariantConst cfgObj) {
3608-
char buffer[1536];
3616+
char buffer[4096];
36093617
size_t len = serializeJson(cfgObj, buffer, sizeof(buffer));
36103618
if (len == 0 || len >= sizeof(buffer)) {
36113619
Serial.println(F("Client config payload too large"));

0 commit comments

Comments
 (0)