Required Components

  • ESP8266 Wemos Mini
  • DFPlayer Mini
  • Speaker
  • UBS Power Cable/Supply


Build







Code

/*
* PushButton
* - Plays a random mp3 file from the SD card.
* 
* - Place mp3s in a folder off of root.
* - Remove all hidden files.
* 
* For Mac User
* NOTE: If you are using Mac OS X to copy the mp3, the file system will automatically add hidden files like: "._0001.mp3" 
* for index, which this module will handle as valid mp3 files. It is really annoying. So you can run following command in 
* terminal to eliminate those files.
* 
* dot_clean /Volumes/<SDVolumeName>
* 
* Please replace the to the volume name of your SD card
*/

#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
#include <JC_Button.h>

#define BUTTON_PIN D1

/**
* Mp3Notify Notification Class
*/
class Mp3Notify
{
  public:
    static void OnError(uint16_t errorCode)
    {
      	// see DfMp3_Error for code meaning
		Serial.println();
		Serial.print("Com Error ");
		Serial.println(errorCode);
	}
	static void OnPlayFinished(uint16_t track)
	{
		Serial.print("Play finished for #");
		Serial.println(track); 
	}
	static void OnCardOnline(uint16_t code)
	{
		Serial.println("Card online ");
	}
	static void OnUsbOnline(uint16_t code)
	{
		Serial.println("USB Disk online ");
	}
	static void OnCardInserted(uint16_t code)
	{
		Serial.println("Card inserted ");
	}
	static void OnUsbInserted(uint16_t code)
	{
		Serial.println("USB Disk inserted ");
	}
	static void OnCardRemoved(uint16_t code)
	{
		Serial.println("Card removed ");
	}
	static void OnUsbRemoved(uint16_t code)
	{
		Serial.println("USB Disk removed ");
	}
};

SoftwareSerial secondarySerial(D5, D0); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
Button button(BUTTON_PIN); 
int *sounds;
int track=0;
int count=0;

/**
* setup
*/
void setup() 
{

	Serial.begin(115200);

	Serial.println("\n\nInitializing...");
	button.begin(); 

	mp3.begin();
	mp3.reset(); 

	// show some properties and set the volume
	uint16_t volume = mp3.getVolume();
	Serial.print("volume ");
	Serial.println(volume);
	// mp3.setVolume(23);
	mp3.setVolume(18);

	count = mp3.getTotalTrackCount();

	Serial.print("files ");
	Serial.println(count);

	uint16_t mode = mp3.getPlaybackMode();
	Serial.print("playback mode ");
	Serial.println(mode);

	Serial.println("starting...");

	mp3.setRepeatPlay(false);


	sounds = new int[count];
	for(int i=0;i<count;i++){
		sounds[i]=i+1;
	}	

	randomize (sounds, count); 

}


/**
* swap 
* - function to swap to integers
*/
void swap (int *a, int *b) 
{ 
	int temp = *a; 
	*a = *b; 
	*b = temp; 
} 

/** 
* printArray 
* - function to print an array
*/
void printArray (int arr[], int n) 
{ 
	for (int i = 0; i < n; i++) 
		printf("%d \n", arr[i]); 
	printf("\n"); 
} 

/** 
* randomize 
* - function to generate a random permutation of arr[] 
*/
void randomize ( int arr[], int n ) 
{ 

	Serial.printf("Randomizing...\n");

	// Use a different seed value so that we don't get same 
	// result each time we run this program 
	uint32_t randNumber = RANDOM_REG32;
	srand ( randNumber ); 

	// Start from the last element and swap one by one. We don't 
	// need to run for the first element that's why i > 0 
	for (int i = n-1; i > 0; i--) 
	{ 
		// Pick a random index from 0 to i 
		int j = rand() % (i+1); 

		// Swap arr[i] with the element at random index 
		swap(&arr[i], &arr[j]); 
	} 
} 


/**
* Loop
*/
void loop() 
{
	// calling mp3.loop() periodically allows for notifications 
	// to be handled without interrupts
	mp3.loop();

	button.read(); // read the button
	if (button.wasReleased()) // if the button was released, change songs
	{
		playRandom();
	}

}

/**
* playRandom
*/
void playRandom(){


	Serial.printf("Playing track: %d [%d]\n",sounds[track], track);
	mp3.playGlobalTrack(sounds[track]);
	track++;
	if(track>=count){
		randomize (sounds, count); 
		track=0;
	}

}



Copying Mp3s to your Micro SD Card

NOTE: The order you copy the mp3 into micro SD card will affect the order mp3 played , which means play(1) function will play the first mp3 copied into micro SD card.

For Mac User

NOTE: If you are using Mac OS X to copy the mp3, the file system will automatically add hidden files like: "._0001.mp3" for index, which this module will handle as valid mp3 files. It is really annoying. So you can run following command in terminal to eliminate those files.

dot_clean /Volumes/<SDVolumeName>

Please replace the to the volume name of your SD card.