#include #include #include #include "Audio.h" const char* ssid = "SmartEstancia"; const char* password = "Smart12345"; IPAddress local_IP(192, 168, 1, 77); IPAddress gateway(192, 168, 1, 254); IPAddress subnet(255, 255, 255, 0); IPAddress primaryDNS(8, 8, 8, 8); IPAddress secondaryDNS(8, 8, 4, 4); #define I2S_BCLK 26 #define I2S_LRC 25 #define I2S_DOUT 33 Audio audio; WebServer server(80); void handlePlay() { if (server.hasArg("url")) { String url = server.arg("url"); Serial.print("🎵 Python ordenó reproducir: "); Serial.println(url); audio.stopSong(); audio.connecttohost(url.c_str()); server.send(200, "text/plain", "Reproduciendo OK"); } else { server.send(400, "text/plain", "Error: Falta la URL"); } } // --- NUEVO: Control de Volumen --- void handleVolume() { if (server.hasArg("level")) { int nivel = server.arg("level").toInt(); if (nivel >= 0 && nivel <= 21) { audio.setVolume(nivel); Serial.print("🔊 Volumen cambiado a: "); Serial.println(nivel); server.send(200, "text/plain", "Volumen OK: " + String(nivel)); } else { server.send(400, "text/plain", "Error: Nivel debe ser 0-21"); } } else { server.send(400, "text/plain", "Error: Falta parametro 'level'"); } } void handleVolumeUp() { int actual = audio.getVolume(); if (actual < 21) audio.setVolume(actual + 1); server.send(200, "text/plain", "Volumen: " + String(audio.getVolume())); } void handleVolumeDown() { int actual = audio.getVolume(); if (actual > 0) audio.setVolume(actual - 1); server.send(200, "text/plain", "Volumen: " + String(audio.getVolume())); } void setup() { Serial.begin(115200); Serial.println("\n--- INICIANDO SISTEMA DE AUDIO ESP32 ---"); if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { Serial.println("Fallo al configurar la IP Estatica"); } WiFi.begin(ssid, password); Serial.print("Conectando a WiFi"); int intento = 0; while (WiFi.status() != WL_CONNECTED && intento < 20) { delay(500); Serial.print("."); intento++; } if (WiFi.status() != WL_CONNECTED) { Serial.println("\nFallo de conexion."); while(true){ delay(1000); } } Serial.println("\nWiFi Conectado!"); Serial.print("IP FIJA: "); Serial.println(WiFi.localIP()); audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setVolume(15); // Volumen inicial medio server.on("/play", HTTP_GET, handlePlay); server.on("/volume", HTTP_GET, handleVolume); server.on("/volume_up", HTTP_GET, handleVolumeUp); server.on("/volume_down", HTTP_GET, handleVolumeDown); server.begin(); Serial.println("Listo para recibir audio..."); } void loop() { audio.loop(); server.handleClient(); if (Serial.available()) { String url = Serial.readStringUntil('\n'); url.trim(); if (url.length() > 5 && url.startsWith("http")) { audio.stopSong(); audio.connecttohost(url.c_str()); } } } void audio_info(const char *info){ Serial.print("INFO AUDIO: "); Serial.println(info); } void audio_id3data(const char *info){ Serial.print("METADATOS: "); Serial.println(info); } void audio_eof_mp3(const char *info){ Serial.print("FIN DEL AUDIO: "); Serial.println(info); } """ #include #include #include #include "Audio.h" // ======================================================= // 1. CONFIGURACIÓN DE RED // ======================================================= const char* ssid = "SmartEstancia"; const char* password = "Smart12345"; // --- CONFIGURACIÓN DE IP ESTÁTICA --- IPAddress local_IP(192, 168, 1, 77); // La IP que le asiganamos a la bocina IPAddress gateway(192, 168, 1, 254); // la IP del router IPAddress subnet(255, 255, 255, 0); // Máscara de subred IPAddress primaryDNS(8, 8, 8, 8); // DNS primario (Google) IPAddress secondaryDNS(8, 8, 4, 4); // DNS secundario (Google) // ======================================================= // 2. CONFIGURACIÓN DE PINES CORRECTOS // ======================================================= #define I2S_BCLK 26 #define I2S_LRC 25 #define I2S_DOUT 33 Audio audio; WebServer server(80); void handlePlay() { if (server.hasArg("url")) { String url = server.arg("url"); Serial.print("🎵 Python ordenó reproducir: "); Serial.println(url); audio.stopSong(); audio.connecttohost(url.c_str()); server.send(200, "text/plain", "Reproduciendo OK"); } else { server.send(400, "text/plain", "Error: Falta la URL"); } } void setup() { Serial.begin(115200); Serial.println("\n--- INICIANDO SISTEMA DE AUDIO ESP32 ---"); if (psramFound()) { Serial.println("PSRAM encontrada."); } else { Serial.println(" Sin PSRAM. Usando RAM interna."); } // ⚡ APLICAR IP ESTÁTICA ANTES DE CONECTAR if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { Serial.println(" Fallo al configurar la IP Estática"); } WiFi.begin(ssid, password); Serial.print("Conectando a WiFi"); int intento = 0; while (WiFi.status() != WL_CONNECTED && intento < 20) { delay(500); Serial.print("."); intento++; } if (WiFi.status() != WL_CONNECTED) { Serial.println("\n Fallo de conexión."); while(true){ delay(1000); } } Serial.println("\n WiFi Conectado de verdad!"); Serial.print("Tu IP es FIJA: "); Serial.println(WiFi.localIP()); audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setVolume(18); server.on("/play", HTTP_GET, handlePlay); server.begin(); Serial.println("Listo para recibir audio..."); } void loop() { audio.loop(); server.handleClient(); if (Serial.available()) { String url = Serial.readStringUntil('\n'); url.trim(); if (url.length() > 5 && url.startsWith("http")) { audio.stopSong(); audio.connecttohost(url.c_str()); } } } void audio_info(const char *info){ Serial.print("INFO AUDIO: "); Serial.println(info); } void audio_id3data(const char *info){ Serial.print("METADATOS: "); Serial.println(info); } void audio_eof_mp3(const char *info){ Serial.print("FIN DEL AUDIO: "); Serial.println(info); } """