Skip to content

Commit

Permalink
fix: sleep duration, define's should be in header file
Browse files Browse the repository at this point in the history
feat: send current sleep duration to mqtt
  • Loading branch information
pimlie committed Sep 2, 2024
1 parent 7ae44e5 commit ea162c1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions hass.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

static bool resetActivity = false;
uint activityCount = 0;
uint timeToSleep = 60;

QueueHandle_t activityQueue = xQueueCreate(1, sizeof(Activity));

Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/config_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/config_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/homeplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -521,7 +538,7 @@ void sendMQTTStatusTask(void *param)
mqttWaiting = true;

waitForMQTT();
mqttSendBootStatus(bootCount, activityCount, bootReason());
mqttSendBootStatus(bootCount, activityCount, bootReason(), timeToSleep);
mqttSendWiFiStatus();

mqttSendTempStatus();
Expand Down

0 comments on commit ea162c1

Please sign in to comment.