works with just blinking backlight for now

This commit is contained in:
2026-02-12 01:07:57 -08:00
parent 5f168f370b
commit f68d820e8b
2 changed files with 180 additions and 107 deletions

View File

@@ -1,6 +1,5 @@
#include <WiFi.h> #include <WiFi.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <TFT_eSPI.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
// ============== CONFIGURATION ============== // ============== CONFIGURATION ==============
@@ -12,108 +11,208 @@ const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1";
const unsigned long POLL_INTERVAL = 15000; const unsigned long POLL_INTERVAL = 15000;
const unsigned long SILENCE_POLL_INTERVAL = 15000; const unsigned long SILENCE_POLL_INTERVAL = 15000;
const unsigned long BLINK_DURATION = 180000; const unsigned long BLINK_DURATION = 180000; // 3 minutes
const unsigned long BLINK_PERIOD = 1000; const unsigned long BLINK_PERIOD = 1000; // 1 second on/off
// Hardware pins
#define BACKLIGHT_PIN 22 // GPIO 22 - backlight control
#define BUTTON_PIN 9 // GPIO 9 - BOOT button
// Backlight brightness levels (0-255)
#define BRIGHTNESS_OFF 0
#define BRIGHTNESS_DIM 50
#define BRIGHTNESS_FULL 255
#define COLOR_RED 0xF800
#define COLOR_WHITE 0xFFFF
#define COLOR_BLACK 0x0000
#define COLOR_GREEN 0x07E0
// =========================================== // ===========================================
TFT_eSPI tft = TFT_eSPI(); // STATE MACHINE
enum State {
STATE_SILENT, // Backlight off or dim
STATE_ALARM // Backlight blinking full brightness
};
State currentState = STATE_SILENT;
unsigned long lastPoll = 0; unsigned long lastPoll = 0;
unsigned long lastSilencePoll = 0; unsigned long lastSilencePoll = 0;
unsigned long blinkStartTime = 0; unsigned long blinkStartTime = 0;
bool isBlinking = false; bool blinkState = false; // false = off, true = on
bool blinkState = false;
String lastAlertId = ""; String lastAlertId = "";
String lastSilenceId = ""; String lastSilenceId = "";
bool buttonWasPressed = false;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
// CRITICAL: Long delay for ESP32-C6 USB enumeration
delay(3000); delay(3000);
Serial.println("=== BOOT START ==="); Serial.println("\n=== BACKLIGHT DOORBELL ===");
// Initialize TFT with error checking // Initialize backlight PWM
Serial.println("Init TFT..."); pinMode(BACKLIGHT_PIN, OUTPUT);
tft.init(); analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
Serial.println("TFT init OK"); Serial.println("Backlight PWM ready on GPIO 22");
tft.setRotation(0); // Initialize button
tft.fillScreen(COLOR_BLACK); pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("TFT cleared"); Serial.println("Button ready on GPIO 9");
// WiFi with timeout to prevent watchdog crash // Test sequence: flash backlight to confirm working
Serial.println("Backlight test sequence...");
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_FULL);
delay(200);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
delay(200);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_FULL);
delay(200);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
Serial.println("Test complete");
// Start in silent state
transitionTo(STATE_SILENT);
// WiFi
Serial.println("Connecting WiFi..."); Serial.println("Connecting WiFi...");
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
int wifiTimeout = 0; int wifiTimeout = 0;
while (WiFi.status() != WL_CONNECTED && wifiTimeout < 40) { // 20 second max while (WiFi.status() != WL_CONNECTED && wifiTimeout < 40) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
wifiTimeout++; wifiTimeout++;
} }
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nWiFi FAILED - continuing anyway"); Serial.println("\nWiFi FAILED - will retry");
// Flash error pattern
for (int i = 0; i < 5; i++) {
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_FULL);
delay(100);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
delay(100);
}
} else { } else {
Serial.println("\nWiFi connected"); Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
Serial.print("IP: "); // Brief success flash
Serial.println(WiFi.localIP()); analogWrite(BACKLIGHT_PIN, BRIGHTNESS_DIM);
delay(500);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
} }
tft.setTextColor(COLOR_WHITE, COLOR_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Ready");
Serial.println("=== SETUP COMPLETE ==="); Serial.println("=== SETUP COMPLETE ===");
Serial.println(String("State: ") + getStateName(currentState));
} }
void loop() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
handleButton();
// Check silence topic every 5 seconds switch (currentState) {
if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) { case STATE_SILENT:
lastSilencePoll = now; // Check silence topic frequently
Serial.println("Checking silence topic..."); if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) {
checkSilenceTopic(); lastSilencePoll = now;
checkSilenceTopic();
}
// Check alert topic periodically
if (now - lastPoll >= POLL_INTERVAL) {
lastPoll = now;
checkAlertTopic();
}
break;
case STATE_ALARM:
// Check silence topic for early stop
if (now - lastSilencePoll >= SILENCE_POLL_INTERVAL) {
lastSilencePoll = now;
checkSilenceTopic();
}
// Update blinking
updateBlink(now);
// Auto-timeout
if (now - blinkStartTime >= BLINK_DURATION) {
Serial.println("ALARM TIMEOUT - auto silence");
transitionTo(STATE_SILENT);
}
break;
} }
// Check alert topic every 30 seconds delay(10);
if (now - lastPoll >= POLL_INTERVAL) {
lastPoll = now;
Serial.println("Checking alert topic...");
checkAlertTopic();
}
// Handle blinking
if (isBlinking) {
updateBlink(now);
}
delay(10); // Yield to prevent watchdog
} }
// ============== 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:
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
Serial.println("Backlight OFF");
break;
case STATE_ALARM:
blinkStartTime = millis();
blinkState = false;
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_FULL);
Serial.println("Backlight BLINK started");
break;
}
}
const char* getStateName(State s) {
return (s == STATE_SILENT) ? "SILENT" : "ALARM";
}
// ============== BUTTON HANDLING ==============
void handleButton() {
bool pressed = (digitalRead(BUTTON_PIN) == LOW); // Active low
if (pressed && !buttonWasPressed) {
// Button just pressed
Serial.println("BUTTON PRESSED - test mode");
buttonWasPressed = true;
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_FULL);
Serial.println("Backlight FULL (held)");
}
else if (!pressed && buttonWasPressed) {
// Button released
Serial.println("BUTTON RELEASED");
buttonWasPressed = false;
// Restore state
if (currentState == STATE_SILENT) {
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
Serial.println("Backlight OFF (silent)");
} else {
// Let updateBlink handle it on next loop
Serial.println("Backlight returns to blink mode");
}
}
}
// ============== NETWORK FUNCTIONS ==============
void checkAlertTopic() { void checkAlertTopic() {
Serial.println("Poll alert...");
String response = fetchNtfy(alertTopic); String response = fetchNtfy(alertTopic);
if (response.length() == 0) { if (response.length() == 0) {
Serial.println("No response from alert topic"); Serial.println(" No response");
return; return;
} }
Serial.print("Alert response: ");
Serial.println(response);
StaticJsonDocument<512> doc; StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, response); DeserializationError error = deserializeJson(doc, response);
if (error) { if (error) {
Serial.print("JSON parse failed: "); Serial.print(" JSON error: ");
Serial.println(error.c_str()); Serial.println(error.c_str());
return; return;
} }
@@ -122,21 +221,24 @@ void checkAlertTopic() {
String message = doc["message"] | ""; String message = doc["message"] | "";
if (id == lastAlertId) { if (id == lastAlertId) {
Serial.println("Same alert ID, skipping"); Serial.println(" Same ID, skip");
return; return;
} }
lastAlertId = id; lastAlertId = id;
Serial.print("New alert: "); Serial.print(" New message: ");
Serial.println(message); Serial.println(message);
if (message.equalsIgnoreCase("SILENCE")) { if (message.equalsIgnoreCase("SILENCE")) {
Serial.println("SILENCE command received"); Serial.println(" -> SILENCE command");
stopBlinking(); transitionTo(STATE_SILENT);
showStatus("SILENCE", COLOR_GREEN); // Brief green-like flash (dim) to confirm
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_DIM);
delay(500);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
} else { } else {
Serial.println("TRIGGERING ALARM"); Serial.println(" -> TRIGGER ALARM");
startBlinking(); transitionTo(STATE_ALARM);
} }
} }
@@ -146,24 +248,22 @@ void checkSilenceTopic() {
StaticJsonDocument<512> doc; StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, response); DeserializationError error = deserializeJson(doc, response);
if (error) return; if (error) return;
String id = doc["id"] | ""; String id = doc["id"] | "";
String message = doc["message"] | "";
if (id == lastSilenceId) return; if (id == lastSilenceId) return;
lastSilenceId = id; lastSilenceId = id;
Serial.print("Silence topic: "); Serial.print("Silence topic: ");
Serial.println(message); Serial.println((const char*)doc["message"]);
if (isBlinking) { if (currentState == STATE_ALARM) {
Serial.println("Stopping alarm via silence topic"); Serial.println(" -> Stopping alarm");
stopBlinking(); transitionTo(STATE_SILENT);
showStatus("SILENCED", COLOR_GREEN); // Brief dim flash to confirm
delay(1000); analogWrite(BACKLIGHT_PIN, BRIGHTNESS_DIM);
tft.fillScreen(COLOR_BLACK); delay(500);
analogWrite(BACKLIGHT_PIN, BRIGHTNESS_OFF);
} }
} }
@@ -172,11 +272,8 @@ String fetchNtfy(const char* url) {
http.begin(url); http.begin(url);
http.setTimeout(5000); http.setTimeout(5000);
Serial.print("HTTP GET: ");
Serial.println(url);
int code = http.GET(); int code = http.GET();
Serial.print("HTTP code: "); Serial.print(" HTTP ");
Serial.println(code); Serial.println(code);
if (code != 200) { if (code != 200) {
@@ -188,48 +285,21 @@ String fetchNtfy(const char* url) {
http.end(); http.end();
int newline = payload.indexOf('\n'); int newline = payload.indexOf('\n');
if (newline > 0) { if (newline > 0) payload = payload.substring(0, newline);
payload = payload.substring(0, newline);
}
return payload; return payload;
} }
void startBlinking() { // ============== BLINK CONTROL ==============
isBlinking = true;
blinkStartTime = millis();
blinkState = false;
tft.fillScreen(COLOR_RED);
Serial.println("ALARM ON");
}
void stopBlinking() {
isBlinking = false;
tft.fillScreen(COLOR_BLACK);
Serial.println("ALARM OFF");
}
void updateBlink(unsigned long now) { void updateBlink(unsigned long now) {
if (now - blinkStartTime >= BLINK_DURATION) {
stopBlinking();
return;
}
unsigned long elapsed = now - blinkStartTime; unsigned long elapsed = now - blinkStartTime;
bool newState = (elapsed / BLINK_PERIOD) % 2 == 1; bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1;
if (newState != blinkState) { if (newState != blinkState) {
blinkState = newState; blinkState = newState;
tft.fillScreen(blinkState ? COLOR_WHITE : COLOR_RED); analogWrite(BACKLIGHT_PIN, blinkState ? BRIGHTNESS_FULL : BRIGHTNESS_OFF);
Serial.println(blinkState ? "WHITE" : "RED"); Serial.println(blinkState ? "BLINK: ON" : "BLINK: OFF");
} }
} }
void showStatus(const char* text, uint16_t color) {
tft.fillScreen(COLOR_BLACK);
tft.setTextColor(color, COLOR_BLACK);
tft.setTextSize(3);
tft.setCursor(20, 140);
tft.println(text);
}

3
sketches/mise.toml Normal file
View File

@@ -0,0 +1,3 @@
[tasks.upload]
run = "arduino-cli compile --upload; arduino-cli monitor"
dir = "{{cwd}}"