This commit is contained in:
2026-02-12 05:12:42 -08:00
parent 62eff3427b
commit 127b25adcc

View File

@@ -12,11 +12,11 @@ const char* COMMAND_POLL_URL = "http://ntfy.sh/ALERT_klubhaus_topic/json";
const char* SILENCE_POLL_URL = "http://ntfy.sh/SILENCE_klubhaus_topic/json"; const char* SILENCE_POLL_URL = "http://ntfy.sh/SILENCE_klubhaus_topic/json";
const char* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic"; const char* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic";
const unsigned long COMMAND_POLL_INTERVAL = 30000; // 30s normal const unsigned long COMMAND_POLL_INTERVAL = 30000;
const unsigned long SILENCE_POLL_INTERVAL = 5000; // 5s when alerting const unsigned long SILENCE_POLL_INTERVAL = 5000;
const unsigned long BLINK_DURATION = 180000; const unsigned long BLINK_DURATION = 180000;
const unsigned long BLINK_PERIOD = 1000; const unsigned long BLINK_PERIOD = 1000;
const unsigned long ERROR_BACKOFF = 60000; // Back off on 429/500 const unsigned long ERROR_BACKOFF = 60000;
#define BACKLIGHT_PIN 22 #define BACKLIGHT_PIN 22
#define BUTTON_PIN 9 #define BUTTON_PIN 9
@@ -48,7 +48,7 @@ State lastPublishedState = STATE_SILENT;
unsigned long lastCommandPoll = 0; unsigned long lastCommandPoll = 0;
unsigned long lastSilencePoll = 0; unsigned long lastSilencePoll = 0;
unsigned long nextPollTime = 0; // Backoff timer unsigned long nextPollTime = 0;
unsigned long blinkStartTime = 0; unsigned long blinkStartTime = 0;
bool blinkPhase = false; bool blinkPhase = false;
@@ -60,8 +60,8 @@ String alertMessage = "";
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(3000); delay(3000);
Serial.println("\n=== KLUBHAUS DOORBELL v3.4 ==="); Serial.println("\n=== KLUBHAUS DOORBELL v3.5 ===");
Serial.println("Fix: Short polling, 204 handling, rate limit backoff"); Serial.println("Fix: Arduino String.trim() in-place modification");
pinMode(BACKLIGHT_PIN, OUTPUT); pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
@@ -96,7 +96,6 @@ void setup() {
void loop() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
// Respect backoff after errors
if (now < nextPollTime) { if (now < nextPollTime) {
delay(100); delay(100);
return; return;
@@ -129,7 +128,7 @@ void loop() {
break; break;
} }
delay(50); // Small delay to prevent tight loops delay(50);
} }
// ============== STATUS PUBLISHING ============== // ============== STATUS PUBLISHING ==============
@@ -207,8 +206,10 @@ void checkCommandTopic() {
String id = doc["id"] | ""; String id = doc["id"] | "";
String message = doc["message"] | ""; String message = doc["message"] | "";
// CRITICAL: Skip empty messages // FIX: Arduino String.trim() modifies in place, returns void
if (message.length() == 0 || message.trim().length() == 0) { // Trim first, then check length
message.trim();
if (message.length() == 0) {
Serial.println(" Empty message, skip"); Serial.println(" Empty message, skip");
return; return;
} }
@@ -245,8 +246,9 @@ void checkSilenceTopic() {
String id = doc["id"] | ""; String id = doc["id"] | "";
String message = doc["message"] | ""; String message = doc["message"] | "";
// Skip empty messages // FIX: Arduino String.trim() modifies in place
if (message.length() == 0 || message.trim().length() == 0) return; message.trim();
if (message.length() == 0) return;
if (id == lastSilenceId && id.length() > 0) return; if (id == lastSilenceId && id.length() > 0) return;
lastSilenceId = id; lastSilenceId = id;
@@ -265,17 +267,15 @@ void checkSilenceTopic() {
String fetchNtfyJson(const char* url) { String fetchNtfyJson(const char* url) {
HTTPClient http; HTTPClient http;
http.begin(url); http.begin(url);
http.setTimeout(8000); // 8s timeout for short poll http.setTimeout(8000);
int code = http.GET(); int code = http.GET();
// 204 No Content = no new messages (normal, not an error)
if (code == 204) { if (code == 204) {
http.end(); http.end();
return ""; return "";
} }
// Rate limited or server error — back off
if (code == 429 || code == 500 || code == 502 || code == 503) { if (code == 429 || code == 500 || code == 502 || code == 503) {
Serial.print(" HTTP error "); Serial.print(" HTTP error ");
Serial.print(code); Serial.print(code);
@@ -295,7 +295,6 @@ String fetchNtfyJson(const char* url) {
String payload = http.getString(); String payload = http.getString();
http.end(); http.end();
// Take first line if multiple JSON objects
int newline = payload.indexOf('\n'); int newline = payload.indexOf('\n');
if (newline > 0) payload = payload.substring(0, newline); if (newline > 0) payload = payload.substring(0, newline);