I want to play a song using DFPlayer Mini via Telegram bot. But I'm confused how to stop the song. Can you guys help me?
#include <DFPlayer_Mini_Mp3.h>
#include <SoftwareSerial.h>
#include "WiFiClientSecure.h"
#include "ESP8266WiFi.h"
#include "UniversalTelegramBot.h"
#include <ArduinoJson.h>
const char* ssid = "?"; // NAMA WIFI
const char* pass = "xxxx"; // PW WIFI
String BOTtoken = "xxxxxxx:xxxxxxxxxxxxxxx"; // your Bot Token (Get from Botfather)
const int chat_id = xxx;
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs =1000; // time between scan messages
long Bot_lasttime; // last time messages scan has been done
bool Start = false;
bool lagu = false;
#define PIN_BUSY A0
SoftwareSerial mp3Serial(D3, D4); // RX, TX
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/start") {
String welcome = "Hallo,selamat datang di sistem monitoring dan keamanan kamar batita, " + from_name + ".\n";
welcome += "berikut ini adalah perintah-perintah yang dapat digunakan\n\n";
welcome += "/lagu : Request lagu\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
if (text == "/lagu") {
lagu = true;
Serial.println("Music Request");
bot.sendMessage(chat_id, "musik diputar", "");
}
}
}
void setup () {
Serial.begin (115200);
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
pinMode(PIN_BUSY, INPUT);
Serial.println("Setting up software serial");
mp3Serial.begin (9600);
Serial.println("Setting up mp3 player");
mp3_set_serial (mp3Serial);
// Delay is required before accessing player. From my experience it's ~1 sec
delay(1000);
mp3_set_volume (15);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println();
Serial.print("WiFi Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop () {
if (millis() > Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if(lagu){
Serial.println("Stop");
mp3_stop ();
Serial.print("Busy: ");
Serial.println(analogRead(PIN_BUSY));
//delay(500);
Serial.println("play next");
mp3_next ();
Serial.print("Busy: ");
Serial.println(analogRead(PIN_BUSY));
// delay (12000);
}
Bot_lasttime = millis();
}
}