diff --git a/sketches/doorbell/doorbell.ino b/sketches/doorbell/doorbell.ino index 107ae71..ddeedd6 100644 --- a/sketches/doorbell/doorbell.ino +++ b/sketches/doorbell/doorbell.ino @@ -7,84 +7,74 @@ const char* ssid = "iot-2GHz"; const char* password = "lesson-greater"; -const char* alertTopic = "http://ntfy.sh/ALERT_klubhaus_topic/json?poll=1"; -const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1"; +// Refactored topic architecture +const char* commandTopic = "http://ntfy.sh/ALERT_klubhaus_topic/json?poll=1"; // Incoming: FRONT DOOR, LOADING DOCK, custom +const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1"; // Incoming: any message = silence +const char* statusTopic = "http://ntfy.sh/STATUS_klubhaus_topic"; // Outgoing: device publishes state -const unsigned long POLL_INTERVAL = 30000; +const unsigned long POLL_INTERVAL = 15000; const unsigned long SILENCE_POLL_INTERVAL = 5000; const unsigned long BLINK_DURATION = 180000; const unsigned long BLINK_PERIOD = 1000; +const unsigned long STATUS_PUBLISH_INTERVAL = 30000; // Periodic status broadcast #define BACKLIGHT_PIN 22 #define BUTTON_PIN 9 // ============== COCAINE CHIC PALETTE ============== -// Miami Vice meets gallery minimal — neon pastels on stark grounds +#define COLOR_BLACK 0x0000 // #000000 +#define COLOR_COCAINE 0xFFFE // #fff8fa — warm white +#define COLOR_TEAL 0x05F5 // #0bd3d3 — electric teal +#define COLOR_FUCHSIA 0xFC6E // #f890e7 — hot pink +#define COLOR_MINT 0x674D // #64e8ba — confirmation -#define COLOR_BLACK 0x0000 // Midnight void -#define COLOR_COCAINE 0xFFFE // #fff8fa — warm white, almost pink -#define COLOR_TEAL 0x05F5 // #0bd3d3 — electric neon teal -#define COLOR_FUCHSIA 0xFC6E // #f890e7 — hot pink neon -#define COLOR_MINT 0x674D // #64e8ba — mint confirmation +#define ALERT_BG_1 COLOR_TEAL +#define ALERT_TEXT_1 COLOR_COCAINE +#define ALERT_BG_2 COLOR_FUCHSIA +#define ALERT_TEXT_2 COLOR_COCAINE -// Alert alternates between these two states: -#define ALERT_BG_1 COLOR_TEAL // Teal background -#define ALERT_TEXT_1 COLOR_COCAINE // White text -#define ALERT_BG_2 COLOR_FUCHSIA // Fuchsia background -#define ALERT_TEXT_2 COLOR_COCAINE // White text (or try COLOR_BLACK for contrast) - -// Screen dimensions (LANDSCAPE: 320x172) +// ============== DISPLAY SETUP (LANDSCAPE) ============== #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 172 -// =========================================== - -// Arduino_GFX for Waveshare ESP32-C6-LCD-1.47 — LANDSCAPE mode -Arduino_DataBus *bus = new Arduino_HWSPI( - 15 /* DC */, 14 /* CS */, - 7 /* SCK */, 6 /* MOSI */, -1 /* MISO */); - -Arduino_GFX *gfx = new Arduino_ST7789( - bus, 21 /* RST */, 1 /* rotation: LANDSCAPE */, true /* IPS */, - 172 /* native width */, 320 /* native height */, - 34 /* col offset 1 */, 0 /* row offset 1 */, - 34 /* col offset 2 */, 0 /* row offset 2 */); - -// =========================================== +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; +State lastPublishedState = STATE_SILENT; unsigned long lastPoll = 0; unsigned long lastSilencePoll = 0; +unsigned long lastStatusPublish = 0; unsigned long blinkStartTime = 0; bool blinkState = false; -String lastAlertId = ""; +String lastCommandId = ""; String lastSilenceId = ""; bool lastButtonState = false; - -String alertMessage = ""; +String alertMessage = ""; // The message that triggered current alert // =========================================== void setup() { Serial.begin(115200); delay(3000); - Serial.println("\n=== COCAINE CHIC DOORBELL ==="); - Serial.println("Aesthetic: Miami Vice minimal, gallery alert"); + Serial.println("\n=== KLUBHAUS DOORBELL v3 ==="); + Serial.println("Topics: COMMAND / SILENCE / STATUS"); + Serial.println("Aesthetic: Cocaine Chic (Miami Vice minimal)"); pinMode(BACKLIGHT_PIN, OUTPUT); digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Backlight ready"); pinMode(BUTTON_PIN, INPUT_PULLUP); - Serial.println("Button ready"); + Serial.println("GPIO ready"); Serial.println("Init GFX..."); gfx->begin(); - Serial.println("GFX begin OK — LANDSCAPE 320x172"); + Serial.println("GFX ready — 320x172 landscape"); digitalWrite(BACKLIGHT_PIN, HIGH); testDisplay(); @@ -112,26 +102,22 @@ void setup() { digitalWrite(BACKLIGHT_PIN, LOW); } else { Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); - digitalWrite(BACKLIGHT_PIN, HIGH); - gfx->fillScreen(COLOR_BLACK); - drawCenteredText("CONNECTED", 2, COLOR_MINT, COLOR_BLACK, 50); - gfx->setTextSize(1); - gfx->setCursor(80, 90); - gfx->println(WiFi.localIP().toString()); - delay(800); - digitalWrite(BACKLIGHT_PIN, LOW); - gfx->fillScreen(COLOR_BLACK); + publishStatus(); // Initial status } Serial.println("=== SETUP COMPLETE ==="); - Serial.println(String("State: ") + getStateName(currentState)); - Serial.println("Silent: screen dark, gallery-ready"); } void loop() { unsigned long now = millis(); handleButton(); + // Publish status periodically and on state change + if (now - lastStatusPublish >= STATUS_PUBLISH_INTERVAL || currentState != lastPublishedState) { + lastStatusPublish = now; + publishStatus(); + } + switch (currentState) { case STATE_SILENT: if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { @@ -140,7 +126,7 @@ void loop() { } if (now - lastPoll >= POLL_INTERVAL) { lastPoll = now; - checkAlertTopic(); + checkCommandTopic(); } break; @@ -151,7 +137,7 @@ void loop() { } updateBlink(now); if (now - blinkStartTime >= BLINK_DURATION) { - Serial.println("ALERT TIMEOUT"); + Serial.println("ALERT TIMEOUT — auto silence"); transitionTo(STATE_SILENT); } break; @@ -160,15 +146,46 @@ void loop() { delay(10); } -// ============== STATE MANAGEMENT ============== +// ============== STATUS PUBLISHING ============== + +void publishStatus() { + if (WiFi.status() != WL_CONNECTED) { + Serial.println("Status: skip (no WiFi)"); + return; + } + + HTTPClient http; + http.begin(statusTopic); + http.addHeader("Content-Type", "application/json"); + + StaticJsonDocument<256> doc; + doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING"; + doc["message"] = alertMessage; + doc["timestamp"] = millis(); + + String payload; + serializeJson(doc, payload); + + int code = http.POST(payload); + Serial.print("Status publish: "); + Serial.print(payload); + Serial.print(" (HTTP "); + Serial.print(code); + Serial.println(")"); + + http.end(); + lastPublishedState = currentState; +} + +// ============== STATE TRANSITIONS ============== void transitionTo(State newState) { if (newState == currentState) return; Serial.print("STATE: "); - Serial.print(getStateName(currentState)); + Serial.print(currentState == STATE_SILENT ? "SILENT" : "ALERT"); Serial.print(" -> "); - Serial.println(getStateName(newState)); + Serial.println(newState == STATE_SILENT ? "SILENT" : "ALERT"); currentState = newState; @@ -177,7 +194,7 @@ void transitionTo(State newState) { alertMessage = ""; gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Silent: cocaine white standby, screen dark"); + Serial.println("Silent: screen dark, gallery void"); break; case STATE_ALERT: @@ -185,24 +202,120 @@ void transitionTo(State newState) { blinkStartTime = millis(); blinkState = false; drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1); - Serial.println("ALERT: neon pulse activated"); + Serial.println("Alert: neon pulse active"); break; } + + publishStatus(); // Immediate state broadcast +} + +// ============== TOPIC POLLING ============== + +void checkCommandTopic() { + Serial.println("Poll COMMAND..."); + String response = fetchNtfyJson(commandTopic); + if (response.length() == 0) { + Serial.println(" No new command"); + return; + } + + StaticJsonDocument<512> doc; + DeserializationError error = deserializeJson(doc, response); + if (error) { + Serial.print(" JSON error: "); + Serial.println(error.c_str()); + return; + } + + String id = doc["id"] | ""; + if (id == lastCommandId) { + Serial.println(" Same ID, skip"); + return; + } + lastCommandId = id; + + String message = doc["message"] | ""; + Serial.print(" NEW: "); + Serial.println(message); + + // SILENCE command = go silent + if (message.equalsIgnoreCase("SILENCE")) { + Serial.println(" -> SILENCE command"); + transitionTo(STATE_SILENT); + flashConfirm("SILENCE", COLOR_MINT, COLOR_BLACK); + } + // Any other message = ALERT (FRONT DOOR, LOADING DOCK, custom text) + else { + Serial.println(" -> ALERT trigger"); + alertMessage = message; // Store for display + transitionTo(STATE_ALERT); + } } -const char* getStateName(State s) { - return (s == STATE_SILENT) ? "SILENT" : "ALERT"; +void checkSilenceTopic() { + String response = fetchNtfyJson(silenceTopic); + if (response.length() == 0) return; + + StaticJsonDocument<512> doc; + DeserializationError error = deserializeJson(doc, response); + if (error) return; + + String id = doc["id"] | ""; + if (id == lastSilenceId) return; + lastSilenceId = id; + + String message = doc["message"] | ""; + Serial.print("Silence topic: "); + Serial.println(message); + + if (currentState == STATE_ALERT) { + Serial.println(" -> Force silence"); + transitionTo(STATE_SILENT); + flashConfirm("SILENCED", COLOR_MINT, COLOR_BLACK); + } } -// ============== ALERT SCREEN ============== +String fetchNtfyJson(const char* url) { + HTTPClient http; + http.begin(url); + http.setTimeout(5000); + + int code = http.GET(); + if (code != 200) { + Serial.print(" HTTP error: "); + Serial.println(code); + http.end(); + return ""; + } + + String payload = http.getString(); + http.end(); + + // ntfy JSON stream may have multiple lines; take first + int newline = payload.indexOf('\n'); + if (newline > 0) payload = payload.substring(0, newline); + + return payload; +} + +// ============== VISUAL FEEDBACK ============== + +void flashConfirm(const char* text, uint16_t bgColor, uint16_t textColor) { + digitalWrite(BACKLIGHT_PIN, HIGH); + gfx->fillScreen(bgColor); + drawCenteredText(text, 3, textColor, bgColor, 70); + delay(500); + gfx->fillScreen(COLOR_BLACK); + digitalWrite(BACKLIGHT_PIN, LOW); +} void drawAlertScreen(uint16_t bgColor, uint16_t textColor) { gfx->fillScreen(bgColor); - // "ALERT" header — bold, centered top + // "ALERT" header drawCenteredText("ALERT", 3, textColor, bgColor, 20); - // The message — auto-sized, centered below + // The triggering message, auto-sized if (alertMessage.length() > 0) { drawAutoSizedMessage(alertMessage, textColor, bgColor, 70); } @@ -256,128 +369,24 @@ void handleButton() { Serial.println("BUTTON PRESSED"); digitalWrite(BACKLIGHT_PIN, HIGH); gfx->fillScreen(COLOR_COCAINE); - drawCenteredText("TEST", 3, COLOR_BLACK, COLOR_COCAINE, 70); - Serial.println("Screen COCAINE WHITE (held)"); + drawCenteredText("TEST MODE", 2, COLOR_BLACK, COLOR_COCAINE, 70); } else if (!pressed && lastButtonState) { Serial.println("BUTTON RELEASED"); if (currentState == STATE_ALERT) { - Serial.println("Reset ALERT -> SILENT"); + Serial.println(" -> Reset to SILENT"); transitionTo(STATE_SILENT); - // Mint confirmation flash - digitalWrite(BACKLIGHT_PIN, HIGH); - gfx->fillScreen(COLOR_MINT); - drawCenteredText("SILENCED", 3, COLOR_BLACK, COLOR_MINT, 70); - delay(400); - gfx->fillScreen(COLOR_BLACK); - digitalWrite(BACKLIGHT_PIN, LOW); + flashConfirm("SILENCED", COLOR_MINT, COLOR_BLACK); } else { gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); - Serial.println("Screen BLACK, silent"); } } lastButtonState = pressed; } -// ============== NETWORK FUNCTIONS ============== - -void checkAlertTopic() { - Serial.println("Poll alert..."); - String response = fetchNtfy(alertTopic); - if (response.length() == 0) { - Serial.println(" No response"); - return; - } - - StaticJsonDocument<512> doc; - DeserializationError error = deserializeJson(doc, response); - if (error) { - Serial.print(" JSON error: "); - Serial.println(error.c_str()); - return; - } - - String id = doc["id"] | ""; - String message = doc["message"] | ""; - - if (id == lastAlertId) { - Serial.println(" Same ID, skip"); - return; - } - - lastAlertId = id; - Serial.print(" New: "); - Serial.println(message); - - if (message.equalsIgnoreCase("SILENCE")) { - Serial.println(" -> SILENCE command"); - transitionTo(STATE_SILENT); - digitalWrite(BACKLIGHT_PIN, HIGH); - gfx->fillScreen(COLOR_MINT); - drawCenteredText("SILENCE", 3, COLOR_BLACK, COLOR_MINT, 70); - delay(800); - gfx->fillScreen(COLOR_BLACK); - digitalWrite(BACKLIGHT_PIN, LOW); - } else { - Serial.println(" -> TRIGGER ALERT"); - alertMessage = message; - transitionTo(STATE_ALERT); - } -} - -void checkSilenceTopic() { - String response = fetchNtfy(silenceTopic); - if (response.length() == 0) return; - - StaticJsonDocument<512> doc; - DeserializationError error = deserializeJson(doc, response); - if (error) return; - - String id = doc["id"] | ""; - if (id == lastSilenceId) return; - - lastSilenceId = id; - Serial.print("Silence: "); - Serial.println((const char*)doc["message"]); - - if (currentState == STATE_ALERT) { - Serial.println(" -> Stop alert via silence topic"); - transitionTo(STATE_SILENT); - digitalWrite(BACKLIGHT_PIN, HIGH); - gfx->fillScreen(COLOR_MINT); - drawCenteredText("SILENCED", 3, COLOR_BLACK, COLOR_MINT, 70); - delay(800); - gfx->fillScreen(COLOR_BLACK); - digitalWrite(BACKLIGHT_PIN, LOW); - } -} - -String fetchNtfy(const char* url) { - HTTPClient http; - http.begin(url); - http.setTimeout(5000); - - int code = http.GET(); - Serial.print(" HTTP "); - Serial.println(code); - - if (code != 200) { - http.end(); - return ""; - } - - String payload = http.getString(); - http.end(); - - int newline = payload.indexOf('\n'); - if (newline > 0) payload = payload.substring(0, newline); - - return payload; -} - // ============== BLINK CONTROL ============== void updateBlink(unsigned long now) { @@ -386,43 +395,31 @@ void updateBlink(unsigned long now) { if (newState != blinkState) { blinkState = newState; - - // Cocaine chic alternation: TEAL ↔ FUCHSIA uint16_t bgColor = blinkState ? ALERT_BG_2 : ALERT_BG_1; uint16_t textColor = blinkState ? ALERT_TEXT_2 : ALERT_TEXT_1; - drawAlertScreen(bgColor, textColor); - Serial.println(blinkState ? "FUCHSIA flash" : "TEAL flash"); + Serial.println(blinkState ? " FUCHSIA flash" : " TEAL flash"); } } // ============== DISPLAY TEST ============== void testDisplay() { - Serial.println(" TEAL"); - gfx->fillScreen(COLOR_TEAL); - delay(150); + Serial.println("Display test:"); - Serial.println(" FUCHSIA"); - gfx->fillScreen(COLOR_FUCHSIA); - delay(150); + const char* colors[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"}; + uint16_t colorVals[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK}; - Serial.println(" COCAINE WHITE"); - gfx->fillScreen(COLOR_COCAINE); - delay(150); + for (int i = 0; i < 5; i++) { + Serial.print(" "); + Serial.println(colors[i]); + gfx->fillScreen(colorVals[i]); + delay(100); + } - Serial.println(" MINT"); - gfx->fillScreen(COLOR_MINT); - delay(150); - - Serial.println(" BLACK"); gfx->fillScreen(COLOR_BLACK); - delay(100); - - Serial.println(" LANDSCAPE TEXT TEST"); - gfx->fillScreen(COLOR_BLACK); - drawCenteredText("COCAINE", 3, COLOR_COCAINE, COLOR_BLACK, 50); - drawCenteredText("CHIC", 3, COLOR_TEAL, COLOR_BLACK, 90); + drawCenteredText("KLUBHAUS", 3, COLOR_TEAL, COLOR_BLACK, 50); + drawCenteredText("DOORBELL", 3, COLOR_FUCHSIA, COLOR_BLACK, 90); delay(400); gfx->fillScreen(COLOR_BLACK);