This commit is contained in:
2026-02-12 04:25:18 -08:00
parent 0c10cbe494
commit 083b23b3fa

View File

@@ -7,33 +7,35 @@
const char* ssid = "iot-2GHz"; const char* ssid = "iot-2GHz";
const char* password = "lesson-greater"; const char* password = "lesson-greater";
// Refactored topic architecture // TOPIC URLS — naming indicates direction and method
const char* commandTopic = "http://ntfy.sh/ALERT_klubhaus_topic/json?poll=1"; // Incoming: FRONT DOOR, LOADING DOCK, custom // Device SUBSCRIBES (GET poll) to these:
const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1"; // Incoming: any message = silence const char* COMMAND_POLL_URL = "http://ntfy.sh/ALERT_klubhaus_topic/json?poll=1";
const char* statusTopic = "http://ntfy.sh/STATUS_klubhaus_topic"; // Outgoing: device publishes state const char* SILENCE_POLL_URL = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1";
const unsigned long POLL_INTERVAL = 15000; // Device PUBLISHES (POST) to this:
const char* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic";
const unsigned long COMMAND_POLL_INTERVAL = 30000;
const unsigned long SILENCE_POLL_INTERVAL = 5000; 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 STATUS_PUBLISH_INTERVAL = 30000; // Periodic status broadcast
#define BACKLIGHT_PIN 22 #define BACKLIGHT_PIN 22
#define BUTTON_PIN 9 #define BUTTON_PIN 9
// ============== COCAINE CHIC PALETTE ============== // ============== COCAINE CHIC PALETTE ==============
#define COLOR_BLACK 0x0000 // #000000 #define COLOR_BLACK 0x0000
#define COLOR_COCAINE 0xFFFE // #fff8fa — warm white #define COLOR_COCAINE 0xFFFE
#define COLOR_TEAL 0x05F5 // #0bd3d3 — electric teal #define COLOR_TEAL 0x05F5
#define COLOR_FUCHSIA 0xFC6E // #f890e7 — hot pink #define COLOR_FUCHSIA 0xFC6E
#define COLOR_MINT 0x674D // #64e8ba — confirmation #define COLOR_MINT 0x674D
#define ALERT_BG_1 COLOR_TEAL #define ALERT_BG_1 COLOR_TEAL
#define ALERT_TEXT_1 COLOR_COCAINE #define ALERT_TEXT_1 COLOR_COCAINE
#define ALERT_BG_2 COLOR_FUCHSIA #define ALERT_BG_2 COLOR_FUCHSIA
#define ALERT_TEXT_2 COLOR_COCAINE #define ALERT_TEXT_2 COLOR_COCAINE
// ============== DISPLAY SETUP (LANDSCAPE) ============== // ============== DISPLAY SETUP ==============
#define SCREEN_WIDTH 320 #define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 172 #define SCREEN_HEIGHT 172
@@ -46,86 +48,64 @@ enum State { STATE_SILENT, STATE_ALERT };
State currentState = STATE_SILENT; State currentState = STATE_SILENT;
State lastPublishedState = STATE_SILENT; State lastPublishedState = STATE_SILENT;
unsigned long lastPoll = 0; unsigned long lastCommandPoll = 0;
unsigned long lastSilencePoll = 0; unsigned long lastSilencePoll = 0;
unsigned long lastStatusPublish = 0;
unsigned long blinkStartTime = 0; unsigned long blinkStartTime = 0;
bool blinkState = false; bool blinkPhase = false;
String lastCommandId = ""; String lastCommandId = "";
String lastSilenceId = ""; String lastSilenceId = "";
bool lastButtonState = false; bool lastButtonState = false;
String alertMessage = ""; // The message that triggered current alert String alertMessage = "";
// ===========================================
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(3000); delay(3000);
Serial.println("\n=== KLUBHAUS DOORBELL v3 ==="); Serial.println("\n=== KLUBHAUS DOORBELL v3.3 ===");
Serial.println("Topics: COMMAND / SILENCE / STATUS"); Serial.println("URLs: COMMAND_POLL_URL, SILENCE_POLL_URL, STATUS_POST_URL");
Serial.println("Aesthetic: Cocaine Chic (Miami Vice minimal)");
pinMode(BACKLIGHT_PIN, OUTPUT); pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("GPIO ready");
Serial.println("Init GFX...");
gfx->begin(); gfx->begin();
Serial.println("GFX ready — 320x172 landscape");
digitalWrite(BACKLIGHT_PIN, HIGH); digitalWrite(BACKLIGHT_PIN, HIGH);
testDisplay(); testDisplay();
delay(300); delay(300);
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
// WiFi
Serial.println("Connecting WiFi...");
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
int timeout = 0;
int wifiTimeout = 0; while (WiFi.status() != WL_CONNECTED && timeout < 40) {
while (WiFi.status() != WL_CONNECTED && wifiTimeout < 40) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
wifiTimeout++; timeout++;
} }
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nWiFi FAILED"); Serial.println("\nWiFi FAILED");
digitalWrite(BACKLIGHT_PIN, HIGH); showFatalError("OFFLINE");
gfx->fillScreen(COLOR_BLACK);
drawCenteredText("OFFLINE", 3, COLOR_FUCHSIA, COLOR_BLACK, 70);
delay(3000);
digitalWrite(BACKLIGHT_PIN, LOW);
} else { } else {
Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
publishStatus(); // Initial status publishStatus();
} }
Serial.println("=== SETUP COMPLETE ==="); Serial.println("=== READY ===");
} }
void loop() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
handleButton(); handleButton();
// Publish status periodically and on state change
if (now - lastStatusPublish >= STATUS_PUBLISH_INTERVAL || currentState != lastPublishedState) {
lastStatusPublish = now;
publishStatus();
}
switch (currentState) { switch (currentState) {
case STATE_SILENT: case STATE_SILENT:
if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) {
lastSilencePoll = now; lastSilencePoll = now;
checkSilenceTopic(); checkSilenceTopic();
} }
if (now - lastPoll >= POLL_INTERVAL) { if (now - lastCommandPoll >= COMMAND_POLL_INTERVAL) {
lastPoll = now; lastCommandPoll = now;
checkCommandTopic(); checkCommandTopic();
} }
break; break;
@@ -137,7 +117,7 @@ void loop() {
} }
updateBlink(now); updateBlink(now);
if (now - blinkStartTime >= BLINK_DURATION) { if (now - blinkStartTime >= BLINK_DURATION) {
Serial.println("ALERT TIMEOUT — auto silence"); Serial.println("ALERT TIMEOUT");
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
} }
break; break;
@@ -155,8 +135,8 @@ void publishStatus() {
} }
HTTPClient http; HTTPClient http;
http.begin(statusTopic); http.begin(STATUS_POST_URL);
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "text/plain");
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc;
doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING"; doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING";
@@ -167,18 +147,14 @@ void publishStatus() {
serializeJson(doc, payload); serializeJson(doc, payload);
int code = http.POST(payload); int code = http.POST(payload);
Serial.print("Status publish: "); Serial.print("Status: ");
Serial.print(payload); Serial.print(payload);
Serial.print(" (HTTP "); Serial.println(String(" (") + code + ")");
Serial.print(code);
Serial.println(")");
http.end(); http.end();
lastPublishedState = currentState; lastPublishedState = currentState;
} }
// ============== STATE TRANSITIONS ==============
void transitionTo(State newState) { void transitionTo(State newState) {
if (newState == currentState) return; if (newState == currentState) return;
@@ -194,26 +170,24 @@ void transitionTo(State newState) {
alertMessage = ""; alertMessage = "";
gfx->fillScreen(COLOR_BLACK); gfx->fillScreen(COLOR_BLACK);
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
Serial.println("Silent: screen dark, gallery void");
break; break;
case STATE_ALERT: case STATE_ALERT:
digitalWrite(BACKLIGHT_PIN, HIGH); digitalWrite(BACKLIGHT_PIN, HIGH);
blinkStartTime = millis(); blinkStartTime = millis();
blinkState = false; blinkPhase = false;
drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1); drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1);
Serial.println("Alert: neon pulse active");
break; break;
} }
publishStatus(); // Immediate state broadcast publishStatus();
} }
// ============== TOPIC POLLING ============== // ============== TOPIC POLLING ==============
void checkCommandTopic() { void checkCommandTopic() {
Serial.println("Poll COMMAND..."); Serial.println("Poll COMMAND...");
String response = fetchNtfyJson(commandTopic); String response = fetchNtfyJson(COMMAND_POLL_URL);
if (response.length() == 0) { if (response.length() == 0) {
Serial.println(" No new command"); Serial.println(" No new command");
return; return;
@@ -238,22 +212,19 @@ void checkCommandTopic() {
Serial.print(" NEW: "); Serial.print(" NEW: ");
Serial.println(message); Serial.println(message);
// SILENCE command = go silent
if (message.equalsIgnoreCase("SILENCE")) { if (message.equalsIgnoreCase("SILENCE")) {
Serial.println(" -> SILENCE command"); Serial.println(" -> SILENCE command");
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
flashConfirm("SILENCE", COLOR_MINT, COLOR_BLACK); flashConfirm("SILENCE", COLOR_MINT, COLOR_BLACK);
} } else {
// Any other message = ALERT (FRONT DOOR, LOADING DOCK, custom text)
else {
Serial.println(" -> ALERT trigger"); Serial.println(" -> ALERT trigger");
alertMessage = message; // Store for display alertMessage = message;
transitionTo(STATE_ALERT); transitionTo(STATE_ALERT);
} }
} }
void checkSilenceTopic() { void checkSilenceTopic() {
String response = fetchNtfyJson(silenceTopic); String response = fetchNtfyJson(SILENCE_POLL_URL);
if (response.length() == 0) return; if (response.length() == 0) return;
StaticJsonDocument<512> doc; StaticJsonDocument<512> doc;
@@ -291,14 +262,13 @@ String fetchNtfyJson(const char* url) {
String payload = http.getString(); String payload = http.getString();
http.end(); http.end();
// ntfy JSON stream may have multiple lines; take first
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);
return payload; return payload;
} }
// ============== VISUAL FEEDBACK ============== // ============== VISUALS ==============
void flashConfirm(const char* text, uint16_t bgColor, uint16_t textColor) { void flashConfirm(const char* text, uint16_t bgColor, uint16_t textColor) {
digitalWrite(BACKLIGHT_PIN, HIGH); digitalWrite(BACKLIGHT_PIN, HIGH);
@@ -311,11 +281,7 @@ void flashConfirm(const char* text, uint16_t bgColor, uint16_t textColor) {
void drawAlertScreen(uint16_t bgColor, uint16_t textColor) { void drawAlertScreen(uint16_t bgColor, uint16_t textColor) {
gfx->fillScreen(bgColor); gfx->fillScreen(bgColor);
// "ALERT" header
drawCenteredText("ALERT", 3, textColor, bgColor, 20); drawCenteredText("ALERT", 3, textColor, bgColor, 20);
// The triggering message, auto-sized
if (alertMessage.length() > 0) { if (alertMessage.length() > 0) {
drawAutoSizedMessage(alertMessage, textColor, bgColor, 70); drawAutoSizedMessage(alertMessage, textColor, bgColor, 70);
} }
@@ -323,11 +289,9 @@ void drawAlertScreen(uint16_t bgColor, uint16_t textColor) {
void drawAutoSizedMessage(String message, uint16_t textColor, uint16_t bgColor, int yPos) { void drawAutoSizedMessage(String message, uint16_t textColor, uint16_t bgColor, int yPos) {
int textSize = calculateOptimalTextSize(message, SCREEN_WIDTH - 40, SCREEN_HEIGHT - yPos - 20); int textSize = calculateOptimalTextSize(message, SCREEN_WIDTH - 40, SCREEN_HEIGHT - yPos - 20);
gfx->setTextSize(textSize); gfx->setTextSize(textSize);
gfx->setTextColor(textColor, bgColor); gfx->setTextColor(textColor, bgColor);
gfx->setTextBound(20, yPos, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 10); gfx->setTextBound(20, yPos, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 10);
gfx->setCursor(20, yPos); gfx->setCursor(20, yPos);
gfx->println(message); gfx->println(message);
} }
@@ -340,10 +304,7 @@ int calculateOptimalTextSize(String message, int maxWidth, int maxHeight) {
int charsPerLine = maxWidth / charWidth; int charsPerLine = maxWidth / charWidth;
int numLines = (message.length() + charsPerLine - 1) / charsPerLine; int numLines = (message.length() + charsPerLine - 1) / charsPerLine;
int totalHeight = numLines * lineHeight; int totalHeight = numLines * lineHeight;
if (totalHeight <= maxHeight && charsPerLine >= 4) return size;
if (totalHeight <= maxHeight && charsPerLine >= 4) {
return size;
}
} }
return 1; return 1;
} }
@@ -351,16 +312,22 @@ int calculateOptimalTextSize(String message, int maxWidth, int maxHeight) {
void drawCenteredText(const char* text, int textSize, uint16_t textColor, uint16_t bgColor, int yPos) { void drawCenteredText(const char* text, int textSize, uint16_t textColor, uint16_t bgColor, int yPos) {
gfx->setTextSize(textSize); gfx->setTextSize(textSize);
gfx->setTextColor(textColor, bgColor); gfx->setTextColor(textColor, bgColor);
int textWidth = strlen(text) * 6 * textSize; int textWidth = strlen(text) * 6 * textSize;
int xPos = (SCREEN_WIDTH - textWidth) / 2; int xPos = (SCREEN_WIDTH - textWidth) / 2;
if (xPos < 20) xPos = 20; if (xPos < 20) xPos = 20;
gfx->setCursor(xPos, yPos); gfx->setCursor(xPos, yPos);
gfx->println(text); gfx->println(text);
} }
// ============== BUTTON HANDLING ============== void showFatalError(const char* text) {
digitalWrite(BACKLIGHT_PIN, HIGH);
gfx->fillScreen(COLOR_BLACK);
drawCenteredText(text, 3, COLOR_FUCHSIA, COLOR_BLACK, 70);
delay(3000);
digitalWrite(BACKLIGHT_PIN, LOW);
}
// ============== BUTTON ==============
void handleButton() { void handleButton() {
bool pressed = (digitalRead(BUTTON_PIN) == LOW); bool pressed = (digitalRead(BUTTON_PIN) == LOW);
@@ -373,7 +340,6 @@ void handleButton() {
} }
else if (!pressed && lastButtonState) { else if (!pressed && lastButtonState) {
Serial.println("BUTTON RELEASED"); Serial.println("BUTTON RELEASED");
if (currentState == STATE_ALERT) { if (currentState == STATE_ALERT) {
Serial.println(" -> Reset to SILENT"); Serial.println(" -> Reset to SILENT");
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
@@ -383,46 +349,35 @@ void handleButton() {
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
} }
} }
lastButtonState = pressed; lastButtonState = pressed;
} }
// ============== BLINK CONTROL ============== // ============== BLINK ==============
void updateBlink(unsigned long now) { void updateBlink(unsigned long now) {
unsigned long elapsed = now - blinkStartTime; unsigned long elapsed = now - blinkStartTime;
bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1; bool newPhase = ((elapsed / BLINK_PERIOD) % 2) == 1;
if (newPhase != blinkPhase) {
if (newState != blinkState) { blinkPhase = newPhase;
blinkState = newState; uint16_t bgColor = blinkPhase ? ALERT_BG_2 : ALERT_BG_1;
uint16_t bgColor = blinkState ? ALERT_BG_2 : ALERT_BG_1; uint16_t textColor = blinkPhase ? ALERT_TEXT_2 : ALERT_TEXT_1;
uint16_t textColor = blinkState ? ALERT_TEXT_2 : ALERT_TEXT_1;
drawAlertScreen(bgColor, textColor); drawAlertScreen(bgColor, textColor);
Serial.println(blinkState ? " FUCHSIA flash" : " TEAL flash");
} }
} }
// ============== DISPLAY TEST ============== // ============== TEST ==============
void testDisplay() { void testDisplay() {
Serial.println("Display test:"); const char* names[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"};
uint16_t colors[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK};
const char* colors[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"};
uint16_t colorVals[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK};
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Serial.print(" "); gfx->fillScreen(colors[i]);
Serial.println(colors[i]);
gfx->fillScreen(colorVals[i]);
delay(100); delay(100);
} }
gfx->fillScreen(COLOR_BLACK); gfx->fillScreen(COLOR_BLACK);
drawCenteredText("KLUBHAUS", 3, COLOR_TEAL, COLOR_BLACK, 50); drawCenteredText("KLUBHAUS", 3, COLOR_TEAL, COLOR_BLACK, 50);
drawCenteredText("DOORBELL", 3, COLOR_FUCHSIA, COLOR_BLACK, 90); drawCenteredText("DOORBELL", 3, COLOR_FUCHSIA, COLOR_BLACK, 90);
delay(400); delay(400);
gfx->fillScreen(COLOR_BLACK); gfx->fillScreen(COLOR_BLACK);
Serial.println("Test complete");
} }