Skip to content
This repository was archived by the owner on Jul 31, 2026. It is now read-only.

Commit 00564c4

Browse files
fix: clamp displayed weight to 0g when below MIN_WEIGHT_TO_SEND_G
Negative weights (-1g, -2g, -3g) caused by HX711 tare drift/dead zone were displayed as-is in the web UI. Firestore and OLED were already protected; the WS frame and /api/status were the only unprotected paths. - WS frame: displayW = weight < 5g ? 0 : roundWeight(weight) - /api/status: same clamp on weight field; rawWeight kept unfiltered for debug - Internal values (currentWeight, slope, tare) remain unclamped Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b4e260c commit 00564c4

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

‎src/main.cpp‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
// §12 FIRESTORE SCALE HEARTBEAT & SYNC 1101– 2031
2222
// §13 WEBSOCKET 2032– 2058
2323
// §14 5 — CLOUD WORKER TASK (non-blocking Firestore on core 0) 2059– 2118
24-
// §15 5 — UNIFIED WS FRAME BUILDER 2119– 2158
25-
// §16 WEIGHT FILTER HELPERS 2159– 2173
26-
// §17 POST-SEND STATE RESET (shared by all send paths) 2174– 2192
27-
// §18 SHARED WEIGHT PUSH HANDLER (used by /api/weight and /api/push-weight) 2193– 2265
28-
// §19 WEB SERVER 2266– 2966
29-
// §20 CLOUD COMMUNICATION 2967– 3149
30-
// §21 WEIGH WORKFLOW (IDLE → SCANNING → STABLE_WAIT → SENDING) 3150– 3385
31-
// §22 mDNS 3386– 3423
32-
// §23 SCALE 3424– 3515
33-
// §24 RFID 3516– 3845
34-
// §25 OTA — Over-the-air firmware + filesystem update 3846– 4214
35-
// §26 SETUP & LOOP 4215– 4591
24+
// §15 5 — UNIFIED WS FRAME BUILDER 2119– 2162
25+
// §16 WEIGHT FILTER HELPERS 2163– 2177
26+
// §17 POST-SEND STATE RESET (shared by all send paths) 2178– 2196
27+
// §18 SHARED WEIGHT PUSH HANDLER (used by /api/weight and /api/push-weight) 2197– 2269
28+
// §19 WEB SERVER 2270– 2970
29+
// §20 CLOUD COMMUNICATION 2971– 3153
30+
// §21 WEIGH WORKFLOW (IDLE → SCANNING → STABLE_WAIT → SENDING) 3154– 3389
31+
// §22 mDNS 3390– 3427
32+
// §23 SCALE 3428– 3519
33+
// §24 RFID 3520– 3849
34+
// §25 OTA — Over-the-air firmware + filesystem update 3850– 4218
35+
// §26 SETUP & LOOP 4219– 4595
3636
//
3737
// To regenerate this block: ./scripts/update_toc.sh
3838
// ─── TOC END ───────────────────────────────────────────────
@@ -2132,10 +2132,14 @@ static String buildWsFrame(float weight) {
21322132
else if (sendPhase == "error") stcWs = "error";
21332133

21342134
int cwt = (gLastContainer > 0.0f) ? (int)roundf(gLastContainer) : 0;
2135-
int nwt = (cwt > 0 && roundWeight(weight) > cwt) ? roundWeight(weight) - cwt : 0;
2135+
// Clamp displayed weight to 0 when below MIN_WEIGHT_TO_SEND_G — avoids showing
2136+
// "-1g / -2g" on the web UI due to tare dead zone or HX711 drift.
2137+
// Raw value is kept internally for tare and slope calculations.
2138+
int displayW = (weight < MIN_WEIGHT_TO_SEND_G) ? 0 : roundWeight(weight);
2139+
int nwt = (cwt > 0 && displayW > cwt) ? displayW - cwt : 0;
21362140

21372141
StaticJsonDocument<768> doc;
2138-
doc["weight"] = roundWeight(weight);
2142+
doc["weight"] = displayW;
21392143
doc["netWeight"] = nwt;
21402144
doc["containerWeight"] = cwt;
21412145
doc["uid"] = lastUID;
@@ -2383,8 +2387,8 @@ void setupWebServer() {
23832387
// Status - uses ArduinoJson
23842388
server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){
23852389
StaticJsonDocument<640> doc;
2386-
doc["weight"] = roundWeight(currentWeight);
2387-
doc["rawWeight"] = (float)((int)(currentWeight * 100)) / 100.0f;
2390+
doc["weight"] = (currentWeight < MIN_WEIGHT_TO_SEND_G) ? 0 : roundWeight(currentWeight);
2391+
doc["rawWeight"] = (float)((int)(currentWeight * 100)) / 100.0f; // unfiltered, for debug
23882392
doc["uid"] = lastUID;
23892393
doc["uid_hex"] = lastUIDHex;
23902394
doc["uid2"] = lastUID2;

0 commit comments

Comments
 (0)