Making Music

Table of contents

  1. Core functions
  2. Hooking up MP3 Playback: Modify setup
  3. Hooking up MP3 Playback: Modify loop
  4. Hooking up MP3 Playback: Remote Activation
  5. Flash it

The last piece is to hook up the DF Player and have it play sound.

Core functions

To make the DFPlayer work, we need a set of functions:

Copy and paste the following into the bottom of your code file:


void setupDFPlayer(){

  // Set up the DF Player
  Serial.begin(9600);
  Serial1.begin(9600);
  delay( 1000 );

  execute_CMD(0x3F, 0, 0); // Send request for initialization parameters
  delay( 2000 );

  // while (Serial1.available()<10){ // Wait until initialization parameters are received (10 bytes)
  //   delay(30); // Pretty long delays between succesive commands needed (not always the same)
  // }
  // Initialize sound to very low volume. Adapt according used speaker and wanted volume
  //execute_CMD(0x06, 0, 0x30); // Set the volume (0x00~0x30)
  execute_CMD(0x06, 0, 0x20); // Set the volume (0x00~0x30)
  delay( 2000 );


}

void beginPlayBack( ){

  execute_CMD(0x0D,0,2);
  delay( 500 );

}

void stopPlayBack( ){

  execute_CMD(0x11, 0, 0x00);
  delay( 500 );

}



// Contain the values for the DF Player
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]


// By Ype Brada 2015-04-06
// https://community.particle.io/t/a-great-very-cheap-mp3-sound-module-without-need-for-a-library/20111
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
 // Calculate the checksum (2 bytes)
 int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);

 // Build the command line
 byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, checksum >> 8, checksum & 0xFF, End_Byte};

 //Send the command line to the module
 for (byte k=0; k<10; k++)
 {
  Serial1.write( Command_line[k]);
 }
}

This contains four functions

  • execute_CMD() is a function to communicate and send commands to the MP3 player. It’s essential
  • setupDFPlayer() will initialize the player
  • beginPlayBack( ) will start the playback of the first mp3 on the device
  • stopPlayBack( ) will pause playback

Hooking up MP3 Playback: Modify setup

Once you’ve added these

Modify setup to include the line

setupDFPlayer();

Add this after the Particle.function

Hooking up MP3 Playback: Modify loop

In the loop then we’ll add the controls for Mp3 playback:

  • When we change from open to closed we’ll pause playback
  • When we change from closed to open we’ll resume playback

Modify the open detection to look like this. Note the addition of the beginPlayBack();

	 if( isBoxOpen == false ){
		 isBoxOpen = true;

     beginPlayBack();

	    // next play a song...
		 Particle.publish( "box-opened" );

	 }

Modify the closed detection to look like this. Note the addtion of stopPlayBack( );

	 if( isBoxOpen == true ){
	 	 stopPlayBack( );
		 Particle.publish( "box-closed" );
	 }

Hooking up MP3 Playback: Remote Activation

Add one last line to the handleActivation to allow someone to remotely trigger your device

Modify the handleActivation method to include beginPlayback

int handleActivation( String command ){

  for( int i = 0; i < 3; i++ ){
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }

  beginPlayBack( );

  return 1;
}

Flash it

Save and flash your code!

Now try the photo resistor and see if the music plays!

Fingers crossed you’ve got an internet-enabled memory box!


Table of contents