#25projectsofchristmas challenge
I wanted to have some nice melody when we are in front of the Christmas tree,so I made a small jukebox that play automatically a random song from SD card every time detect someone on the front, using the motion detector (PIR) unit.
HardwareMost of the job is made by the M5Stack node module, with the chip WM8978 onboard. The M5 core is responsible to check the PIR sensor connected on port B and read from the SD card inserted.
Most of the work is made by these libraries: ESP32-audioI2S and wm8978-esp32 I used on my project.During setup, the code initialize the WM8978 chip and scan the SD card searching for mp3 files. This scan generates a list that will be used later.
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
String filename;
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
} else {
filename = file.name();
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
if ((filename.indexOf(".mp3") > 0) && (filename.indexOf(".") > 0) ) {
Serial.println("Found MP3");
myfiles[filenum].name = filename; // Add the file to the list
filenum++;
}
}
if (filenum > 100) {
break ;
};
file = root.openNextFile();
}
}
To not interrupt or restart a song every time the PIR detect people, I block detection using a flag (var play, 0=ready, 1=playing).If the flag is 0 and the PIR detect people, I call the function startsong(), that generated a random number and start playing the correspondent song.Also print the name of the song in a box in the middle of the screen
I added some small graphic, just a list of the songs on the screen and the Title.The loop function is very simple: if is playing a song, just send new data to the decoder
audio.loop();
and eventually check the button A, that can stop the play anytime.
EvolutionsThis project is just a test for the node module. Even using the internal speaker on the M5Stack, the quality of the sound is really high (check on the video). Additionally, as the module offer a 3.5 mm jack, it can send output to some HIFI system.
Comments