#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 // Display colors #define COLOR_BLACK 0x0000 #define COLOR_RED 0xF800 #define COLOR_GREEN 0x07E0 #define COLOR_BLUE 0x001F #define COLOR_WHITE 0xFFFF #define COLOR_YELLOW 0xFFE0 // Screen dimensions #define SCREEN_WIDTH 172 #define SCREEN_HEIGHT 320 // =========================================== // Arduino_GFX for Waveshare ESP32-C6-LCD-1.47 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 */, 0 /* rotation */, true /* IPS */, SCREEN_WIDTH, SCREEN_HEIGHT, 34 /* col offset 1 */, 0 /* row offset 1 */, 34 /* col offset 2 */, 0 /* row offset 2 */); // =========================================== enum State { STATE_SILENT, STATE_ALARM }; 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; // Store the triggering message for display String alarmMessage = ""; // =========================================== void setup() { Serial.begin(115200); delay(3000); Serial.println("\n=== MESSAGE DOORBELL ==="); 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"); 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); // FIXED: Added yPos parameter (140 = center vertically) drawCenteredText("NO WIFI", 2, COLOR_RED, COLOR_BLACK, 140); delay(3000); digitalWrite(BACKLIGHT_PIN, LOW); } else { Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); // Brief status flash digitalWrite(BACKLIGHT_PIN, HIGH); gfx->fillScreen(COLOR_BLACK); // FIXED: Added yPos parameter (10 = near top) drawCenteredText("WiFi OK", 2, COLOR_GREEN, COLOR_BLACK, 10); gfx->setCursor(10, 50); 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)); } 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_ALARM: if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { lastSilencePoll = now; checkSilenceTopic(); } updateBlink(now); if (now - blinkStartTime >= BLINK_DURATION) { Serial.println("ALARM 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: alarmMessage = ""; gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); Serial.println("Silent: screen off, message cleared"); break; case STATE_ALARM: digitalWrite(BACKLIGHT_PIN, HIGH); blinkStartTime = millis(); blinkState = false; drawAlarmScreen(COLOR_RED, COLOR_WHITE); Serial.println("ALARM: message displayed"); break; } } const char* getStateName(State s) { return (s == STATE_SILENT) ? "SILENT" : "ALARM"; } // ============== ALARM SCREEN WITH MESSAGE ============== void drawAlarmScreen(uint16_t bgColor, uint16_t textColor) { gfx->fillScreen(bgColor); // Draw "ALARM!" header at top gfx->setTextSize(2); gfx->setTextColor(textColor, bgColor); drawCenteredText("ALARM!", 2, textColor, bgColor, 30); // Draw the triggering message below, auto-sized if (alarmMessage.length() > 0) { drawAutoSizedMessage(alarmMessage, textColor, bgColor, 80); } } void drawAutoSizedMessage(String message, uint16_t textColor, uint16_t bgColor, int yPos) { int textSize = calculateOptimalTextSize(message, SCREEN_WIDTH - 20, SCREEN_HEIGHT - yPos - 20); gfx->setTextSize(textSize); gfx->setTextColor(textColor, bgColor); gfx->setTextBound(10, yPos, SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10); gfx->setCursor(10, yPos); gfx->println(message); } int calculateOptimalTextSize(String message, int maxWidth, int maxHeight) { for (int size = 4; 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 >= 5) { 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 < 0) xPos = 0; 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_WHITE); drawCenteredText("TEST MODE", 2, COLOR_BLACK, COLOR_WHITE, 140); Serial.println("Screen WHITE (held)"); } else if (!pressed && lastButtonState) { Serial.println("BUTTON RELEASED"); if (currentState == STATE_ALARM) { Serial.println("Reset ALARM -> SILENT"); transitionTo(STATE_SILENT); digitalWrite(BACKLIGHT_PIN, HIGH); gfx->fillScreen(COLOR_GREEN); drawCenteredText("SILENCED", 3, COLOR_BLACK, COLOR_GREEN, 140); delay(500); gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); } else { gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); Serial.println("Screen BLACK, backlight OFF"); } } 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_GREEN); drawCenteredText("SILENCE", 3, COLOR_BLACK, COLOR_GREEN, 140); delay(800); gfx->fillScreen(COLOR_BLACK); digitalWrite(BACKLIGHT_PIN, LOW); } else { Serial.println(" -> TRIGGER ALARM"); alarmMessage = message; transitionTo(STATE_ALARM); } } 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_ALARM) { Serial.println(" -> Stop alarm via silence topic"); transitionTo(STATE_SILENT); digitalWrite(BACKLIGHT_PIN, HIGH); gfx->fillScreen(COLOR_GREEN); drawCenteredText("SILENCED", 3, COLOR_BLACK, COLOR_GREEN, 140); 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; uint16_t bgColor = blinkState ? COLOR_WHITE : COLOR_RED; uint16_t textColor = blinkState ? COLOR_RED : COLOR_WHITE; drawAlarmScreen(bgColor, textColor); Serial.println(blinkState ? "WHITE bg / RED text" : "RED bg / WHITE text"); } } // ============== DISPLAY TEST ============== void testDisplay() { Serial.println(" RED"); gfx->fillScreen(COLOR_RED); delay(100); Serial.println(" GREEN"); gfx->fillScreen(COLOR_GREEN); delay(100); Serial.println(" BLUE"); gfx->fillScreen(COLOR_BLUE); delay(100); Serial.println(" WHITE"); gfx->fillScreen(COLOR_WHITE); delay(100); Serial.println(" BLACK"); gfx->fillScreen(COLOR_BLACK); delay(100); Serial.println(" TEXT TEST"); gfx->fillScreen(COLOR_BLACK); drawCenteredText("GFX OK!", 2, COLOR_WHITE, COLOR_BLACK, 50); drawCenteredText("Text works!", 2, COLOR_WHITE, COLOR_BLACK, 80); delay(500); gfx->fillScreen(COLOR_BLACK); Serial.println("Test complete"); }