/*
ESP32 SD I2S Music Player
esp32-i2s-sd-player.ino
Plays MP3 file from microSD card
Uses MAX98357 I2S Amplifier Module
Uses ESP32-audioI2S Library - https://github.com/schreibfaul1/ESP32-audioI2S
DroneBot Workshop 2022
https://dronebotworkshop.com
https://github.com/schreibfaul1/ESP32-audioI2S/wiki
https://dronebotworkshop.com/esp32-i2s/
https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
https://garrysblog.com/2023/05/03/calculating-bitmask-value-for-esp32-external-wake-up-google-is-your-friend/
https://www.youtube.com/watch?v=-dW4XsBo3Mk
https://garrysblog.com/2020/07/05/using-the-ds3231-real-time-clock-alarm-with-the-adafruit-rtclib-library/
*/
// Include required libraries
#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
// microSD Card Reader connections
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
// I2S Connections
#define I2S_DOUT 22
#define I2S_BCLK 26
#define I2S_LRC 25
// Create Audio object
Audio audio;
//========================================================
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
//==================================================================
void setup() {
// Set microSD Card CS as OUTPUT and set HIGH
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize SPI bus for microSD Card
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
// Start Serial Port
Serial.begin(115200);
//=================================================================
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
/*
First we configure the wake up source
We set our ESP32 to wake up every 5 seconds
*/
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
//=====================================================================
// Start microSD Card
if (!SD.begin(SD_CS))
{
Serial.println("Error accessing microSD card!");
while (true);
}
// Setup I2S
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
// Set Volume
audio.setVolume(21);
// Open music file
audio.connecttoFS(SD, "/MYMUSIC.mp3");
}
void loop()
{
audio.loop();
}
void audio_eof_mp3(const char *info) { //end of file
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}