From ea162c11f3645f3032f562d07b109eae368a7bd2 Mon Sep 17 00:00:00 2001 From: pimlie Date: Mon, 2 Sep 2024 10:26:26 +0200 Subject: [PATCH] fix: sleep duration, define's should be in header file feat: send current sleep duration to mqtt --- README.md | 4 +++- hass.md | 2 ++ src/activity.cpp | 6 ++++-- src/config_example.cpp | 1 - src/config_example.h | 1 + src/homeplate.h | 2 +- src/mqtt.cpp | 21 +++++++++++++++++++-- 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7b330c8..a18ea1f 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,12 @@ Copy `config_example.h` to `config.h` and add/change your settings. ##### Variable sleep intervals -If you want your inkplate to sleep with different intervals, copy `config_example.cpp` to `config.cpp` and uncomment the 3 lines in `config.h` starting from `#include "sleep_duration.h"`. Then configure your `sleepSchedule`. +If you want your inkplate to sleep with different intervals, copy `config_example.cpp` to `config.cpp` and uncomment the 4 lines in `config.h` starting from `#define CONFIG_CPP`. Then configure your `sleepSchedule` in config.cpp. Note that schedule slots do not span multiple days, this means that the *day of week* setting is similar to configuring a cronjob. F.e. the settings below should be read as *between Xam to Ypm on every weekday*, and **not** as *from monday Xam to friday Ypm*. +To help with debugging the current sleep duration is also send to mqtt, so you can monitor it in home assistant. + ```cpp { // on every weekday sleep for 1 hour between 12am and 8am diff --git a/hass.md b/hass.md index 863bebf..e9c601e 100644 --- a/hass.md +++ b/hass.md @@ -87,6 +87,8 @@ cards: secondary_info: last-updated - entity: sensor.homeplate_wifi_signal secondary_info: last-updated + - entity: sensor.homeplate_sleep_duration + secondary_info: last-updated title: HomePlate state_color: false - type: grid diff --git a/src/activity.cpp b/src/activity.cpp index 2a2838a..cc101b2 100644 --- a/src/activity.cpp +++ b/src/activity.cpp @@ -4,6 +4,7 @@ static bool resetActivity = false; uint activityCount = 0; +uint timeToSleep = 60; QueueHandle_t activityQueue = xQueueCreate(1, sizeof(Activity)); @@ -91,9 +92,10 @@ void runActivities(void *params) .normalSleep = TIME_TO_SLEEP_SEC, .quickSleep = TIME_TO_QUICK_SLEEP_SEC, }; - uint timeToSleep = getSleepDuration(sleepSchedule, sleepScheduleSize, time, defaults, doQuickSleep); + timeToSleep = getSleepDuration(sleepSchedule, sleepScheduleSize, time, defaults, doQuickSleep); + Serial.printf("[ACTIVITY SLEEP] Will sleep for %d\n", timeToSleep); #else - uint timeToSleep = doQuickSleep ? TIME_TO_QUICK_SLEEP_SEC : TIME_TO_SLEEP_SEC; + timeToSleep = doQuickSleep ? TIME_TO_QUICK_SLEEP_SEC : TIME_TO_SLEEP_SEC; #endif switch (activityNext) diff --git a/src/config_example.cpp b/src/config_example.cpp index d2e8834..c9ee771 100644 --- a/src/config_example.cpp +++ b/src/config_example.cpp @@ -3,7 +3,6 @@ // If using a single/static sleep duration then this file can be omitted /* REMOVE THIS LINE TO ENABLE THE SLEEP SCHEDULE #include "config.h" -#define CONFIG_CPP // How long to sleep between image refreshes // - there is no validation of any kind, make sure your slots are continuous diff --git a/src/config_example.h b/src/config_example.h index 98f3d62..080ea9d 100644 --- a/src/config_example.h +++ b/src/config_example.h @@ -18,6 +18,7 @@ // Configure a custom sleep schedule // NOTE: configure the actual sleep schedule in config.cpp, see config_example.cpp +//#define CONFIG_CPP //#include "sleep_duration.h" //extern SleepScheduleSlot sleepSchedule[]; //extern const size_t sleepScheduleSize; diff --git a/src/homeplate.h b/src/homeplate.h index a39414f..5d30168 100644 --- a/src/homeplate.h +++ b/src/homeplate.h @@ -21,7 +21,7 @@ extern void vApplicationStackOverflowHook(xTaskHandle *pxTask, extern Inkplate display; extern SemaphoreHandle_t mutexI2C, mutexDisplay, mutexSPI; extern bool sleepBoot; -extern uint bootCount, activityCount; +extern uint bootCount, activityCount, timeToSleep; #define i2cStart() xSemaphoreTake(mutexI2C, portMAX_DELAY) #define i2cEnd() xSemaphoreGive(mutexI2C) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 556618d..82e3a23 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -120,13 +120,14 @@ void mqttSendBatteryStatus() mqttClient.publish(state_topic_battery, 1, MQTT_RETAIN_SENSOR_VALUE, buff); } -void mqttSendBootStatus(uint boot, uint activityCount, const char *bootReason) +void mqttSendBootStatus(uint boot, uint activityCount, const char *bootReason, uint sleepDuration) { char buff[512]; JsonDocument doc; doc["boot"] = boot; doc["activity_count"] = activityCount; doc["boot_reason"] = bootReason; + doc["sleep_duration"] = sleepDuration; serializeJson(doc, buff); Serial.printf("[MQTT] Sending MQTT State: [%s] %s\n", state_topic_boot, buff); mqttClient.publish(state_topic_boot, 1, MQTT_RETAIN_SENSOR_VALUE, buff); @@ -259,6 +260,22 @@ void sendHAConfig() doc["device"] = deviceInfo; serializeJson(doc, buff); mqttClient.publish(mqtt_base_sensor("activity_count/config"), qos, retain, buff); + + // sleepTime + doc.clear(); + doc["unique_id"] = mqtt_unique_id("sleep_duration"); + doc["state_class"] = "measurement"; + doc["name"] = "Sleep Duration"; + doc["state_topic"] = state_topic_boot; + doc["unit_of_measurement"] = "s"; + doc["icon"] = "mdi:power-sleep"; + doc["value_template"] = "{{ value_json.sleep_duration }}"; + doc["expire_after"] = TIME_TO_SLEEP_SEC * 2; + doc["entity_category"] = "diagnostic"; + doc["enabled_by_default"] = false; + doc["device"] = deviceInfo; + serializeJson(doc, buff); + mqttClient.publish(mqtt_base_sensor("sleep_duration/config"), qos, retain, buff); } void connectToMqtt(void *params) @@ -521,7 +538,7 @@ void sendMQTTStatusTask(void *param) mqttWaiting = true; waitForMQTT(); - mqttSendBootStatus(bootCount, activityCount, bootReason()); + mqttSendBootStatus(bootCount, activityCount, bootReason(), timeToSleep); mqttSendWiFiStatus(); mqttSendTempStatus();