snapshot
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
// ============== KLUBHAUS DOORBELL v3.8.2 ==============
|
// ============== KLUBHAUS DOORBELL v3.8.4 ==============
|
||||||
// Fix: Remove ?poll=1 for short-polling, add raw response logging
|
// Fix: Use ?since=all to retrieve cached messages, decode HTTP errors
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
@@ -11,10 +11,9 @@
|
|||||||
#define WIFI_PASS "lesson-greater"
|
#define WIFI_PASS "lesson-greater"
|
||||||
|
|
||||||
// ntfy.sh topics — naming indicates direction
|
// ntfy.sh topics — naming indicates direction
|
||||||
#define NTFY_ALERT_TOPIC "ALERT_klubhaus_topic" // Web POSTs here, device polls
|
#define NTFY_ALERT_TOPIC "ALERT_klubhaus_topic"
|
||||||
#define NTFY_SILENCE_TOPIC "SILENCE_klubhaus_topic" // Web POSTs here, device polls
|
#define NTFY_SILENCE_TOPIC "SILENCE_klubhaus_topic"
|
||||||
#define NTFY_STATUS_TOPIC "STATUS_klubhaus_topic" // Device POSTs here, web listens
|
#define NTFY_STATUS_TOPIC "STATUS_klubhaus_topic"
|
||||||
|
|
||||||
#define NTFY_SERVER "ntfy.sh"
|
#define NTFY_SERVER "ntfy.sh"
|
||||||
|
|
||||||
// Display pins for ESP32-C6-LCD-1.47
|
// Display pins for ESP32-C6-LCD-1.47
|
||||||
@@ -24,228 +23,158 @@
|
|||||||
#define TFT_DC 15
|
#define TFT_DC 15
|
||||||
#define TFT_RST 21
|
#define TFT_RST 21
|
||||||
#define TFT_BL 22
|
#define TFT_BL 22
|
||||||
|
|
||||||
#define BUTTON_PIN 9
|
#define BUTTON_PIN 9
|
||||||
|
|
||||||
// ============== COLORS (Cocaine Chic) ==============
|
// ============== COLORS ==============
|
||||||
#define COLOR_TEAL 0x0BD3D3
|
#define COLOR_TEAL 0x0BD3D3
|
||||||
#define COLOR_FUCHSIA 0xF890E7
|
#define COLOR_FUCHSIA 0xF890E7
|
||||||
#define COLOR_WHITE 0xFFF8FA
|
#define COLOR_WHITE 0xFFF8FA
|
||||||
#define COLOR_MINT 0x64E8BA
|
#define COLOR_MINT 0x64E8BA
|
||||||
#define COLOR_BLACK 0x0A0A0A
|
#define COLOR_BLACK 0x0A0A0A
|
||||||
#define COLOR_DARK_GRAY 0x1A1A1A
|
|
||||||
|
|
||||||
// ============== DISPLAY ==============
|
// ============== DISPLAY ==============
|
||||||
Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, TFT_SCLK, TFT_MOSI, GFX_NOT_DEFINED);
|
Arduino_DataBus *bus = new Arduino_SWSPI(TFT_DC, TFT_CS, TFT_SCLK, TFT_MOSI, GFX_NOT_DEFINED);
|
||||||
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0 /* rotation */, true /* IPS */, 172, 320);
|
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0, true, 172, 320);
|
||||||
|
|
||||||
// ============== STATE ==============
|
|
||||||
enum State { SILENT, ALERTING };
|
enum State { SILENT, ALERTING };
|
||||||
State currentState = SILENT;
|
State currentState = SILENT;
|
||||||
String currentMessage = "";
|
String currentMessage = "";
|
||||||
String lastProcessedId = "";
|
String lastProcessedId = "";
|
||||||
unsigned long lastAlertTime = 0;
|
|
||||||
|
|
||||||
// ============== TIMING ==============
|
|
||||||
unsigned long lastAlertPoll = 0;
|
unsigned long lastAlertPoll = 0;
|
||||||
unsigned long lastSilencePoll = 0;
|
unsigned long lastSilencePoll = 0;
|
||||||
const unsigned long POLL_INTERVAL_MS = 5000; // 5 second short-poll
|
const unsigned long POLL_INTERVAL_MS = 3000; // Faster polling for testing
|
||||||
const unsigned long ERROR_BACKOFF_MS = 60000; // 60s on error
|
|
||||||
|
|
||||||
unsigned long errorBackoffUntil = 0;
|
|
||||||
|
|
||||||
// ============== LOGGING ==============
|
// ============== LOGGING ==============
|
||||||
enum LogLevel { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
|
enum LogLevel { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
|
||||||
|
|
||||||
void logString(LogLevel level, const char* component, const char* event, const String& detail) {
|
void logMsg(LogLevel level, const char* component, const String& msg) {
|
||||||
const char* levelStr[] = {"DEBUG", "INFO ", "WARN ", "ERROR", "FATAL"};
|
const char* lvl[] = {"DBG", "INF", "WRN", "ERR", "FTL"};
|
||||||
|
Serial.printf("[%s] %s: %s\n", lvl[level], component, msg.c_str());
|
||||||
unsigned long epochMs = getEpochMillis();
|
|
||||||
char ts[32];
|
|
||||||
snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
|
|
||||||
(int)(epochMs / 31536000000ULL + 1970), // year approx
|
|
||||||
1, 1, 0, 0, 0, (int)(epochMs % 1000));
|
|
||||||
|
|
||||||
// Simplified timestamp — use NTP time if available
|
|
||||||
Serial.printf("%s [%s] %s: %s", ts, levelStr[level], component, event);
|
|
||||||
if (detail.length() > 0) {
|
|
||||||
Serial.printf(" | %s", detail.c_str());
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix: Separate overload to avoid recursive call bug
|
void logMsg(LogLevel level, const char* component, const char* msg) {
|
||||||
void logString(LogLevel level, const char* component, const char* event) {
|
logMsg(level, component, String(msg));
|
||||||
logString(level, component, event, String(""));
|
}
|
||||||
|
|
||||||
|
// ============== HTTP ERROR DECODER ==============
|
||||||
|
String decodeHttpError(int code) {
|
||||||
|
// HTTPClient error codes (negative values)
|
||||||
|
if (code == -1) return "CONNECTION_REFUSED/SEND_FAILED";
|
||||||
|
if (code == -2) return "SEND_PAYLOAD_FAILED";
|
||||||
|
if (code == -3) return "NOT_CONNECTED";
|
||||||
|
if (code == -4) return "CONNECTION_LOST";
|
||||||
|
if (code == -5) return "NO_STREAM";
|
||||||
|
if (code == -6) return "NO_HTTP_SERVER";
|
||||||
|
if (code == -7) return "TOO_LESS_RAM";
|
||||||
|
if (code == -8) return "ENCODING";
|
||||||
|
if (code == -9) return "STREAM_WRITE";
|
||||||
|
if (code == -10) return "READ_TIMEOUT";
|
||||||
|
if (code == -11) return "CONNECTION_TIMEOUT";
|
||||||
|
// Positive = HTTP status
|
||||||
|
return "HTTP_" + String(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============== TIME ==============
|
// ============== TIME ==============
|
||||||
unsigned long epochOffsetMs = 0;
|
unsigned long getEpochMs() {
|
||||||
bool timeSynced = false;
|
return millis(); // Simplified for now
|
||||||
|
|
||||||
unsigned long getEpochMillis() {
|
|
||||||
if (!timeSynced) return millis();
|
|
||||||
return epochOffsetMs + millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initTime() {
|
// ============== DISPLAY ==============
|
||||||
logString(LOG_INFO, "TIME", "Initializing NTP...");
|
|
||||||
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
|
|
||||||
|
|
||||||
// Wait for sync (max 10s)
|
|
||||||
for (int i = 0; i < 20 && !timeSynced; i++) {
|
|
||||||
struct tm timeinfo;
|
|
||||||
if (getLocalTime(&timeinfo, 500)) {
|
|
||||||
time_t now = time(nullptr);
|
|
||||||
epochOffsetMs = (unsigned long)now * 1000UL - millis();
|
|
||||||
timeSynced = true;
|
|
||||||
logString(LOG_INFO, "TIME", "synced", String(epochOffsetMs));
|
|
||||||
}
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============== DISPLAY FUNCTIONS ==============
|
|
||||||
void initDisplay() {
|
void initDisplay() {
|
||||||
pinMode(TFT_BL, OUTPUT);
|
pinMode(TFT_BL, OUTPUT);
|
||||||
digitalWrite(TFT_BL, LOW); // Start with backlight off
|
digitalWrite(TFT_BL, LOW);
|
||||||
|
|
||||||
gfx->begin();
|
gfx->begin();
|
||||||
gfx->setRotation(1); // Landscape
|
gfx->setRotation(1);
|
||||||
gfx->fillScreen(COLOR_BLACK);
|
gfx->fillScreen(COLOR_BLACK);
|
||||||
|
|
||||||
// Color test
|
// Test pattern
|
||||||
logString(LOG_DEBUG, "DISPLAY", "Running color test...");
|
|
||||||
gfx->fillRect(0, 0, 80, 172, COLOR_TEAL);
|
gfx->fillRect(0, 0, 80, 172, COLOR_TEAL);
|
||||||
gfx->fillRect(80, 0, 80, 172, COLOR_FUCHSIA);
|
gfx->fillRect(80, 0, 80, 172, COLOR_FUCHSIA);
|
||||||
gfx->fillRect(160, 0, 80, 172, COLOR_WHITE);
|
delay(500);
|
||||||
gfx->fillRect(240, 0, 80, 172, COLOR_MINT);
|
digitalWrite(TFT_BL, LOW);
|
||||||
delay(1000);
|
|
||||||
|
|
||||||
setBacklight(false);
|
|
||||||
showSilentScreen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBacklight(bool on) {
|
void setBacklight(bool on) {
|
||||||
digitalWrite(TFT_BL, on ? HIGH : LOW);
|
digitalWrite(TFT_BL, on ? HIGH : LOW);
|
||||||
logString(LOG_DEBUG, "BACKLIGHT", on ? "state=1" : "state=0");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void showSilentScreen() {
|
void showSilent() {
|
||||||
gfx->fillScreen(COLOR_BLACK);
|
gfx->fillScreen(COLOR_BLACK);
|
||||||
setBacklight(false); // Power saving in silent mode
|
setBacklight(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showAlertScreen(const String& message) {
|
void showAlert(const String& msg) {
|
||||||
setBacklight(true);
|
setBacklight(true);
|
||||||
|
int fontSize = msg.length() > 20 ? 2 : (msg.length() > 10 ? 3 : 4);
|
||||||
// Auto-size text to fit
|
|
||||||
int textLen = message.length();
|
|
||||||
int fontSize = textLen > 20 ? 2 : (textLen > 10 ? 3 : 4);
|
|
||||||
|
|
||||||
gfx->fillScreen(COLOR_TEAL);
|
gfx->fillScreen(COLOR_TEAL);
|
||||||
gfx->setTextColor(COLOR_WHITE);
|
gfx->setTextColor(COLOR_WHITE);
|
||||||
gfx->setTextSize(fontSize);
|
gfx->setTextSize(fontSize);
|
||||||
|
|
||||||
// Center text
|
|
||||||
int16_t x1, y1;
|
int16_t x1, y1;
|
||||||
uint16_t w, h;
|
uint16_t w, h;
|
||||||
gfx->getTextBounds(message.c_str(), 0, 0, &x1, &y1, &w, &h);
|
gfx->getTextBounds(msg.c_str(), 0, 0, &x1, &y1, &w, &h);
|
||||||
int x = (320 - w) / 2;
|
gfx->setCursor((320-w)/2, (172-h)/2);
|
||||||
int y = (172 - h) / 2;
|
gfx->println(msg);
|
||||||
gfx->setCursor(x, y);
|
|
||||||
gfx->println(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void flashConfirm(const char* text) {
|
|
||||||
logString(LOG_DEBUG, "DISPLAY", "flashConfirm", text);
|
|
||||||
setBacklight(true);
|
|
||||||
gfx->fillScreen(COLOR_MINT);
|
|
||||||
gfx->setTextColor(COLOR_BLACK);
|
|
||||||
gfx->setTextSize(2);
|
|
||||||
gfx->setCursor(100, 80);
|
|
||||||
gfx->println(text);
|
|
||||||
delay(500);
|
|
||||||
setBacklight(false);
|
|
||||||
showSilentScreen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============== NETWORK ==============
|
// ============== NETWORK ==============
|
||||||
void initWiFi() {
|
void initWiFi() {
|
||||||
logString(LOG_INFO, "WIFI", "Connecting...");
|
logMsg(LOG_INFO, "WIFI", "Connecting...");
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500); Serial.print(".");
|
||||||
Serial.print(".");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
logString(LOG_INFO, "WIFI", "Connected", WiFi.localIP().toString());
|
logMsg(LOG_INFO, "WIFI", "IP: " + WiFi.localIP().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============== ntfy.sh HTTP ==============
|
// ============== ntfy.sh WITH ?since=all ==============
|
||||||
|
|
||||||
// NEW: Parse single NDJSON line, return true if valid message found
|
bool parseNtfyLine(const String& line, String& outId, String& outMessage, String& outEvent) {
|
||||||
bool parseNtfyLine(const String& line, String& outId, String& outMessage, unsigned long& outTime) {
|
if (line.length() == 0 || line.indexOf('{') < 0) return false;
|
||||||
if (line.length() == 0) return false;
|
|
||||||
|
|
||||||
// Check for JSON object start
|
// Quick JSON extraction (no library needed for simple fields)
|
||||||
if (line.indexOf('{') < 0) return false;
|
auto extract = [&](const char* key) -> String {
|
||||||
|
String search = String("\"") + key + "\":\"";
|
||||||
|
int start = line.indexOf(search);
|
||||||
|
if (start < 0) {
|
||||||
|
// Try number format: "key":123
|
||||||
|
search = String("\"") + key + "\":";
|
||||||
|
start = line.indexOf(search);
|
||||||
|
if (start < 0) return "";
|
||||||
|
start += search.length();
|
||||||
|
int end = line.indexOf(",", start);
|
||||||
|
if (end < 0) end = line.indexOf("}", start);
|
||||||
|
return line.substring(start, end);
|
||||||
|
}
|
||||||
|
start += search.length();
|
||||||
|
int end = line.indexOf("\"", start);
|
||||||
|
return line.substring(start, end);
|
||||||
|
};
|
||||||
|
|
||||||
// Extract id
|
outId = extract("id");
|
||||||
int idStart = line.indexOf("\"id\":\"");
|
outMessage = extract("message");
|
||||||
if (idStart < 0) return false;
|
outEvent = extract("event");
|
||||||
idStart += 6;
|
|
||||||
int idEnd = line.indexOf("\"", idStart);
|
|
||||||
outId = line.substring(idStart, idEnd);
|
|
||||||
|
|
||||||
// Extract message
|
|
||||||
int msgStart = line.indexOf("\"message\":\"");
|
|
||||||
if (msgStart < 0) {
|
|
||||||
// Some messages might not have message field (keepalive, etc)
|
|
||||||
outMessage = "";
|
|
||||||
} else {
|
|
||||||
msgStart += 11;
|
|
||||||
int msgEnd = line.indexOf("\"", msgStart);
|
|
||||||
outMessage = line.substring(msgStart, msgEnd);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract time
|
|
||||||
int timeStart = line.indexOf("\"time\":");
|
|
||||||
if (timeStart >= 0) {
|
|
||||||
timeStart += 7;
|
|
||||||
int timeEnd = line.indexOf(",", timeStart);
|
|
||||||
if (timeEnd < 0) timeEnd = line.indexOf("}", timeStart);
|
|
||||||
String timeStr = line.substring(timeStart, timeEnd);
|
|
||||||
outTime = timeStr.toInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
return outId.length() > 0;
|
return outId.length() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NEW: Fetch and parse ntfy topic — short poll without ?poll=1
|
bool fetchNtfy(const char* topic, String& outId, String& outMessage, String& outEvent) {
|
||||||
bool fetchNtfyJson(const char* topic, String& outId, String& outMessage, unsigned long& outTime) {
|
|
||||||
if (millis() < errorBackoffUntil) {
|
|
||||||
return false; // In backoff period
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
String url = String("https://") + NTFY_SERVER + "/" + topic + "/json";
|
|
||||||
// NOTE: No ?poll=1 — we want short-poll for ESP32 reliability
|
|
||||||
|
|
||||||
logString(LOG_DEBUG, "HTTP", "GET", url);
|
// KEY FIX: ?since=all forces retrieval of cached messages
|
||||||
|
// Without this, we only get "open" subscription events
|
||||||
|
String url = String("https://") + NTFY_SERVER + "/" + topic + "/json?since=all";
|
||||||
|
|
||||||
http.setTimeout(10000); // 10s max
|
logMsg(LOG_DEBUG, "HTTP", "GET " + url);
|
||||||
|
|
||||||
|
http.setTimeout(8000);
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
int httpCode = http.GET();
|
int code = http.GET();
|
||||||
|
|
||||||
if (httpCode != 200) {
|
if (code != 200) {
|
||||||
logString(LOG_ERROR, "HTTP", "status=" + String(httpCode), url);
|
logMsg(LOG_ERROR, "HTTP", String(code) + "=" + decodeHttpError(code) + " | " + url);
|
||||||
if (httpCode == 500 || httpCode == 502 || httpCode == 503) {
|
|
||||||
errorBackoffUntil = millis() + ERROR_BACKOFF_MS;
|
|
||||||
logString(LOG_WARN, "HTTP", "Server error — backing off 60s");
|
|
||||||
}
|
|
||||||
http.end();
|
http.end();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -253,104 +182,71 @@ bool fetchNtfyJson(const char* topic, String& outId, String& outMessage, unsigne
|
|||||||
String payload = http.getString();
|
String payload = http.getString();
|
||||||
http.end();
|
http.end();
|
||||||
|
|
||||||
// NEW: Log raw response for debugging (truncated)
|
// Log first 150 chars of raw response
|
||||||
String payloadPreview = payload.substring(0, min((int)payload.length(), 200));
|
String preview = payload.substring(0, min((int)payload.length(), 150));
|
||||||
payloadPreview.replace("\n", "\\n");
|
preview.replace("\n", "\\n");
|
||||||
logString(LOG_DEBUG, "HTTP", "rawResponse", payloadPreview);
|
logMsg(LOG_DEBUG, "RAW", preview);
|
||||||
|
|
||||||
// Parse NDJSON — split by newlines, process each
|
// Parse all lines, return LAST valid message (newest)
|
||||||
|
bool found = false;
|
||||||
|
int lineStart = 0;
|
||||||
int lineNum = 0;
|
int lineNum = 0;
|
||||||
bool foundMessage = false;
|
|
||||||
|
|
||||||
int startIdx = 0;
|
while (lineStart < payload.length()) {
|
||||||
while (startIdx < payload.length()) {
|
int lineEnd = payload.indexOf('\n', lineStart);
|
||||||
int endIdx = payload.indexOf('\n', startIdx);
|
if (lineEnd < 0) lineEnd = payload.length();
|
||||||
if (endIdx < 0) endIdx = payload.length();
|
|
||||||
|
|
||||||
String line = payload.substring(startIdx, endIdx);
|
String line = payload.substring(lineStart, lineEnd);
|
||||||
line.trim();
|
line.trim();
|
||||||
|
|
||||||
lineNum++;
|
lineNum++;
|
||||||
|
|
||||||
if (line.length() > 0 && parseNtfyLine(line, outId, outMessage, outTime)) {
|
if (line.length() > 0) {
|
||||||
logString(LOG_DEBUG, "JSON", String("line[") + lineNum + "] id=[" + outId + "] msg=[" + outMessage + "]");
|
String id, msg, evt;
|
||||||
foundMessage = true;
|
if (parseNtfyLine(line, id, msg, evt)) {
|
||||||
// Continue parsing to consume all lines, but we'll use the last valid one
|
logMsg(LOG_DEBUG, "JSON",
|
||||||
}
|
String("L") + lineNum + " evt=" + evt + " id=" + id + " msg=" + (msg.length() ? msg : "EMPTY"));
|
||||||
|
|
||||||
startIdx = endIdx + 1;
|
// Only count actual messages, not "open" events
|
||||||
|
if (evt == "message" && msg.length() > 0) {
|
||||||
|
outId = id;
|
||||||
|
outMessage = msg;
|
||||||
|
outEvent = evt;
|
||||||
|
found = true;
|
||||||
|
// Continue to get the LAST (newest) message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineStart = lineEnd + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
logString(LOG_INFO, "NTFY", "linesParsed", String(lineNum));
|
return found;
|
||||||
return foundMessage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void postStatus(const char* state, const String& message) {
|
void postStatus(const char* state, const String& msg) {
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
String url = String("https://") + NTFY_SERVER + "/" + NTFY_STATUS_TOPIC;
|
String url = String("https://") + NTFY_SERVER + "/" + NTFY_STATUS_TOPIC;
|
||||||
String payload = "{\"state\":\"" + String(state) + "\",\"message\":\"" + message + "\",\"timestamp\":" + String(getEpochMillis()) + "}";
|
String payload = "{\"state\":\"" + String(state) + "\",\"message\":\"" + msg + "\"}";
|
||||||
|
|
||||||
logString(LOG_DEBUG, "HTTP", "POST", url);
|
|
||||||
|
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
http.addHeader("Content-Type", "application/json");
|
http.addHeader("Content-Type", "application/json");
|
||||||
int httpCode = http.POST(payload);
|
int code = http.POST(payload);
|
||||||
|
logMsg(LOG_INFO, "STATUS", String("POST ") + code + " " + payload);
|
||||||
logString(LOG_INFO, "HTTP", "status=" + String(httpCode) + " dur=" + String(millis() % 1000) + "ms", payload);
|
|
||||||
http.end();
|
http.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============== STATE MACHINE ==============
|
// ============== STATE MACHINE ==============
|
||||||
void setState(State newState, const String& message) {
|
void setState(State newState, const String& msg) {
|
||||||
if (currentState == newState && currentMessage == message) return;
|
if (currentState == newState && currentMessage == msg) return;
|
||||||
|
|
||||||
logString(LOG_INFO, "STATE", newState == ALERTING ? "ALERT <- SILENT" : "SILENT <- ALERT");
|
|
||||||
|
|
||||||
currentState = newState;
|
currentState = newState;
|
||||||
currentMessage = message;
|
currentMessage = msg;
|
||||||
|
|
||||||
if (newState == ALERTING) {
|
if (newState == ALERTING) {
|
||||||
lastAlertTime = millis();
|
showAlert(msg);
|
||||||
showAlertScreen(message);
|
|
||||||
} else {
|
} else {
|
||||||
showSilentScreen();
|
showSilent();
|
||||||
}
|
}
|
||||||
|
postStatus(newState == ALERTING ? "ALERTING" : "SILENT", msg);
|
||||||
// Publish status on every transition
|
|
||||||
postStatus(newState == ALERTING ? "ALERTING" : "SILENT", message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============== BUTTON ==============
|
|
||||||
void checkButton() {
|
|
||||||
static bool lastState = HIGH;
|
|
||||||
static unsigned long pressStart = 0;
|
|
||||||
static bool wasPressed = false;
|
|
||||||
|
|
||||||
bool pressed = (digitalRead(BUTTON_PIN) == LOW); // Active low
|
|
||||||
|
|
||||||
if (pressed && !lastState) {
|
|
||||||
// Pressed
|
|
||||||
logString(LOG_INFO, "BUTTON", "PRESSED");
|
|
||||||
setBacklight(true); // Sanity check: flash backlight
|
|
||||||
pressStart = millis();
|
|
||||||
wasPressed = true;
|
|
||||||
} else if (!pressed && lastState && wasPressed) {
|
|
||||||
// Released
|
|
||||||
unsigned long duration = millis() - pressStart;
|
|
||||||
logString(LOG_INFO, "BUTTON", "RELEASED", String(duration) + "ms");
|
|
||||||
|
|
||||||
if (currentState == ALERTING) {
|
|
||||||
// Force silence
|
|
||||||
logString(LOG_INFO, "BUTTON", "Force silence");
|
|
||||||
setState(SILENT, "");
|
|
||||||
flashConfirm("SILENCED");
|
|
||||||
} else {
|
|
||||||
setBacklight(false); // Turn off after sanity check
|
|
||||||
}
|
|
||||||
wasPressed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
lastState = pressed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============== MAIN ==============
|
// ============== MAIN ==============
|
||||||
@@ -358,86 +254,60 @@ void setup() {
|
|||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
logString(LOG_INFO, "MAIN", "=== KLUBHAUS DOORBELL v3.8.2 ===");
|
logMsg(LOG_INFO, "MAIN", "=== KLUBHAUS DOORBELL v3.8.4 ===");
|
||||||
logString(LOG_INFO, "MAIN", "Fix: Short-poll, raw response logging");
|
logMsg(LOG_INFO, "MAIN", "Fix: ?since=all for cached messages");
|
||||||
|
|
||||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
|
||||||
|
|
||||||
initDisplay();
|
initDisplay();
|
||||||
initWiFi();
|
initWiFi();
|
||||||
initTime();
|
|
||||||
|
|
||||||
// Initial status
|
|
||||||
postStatus("SILENT", "");
|
postStatus("SILENT", "");
|
||||||
|
logMsg(LOG_INFO, "MAIN", "READY — send FRONT DOOR from web client");
|
||||||
logString(LOG_INFO, "MAIN", "=== READY ===");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
checkButton();
|
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
|
||||||
// Poll ALERT topic
|
// Poll ALERT topic
|
||||||
if (now - lastAlertPoll >= POLL_INTERVAL_MS) {
|
if (now - lastAlertPoll >= POLL_INTERVAL_MS) {
|
||||||
lastAlertPoll = now;
|
lastAlertPoll = now;
|
||||||
|
|
||||||
String id, message;
|
String id, message, event;
|
||||||
unsigned long msgTime;
|
|
||||||
|
|
||||||
unsigned long startMs = millis();
|
if (fetchNtfy(NTFY_ALERT_TOPIC, id, message, event)) {
|
||||||
bool gotMessage = fetchNtfyJson(NTFY_ALERT_TOPIC, id, message, msgTime);
|
logMsg(LOG_INFO, "ALERT", "GOT: id=" + id + " msg=[" + message + "]");
|
||||||
unsigned long duration = millis() - startMs;
|
|
||||||
|
|
||||||
if (gotMessage) {
|
if (id != lastProcessedId) {
|
||||||
logString(LOG_INFO, "COMMAND", "durationMs=" + String(duration));
|
|
||||||
logString(LOG_INFO, "COMMAND", "id=[" + id + "]");
|
|
||||||
logString(LOG_INFO, "COMMAND", "message=[" + message + "]");
|
|
||||||
|
|
||||||
// Deduplication check
|
|
||||||
if (id == lastProcessedId) {
|
|
||||||
logString(LOG_DEBUG, "COMMAND", "Duplicate ID — discarded");
|
|
||||||
} else {
|
|
||||||
lastProcessedId = id;
|
lastProcessedId = id;
|
||||||
|
if (message != "SILENCE") {
|
||||||
// Process command
|
logMsg(LOG_INFO, "ALERT", "TRIGGER: " + message);
|
||||||
if (message.length() > 0 && message != "SILENCE") {
|
|
||||||
logString(LOG_INFO, "COMMAND", "ALERT trigger", message.c_str());
|
|
||||||
setState(ALERTING, message);
|
setState(ALERTING, message);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
logMsg(LOG_DEBUG, "ALERT", "Duplicate ID, ignored");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
logMsg(LOG_DEBUG, "ALERT", "No messages or error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poll SILENCE topic (staggered)
|
// Poll SILENCE topic (staggered)
|
||||||
if (now - lastSilencePoll >= POLL_INTERVAL_MS + 2500) { // Offset by 2.5s
|
if (now - lastSilencePoll >= POLL_INTERVAL_MS + 1500) {
|
||||||
lastSilencePoll = now;
|
lastSilencePoll = now;
|
||||||
|
|
||||||
String id, message;
|
String id, message, event;
|
||||||
unsigned long msgTime;
|
|
||||||
|
|
||||||
unsigned long startMs = millis();
|
if (fetchNtfy(NTFY_SILENCE_TOPIC, id, message, event)) {
|
||||||
bool gotMessage = fetchNtfyJson(NTFY_SILENCE_TOPIC, id, message, msgTime);
|
logMsg(LOG_INFO, "SILENCE", "GOT: id=" + id + " msg=[" + message + "]");
|
||||||
unsigned long duration = millis() - startMs;
|
|
||||||
|
|
||||||
if (gotMessage) {
|
|
||||||
logString(LOG_INFO, "SILENCE", "durationMs=" + String(duration));
|
|
||||||
logString(LOG_INFO, "SILENCE", "id=[" + id + "]");
|
|
||||||
logString(LOG_INFO, "SILENCE", "message=[" + message + "]");
|
|
||||||
|
|
||||||
// Force silence regardless of ID (silence is always processed)
|
|
||||||
if (message.indexOf("silence") >= 0 || message == "SILENCE") {
|
if (message.indexOf("silence") >= 0 || message == "SILENCE") {
|
||||||
logString(LOG_INFO, "SILENCE", "Force silence — acknowledging");
|
logMsg(LOG_INFO, "SILENCE", "ACKNOWLEDGED");
|
||||||
if (currentState == ALERTING) {
|
if (currentState == ALERTING) {
|
||||||
setState(SILENT, "");
|
setState(SILENT, "");
|
||||||
flashConfirm("SILENCED");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-return to silent after alert? No — manual only via button or silence topic
|
delay(50);
|
||||||
|
|
||||||
delay(10); // Small yield
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ run = "arduino-cli monitor"
|
|||||||
dir = "{{cwd}}"
|
dir = "{{cwd}}"
|
||||||
|
|
||||||
[tasks.snap]
|
[tasks.snap]
|
||||||
run = "git add .; git commit -am snapshot"
|
run = "git add .;git commit -am snapshot"
|
||||||
|
|||||||
Reference in New Issue
Block a user