carlosvolt
Published © LGPL

MP3 player control with webserver using ESP32 WIFI

In this tutorial you will learn how to build a web server using the ESP32 to control the YX5300 module, a serial-controlled MP3 audio player

IntermediateProtip90
MP3 player control with webserver using ESP32 WIFI

Things used in this project

Hardware components

ESP32
Espressif ESP32
×1

Story

Read more

Code

Source code

C/C++
Source code
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <HardwareSerial.h>
// Configuración de la red Wi-Fi
const char* ssid = "Tu_Red_Wifi";
const char* password = "Tu_Clave_wifi";
// Creación del servidor web
AsyncWebServer server(80);
// Configuración del puerto serial para el YX5300
HardwareSerial mp3Serial(2); // Usamos Serial2 para la comunicación con el YX5300
// Función para enviar comandos al módulo YX5300
void sendCommand(uint8_t cmd, uint8_t arg1 = 0, uint8_t arg2 = 0) {
  uint8_t command[8] = {0x7E, 0xFF, 0x06, cmd, 0x00, arg1, arg2, 0xEF};
  mp3Serial.write(command, sizeof(command));
}
void setup() {
  // Inicialización de seriales
  Serial.begin(115200);
  mp3Serial.begin(9600, SERIAL_8N1, 16, 17); // RX=16, TX=17
  // Conexión a la red Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Conectando a WiFi...");
  }
  Serial.println("Conectado a WiFi");
  Serial.println(WiFi.localIP());
  // Configuración de las rutas en el servidor web
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    String html = "<h1>Control de Modulo YX5300</h1>";
    html += "<button onclick=\"fetch('/play')\">Play</button><br>";
    html += "<button onclick=\"fetch('/pause')\">Paus</button><br>";
    html += "<button onclick=\"fetch('/stop')\">Stop</button><br>";
    html += "<button onclick=\"fetch('/next')\">Prox</button><br>";
    html += "<button onclick=\"fetch('/previous')\">Prev</button><br>";
    html += "<button onclick=\"fetch('/volumeup')\">Vol +</button><br>";
    html += "<button onclick=\"fetch('/volumedown')\">Vol -</button><br>";
    request->send(200, "text/html", html);
  });
  // Rutas para controlar el módulo
  server.on("/play", HTTP_GET, [](AsyncWebServerRequest *request) {
    sendCommand(0x0D); // Play
    request->send(200, "text/plain", "Playing Track 1");
  });
  server.on("/pause", HTTP_GET, [](AsyncWebServerRequest *request) {
    sendCommand(0x0E); // Pausar
    request->send(200, "text/plain", "Paused");
  });
  server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) {
    sendCommand(0x16); // Detener
    request->send(200, "text/plain", "Stopped");
  });
  server.on("/next", HTTP_GET, [](AsyncWebServerRequest *request) {
    sendCommand(0x01); // Siguiente pista
    request->send(200, "text/plain", "Next Track");
  });
  server.on("/previous", HTTP_GET, [](AsyncWebServerRequest *request) {
    sendCommand(0x02); // Pista anterior
    request->send(200, "text/plain", "Previous Track");
  });
  server.on("/volumeup", HTTP_GET, [](AsyncWebServerRequest *request) {
    static uint8_t volume = 15;
    if (volume < 30) volume++;
    sendCommand(0x04, 0x00, volume); // Subir volumen
    request->send(200, "text/plain", "Volume Up");
  });
  server.on("/volumedown", HTTP_GET, [](AsyncWebServerRequest *request) {
    static uint8_t volume = 15;
    if (volume > 0) volume--;
    sendCommand(0x05, 0x00, volume); // Bajar volumen
    request->send(200, "text/plain", "Volume Down");
  });
  // Iniciar el servidor
  server.begin();
}
void loop() {
}

Credits

carlosvolt

carlosvolt

32 projects • 3 followers

Comments