From 10bb057b108ace2e0b6f069568193e2fa8d5636d Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Thu, 12 Feb 2026 05:23:34 -0800 Subject: [PATCH] snapshot --- sketches/doorbell/doorbell.ino | 53 +++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/sketches/doorbell/doorbell.ino b/sketches/doorbell/doorbell.ino index 33c9fba..3dcf228 100644 --- a/sketches/doorbell/doorbell.ino +++ b/sketches/doorbell/doorbell.ino @@ -2,12 +2,12 @@ #include #include #include +#include // ============== CONFIGURATION ============== const char* ssid = "iot-2GHz"; const char* password = "lesson-greater"; -// TOPIC URLS — short poll (no ?poll=1) for ESP32 HTTP client compatibility 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* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic"; @@ -21,7 +21,6 @@ const unsigned long ERROR_BACKOFF = 60000; #define BACKLIGHT_PIN 22 #define BUTTON_PIN 9 -// ============== COCAINE CHIC PALETTE ============== #define COLOR_BLACK 0x0000 #define COLOR_COCAINE 0xFFFE #define COLOR_TEAL 0x05F5 @@ -33,14 +32,12 @@ const unsigned long ERROR_BACKOFF = 60000; #define ALERT_BG_2 COLOR_FUCHSIA #define ALERT_TEXT_2 COLOR_COCAINE -// ============== DISPLAY SETUP ============== #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 172 Arduino_DataBus *bus = new Arduino_HWSPI(15, 14, 7, 6, -1); Arduino_GFX *gfx = new Arduino_ST7789(bus, 21, 1, true, 172, 320, 34, 0, 34, 0); -// ============== STATE MANAGEMENT ============== enum State { STATE_SILENT, STATE_ALERT }; State currentState = STATE_SILENT; @@ -57,11 +54,13 @@ String lastSilenceId = ""; bool lastButtonState = false; String alertMessage = ""; +unsigned long long epochOffsetMs = 0; + void setup() { Serial.begin(115200); delay(3000); - Serial.println("\n=== KLUBHAUS DOORBELL v3.5 ==="); - Serial.println("Fix: Arduino String.trim() in-place modification"); + Serial.println("\n=== KLUBHAUS DOORBELL v3.6.1 ==="); + Serial.println("Fix: restored missing loop() function"); pinMode(BACKLIGHT_PIN, OUTPUT); digitalWrite(BACKLIGHT_PIN, LOW); @@ -87,12 +86,48 @@ void setup() { showFatalError("OFFLINE"); } else { Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); + initEpochTime(); publishStatus(); } Serial.println("=== READY ==="); } +// ============== EPOCH TIME ============== + +void initEpochTime() { + configTime(0, 0, "pool.ntp.org", "time.nist.gov"); + + Serial.print("Syncing NTP..."); + time_t now = 0; + int retries = 0; + while (now < 1000000000 && retries < 30) { + delay(500); + now = time(nullptr); + Serial.print("."); + retries++; + } + + if (now > 1000000000) { + epochOffsetMs = (now * 1000ULL) - millis(); + Serial.println(" OK"); + Serial.print("Epoch offset: "); + Serial.println((unsigned long)epochOffsetMs); + } else { + Serial.println(" FAILED — using boot-relative time"); + epochOffsetMs = 0; + } +} + +unsigned long long getEpochMs() { + if (epochOffsetMs == 0) { + return millis(); + } + return epochOffsetMs + millis(); +} + +// ============== MAIN LOOP (RESTORED) ============== + void loop() { unsigned long now = millis(); @@ -146,7 +181,7 @@ void publishStatus() { StaticJsonDocument<256> doc; doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING"; doc["message"] = alertMessage; - doc["timestamp"] = millis(); + doc["timestamp"] = (unsigned long long)getEpochMs(); String payload; serializeJson(doc, payload); @@ -206,8 +241,6 @@ void checkCommandTopic() { String id = doc["id"] | ""; String message = doc["message"] | ""; - // FIX: Arduino String.trim() modifies in place, returns void - // Trim first, then check length message.trim(); if (message.length() == 0) { Serial.println(" Empty message, skip"); @@ -246,7 +279,6 @@ void checkSilenceTopic() { String id = doc["id"] | ""; String message = doc["message"] | ""; - // FIX: Arduino String.trim() modifies in place message.trim(); if (message.length() == 0) return; @@ -401,7 +433,6 @@ void updateBlink(unsigned long now) { // ============== TEST ============== void testDisplay() { - const char* names[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"}; uint16_t colors[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK}; for (int i = 0; i < 5; i++) { gfx->fillScreen(colors[i]);