/*Love the project? Consider Supporting :)
https://www.linktr.ee/padmalaya_rawal */

#include "DFRobot_DF2301Q.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

//I2C communication
DFRobot_DF2301Q_I2C DF2301Q;

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX 
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {

  Serial.begin(115200);

  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) {
   Serial.println("OK");

    // Set volume to maximum (0 to 30).
    player.volume(15);
    // Play the first MP3 file on the SD card
    player.play(1);
  } else {
    Serial.println("Connecting to DFPlayer Mini failed!");
  }

// Init the voice reognition module
  while( !( DF2301Q.begin() ) ) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");
  DF2301Q.setVolume(2);
  DF2301Q.setMuteMode(0);
  DF2301Q.setWakeTime(15);

  uint8_t wakeTime = 0;
  wakeTime = DF2301Q.getWakeTime();
  Serial.print("wakeTime = ");
  Serial.println(wakeTime);

  // DF2301Q.playByCMDID(1);   // Wake-up command
  DF2301Q.playByCMDID(23);   // Common word ID

}

void loop() {
  // Check for CMID from DF2301Q
  uint8_t CMDID = 0;
  CMDID = DF2301Q.getCMDID();

  if (CMDID == 1 ) {
    Serial.println("Specific CMID detected, playing audio file 1.mp3");
    player.play(1);
    // You can add additional logic or commands here as needed
  }
delay(500);

}
