Skip to content

Commit

Permalink
Merge pull request #327 from ejalal/main
Browse files Browse the repository at this point in the history
implement deep sleep
  • Loading branch information
Blueforcer authored Oct 14, 2023
2 parents 86091a1 + d87b252 commit 31ac050
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Toggle the matrix on or off:
| ---------------- | ----------------------------- | ------------------------- | ----------- |
| `[PREFIX]/power` | `http://[IP]/api/power` | `{"power": true}` or `{"power": false}` | POST |

Send the board in deep sleep mode (turns off the matrix as well), good for saving battery life:

| MQTT Topic | HTTP URL | Payload/Body | HTTP Method |
| ---------------- | ----------------------------- | ------------------------- | ----------- |
| `[PREFIX]/sleep` | `http://[IP]/api/sleep` | `{"sleep": X}` where X is number of seconds | POST |

## Sound Playback

Expand Down
22 changes: 22 additions & 0 deletions src/MQTTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Dictionary.h"
#include "PeripheryManager.h"
#include "UpdateManager.h"
#include "PowerManager.h"

WiFiClient espClient;
HADevice device;
Expand Down Expand Up @@ -283,6 +284,26 @@ void onMqttMessage(const char *topic, const uint8_t *payload, uint16_t length)
return;
}

if (strTopic.equals(MQTT_PREFIX + "/sleep"))
{
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error)
{
if (DEBUG_MODE)
DEBUG_PRINTLN(F("Failed to parse json"));
return;
}
if (doc.containsKey("sleep"))
{
DisplayManager.setPower(false);
PowerManager.sleep(doc["sleep"].as<uint64_t>());
}

delete[] payloadCopy;
return;
}

if (strTopic.equals(MQTT_PREFIX + "/indicator1"))
{
DisplayManager.indicatorParser(1, payloadCopy);
Expand Down Expand Up @@ -360,6 +381,7 @@ void onMqttConnected()
"/nextapp",
"/apps",
"/power",
"/sleep",
"/indicator1",
"/indicator2",
"/indicator3",
Expand Down
45 changes: 45 additions & 0 deletions src/PowerManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <PowerManager.h>
#include <ArduinoJson.h>
#include "Globals.h"

#define uS_TO_S_FACTOR 1000000L

// The getter for the instantiated singleton instance
PowerManager_ &PowerManager_::getInstance()
{
static PowerManager_ instance;
return instance;
}

// Initialize the global shared instance
PowerManager_ &PowerManager = PowerManager.getInstance();

void PowerManager_::setup()
{

}

void PowerManager_::sleepParser(const char *json)
{
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, json);
if (error)
{
if (DEBUG_MODE)
DEBUG_PRINTLN(F("Failed to parse json"));
return;
}

if (doc.containsKey("sleep"))
{
uint64_t seconds = doc["sleep"].as<uint64_t>();
sleep(seconds);
}
}

void PowerManager_::sleep(uint64_t seconds)
{
esp_sleep_enable_timer_wakeup(seconds * uS_TO_S_FACTOR);
Serial.print("Going to sleep...\n");
esp_deep_sleep_start();
}
18 changes: 18 additions & 0 deletions src/PowerManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef PowerManager_h
#define PowerManager_h

#include <Arduino.h>

class PowerManager_
{
private:
PowerManager_() = default;
public:
static PowerManager_ &getInstance();
void setup();
void sleepParser(const char*);
void sleep(uint64_t);
};

extern PowerManager_ &PowerManager;
#endif
8 changes: 8 additions & 0 deletions src/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "DisplayManager.h"
#include "UpdateManager.h"
#include "PeripheryManager.h"
#include "PowerManager.h"
#include <WiFiUdp.h>

WiFiUDP udp;
Expand Down Expand Up @@ -46,6 +47,13 @@ void addHandler()
{
mws.addHandler("/api/power", HTTP_POST, []()
{ DisplayManager.powerStateParse(mws.webserver->arg("plain").c_str()); mws.webserver->send(200,F("text/plain"),F("OK")); });
mws.addHandler(
"/api/sleep", HTTP_POST, []()
{
mws.webserver->send(200,F("text/plain"),F("OK"));
DisplayManager.setPower(false);
PowerManager.sleepParser(mws.webserver->arg("plain").c_str());
});
mws.addHandler("/api/loop", HTTP_GET, []()
{ mws.webserver->send_P(200, "application/json", DisplayManager.getAppsAsJson().c_str()); });
mws.addHandler("/api/effects", HTTP_GET, []()
Expand Down

0 comments on commit 31ac050

Please sign in to comment.