snapshot
This commit is contained in:
@@ -2,12 +2,12 @@
|
|||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <Arduino_GFX_Library.h>
|
#include <Arduino_GFX_Library.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
// ============== CONFIGURATION ==============
|
// ============== CONFIGURATION ==============
|
||||||
const char* ssid = "iot-2GHz";
|
const char* ssid = "iot-2GHz";
|
||||||
const char* password = "lesson-greater";
|
const char* password = "lesson-greater";
|
||||||
|
|
||||||
// TOPIC URLS — short poll (no ?poll=1) for ESP32 HTTP client compatibility
|
|
||||||
const char* COMMAND_POLL_URL = "http://ntfy.sh/ALERT_klubhaus_topic/json";
|
const char* COMMAND_POLL_URL = "http://ntfy.sh/ALERT_klubhaus_topic/json";
|
||||||
const char* SILENCE_POLL_URL = "http://ntfy.sh/SILENCE_klubhaus_topic/json";
|
const char* SILENCE_POLL_URL = "http://ntfy.sh/SILENCE_klubhaus_topic/json";
|
||||||
const char* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic";
|
const char* STATUS_POST_URL = "http://ntfy.sh/STATUS_klubhaus_topic";
|
||||||
@@ -21,7 +21,6 @@ const unsigned long ERROR_BACKOFF = 60000;
|
|||||||
#define BACKLIGHT_PIN 22
|
#define BACKLIGHT_PIN 22
|
||||||
#define BUTTON_PIN 9
|
#define BUTTON_PIN 9
|
||||||
|
|
||||||
// ============== COCAINE CHIC PALETTE ==============
|
|
||||||
#define COLOR_BLACK 0x0000
|
#define COLOR_BLACK 0x0000
|
||||||
#define COLOR_COCAINE 0xFFFE
|
#define COLOR_COCAINE 0xFFFE
|
||||||
#define COLOR_TEAL 0x05F5
|
#define COLOR_TEAL 0x05F5
|
||||||
@@ -33,14 +32,12 @@ const unsigned long ERROR_BACKOFF = 60000;
|
|||||||
#define ALERT_BG_2 COLOR_FUCHSIA
|
#define ALERT_BG_2 COLOR_FUCHSIA
|
||||||
#define ALERT_TEXT_2 COLOR_COCAINE
|
#define ALERT_TEXT_2 COLOR_COCAINE
|
||||||
|
|
||||||
// ============== DISPLAY SETUP ==============
|
|
||||||
#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_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 *gfx = new Arduino_ST7789(bus, 21, 1, true, 172, 320, 34, 0, 34, 0);
|
||||||
|
|
||||||
// ============== STATE MANAGEMENT ==============
|
|
||||||
enum State { STATE_SILENT, STATE_ALERT };
|
enum State { STATE_SILENT, STATE_ALERT };
|
||||||
|
|
||||||
State currentState = STATE_SILENT;
|
State currentState = STATE_SILENT;
|
||||||
@@ -57,11 +54,13 @@ String lastSilenceId = "";
|
|||||||
bool lastButtonState = false;
|
bool lastButtonState = false;
|
||||||
String alertMessage = "";
|
String alertMessage = "";
|
||||||
|
|
||||||
|
unsigned long long epochOffsetMs = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(3000);
|
delay(3000);
|
||||||
Serial.println("\n=== KLUBHAUS DOORBELL v3.5 ===");
|
Serial.println("\n=== KLUBHAUS DOORBELL v3.6.1 ===");
|
||||||
Serial.println("Fix: Arduino String.trim() in-place modification");
|
Serial.println("Fix: restored missing loop() function");
|
||||||
|
|
||||||
pinMode(BACKLIGHT_PIN, OUTPUT);
|
pinMode(BACKLIGHT_PIN, OUTPUT);
|
||||||
digitalWrite(BACKLIGHT_PIN, LOW);
|
digitalWrite(BACKLIGHT_PIN, LOW);
|
||||||
@@ -87,12 +86,48 @@ void setup() {
|
|||||||
showFatalError("OFFLINE");
|
showFatalError("OFFLINE");
|
||||||
} else {
|
} else {
|
||||||
Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
|
Serial.println("\nWiFi OK: " + WiFi.localIP().toString());
|
||||||
|
initEpochTime();
|
||||||
publishStatus();
|
publishStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("=== READY ===");
|
Serial.println("=== READY ===");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============== EPOCH TIME ==============
|
||||||
|
|
||||||
|
void initEpochTime() {
|
||||||
|
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
|
||||||
|
|
||||||
|
Serial.print("Syncing NTP...");
|
||||||
|
time_t now = 0;
|
||||||
|
int retries = 0;
|
||||||
|
while (now < 1000000000 && retries < 30) {
|
||||||
|
delay(500);
|
||||||
|
now = time(nullptr);
|
||||||
|
Serial.print(".");
|
||||||
|
retries++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (now > 1000000000) {
|
||||||
|
epochOffsetMs = (now * 1000ULL) - millis();
|
||||||
|
Serial.println(" OK");
|
||||||
|
Serial.print("Epoch offset: ");
|
||||||
|
Serial.println((unsigned long)epochOffsetMs);
|
||||||
|
} else {
|
||||||
|
Serial.println(" FAILED — using boot-relative time");
|
||||||
|
epochOffsetMs = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long getEpochMs() {
|
||||||
|
if (epochOffsetMs == 0) {
|
||||||
|
return millis();
|
||||||
|
}
|
||||||
|
return epochOffsetMs + millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============== MAIN LOOP (RESTORED) ==============
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
|
||||||
@@ -146,7 +181,7 @@ void publishStatus() {
|
|||||||
StaticJsonDocument<256> doc;
|
StaticJsonDocument<256> doc;
|
||||||
doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING";
|
doc["state"] = (currentState == STATE_SILENT) ? "SILENT" : "ALERTING";
|
||||||
doc["message"] = alertMessage;
|
doc["message"] = alertMessage;
|
||||||
doc["timestamp"] = millis();
|
doc["timestamp"] = (unsigned long long)getEpochMs();
|
||||||
|
|
||||||
String payload;
|
String payload;
|
||||||
serializeJson(doc, payload);
|
serializeJson(doc, payload);
|
||||||
@@ -206,8 +241,6 @@ void checkCommandTopic() {
|
|||||||
String id = doc["id"] | "";
|
String id = doc["id"] | "";
|
||||||
String message = doc["message"] | "";
|
String message = doc["message"] | "";
|
||||||
|
|
||||||
// FIX: Arduino String.trim() modifies in place, returns void
|
|
||||||
// Trim first, then check length
|
|
||||||
message.trim();
|
message.trim();
|
||||||
if (message.length() == 0) {
|
if (message.length() == 0) {
|
||||||
Serial.println(" Empty message, skip");
|
Serial.println(" Empty message, skip");
|
||||||
@@ -246,7 +279,6 @@ void checkSilenceTopic() {
|
|||||||
String id = doc["id"] | "";
|
String id = doc["id"] | "";
|
||||||
String message = doc["message"] | "";
|
String message = doc["message"] | "";
|
||||||
|
|
||||||
// FIX: Arduino String.trim() modifies in place
|
|
||||||
message.trim();
|
message.trim();
|
||||||
if (message.length() == 0) return;
|
if (message.length() == 0) return;
|
||||||
|
|
||||||
@@ -401,7 +433,6 @@ void updateBlink(unsigned long now) {
|
|||||||
// ============== TEST ==============
|
// ============== TEST ==============
|
||||||
|
|
||||||
void testDisplay() {
|
void testDisplay() {
|
||||||
const char* names[] = {"TEAL", "FUCHSIA", "COCAINE", "MINT", "BLACK"};
|
|
||||||
uint16_t colors[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK};
|
uint16_t colors[] = {COLOR_TEAL, COLOR_FUCHSIA, COLOR_COCAINE, COLOR_MINT, COLOR_BLACK};
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
gfx->fillScreen(colors[i]);
|
gfx->fillScreen(colors[i]);
|
||||||
|
|||||||
Reference in New Issue
Block a user