feat: add multiple radios switching and web edit

This commit is contained in:
2025-08-20 22:03:58 +02:00
parent 61d9502e55
commit 81fedb1260

View File

@@ -2,6 +2,8 @@
#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"
#include "WebServer.h"
#include "EEPROM.h"
#define I2S_DOUT 25
@@ -11,6 +13,9 @@
#define POWERBUTTON GPIO_NUM_33
#define VOLUMERES 34
Audio audio;
WebServer server(80);
uint16_t timeout_ms = 300;
uint16_t timeout_ms_ssl = 3000;
#define CONNECTION_TIMEOUT 10 // 10 seconds
wifi_config_t wifi_config = {0};
@@ -22,12 +27,44 @@ String hostname = HOSTNAME;
int volumeresistor;
int powerButtonState = 0;
// EEPROM configuration
#define EEPROM_SIZE 2048
#define MAX_STATIONS 10
#define MAX_URL_LENGTH 150
#define MAX_NAME_LENGTH 50
#define STATIONS_START_ADDR 100
struct RadioStation {
char name[MAX_NAME_LENGTH];
char url[MAX_URL_LENGTH];
};
// Station selection variables
RadioStation radioStations[MAX_STATIONS];
int numStations = 0;
int currentStation = 0;
// Button timing
unsigned long buttonPressTime = 0;
bool buttonPressed = false;
bool selectionMode = false;
const unsigned long LONG_PRESS_TIME = 2000; // 2 seconds for long press
// Selection mode timeout
unsigned long lastPotentiometerChange = 0;
int lastPotentiometerValue = 0;
const unsigned long SELECTION_TIMEOUT = 5000; // 5 seconds without potentiometer change
void setup() {
pinMode(STATUSLED, OUTPUT);
pinMode(POWERBUTTON, INPUT);
esp_sleep_enable_ext0_wakeup(POWERBUTTON, 1);
// Initialize EEPROM
EEPROM.begin(EEPROM_SIZE);
loadStationsFromEEPROM();
esp_sleep_wakeup_cause_t source_reveil;
source_reveil = esp_sleep_get_wakeup_cause();
@@ -47,11 +84,25 @@ void setup() {
wifi_init();
wifi_info();
// Setup web server
server.on("/", handleRoot);
server.on("/add", HTTP_POST, handleAdd);
server.on("/delete", HTTP_POST, handleDelete);
server.on("/play", HTTP_POST, handlePlay);
server.on("/stop", HTTP_POST, handleStop);
server.begin();
Serial.println("Serveur web demarré sur port 80");
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(30);
// audio.connecttohost("http://192.168.2.216:8000/06%20-%20H4dopi.mp3");
audio.connecttohost("http://icecast.radiofrance.fr/franceinter-midfi.mp3");
// audio.connecttohost("0n-80s.radionetz.de:8000/0n-70s.mp3");
audio.setConnectionTimeout(timeout_ms, timeout_ms_ssl);
Serial.println("Wait before connect");
delay(5000);
Serial.println("Start Connecting");
if (numStations > 0) {
audio.connecttohost(radioStations[currentStation].url);
}
Serial.println("End Connecting");
}
@@ -59,24 +110,101 @@ void loop()
{
audio.loop();
server.handleClient();
volumeAdjust();
powerButton();
}
void volumeAdjust () {
void volumeAdjust() {
volumeresistor = analogRead(VOLUMERES);
// Serial.println(volumeresistor);
audio.setVolume(volumeresistor*30/4095);
if (selectionMode) {
// Check for potentiometer movement (with tolerance for noise)
if (abs(volumeresistor - lastPotentiometerValue) > 20) {
lastPotentiometerChange = millis();
lastPotentiometerValue = volumeresistor;
}
// Check for timeout to exit selection mode
if (millis() - lastPotentiometerChange > SELECTION_TIMEOUT) {
selectionMode = false;
Serial.println("Selection mode: OFF (timeout)");
// Flash LED to indicate exit
for (int i = 0; i < 2; i++) {
digitalWrite(STATUSLED, LOW);
delay(150);
digitalWrite(STATUSLED, HIGH);
delay(150);
}
}
// In selection mode: potentiometer selects radio station
if (numStations > 0) {
int newStation = map(volumeresistor, 0, 4095, 0, numStations - 1);
if (newStation != currentStation) {
currentStation = newStation;
Serial.print("Changing to station ");
Serial.print(currentStation);
Serial.print(": ");
Serial.print(radioStations[currentStation].name);
Serial.print(" - ");
Serial.println(radioStations[currentStation].url);
// Disconnect current stream and connect to new one
audio.stopSong();
delay(100);
audio.connecttohost(radioStations[currentStation].url);
}
}
} else {
// Normal mode: potentiometer controls volume
audio.setVolume(volumeresistor * 30 / 4095);
}
}
void powerButton () {
powerButtonState = digitalRead(POWERBUTTON);
if (powerButtonState == 1) {
Serial.println("Deepsleep mod");
delay(500);
esp_deep_sleep_start();
void powerButton() {
int currentButtonState = digitalRead(POWERBUTTON);
// Button pressed (rising edge)
if (currentButtonState == 1 && powerButtonState == 0) {
buttonPressTime = millis();
buttonPressed = true;
}
// Button released (falling edge)
if (currentButtonState == 0 && powerButtonState == 1) {
unsigned long pressDuration = millis() - buttonPressTime;
if (pressDuration >= LONG_PRESS_TIME) {
// Long press: enter selection mode
if (!selectionMode) {
selectionMode = true;
lastPotentiometerChange = millis();
lastPotentiometerValue = volumeresistor;
Serial.println("Selection mode: ON");
// Flash LED to indicate selection mode
for (int i = 0; i < 3; i++) {
digitalWrite(STATUSLED, LOW);
delay(100);
digitalWrite(STATUSLED, HIGH);
delay(100);
}
}
} else {
// Short press: deep sleep (only if not in selection mode)
if (!selectionMode) {
Serial.println("Deepsleep mod");
delay(500);
esp_deep_sleep_start();
}
}
buttonPressed = false;
}
powerButtonState = currentButtonState;
}
void wifi_info() {
@@ -147,3 +275,180 @@ void audio_lasthost(const char *info) { //stream URL played
void audio_eof_speech(const char *info) {
Serial.print("eof_speech "); Serial.println(info);
}
// EEPROM functions
void saveStationsToEEPROM() {
int addr = STATIONS_START_ADDR;
EEPROM.write(addr, numStations);
addr++;
for (int i = 0; i < numStations; i++) {
for (int j = 0; j < MAX_NAME_LENGTH; j++) {
EEPROM.write(addr++, radioStations[i].name[j]);
}
for (int j = 0; j < MAX_URL_LENGTH; j++) {
EEPROM.write(addr++, radioStations[i].url[j]);
}
}
EEPROM.commit();
}
void loadStationsFromEEPROM() {
int addr = STATIONS_START_ADDR;
numStations = EEPROM.read(addr);
addr++;
if (numStations > MAX_STATIONS) numStations = 0;
for (int i = 0; i < numStations; i++) {
for (int j = 0; j < MAX_NAME_LENGTH; j++) {
radioStations[i].name[j] = EEPROM.read(addr++);
}
for (int j = 0; j < MAX_URL_LENGTH; j++) {
radioStations[i].url[j] = EEPROM.read(addr++);
}
}
// If no stations in EEPROM, load defaults
if (numStations == 0) {
numStations = 5;
strcpy(radioStations[0].name, "France Inter");
strcpy(radioStations[0].url, "https://icecast.radiofrance.fr/franceinter-midfi.mp3");
strcpy(radioStations[1].name, "FIP");
strcpy(radioStations[1].url, "https://icecast.radiofrance.fr/fip-midfi.mp3");
strcpy(radioStations[2].name, "France Info");
strcpy(radioStations[2].url, "https://icecast.radiofrance.fr/franceinfo-midfi.mp3");
strcpy(radioStations[3].name, "80s Hits");
strcpy(radioStations[3].url, "http://0n-80s.radionetz.de:8000/0n-70s.mp3");
strcpy(radioStations[4].name, "BBC Radio 1");
strcpy(radioStations[4].url, "http://stream.live.vc.bbcmedia.co.uk/bbc_radio_one");
saveStationsToEEPROM();
}
}
// Web server functions
void handleRoot() {
String html = "<!DOCTYPE html><html><head><title>ESP32 Radio Config</title>";
html += "<style>body{font-family:Arial;margin:20px}table{border-collapse:collapse;width:100%}";
html += "th,td{border:1px solid #ddd;padding:8px;text-align:left}th{background-color:#f2f2f2}";
html += "input[type=text]{width:90%;padding:5px}button{padding:10px;margin:5px}</style></head><body>";
html += "<h1>Configuration Radio ESP32</h1>";
html += "<p>IP: " + WiFi.localIP().toString() + "</p>";
if (numStations > 0) {
html += "<h2>Station actuelle</h2>";
html += "<p><strong>" + String(radioStations[currentStation].name) + "</strong></p>";
html += "<p>URL: " + String(radioStations[currentStation].url) + "</p>";
html += "<button onclick=\"playStation(" + String(currentStation) + ")\">🎵 Lancer cette station</button> ";
html += "<button onclick=\"stopRadio()\">⏹️ Arrêter</button>";
}
html += "<h2>Stations disponibles (" + String(numStations) + "/" + String(MAX_STATIONS) + ")</h2>";
html += "<table><tr><th>Nom</th><th>URL</th><th>Actions</th></tr>";
for (int i = 0; i < numStations; i++) {
html += "<tr><td>" + String(radioStations[i].name) + "</td>";
html += "<td>" + String(radioStations[i].url) + "</td>";
html += "<td>";
html += "<button onclick=\"playStation(" + String(i) + ")\">▶️ Jouer</button> ";
html += "<button onclick=\"deleteStation(" + String(i) + ")\">🗑️ Supprimer</button>";
html += "</td></tr>";
}
html += "</table>";
if (numStations < MAX_STATIONS) {
html += "<h2>Ajouter une station</h2>";
html += "<form method='POST' action='/add'>";
html += "Nom: <input type='text' name='name' maxlength='" + String(MAX_NAME_LENGTH-1) + "' required><br><br>";
html += "URL: <input type='text' name='url' maxlength='" + String(MAX_URL_LENGTH-1) + "' required><br><br>";
html += "<button type='submit'>Ajouter</button></form>";
}
html += "<script>";
html += "function deleteStation(id){if(confirm('Supprimer cette station?')){";
html += "fetch('/delete?id='+id,{method:'POST'}).then(()=>location.reload());}}";
html += "function playStation(id){";
html += "fetch('/play?id='+id,{method:'POST'}).then(()=>location.reload());}";
html += "function stopRadio(){";
html += "fetch('/stop',{method:'POST'}).then(()=>location.reload());}";
html += "</script></body></html>";
server.send(200, "text/html", html);
}
void handleAdd() {
if (numStations >= MAX_STATIONS) {
server.send(400, "text/plain", "Maximum de stations atteint");
return;
}
String name = server.arg("name");
String url = server.arg("url");
if (name.length() == 0 || url.length() == 0) {
server.send(400, "text/plain", "Nom et URL requis");
return;
}
name.toCharArray(radioStations[numStations].name, MAX_NAME_LENGTH);
url.toCharArray(radioStations[numStations].url, MAX_URL_LENGTH);
numStations++;
saveStationsToEEPROM();
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
}
void handleDelete() {
int id = server.arg("id").toInt();
if (id < 0 || id >= numStations) {
server.send(400, "text/plain", "ID invalide");
return;
}
// Shift stations
for (int i = id; i < numStations - 1; i++) {
strcpy(radioStations[i].name, radioStations[i + 1].name);
strcpy(radioStations[i].url, radioStations[i + 1].url);
}
numStations--;
if (currentStation >= numStations && numStations > 0) {
currentStation = numStations - 1;
}
saveStationsToEEPROM();
server.send(200, "text/plain", "Station supprimee");
}
void handlePlay() {
int id = server.arg("id").toInt();
if (id < 0 || id >= numStations) {
server.send(400, "text/plain", "ID invalide");
return;
}
currentStation = id;
Serial.print("Web: Changement vers station ");
Serial.print(currentStation);
Serial.print(": ");
Serial.print(radioStations[currentStation].name);
Serial.print(" - ");
Serial.println(radioStations[currentStation].url);
audio.stopSong();
delay(100);
audio.connecttohost(radioStations[currentStation].url);
server.send(200, "text/plain", "Lecture demarree");
}
void handleStop() {
Serial.println("Web: Arret de la radio - Passage en veille");
audio.stopSong();
server.send(200, "text/plain", "Radio arretee - Mise en veille");
delay(1000); // Laisser le temps de recevoir la réponse
esp_deep_sleep_start();
}