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);
Audio audio;
Preferences preferences;
long lastActionTime = 0;

View file

@ -19,8 +19,10 @@
// GPIO
#define LED 2
#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
#define SSD1306_NO_SPLASH
@ -54,5 +56,6 @@ extern Adafruit_SSD1306 display;
extern AsyncWebServer server;
extern Audio audio;
extern Preferences preferences;
extern long lastActionTime;
#endif

View file

@ -10,6 +10,7 @@
byte buttonLastState = HIGH;
long lastDebounceTime = 0;
bool wasRunning = false;
void setup()
{
@ -38,32 +39,43 @@ void setup()
displayWifiCreds();
displayStatus();
// Setup is done, light up the LED
Serial.println("All setup & ready to go!");
setCpuFrequencyMhz(80);
// Setup is done, light up the LED
delay(500);
Serial.println("All setup & ready to go!");
digitalWrite(LED, HIGH);
updateLastAction();
wasRunning = audio.isRunning();
}
void loop()
{
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY_MS)
{
byte buttonCurrentState = digitalRead(BUTTON);
if (buttonCurrentState == LOW && buttonLastState == HIGH)
play();
buttonLastState = buttonCurrentState;
}
audio.loop();
}
void audio_info(const char *info){
String s_info = info;
s_info.toLowerCase();
s_info.trim();
if (s_info == "closing audio file") {
setCpuFrequencyMhz(80);
}
else if (s_info == "stream ready") {
audio.loop();
bool running = audio.isRunning();
if (running && !wasRunning)
{
wasRunning = true;
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 "config.h"
#include "utils.h"
bool fileIsValid(String fileName)
{
@ -31,6 +32,7 @@ void displayText(String text)
void play()
{
updateLastAction();
String selectedFile = preferences.getString(SELECTED_FILE);
String path = "/" + selectedFile;
Serial.println("Playing file: " + path);
@ -38,7 +40,6 @@ void play()
audio.connecttoFS(SD, path.c_str());
}
void diagnosticPrint(String text)
{
Serial.print(text);
@ -97,3 +98,21 @@ void selectFile(String fileName)
Serial.print("Select new file: ");
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 diagnosticPrintln(String text);
void deepSleep();
void audio_info(const char *info);
void updateLastAction();
#endif

View file

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