Components and supplies
ESP32
Apps and platforms
rogerbit.
Project description
Code
Source code
js
Source code
1#include <WiFi.h> 2#include <ESPAsyncWebServer.h> 3#include <HardwareSerial.h> 4// Configuración de la red Wi-Fi 5const char* ssid = "Tu_Red_Wifi"; 6const char* password = "Tu_Clave_wifi"; 7// Creación del servidor web 8AsyncWebServer server(80); 9// Configuración del puerto serial para el YX5300 10HardwareSerial mp3Serial(2); // Usamos Serial2 para la comunicación con el YX5300 11// Función para enviar comandos al módulo YX5300 12void sendCommand(uint8_t cmd, uint8_t arg1 = 0, uint8_t arg2 = 0) { 13 uint8_t command[8] = {0x7E, 0xFF, 0x06, cmd, 0x00, arg1, arg2, 0xEF}; 14 mp3Serial.write(command, sizeof(command)); 15} 16void setup() { 17 // Inicialización de seriales 18 Serial.begin(115200); 19 mp3Serial.begin(9600, SERIAL_8N1, 16, 17); // RX=16, TX=17 20 // Conexión a la red Wi-Fi 21 WiFi.begin(ssid, password); 22 while (WiFi.status() != WL_CONNECTED) { 23 delay(1000); 24 Serial.println("Conectando a WiFi..."); 25 } 26 Serial.println("Conectado a WiFi"); 27 Serial.println(WiFi.localIP()); 28 // Configuración de las rutas en el servidor web 29 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { 30 String html = "<h1>Control de Modulo YX5300</h1>"; 31 html += "<button onclick=\"fetch('/play')\">Play</button><br>"; 32 html += "<button onclick=\"fetch('/pause')\">Paus</button><br>"; 33 html += "<button onclick=\"fetch('/stop')\">Stop</button><br>"; 34 html += "<button onclick=\"fetch('/next')\">Prox</button><br>"; 35 html += "<button onclick=\"fetch('/previous')\">Prev</button><br>"; 36 html += "<button onclick=\"fetch('/volumeup')\">Vol +</button><br>"; 37 html += "<button onclick=\"fetch('/volumedown')\">Vol -</button><br>"; 38 request->send(200, "text/html", html); 39 }); 40 // Rutas para controlar el módulo 41 server.on("/play", HTTP_GET, [](AsyncWebServerRequest *request) { 42 sendCommand(0x0D); // Play 43 request->send(200, "text/plain", "Playing Track 1"); 44 }); 45 server.on("/pause", HTTP_GET, [](AsyncWebServerRequest *request) { 46 sendCommand(0x0E); // Pausar 47 request->send(200, "text/plain", "Paused"); 48 }); 49 server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) { 50 sendCommand(0x16); // Detener 51 request->send(200, "text/plain", "Stopped"); 52 }); 53 server.on("/next", HTTP_GET, [](AsyncWebServerRequest *request) { 54 sendCommand(0x01); // Siguiente pista 55 request->send(200, "text/plain", "Next Track"); 56 }); 57 server.on("/previous", HTTP_GET, [](AsyncWebServerRequest *request) { 58 sendCommand(0x02); // Pista anterior 59 request->send(200, "text/plain", "Previous Track"); 60 }); 61 server.on("/volumeup", HTTP_GET, [](AsyncWebServerRequest *request) { 62 static uint8_t volume = 15; 63 if (volume < 30) volume++; 64 sendCommand(0x04, 0x00, volume); // Subir volumen 65 request->send(200, "text/plain", "Volume Up"); 66 }); 67 server.on("/volumedown", HTTP_GET, [](AsyncWebServerRequest *request) { 68 static uint8_t volume = 15; 69 if (volume > 0) volume--; 70 sendCommand(0x05, 0x00, volume); // Bajar volumen 71 request->send(200, "text/plain", "Volume Down"); 72 }); 73 // Iniciar el servidor 74 server.begin(); 75} 76void loop() { 77}
Comments
Only logged in users can leave comments
carlosvolt
0 Followers
•0 Projects
0