Add queuing of messages

This commit is contained in:
Dejvino 2026-02-12 19:28:30 +01:00
parent 72b8ca93d1
commit f388365609

View File

@ -12,6 +12,11 @@ const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
// ntfy.sh topic // ntfy.sh topic
const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC"; const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC";
// Notification queue
String queuedNotification = "";
String queuedTitle = "";
String queuedPriority = "";
// Pin definitions // Pin definitions
const int VIBRATION_PIN = 4; const int VIBRATION_PIN = 4;
@ -94,10 +99,22 @@ void setup() {
delay(1000); delay(1000);
} }
void sendNotification(String message); bool sendNotification(String message, String title, String priority);
void updateDisplay(); void updateDisplay();
void loop() { void loop() {
// Try to send any queued notification
if (queuedNotification != "") {
if (sendNotification(queuedNotification, queuedTitle, queuedPriority)) {
Serial.println("Successfully sent queued notification.");
queuedNotification = ""; // Clear queue
queuedTitle = "";
queuedPriority = "";
} else {
// Failed, will retry on next loop
}
}
unsigned long currentTime = millis(); unsigned long currentTime = millis();
// Sensor readings // Sensor readings
@ -156,10 +173,23 @@ void loop() {
isDeviceActive = isVibrationActive || isLightActive; isDeviceActive = isVibrationActive || isLightActive;
if (isDeviceActive != previousDeviceState) { if (isDeviceActive != previousDeviceState) {
String message;
String title;
String priority;
if (isDeviceActive) { if (isDeviceActive) {
sendNotification("Washing machine cycle started."); message = "Washing machine cycle started.";
title = "Machine START";
priority = "default";
} else { } else {
sendNotification("Washing machine cycle finished."); message = "Washing machine cycle finished.";
title = "Machine END";
priority = "high";
}
if (!sendNotification(message, title, priority)) {
queuedNotification = message; // Failed to send, queue it (overwrites any older one)
queuedTitle = title;
queuedPriority = priority;
} }
} }
@ -173,26 +203,30 @@ void loop() {
updateDisplay(); updateDisplay();
} }
void sendNotification(String message) { bool sendNotification(String message, String title, String priority) {
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
HTTPClient http; HTTPClient http;
String url = "http://ntfy.sh/" + String(NTFY_TOPIC); String url = "http://ntfy.sh/" + String(NTFY_TOPIC);
http.begin(url); http.begin(url);
http.addHeader("Content-Type", "text/plain"); http.addHeader("Content-Type", "text/plain");
http.addHeader("Title", title);
http.addHeader("Priority", priority);
int httpResponseCode = http.POST(message); int httpResponseCode = http.POST(message);
http.end();
if (httpResponseCode > 0) { if (httpResponseCode > 0) {
Serial.print("HTTP Response code: "); Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
return true;
} else { } else {
Serial.print("Error on sending POST: "); Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
return false;
} }
http.end();
} else { } else {
Serial.println("WiFi Disconnected. Cannot send notification."); Serial.println("WiFi Disconnected. Cannot send notification.");
return false;
} }
} }