This commit is contained in:
2026-02-12 03:10:22 -08:00
parent afe2d7f7e6
commit 0c10cbe494

View File

@@ -7,84 +7,74 @@
const char* ssid = "iot-2GHz"; const char* ssid = "iot-2GHz";
const char* password = "lesson-greater"; const char* password = "lesson-greater";
const char* alertTopic = "http://ntfy.sh/ALERT_klubhaus_topic/json?poll=1"; // Refactored topic architecture
const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1"; 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 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 ==============
// 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 ALERT_BG_1 COLOR_TEAL
#define COLOR_COCAINE 0xFFFE // #fff8fa — warm white, almost pink #define ALERT_TEXT_1 COLOR_COCAINE
#define COLOR_TEAL 0x05F5 // #0bd3d3 — electric neon teal #define ALERT_BG_2 COLOR_FUCHSIA
#define COLOR_FUCHSIA 0xFC6E // #f890e7 — hot pink neon #define ALERT_TEXT_2 COLOR_COCAINE
#define COLOR_MINT 0x674D // #64e8ba — mint confirmation
// Alert alternates between these two states: // ============== DISPLAY SETUP (LANDSCAPE) ==============
#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_WIDTH 320
#define SCREEN_HEIGHT 172 #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);
// 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 */);
// ===========================================
// ============== STATE MANAGEMENT ==============
enum State { STATE_SILENT, STATE_ALERT }; enum State { STATE_SILENT, STATE_ALERT };
State currentState = STATE_SILENT; State currentState = STATE_SILENT;
State lastPublishedState = STATE_SILENT;
unsigned long lastPoll = 0; unsigned long lastPoll = 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 blinkState = false;
String lastAlertId = ""; 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=== COCAINE CHIC DOORBELL ==="); Serial.println("\n=== KLUBHAUS DOORBELL v3 ===");
Serial.println("Aesthetic: Miami Vice minimal, gallery alert"); Serial.println("Topics: COMMAND / SILENCE / STATUS");
Serial.println("Aesthetic: Cocaine Chic (Miami Vice minimal)");
pinMode(BACKLIGHT_PIN, OUTPUT); pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
Serial.println("Backlight ready");
pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("Button ready"); Serial.println("GPIO ready");
Serial.println("Init GFX..."); Serial.println("Init GFX...");
gfx->begin(); gfx->begin();
Serial.println("GFX begin OK — LANDSCAPE 320x172"); Serial.println("GFX ready — 320x172 landscape");
digitalWrite(BACKLIGHT_PIN, HIGH); digitalWrite(BACKLIGHT_PIN, HIGH);
testDisplay(); testDisplay();
@@ -112,26 +102,22 @@ void setup() {
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
} else { } else {
Serial.println("\nWiFi OK: " + WiFi.localIP().toString()); Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
digitalWrite(BACKLIGHT_PIN, HIGH); publishStatus(); // Initial status
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("=== SETUP COMPLETE ===");
Serial.println(String("State: ") + getStateName(currentState));
Serial.println("Silent: screen dark, gallery-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) {
@@ -140,7 +126,7 @@ void loop() {
} }
if (now - lastPoll >= POLL_INTERVAL) { if (now - lastPoll >= POLL_INTERVAL) {
lastPoll = now; lastPoll = now;
checkAlertTopic(); checkCommandTopic();
} }
break; break;
@@ -151,7 +137,7 @@ void loop() {
} }
updateBlink(now); updateBlink(now);
if (now - blinkStartTime >= BLINK_DURATION) { if (now - blinkStartTime >= BLINK_DURATION) {
Serial.println("ALERT TIMEOUT"); Serial.println("ALERT TIMEOUT — auto silence");
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
} }
break; break;
@@ -160,15 +146,46 @@ void loop() {
delay(10); 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) { void transitionTo(State newState) {
if (newState == currentState) return; if (newState == currentState) return;
Serial.print("STATE: "); Serial.print("STATE: ");
Serial.print(getStateName(currentState)); Serial.print(currentState == STATE_SILENT ? "SILENT" : "ALERT");
Serial.print(" -> "); Serial.print(" -> ");
Serial.println(getStateName(newState)); Serial.println(newState == STATE_SILENT ? "SILENT" : "ALERT");
currentState = newState; currentState = newState;
@@ -177,7 +194,7 @@ 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: cocaine white standby, screen dark"); Serial.println("Silent: screen dark, gallery void");
break; break;
case STATE_ALERT: case STATE_ALERT:
@@ -185,24 +202,120 @@ void transitionTo(State newState) {
blinkStartTime = millis(); blinkStartTime = millis();
blinkState = false; blinkState = false;
drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1); drawAlertScreen(ALERT_BG_1, ALERT_TEXT_1);
Serial.println("ALERT: neon pulse activated"); Serial.println("Alert: neon pulse active");
break; 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) { void checkSilenceTopic() {
return (s == STATE_SILENT) ? "SILENT" : "ALERT"; 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) { void drawAlertScreen(uint16_t bgColor, uint16_t textColor) {
gfx->fillScreen(bgColor); gfx->fillScreen(bgColor);
// "ALERT" header — bold, centered top // "ALERT" header
drawCenteredText("ALERT", 3, textColor, bgColor, 20); drawCenteredText("ALERT", 3, textColor, bgColor, 20);
// The message auto-sized, centered below // The triggering message, auto-sized
if (alertMessage.length() > 0) { if (alertMessage.length() > 0) {
drawAutoSizedMessage(alertMessage, textColor, bgColor, 70); drawAutoSizedMessage(alertMessage, textColor, bgColor, 70);
} }
@@ -256,128 +369,24 @@ void handleButton() {
Serial.println("BUTTON PRESSED"); Serial.println("BUTTON PRESSED");
digitalWrite(BACKLIGHT_PIN, HIGH); digitalWrite(BACKLIGHT_PIN, HIGH);
gfx->fillScreen(COLOR_COCAINE); gfx->fillScreen(COLOR_COCAINE);
drawCenteredText("TEST", 3, COLOR_BLACK, COLOR_COCAINE, 70); drawCenteredText("TEST MODE", 2, COLOR_BLACK, COLOR_COCAINE, 70);
Serial.println("Screen COCAINE WHITE (held)");
} }
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 ALERT -> SILENT"); Serial.println(" -> Reset to SILENT");
transitionTo(STATE_SILENT); transitionTo(STATE_SILENT);
// Mint confirmation flash flashConfirm("SILENCED", COLOR_MINT, COLOR_BLACK);
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 { } else {
gfx->fillScreen(COLOR_BLACK); gfx->fillScreen(COLOR_BLACK);
digitalWrite(BACKLIGHT_PIN, LOW); digitalWrite(BACKLIGHT_PIN, LOW);
Serial.println("Screen BLACK, silent");
} }
} }
lastButtonState = pressed; 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 ============== // ============== BLINK CONTROL ==============
void updateBlink(unsigned long now) { void updateBlink(unsigned long now) {
@@ -386,43 +395,31 @@ void updateBlink(unsigned long now) {
if (newState != blinkState) { if (newState != blinkState) {
blinkState = newState; blinkState = newState;
// Cocaine chic alternation: TEAL ↔ FUCHSIA
uint16_t bgColor = blinkState ? ALERT_BG_2 : ALERT_BG_1; uint16_t bgColor = blinkState ? ALERT_BG_2 : ALERT_BG_1;
uint16_t textColor = blinkState ? 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"); Serial.println(blinkState ? " FUCHSIA flash" : " TEAL flash");
} }
} }
// ============== DISPLAY TEST ============== // ============== DISPLAY TEST ==============
void testDisplay() { void testDisplay() {
Serial.println(" TEAL"); Serial.println("Display test:");
gfx->fillScreen(COLOR_TEAL);
delay(150);
Serial.println(" FUCHSIA"); const char* colors[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"};
gfx->fillScreen(COLOR_FUCHSIA); uint16_t colorVals[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK};
delay(150);
Serial.println(" COCAINE WHITE"); for (int i = 0; i < 5; i++) {
gfx->fillScreen(COLOR_COCAINE); Serial.print(" ");
delay(150); 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); gfx->fillScreen(COLOR_BLACK);
delay(100); drawCenteredText("KLUBHAUS", 3, COLOR_TEAL, COLOR_BLACK, 50);
drawCenteredText("DOORBELL", 3, COLOR_FUCHSIA, COLOR_BLACK, 90);
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); delay(400);
gfx->fillScreen(COLOR_BLACK); gfx->fillScreen(COLOR_BLACK);