1. **Display Driver Upgrade**
- Replaced simple backlight control with full **Arduino_GFX library** integration
- Configured ST7789 display controller (172x320, with 34px column offset)
- Added SPI bus setup for display communication
2. **Visual Feedback Enhancement**
- Transitioned from binary backlight (ON/OFF) to **full-color screen displays**:
- **Silent state**: Black screen
- **Alarm state**: Red/White blinking
- **Button press**: White screen
- **Silence confirmed**: Green screen with text
- Added colored status messages (WiFi errors, silence confirmations)
3. **Display Test Function**
- New `testDisplay()` cycles through RED → GREEN → BLUE → WHITE → BLACK on startup
- Verifies display hardware functionality
- **Better user feedback**: Color-coded states and text messages replace simple backlight blinking
- **Easier troubleshooting**: Startup test validates display, WiFi errors shown visually with text
- **Enhanced alarm**: Alternating red/white screens more noticeable than simple blinking
- **Hardware utilization**: Fully leverages the 1.47" color LCD instead of treating it as a simple backlight
345 lines
8.1 KiB
C++
345 lines
8.1 KiB
C++
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
#include <Arduino_GFX_Library.h>
|
|
|
|
// ============== 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 = 15000;
|
|
const unsigned long SILENCE_POLL_INTERVAL = 15000;
|
|
const unsigned long BLINK_DURATION = 180000;
|
|
const unsigned long BLINK_PERIOD = 1000;
|
|
|
|
// Hardware pins (from working Waveshare demo)
|
|
#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
|
|
|
|
// ===========================================
|
|
|
|
// Arduino_GFX setup for Waveshare ESP32-C6-LCD-1.47
|
|
// ST7789, 172x320, with column offset 34 【2】
|
|
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 */,
|
|
172 /* width */, 320 /* 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;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(3000);
|
|
Serial.println("\n=== GFX DOORBELL ===");
|
|
|
|
// Init backlight
|
|
pinMode(BACKLIGHT_PIN, OUTPUT);
|
|
digitalWrite(BACKLIGHT_PIN, HIGH);
|
|
Serial.println("Backlight ON");
|
|
|
|
// Init button
|
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
|
Serial.println("Button ready");
|
|
|
|
// Init display
|
|
Serial.println("Init GFX...");
|
|
gfx->begin();
|
|
Serial.println("GFX begin OK");
|
|
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
Serial.println("Screen cleared");
|
|
|
|
// Test pattern
|
|
Serial.println("Display test...");
|
|
testDisplay();
|
|
|
|
// Start silent
|
|
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");
|
|
gfx->fillScreen(COLOR_RED);
|
|
gfx->setTextColor(COLOR_WHITE, COLOR_RED);
|
|
gfx->setTextSize(2);
|
|
gfx->setCursor(10, 140);
|
|
gfx->println("NO WIFI");
|
|
} else {
|
|
Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
|
|
gfx->setTextColor(COLOR_GREEN, COLOR_BLACK);
|
|
gfx->setTextSize(2);
|
|
gfx->setCursor(10, 10);
|
|
gfx->println("WiFi OK");
|
|
delay(500);
|
|
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:
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
Serial.println("Screen BLACK (silent)");
|
|
break;
|
|
|
|
case STATE_ALARM:
|
|
blinkStartTime = millis();
|
|
blinkState = false;
|
|
gfx->fillScreen(COLOR_RED);
|
|
Serial.println("Screen ALARM started (red/white blink)");
|
|
break;
|
|
}
|
|
}
|
|
|
|
const char* getStateName(State s) {
|
|
return (s == STATE_SILENT) ? "SILENT" : "ALARM";
|
|
}
|
|
|
|
// ============== BUTTON HANDLING ==============
|
|
|
|
void handleButton() {
|
|
bool pressed = (digitalRead(BUTTON_PIN) == LOW);
|
|
|
|
if (pressed && !lastButtonState) {
|
|
Serial.println("BUTTON PRESSED");
|
|
gfx->fillScreen(COLOR_WHITE);
|
|
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);
|
|
// Green flash confirmation
|
|
gfx->fillScreen(COLOR_GREEN);
|
|
delay(300);
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
} else {
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
Serial.println("Screen BLACK (silent)");
|
|
}
|
|
}
|
|
|
|
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");
|
|
transitionTo(STATE_SILENT);
|
|
gfx->fillScreen(COLOR_GREEN);
|
|
gfx->setTextColor(COLOR_BLACK, COLOR_GREEN);
|
|
gfx->setTextSize(3);
|
|
gfx->setCursor(20, 140);
|
|
gfx->println("SILENCE");
|
|
delay(1000);
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
} else {
|
|
Serial.println(" -> ALARM");
|
|
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");
|
|
transitionTo(STATE_SILENT);
|
|
gfx->fillScreen(COLOR_GREEN);
|
|
gfx->setTextColor(COLOR_BLACK, COLOR_GREEN);
|
|
gfx->setTextSize(3);
|
|
gfx->setCursor(20, 140);
|
|
gfx->println("SILENCED");
|
|
delay(1000);
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// ============== DISPLAY FUNCTIONS ==============
|
|
|
|
void testDisplay() {
|
|
Serial.println(" RED");
|
|
gfx->fillScreen(COLOR_RED);
|
|
delay(200);
|
|
|
|
Serial.println(" GREEN");
|
|
gfx->fillScreen(COLOR_GREEN);
|
|
delay(200);
|
|
|
|
Serial.println(" BLUE");
|
|
gfx->fillScreen(COLOR_BLUE);
|
|
delay(200);
|
|
|
|
Serial.println(" WHITE");
|
|
gfx->fillScreen(COLOR_WHITE);
|
|
delay(200);
|
|
|
|
Serial.println(" BLACK");
|
|
gfx->fillScreen(COLOR_BLACK);
|
|
delay(200);
|
|
|
|
Serial.println("Test complete");
|
|
}
|
|
|
|
void updateBlink(unsigned long now) {
|
|
unsigned long elapsed = now - blinkStartTime;
|
|
bool newState = ((elapsed / BLINK_PERIOD) % 2) == 1;
|
|
|
|
if (newState != blinkState) {
|
|
blinkState = newState;
|
|
gfx->fillScreen(blinkState ? COLOR_WHITE : COLOR_RED);
|
|
Serial.println(blinkState ? "WHITE" : "RED");
|
|
}
|
|
}
|
|
|