Implement deep sleep after timeout

This commit is contained in:
Gabriel Augendre 2023-01-08 11:07:23 +01:00
parent f3064fb535
commit 7730389b60
6 changed files with 66 additions and 19 deletions

View file

@ -9,3 +9,5 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
AsyncWebServer server(80); AsyncWebServer server(80);
Audio audio; Audio audio;
Preferences preferences; Preferences preferences;
long lastActionTime = 0;

View file

@ -19,8 +19,10 @@
// GPIO // GPIO
#define LED 2 #define LED 2
#define BUTTON 33 #define BUTTON 33
#define DEEP_SLEEP_WAKEUP GPIO_NUM_33
#define DEBOUNCE_DELAY 1000 #define DEBOUNCE_DELAY_MS 1000
#define DEEP_SLEEP_DELAY_MS 3600000 // 1h
// Screen // Screen
#define SSD1306_NO_SPLASH #define SSD1306_NO_SPLASH
@ -54,5 +56,6 @@ extern Adafruit_SSD1306 display;
extern AsyncWebServer server; extern AsyncWebServer server;
extern Audio audio; extern Audio audio;
extern Preferences preferences; extern Preferences preferences;
extern long lastActionTime;
#endif #endif

View file

@ -10,6 +10,7 @@
byte buttonLastState = HIGH; byte buttonLastState = HIGH;
long lastDebounceTime = 0; long lastDebounceTime = 0;
bool wasRunning = false;
void setup() void setup()
{ {
@ -38,32 +39,43 @@ void setup()
displayWifiCreds(); displayWifiCreds();
displayStatus(); displayStatus();
// Setup is done, light up the LED
Serial.println("All setup & ready to go!");
setCpuFrequencyMhz(80); setCpuFrequencyMhz(80);
// Setup is done, light up the LED
delay(500);
Serial.println("All setup & ready to go!");
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
updateLastAction();
wasRunning = audio.isRunning();
} }
void loop() void loop()
{ {
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY_MS)
{ {
byte buttonCurrentState = digitalRead(BUTTON); byte buttonCurrentState = digitalRead(BUTTON);
if (buttonCurrentState == LOW && buttonLastState == HIGH) if (buttonCurrentState == LOW && buttonLastState == HIGH)
play(); play();
buttonLastState = buttonCurrentState; buttonLastState = buttonCurrentState;
} }
audio.loop();
}
void audio_info(const char *info){ audio.loop();
String s_info = info;
s_info.toLowerCase(); bool running = audio.isRunning();
s_info.trim(); if (running && !wasRunning)
if (s_info == "closing audio file") { {
setCpuFrequencyMhz(80); wasRunning = true;
}
else if (s_info == "stream ready") {
setCpuFrequencyMhz(240); setCpuFrequencyMhz(240);
} }
else if (!running && wasRunning)
{
wasRunning = false;
updateLastAction();
setCpuFrequencyMhz(80);
}
if (!running && ((millis() - lastActionTime) > DEEP_SLEEP_DELAY_MS))
{
deepSleep();
}
} }

View file

@ -5,6 +5,7 @@
#include <SD.h> #include <SD.h>
#include "config.h" #include "config.h"
#include "utils.h"
bool fileIsValid(String fileName) bool fileIsValid(String fileName)
{ {
@ -31,6 +32,7 @@ void displayText(String text)
void play() void play()
{ {
updateLastAction();
String selectedFile = preferences.getString(SELECTED_FILE); String selectedFile = preferences.getString(SELECTED_FILE);
String path = "/" + selectedFile; String path = "/" + selectedFile;
Serial.println("Playing file: " + path); Serial.println("Playing file: " + path);
@ -38,7 +40,6 @@ void play()
audio.connecttoFS(SD, path.c_str()); audio.connecttoFS(SD, path.c_str());
} }
void diagnosticPrint(String text) void diagnosticPrint(String text)
{ {
Serial.print(text); Serial.print(text);
@ -92,8 +93,26 @@ void displayStatus()
void selectFile(String fileName) void selectFile(String fileName)
{ {
preferences.putString(SELECTED_FILE, fileName); preferences.putString(SELECTED_FILE, fileName);
displayStatus(); displayStatus();
Serial.print("Select new file: "); Serial.print("Select new file: ");
Serial.println(fileName); Serial.println(fileName);
}
void deepSleep()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("Veille. Retourne-moi pour me reveiller.");
display.display();
Serial.println("Deep sleep!");
esp_sleep_enable_ext0_wakeup(DEEP_SLEEP_WAKEUP, HIGH);
digitalWrite(LED, LOW);
esp_deep_sleep_start();
}
void updateLastAction()
{
lastActionTime = millis();
} }

View file

@ -19,4 +19,9 @@ void displayStatus();
void diagnosticPrint(String text); void diagnosticPrint(String text);
void diagnosticPrintln(String text); void diagnosticPrintln(String text);
void deepSleep();
void audio_info(const char *info);
void updateLastAction();
#endif #endif

View file

@ -11,6 +11,7 @@
void onStop(AsyncWebServerRequest *request) void onStop(AsyncWebServerRequest *request)
{ {
updateLastAction();
Serial.println("Stop playing"); Serial.println("Stop playing");
audio.stopSong(); audio.stopSong();
request->send(200); request->send(200);
@ -43,6 +44,7 @@ void onStatus(AsyncWebServerRequest *request)
void onListFiles(AsyncWebServerRequest *request) void onListFiles(AsyncWebServerRequest *request)
{ {
updateLastAction();
Serial.print("List files cursor="); Serial.print("List files cursor=");
int cursor = 0; int cursor = 0;
if (request->hasParam("cursor")) { if (request->hasParam("cursor")) {
@ -87,6 +89,7 @@ void onListFiles(AsyncWebServerRequest *request)
void onSelectFile(AsyncWebServerRequest *request) void onSelectFile(AsyncWebServerRequest *request)
{ {
updateLastAction();
Serial.println("Select file"); Serial.println("Select file");
if (request->hasParam("fileName", true)) if (request->hasParam("fileName", true))
{ {
@ -98,6 +101,7 @@ void onSelectFile(AsyncWebServerRequest *request)
void onChangeVolume(AsyncWebServerRequest *request) void onChangeVolume(AsyncWebServerRequest *request)
{ {
updateLastAction();
Serial.print("Volume: "); Serial.print("Volume: ");
if (request->hasParam("modifier", true)) if (request->hasParam("modifier", true))
{ {
@ -120,12 +124,14 @@ void onChangeVolume(AsyncWebServerRequest *request)
void onUpload(AsyncWebServerRequest *request) void onUpload(AsyncWebServerRequest *request)
{ {
updateLastAction();
Serial.println("onUpload"); Serial.println("onUpload");
request->send(200); request->send(200);
} }
void onUploadFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) void onUploadFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{ {
updateLastAction();
if (!index) if (!index)
{ {
Serial.printf("Upload start: %s\n", filename.c_str()); Serial.printf("Upload start: %s\n", filename.c_str());