snapshot
This commit is contained in:
@@ -11,11 +11,12 @@ const char* silenceTopic = "http://ntfy.sh/SILENCE_klubhaus_topic/json?poll=1";
|
||||
|
||||
const unsigned long POLL_INTERVAL = 15000;
|
||||
const unsigned long SILENCE_POLL_INTERVAL = 15000;
|
||||
const unsigned long BLINK_DURATION = 180000;
|
||||
const unsigned long BLINK_PERIOD = 1000;
|
||||
const unsigned long BLINK_DURATION = 180000; // 3 minutes
|
||||
const unsigned long BLINK_PERIOD = 1000; // 1 second on/off
|
||||
|
||||
#define RGB_LED_PIN 8
|
||||
#define BUTTON_PIN 9
|
||||
// Hardware pins (confirmed from Waveshare wiki 【0】【1】)
|
||||
#define BACKLIGHT_PIN 22 // LCD backlight - simple on/off control
|
||||
#define BUTTON_PIN 9 // BOOT button
|
||||
|
||||
// ===========================================
|
||||
|
||||
@@ -30,67 +31,36 @@ bool blinkState = false;
|
||||
|
||||
String lastAlertId = "";
|
||||
String lastSilenceId = "";
|
||||
bool buttonWasPressed = false;
|
||||
bool lastButtonState = false; // For edge detection
|
||||
|
||||
// RGB colors
|
||||
struct Color { uint8_t r, g, b; };
|
||||
const Color COLOR_OFF = {0, 0, 0};
|
||||
const Color COLOR_RED = {255, 0, 0};
|
||||
const Color COLOR_GREEN = {0, 255, 0};
|
||||
const Color COLOR_BLUE = {0, 0, 255};
|
||||
const Color COLOR_WHITE = {255, 255, 255};
|
||||
|
||||
// Simple WS2812B bit-bang for single LED
|
||||
void setRGB(Color c) {
|
||||
uint32_t grb = ((uint32_t)c.g << 16) | ((uint32_t)c.r << 8) | c.b;
|
||||
|
||||
noInterrupts();
|
||||
for (int i = 23; i >= 0; i--) {
|
||||
if (grb & (1 << i)) {
|
||||
digitalWrite(RGB_LED_PIN, HIGH);
|
||||
delayMicroseconds(1);
|
||||
digitalWrite(RGB_LED_PIN, LOW);
|
||||
delayMicroseconds(1);
|
||||
} else {
|
||||
digitalWrite(RGB_LED_PIN, HIGH);
|
||||
delayMicroseconds(1);
|
||||
digitalWrite(RGB_LED_PIN, LOW);
|
||||
delayMicroseconds(2);
|
||||
}
|
||||
}
|
||||
interrupts();
|
||||
delay(1); // Reset time for WS2812B
|
||||
}
|
||||
bool lastButtonState = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(3000);
|
||||
Serial.println("\n=== RGB DOORBELL ===");
|
||||
Serial.println("\n=== BACKLIGHT DOORBELL ===");
|
||||
|
||||
// Init RGB LED pin
|
||||
pinMode(RGB_LED_PIN, OUTPUT);
|
||||
digitalWrite(RGB_LED_PIN, LOW);
|
||||
|
||||
// CRITICAL: Ensure LED is OFF before test
|
||||
setRGB(COLOR_OFF);
|
||||
delay(100);
|
||||
|
||||
Serial.println("RGB LED on GPIO 8 ready");
|
||||
// Init backlight as digital output
|
||||
pinMode(BACKLIGHT_PIN, OUTPUT);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW); // Start OFF
|
||||
Serial.println("Backlight on GPIO 22 ready");
|
||||
|
||||
// Init button
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
Serial.println("Button on GPIO 9 ready");
|
||||
|
||||
// Test sequence with explicit OFF at end
|
||||
Serial.println("RGB test...");
|
||||
setRGB(COLOR_RED); delay(200);
|
||||
setRGB(COLOR_GREEN); delay(200);
|
||||
setRGB(COLOR_BLUE); delay(200);
|
||||
setRGB(COLOR_OFF); delay(100); // CRITICAL: return to off
|
||||
Serial.println("Test complete - LED should be OFF");
|
||||
// Test: flash 3 times to confirm working
|
||||
Serial.println("Backlight test...");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
delay(100);
|
||||
}
|
||||
Serial.println("Test complete - OFF");
|
||||
|
||||
// WiFi with explicit LED management
|
||||
// Start in silent state
|
||||
transitionTo(STATE_SILENT);
|
||||
|
||||
// WiFi
|
||||
Serial.println("Connecting WiFi...");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
@@ -103,28 +73,27 @@ void setup() {
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("\nWiFi FAILED");
|
||||
// Error flash then OFF
|
||||
for (int i = 0; i < 5; i++) {
|
||||
setRGB(COLOR_RED); delay(100);
|
||||
setRGB(COLOR_OFF); delay(100);
|
||||
// Error flash: rapid blinking
|
||||
for (int i = 0; i < 10; i++) {
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(50);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
delay(50);
|
||||
}
|
||||
} else {
|
||||
Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
|
||||
// Success flash then OFF
|
||||
setRGB(COLOR_GREEN); delay(300);
|
||||
setRGB(COLOR_OFF); delay(100);
|
||||
// Success: single long flash
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(500);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
}
|
||||
|
||||
// CRITICAL: Force LED off before state transition
|
||||
setRGB(COLOR_OFF);
|
||||
Serial.println("LED forced OFF after WiFi");
|
||||
|
||||
// Now start in silent state
|
||||
transitionTo(STATE_SILENT);
|
||||
// Ensure OFF before main loop
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
Serial.println("Backlight OFF - ready");
|
||||
|
||||
Serial.println("=== SETUP COMPLETE ===");
|
||||
Serial.println(String("State: ") + getStateName(currentState));
|
||||
Serial.println(String("LED should be OFF, button ready"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -159,6 +128,8 @@ void loop() {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// ============== STATE MANAGEMENT ==============
|
||||
|
||||
void transitionTo(State newState) {
|
||||
if (newState == currentState) return;
|
||||
|
||||
@@ -171,15 +142,15 @@ void transitionTo(State newState) {
|
||||
|
||||
switch (currentState) {
|
||||
case STATE_SILENT:
|
||||
setRGB(COLOR_OFF);
|
||||
Serial.println("LED OFF (silent)");
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
Serial.println("Backlight OFF");
|
||||
break;
|
||||
|
||||
case STATE_ALARM:
|
||||
blinkStartTime = millis();
|
||||
blinkState = false;
|
||||
setRGB(COLOR_RED);
|
||||
Serial.println("LED ALARM started");
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH); // Start ON
|
||||
Serial.println("Backlight BLINK started");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -188,39 +159,42 @@ const char* getStateName(State s) {
|
||||
return (s == STATE_SILENT) ? "SILENT" : "ALARM";
|
||||
}
|
||||
|
||||
// ============== BUTTON HANDLING ==============
|
||||
|
||||
void handleButton() {
|
||||
bool pressed = (digitalRead(BUTTON_PIN) == LOW);
|
||||
|
||||
// Edge detection: only act on changes
|
||||
if (pressed && !lastButtonState) {
|
||||
// Button just pressed
|
||||
Serial.println("BUTTON PRESSED");
|
||||
buttonWasPressed = true;
|
||||
setRGB(COLOR_WHITE);
|
||||
Serial.println("LED WHITE (held)");
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH); // Solid on while held
|
||||
Serial.println("Backlight ON (held)");
|
||||
}
|
||||
else if (!pressed && lastButtonState) {
|
||||
// Button just released
|
||||
Serial.println("BUTTON RELEASED");
|
||||
buttonWasPressed = false;
|
||||
|
||||
if (currentState == STATE_ALARM) {
|
||||
Serial.println("Resetting ALARM -> SILENT");
|
||||
// NEW: Reset to silent when released in alarm state
|
||||
Serial.println("Reset ALARM -> SILENT");
|
||||
transitionTo(STATE_SILENT);
|
||||
// Green confirmation
|
||||
setRGB(COLOR_GREEN);
|
||||
delay(300);
|
||||
setRGB(COLOR_OFF);
|
||||
// Brief confirmation flash
|
||||
delay(100);
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
} else {
|
||||
// Already silent, ensure off
|
||||
setRGB(COLOR_OFF);
|
||||
Serial.println("LED OFF (silent)");
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
Serial.println("Backlight OFF (silent)");
|
||||
}
|
||||
}
|
||||
|
||||
lastButtonState = pressed;
|
||||
}
|
||||
|
||||
// ============== NETWORK FUNCTIONS ==============
|
||||
|
||||
void checkAlertTopic() {
|
||||
Serial.println("Poll alert...");
|
||||
String response = fetchNtfy(alertTopic);
|
||||
@@ -252,9 +226,10 @@ void checkAlertTopic() {
|
||||
if (message.equalsIgnoreCase("SILENCE")) {
|
||||
Serial.println(" -> SILENCE");
|
||||
transitionTo(STATE_SILENT);
|
||||
setRGB(COLOR_GREEN);
|
||||
delay(500);
|
||||
setRGB(COLOR_OFF);
|
||||
// Confirmation flash
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(200);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
} else {
|
||||
Serial.println(" -> ALARM");
|
||||
transitionTo(STATE_ALARM);
|
||||
@@ -279,9 +254,10 @@ void checkSilenceTopic() {
|
||||
if (currentState == STATE_ALARM) {
|
||||
Serial.println(" -> Stop alarm");
|
||||
transitionTo(STATE_SILENT);
|
||||
setRGB(COLOR_GREEN);
|
||||
delay(500);
|
||||
setRGB(COLOR_OFF);
|
||||
// Confirmation flash
|
||||
digitalWrite(BACKLIGHT_PIN, HIGH);
|
||||
delay(200);
|
||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,14 +284,16 @@ String fetchNtfy(const char* url) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
// ============== BLINK CONTROL ==============
|
||||
|
||||
void updateBlink(unsigned long now) {
|
||||
unsigned long elapsed = now - blinkStartTime;
|
||||
bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1;
|
||||
bool newState = ((elapsed / BLINK_PERIOD) % 2) == 0; // Even = ON, odd = OFF
|
||||
|
||||
if (newState != blinkState) {
|
||||
blinkState = newState;
|
||||
setRGB(blinkState ? COLOR_WHITE : COLOR_RED);
|
||||
Serial.println(blinkState ? "WHITE" : "RED");
|
||||
digitalWrite(BACKLIGHT_PIN, blinkState ? HIGH : LOW);
|
||||
Serial.println(blinkState ? "BLINK: ON" : "BLINK: OFF");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user