Back to Parent

Speaker Circuit
//For DFPlayer ONLY
//Plays audio file on it

#include "DFPlayer.h"

    // global variables
DFPlayer dfPlayer;

int folder = 0;
int volume = 0;
bool powerOn = false;

bool playMessage = false;


// This value will store the last time we published an event
long lastPublishedAt = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 10000;

void setup() {
    Particle.subscribe("langley/2019/iot/playMessage/" , handleSharedEvent );

    Particle.variable("playMessage", playMessage);
    Particle.variable("folder", folder);
    Particle.variable("volume", volume);
    Particle.variable("power", powerOn);
    Particle.function("setVolume", setVolume);
    Particle.function("playFolder", playFolder);
    Particle.function("setPower", setPower);
    
    Serial.begin(115200);
    Serial1.begin(9600);
    delay(5000);


    // the following command tells the program to display data on the serial console.
    // logging can be turned on and off at will throughout the user's code
    dfPlayer.setLogging(true);
    volume = dfPlayer.getVolume();
    
    setVolume("20");
    setPower("true");
    folder = 1;
    delay(200);
    //playFolder(String(folder));

    delay(1000);

    dfPlayer.playTrack(1);
    
}

void loop() {

    if( playMessage == true ){
            // otherwise do your stuff to respond to
        // the paired device here
        //playFolder(String(folder));
        dfPlayer.playTrack(1);
        publishMyEvent();
        
        playMessage = false;
        delay( 5000 );
    }
    
}
// playFolder is a Particle Function ... that can be run from the Particle Console ..

int playFolder(String buffer) {
    folder = atoi(buffer);
    
    // the following commands play 6 tracks that were stored in the root folder of the TFCard
    // the dfPlayer.playTrack method uses DFPlayer command 0x03
    // The tracks were named 001 - 006.
    // The tracks were added to the TFCard in the following sequence 001, 002, 003, 005, 004, and 006
    // This code plays the tracks in order 001 - 006
    delay(3000);
    dfPlayer.playTrack(1);
    delay(3000);

    return 0;
}

// setVolume is a Particle method that can be used to adjust volume via the Particle Console
int setVolume(String buffer) {
    volume = atoi(buffer);
    dfPlayer.setVolume(volume);
    return 0;
}

// setPower is a Particle method that that plays a gong when power is toggled false, and chirping birds when power is toggned true 
bool setPower(String buffer) {
    if ((buffer[0] == 'f') && (powerOn == true))  {
        powerOn = false;
        dfPlayer.playFolderTrack(1,2);
    }
    if ((buffer[0] == 't') && (powerOn == false)) {
        powerOn = true;
        dfPlayer.playFolderTrack(1,1);
    }
    return powerOn;
}




// Our event handlde requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
void handleSharedEvent(const char *event, const char *data)
{
    // Now we're getting ALL events published using "db2018/paired/"
    // This includes events from this device.
    // So we need to ignore any events that we sent.

    // Let's check the event name
    String eventName = String( event ); // convert to a string object
    // This gives us access to a bunch of built in methods
    // Like indexOf()
    // Locates a character or String within another String.
    // By default, searches from the beginning of the String,
    // but can also start from a given index,
    // allowing for the locating of all instances of the character or String.
    // It Returns: The index of val within the String, or -1 if not found.

    // We can use this to check if our event name contains the
    // id of this device

    String deviceID = System.deviceID();

    // device id = 0123456789abcdef
    // event = "diot/2019/paired/0123456789abcdef"

    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }
    
    
    playMessage = true;

    
}


void publishMyEvent()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      // Remember our subscribe is matching  "db2018/paired/"
      // We'll append the device id to get more specific
      // about where the event came from

      // System.deviceID() provides an easy way to extract the device
      // ID of your device. It returns a String object of the device ID,
      // which is used to identify your device.

      String eventName = "langley/2019/iot/enchantedBookMessage/" + System.deviceID();

      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info

      // then we share it out
      Particle.publish( eventName, "From_Langley" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0