Add queuing of messages
This commit is contained in:
parent
72b8ca93d1
commit
f388365609
@ -12,6 +12,11 @@ const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
|
||||
// ntfy.sh topic
|
||||
const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC";
|
||||
|
||||
// Notification queue
|
||||
String queuedNotification = "";
|
||||
String queuedTitle = "";
|
||||
String queuedPriority = "";
|
||||
|
||||
// Pin definitions
|
||||
const int VIBRATION_PIN = 4;
|
||||
|
||||
@ -94,10 +99,22 @@ void setup() {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void sendNotification(String message);
|
||||
bool sendNotification(String message, String title, String priority);
|
||||
void updateDisplay();
|
||||
|
||||
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();
|
||||
|
||||
// Sensor readings
|
||||
@ -156,10 +173,23 @@ void loop() {
|
||||
isDeviceActive = isVibrationActive || isLightActive;
|
||||
|
||||
if (isDeviceActive != previousDeviceState) {
|
||||
String message;
|
||||
String title;
|
||||
String priority;
|
||||
if (isDeviceActive) {
|
||||
sendNotification("Washing machine cycle started.");
|
||||
message = "Washing machine cycle started.";
|
||||
title = "Machine START";
|
||||
priority = "default";
|
||||
} 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();
|
||||
}
|
||||
|
||||
void sendNotification(String message) {
|
||||
bool sendNotification(String message, String title, String priority) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
HTTPClient http;
|
||||
String url = "http://ntfy.sh/" + String(NTFY_TOPIC);
|
||||
http.begin(url);
|
||||
http.addHeader("Content-Type", "text/plain");
|
||||
http.addHeader("Title", title);
|
||||
http.addHeader("Priority", priority);
|
||||
|
||||
int httpResponseCode = http.POST(message);
|
||||
http.end();
|
||||
|
||||
if (httpResponseCode > 0) {
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
return true;
|
||||
} else {
|
||||
Serial.print("Error on sending POST: ");
|
||||
Serial.println(httpResponseCode);
|
||||
return false;
|
||||
}
|
||||
|
||||
http.end();
|
||||
} else {
|
||||
Serial.println("WiFi Disconnected. Cannot send notification.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user