#include #include #include #include // ============== CONFIGURATION ============== 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"; const unsigned long POLL_INTERVAL = 30000; const unsigned long SILENCE_POLL_INTERVAL = 5000; const unsigned long BLINK_DURATION = 180000; const unsigned long BLINK_PERIOD = 1000; #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 // 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 // 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) #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 */); // =========================================== enum State { STATE_SILENT, STATE_ALERT }; State currentState = STATE_SILENT; unsigned long lastPoll = 0; unsigned long lastSilencePoll = 0; unsigned long blinkStartTime = 0; bool blinkState = false; String lastAlertId = ""; String lastSilenceId = ""; bool lastButtonState = false; String alertMessage = ""; // =========================================== void setup() { Serial.begin(115200); delay(3000); Serial.println("\n=== COCAINE CHIC DOORBELL ==="); Serial.println("Aesthetic: Miami Vice minimal, gallery alert"); pinMode(BACKLIGHT_PIN, OUTPUT); digitalWrite(BACKLIGHT_PIN, LOW); Serial.println("Backlight ready"); pinMode(BUTTON_PIN, INPUT_PULLUP); Serial.println("Button ready"); Serial.println("Init GFX..."); gfx->begin(); Serial.println("GFX begin OK — LANDSCAPE 320x172"); digitalWrite(BACKLIGHT_PIN, HIGH); testDisplay(); delay(300); transitionTo(STATE_SILENT); // WiFi Serial.println("Connecting WiFi..."); WiFi.begin(ssid, password); int wifiTimeout = 0; while (WiFi.status() != WL_CONNECTED && wifiTimeout < 40) { delay(500); Serial.print("."); wifiTimeout++; } if (WiFi.status() != WL_CONNECTED) { Serial.println("\nWiFi FAILED"); digitalWrite(BACKLIGHT_PIN, HIGH); gfx->fillScreen(COLOR_BLACK); drawCenteredText("OFFLINE", 3, COLOR_FUCHSIA, COLOR_BLACK, 70); delay(3000); 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); } Serial.println("=== SETUP COMPLETE ==="); Serial.println(String("State: ") + getStateName(currentState)); Serial.println("Silent: screen dark, gallery-ready"); } void loop() { unsigned long now = millis(); handleButton(); switch (currentState) { case STATE_SILENT: if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { lastSilencePoll = now; checkSilenceTopic(); } if (now - lastPoll >= POLL_INTERVAL) { lastPoll = now; checkAlertTopic(); } break; case STATE_ALERT: if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { lastSilencePoll = now; checkSilenceTopic(); } updateBlink(now); if (now - blinkStartTime >= BLINK_DURATION) { Serial.println("ALERT TIMEOUT"); transitionTo(STATE_SILENT); } break; } delay(10); } // ============== STATE MANAGEMENT ============== void transitionTo(State newState) { if (newState == currentState) return; Serial.print("STATE: "); Serial.print(getStateName(currentState)); Serial.print(" -> "); Serial.println(getStateName(newState)); currentState = newState; switch (currentState) { case STATE_SILENT: alertMessage = ""; gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); Serial.println("Silent: cocaine white standby, screen dark"); break; case STATE_ALERT: digitalWrite(BACKLIGHT_PIN, HIGH); blinkStartTime = millis(); blinkState = false; drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1); Serial.println("ALERT: neon pulse activated"); break; } } const char* getStateName(State s) { return (s == STATE_SILENT) ? "SILENT" : "ALERT"; } // ============== ALERT SCREEN ============== void drawAlertScreen(uint16_t bgColor, uint16_t textColor) { gfx->fillScreen(bgColor); // "ALERT" header — bold, centered top drawCenteredText("ALERT", 3, textColor, bgColor, 20); // The message — auto-sized, centered below if (alertMessage.length() > 0) { drawAutoSizedMessage(alertMessage, textColor, bgColor, 70); } } void drawAutoSizedMessage(String message, uint16_t textColor, uint16_t bgColor, int yPos) { int textSize = calculateOptimalTextSize(message, SCREEN_WIDTH - 40, SCREEN_HEIGHT - yPos - 20); gfx->setTextSize(textSize); gfx->setTextColor(textColor, bgColor); gfx->setTextBound(20, yPos, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 10); gfx->setCursor(20, yPos); gfx->println(message); } int calculateOptimalTextSize(String message, int maxWidth, int maxHeight) { for (int size = 5; size >= 1; size--) { gfx->setTextSize(size); int charWidth = 6 * size; int lineHeight = 8 * size; int charsPerLine = maxWidth / charWidth; int numLines = (message.length() + charsPerLine - 1) / charsPerLine; int totalHeight = numLines * lineHeight; if (totalHeight <= maxHeight && charsPerLine >= 4) { return size; } } return 1; } void drawCenteredText(const char* text, int textSize, uint16_t textColor, uint16_t bgColor, int yPos) { gfx->setTextSize(textSize); gfx->setTextColor(textColor, bgColor); int textWidth = strlen(text) * 6 * textSize; int xPos = (SCREEN_WIDTH - textWidth) / 2; if (xPos < 20) xPos = 20; gfx->setCursor(xPos, yPos); gfx->println(text); } // ============== BUTTON HANDLING ============== void handleButton() { bool pressed = (digitalRead(BUTTON_PIN) == LOW); if (pressed && !lastButtonState) { 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)"); } else if (!pressed && lastButtonState) { Serial.println("BUTTON RELEASED"); if (currentState == STATE_ALERT) { Serial.println("Reset ALERT -> 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); } 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) { unsigned long elapsed = now - blinkStartTime; bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1; 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"); } } // ============== DISPLAY TEST ============== void testDisplay() { Serial.println(" TEAL"); gfx->fillScreen(COLOR_TEAL); delay(150); Serial.println(" FUCHSIA"); gfx->fillScreen(COLOR_FUCHSIA); delay(150); Serial.println(" COCAINE WHITE"); gfx->fillScreen(COLOR_COCAINE); delay(150); 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); delay(400); gfx->fillScreen(COLOR_BLACK); Serial.println("Test complete"); }