Compare commits
7 Commits
11cf1cf782
...
main
Author | SHA1 | Date | |
---|---|---|---|
e03ef29f4d | |||
fc108a4b7f | |||
fcee080589 | |||
1d2a817b61 | |||
81fedb1260 | |||
61d9502e55 | |||
8bb37341f2 |
26
Makefile
Normal file
26
Makefile
Normal file
@@ -0,0 +1,26 @@
|
||||
.PHONY: compile upload watch all
|
||||
|
||||
# Auto-detect ESP32 USB port
|
||||
ESP32_PORT := $(shell ls /dev/ttyUSB* 2>/dev/null | head -1)
|
||||
|
||||
compile: esp32_radio/esp32_radio.ino esp32_radio/wifiinfo.h
|
||||
arduino-cli compile --fqbn esp32:esp32:esp32 esp32_radio
|
||||
|
||||
upload:
|
||||
@if [ -z "$(ESP32_PORT)" ]; then \
|
||||
echo "Error: No USB device found. Please check ESP32 connection."; \
|
||||
exit 1; \
|
||||
fi
|
||||
@echo "Using port: $(ESP32_PORT)"
|
||||
arduino-cli upload -p $(ESP32_PORT) -b esp32:esp32:esp32 esp32_radio
|
||||
|
||||
watch:
|
||||
@if [ -z "$(ESP32_PORT)" ]; then \
|
||||
echo "Error: No USB device found. Please check ESP32 connection."; \
|
||||
exit 1; \
|
||||
fi
|
||||
@echo "Monitoring port: $(ESP32_PORT)"
|
||||
screen $(ESP32_PORT) 115200
|
||||
|
||||
all: compile upload watch
|
||||
|
165
README.md
165
README.md
@@ -1 +1,164 @@
|
||||
# ESP32 radio
|
||||
# ESP32 Radio
|
||||
|
||||
Radio internet WiFi basée sur ESP32 avec interface web intégrée et contrôles physiques.
|
||||
|
||||
## 🎵 Pour l'utilisateur
|
||||
|
||||
### Première utilisation
|
||||
|
||||
1. **Alimentez la radio** : Au premier démarrage, la radio entre automatiquement en mode configuration
|
||||
2. **Connectez-vous au WiFi** : Réseau `ESP32_Radio_Config` (mot de passe: `radio12345`)
|
||||
3. **Configurez votre WiFi** : Allez sur http://192.168.4.1 et entrez vos paramètres WiFi
|
||||
4. **Redémarrage automatique** : La radio se connecte à votre réseau et est prête
|
||||
|
||||
### Utilisation quotidienne
|
||||
|
||||
#### Contrôles physiques
|
||||
|
||||
- **🔘 Appui court** : Mise en veille (économie d'énergie)
|
||||
- **🔘 Appui long (2s)** : Mode sélection de stations (LED clignote 3x)
|
||||
- **🔘 Appui très long (5s)** : Reset WiFi et retour en mode configuration
|
||||
- **🎛️ Potentiomètre** :
|
||||
- Mode normal : Contrôle du volume
|
||||
- Mode sélection : Choix de la station radio
|
||||
|
||||
#### Interface web
|
||||
|
||||
Accédez à l'interface via l'adresse IP de la radio (affichée au démarrage) :
|
||||
|
||||
- **🎵 Contrôle de lecture** : Play/Stop/Changement de station
|
||||
- **📻 Gestion des stations** : Ajouter/supprimer jusqu'à 10 stations
|
||||
- **🌐 Configuration WiFi** : Modifier les paramètres réseau
|
||||
- **📱 Compatible mobile** : Interface responsive
|
||||
|
||||
### Stations pré-configurées
|
||||
|
||||
- France Inter
|
||||
- FIP
|
||||
- France Info
|
||||
- 80s Hits
|
||||
- BBC Radio 1
|
||||
|
||||
## ⚙️ Pour le développeur
|
||||
|
||||
### Prérequis
|
||||
|
||||
- **Arduino CLI** installé et configuré
|
||||
- **Board ESP32** : `esp32:esp32:esp32`
|
||||
- **Bibliothèques** :
|
||||
- ESP32-audioI2S
|
||||
- WiFi (intégrée)
|
||||
- WebServer (intégrée)
|
||||
- EEPROM (intégrée)
|
||||
|
||||
### Schéma de câblage
|
||||
|
||||
```
|
||||
ESP32 Pin | Composant
|
||||
-----------|----------
|
||||
25 | I2S_DOUT (vers DAC)
|
||||
26 | I2S_LRC (vers DAC)
|
||||
27 | I2S_BCLK (vers DAC)
|
||||
32 | LED de statut
|
||||
33 | Bouton d'alimentation
|
||||
34 | Potentiomètre (signal analogique)
|
||||
```
|
||||
|
||||
### Configuration WiFi
|
||||
|
||||
1. Copiez `wifiinfo.h.sample` vers `wifiinfo.h`
|
||||
2. Modifiez les paramètres par défaut :
|
||||
|
||||
```cpp
|
||||
#define WIFI_SSID "VotreSSID"
|
||||
#define WIFI_PASSWD "VotreMotDePasse"
|
||||
#define HOSTNAME "ESP32-Radio"
|
||||
```
|
||||
|
||||
### Compilation et upload
|
||||
|
||||
#### Méthode simple
|
||||
|
||||
```bash
|
||||
make all # Compile + Upload + Monitor
|
||||
```
|
||||
|
||||
#### Méthode détaillée
|
||||
|
||||
```bash
|
||||
make compile # Compilation uniquement
|
||||
make upload # Upload vers ESP32
|
||||
make watch # Monitoring série (screen)
|
||||
```
|
||||
|
||||
#### Troubleshooting upload
|
||||
|
||||
Si l'upload échoue avec "Device or resource busy" :
|
||||
|
||||
```bash
|
||||
lsof /dev/ttyUSB0 # Vérifier les processus
|
||||
kill <PID> # Tuer le processus si nécessaire
|
||||
```
|
||||
|
||||
### Architecture du code
|
||||
|
||||
#### Fichiers principaux
|
||||
|
||||
- `esp32_radio.ino` : Code principal
|
||||
- `wifiinfo.h` : Configuration WiFi (non versionné)
|
||||
- `Makefile` : Scripts de build
|
||||
|
||||
#### Fonctions clés
|
||||
|
||||
- `setup()` : Initialisation, gestion du réveil, connexion WiFi
|
||||
- `loop()` : Boucle principale (audio, web, contrôles)
|
||||
- `wifi_init()` : Gestion intelligente de la connexion WiFi
|
||||
- `powerButton()` : Gestion multi-niveau des appuis bouton
|
||||
- `volumeAdjust()` : Volume ou sélection selon le mode
|
||||
- `handleRoot()` : Interface web principale
|
||||
|
||||
#### Stockage EEPROM
|
||||
|
||||
```cpp
|
||||
EEPROM_SIZE = 2048
|
||||
├── WiFi credentials (0-96)
|
||||
└── Radio stations (100-2047)
|
||||
├── Nombre de stations (1 byte)
|
||||
└── Stations (200 bytes chacune)
|
||||
```
|
||||
|
||||
#### Modes de fonctionnement
|
||||
|
||||
1. **Normal** : Lecture + contrôles standard
|
||||
2. **Sélection** : Potentiomètre = sélection station
|
||||
3. **Configuration** : Point d'accès + interface web
|
||||
4. **Deep Sleep** : Réveil par bouton uniquement
|
||||
|
||||
### Personnalisation
|
||||
|
||||
#### Ajouter des stations par défaut
|
||||
|
||||
Modifiez `loadStationsFromEEPROM()` ligne 428-441.
|
||||
|
||||
#### Modifier les timeouts
|
||||
|
||||
```cpp
|
||||
#define CONNECTION_TIMEOUT 10 // Timeout WiFi (secondes)
|
||||
#define LONG_PRESS_TIME 2000 // Appui long (ms)
|
||||
#define SELECTION_TIMEOUT 5000 // Timeout sélection (ms)
|
||||
```
|
||||
|
||||
#### Pins personnalisées
|
||||
|
||||
Modifiez les defines lignes 9-14 selon votre câblage.
|
||||
|
||||
### API Web
|
||||
|
||||
- `GET /` : Interface principale
|
||||
- `POST /add` : Ajouter station (`name`, `url`)
|
||||
- `POST /delete?id=X` : Supprimer station
|
||||
- `POST /play?id=X` : Lancer station
|
||||
- `POST /stop` : Arrêter + veille
|
||||
- `GET /wifi-config` : Interface WiFi
|
||||
- `POST /save-wifi` : Sauvegarder WiFi + redémarrage
|
||||
- `POST /reset-wifi` : Reset + mode configuration
|
||||
|
@@ -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,20 +27,106 @@ 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
|
||||
|
||||
// WiFi credentials EEPROM addresses
|
||||
#define WIFI_CREDS_ADDR 0
|
||||
#define MAX_SSID_LENGTH 32
|
||||
#define MAX_PASSWORD_LENGTH 64
|
||||
#define MAX_HOSTNAME_LENGTH 32
|
||||
|
||||
struct WiFiCredentials {
|
||||
char ssid[MAX_SSID_LENGTH];
|
||||
char password[MAX_PASSWORD_LENGTH];
|
||||
char hostname[MAX_HOSTNAME_LENGTH];
|
||||
bool isConfigured;
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
// WiFi configuration variables
|
||||
WiFiCredentials wifiCreds;
|
||||
bool configMode = false;
|
||||
const char* AP_SSID = "ESP32_Radio_Config";
|
||||
const char* AP_PASSWORD = "radio12345";
|
||||
|
||||
// Button timing
|
||||
unsigned long buttonPressTime = 0;
|
||||
bool buttonPressed = false;
|
||||
bool selectionMode = false;
|
||||
const unsigned long LONG_PRESS_TIME = 2000; // 2 seconds for long press
|
||||
const unsigned long VERY_LONG_PRESS_TIME = 5000; // 5 seconds for config mode
|
||||
|
||||
// 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);
|
||||
loadWiFiCredentials();
|
||||
loadStationsFromEEPROM();
|
||||
|
||||
esp_sleep_wakeup_cause_t source_reveil;
|
||||
|
||||
source_reveil = esp_sleep_get_wakeup_cause();
|
||||
|
||||
Serial.println(source_reveil);
|
||||
|
||||
switch(source_reveil){
|
||||
case ESP_SLEEP_WAKEUP_EXT0 :
|
||||
break;
|
||||
default :
|
||||
Serial.println("Deepsleep mod -- first boot");
|
||||
delay(500);
|
||||
esp_deep_sleep_start();
|
||||
break;
|
||||
}
|
||||
|
||||
wifi_init();
|
||||
wifi_info();
|
||||
|
||||
esp_sleep_enable_ext0_wakeup(POWERBUTTON, 1);
|
||||
// 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.on("/wifi-config", handleWiFiConfig);
|
||||
server.on("/save-wifi", HTTP_POST, handleSaveWiFi);
|
||||
server.on("/reset-wifi", HTTP_POST, handleResetWiFi);
|
||||
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");
|
||||
|
||||
}
|
||||
|
||||
@@ -43,24 +134,124 @@ void loop()
|
||||
|
||||
{
|
||||
audio.loop();
|
||||
server.handleClient();
|
||||
volumeAdjust();
|
||||
powerButton();
|
||||
|
||||
// Indicateur LED pour mode configuration
|
||||
if (configMode) {
|
||||
configModeLEDIndicator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void volumeAdjust () {
|
||||
void configModeLEDIndicator() {
|
||||
static unsigned long lastBlink = 0;
|
||||
static bool ledState = false;
|
||||
|
||||
// Clignotement rapide en mode configuration
|
||||
if (millis() - lastBlink > 500) {
|
||||
ledState = !ledState;
|
||||
digitalWrite(STATUSLED, ledState ? HIGH : LOW);
|
||||
lastBlink = millis();
|
||||
}
|
||||
}
|
||||
|
||||
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 >= VERY_LONG_PRESS_TIME) {
|
||||
// Very long press: force config mode
|
||||
Serial.println("Forçage du mode configuration");
|
||||
resetWiFiCredentials();
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
} else 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() {
|
||||
@@ -74,12 +265,20 @@ void wifi_init() {
|
||||
Serial.begin(115200);
|
||||
digitalWrite(STATUSLED, HIGH);
|
||||
WiFi.disconnect();
|
||||
|
||||
// Check if we have valid WiFi credentials
|
||||
if (!wifiCreds.isConfigured || strlen(wifiCreds.ssid) == 0) {
|
||||
startConfigMode();
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to connect to WiFi
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setHostname(hostname.c_str());
|
||||
WiFi.begin(ssid.c_str(), password.c_str());
|
||||
WiFi.setHostname(wifiCreds.hostname);
|
||||
WiFi.begin(wifiCreds.ssid, wifiCreds.password);
|
||||
|
||||
Serial.print("Connexion au reseau ");
|
||||
Serial.println(ssid);
|
||||
Serial.println(wifiCreds.ssid);
|
||||
|
||||
int timeout_counter = 0;
|
||||
|
||||
@@ -91,11 +290,37 @@ void wifi_init() {
|
||||
delay(500);
|
||||
timeout_counter++;
|
||||
if (timeout_counter > CONNECTION_TIMEOUT){
|
||||
ESP.restart();
|
||||
Serial.println("\nConnexion WiFi échouée, passage en mode configuration");
|
||||
startConfigMode();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
configMode = false;
|
||||
WiFi.config(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask(), IPAddress(192,168,2,1));
|
||||
Serial.println("\nConnexion WiFi réussie");
|
||||
}
|
||||
|
||||
void startConfigMode() {
|
||||
configMode = true;
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(AP_SSID, AP_PASSWORD);
|
||||
|
||||
Serial.println("Mode configuration activé");
|
||||
Serial.print("Nom du réseau: ");
|
||||
Serial.println(AP_SSID);
|
||||
Serial.print("Mot de passe: ");
|
||||
Serial.println(AP_PASSWORD);
|
||||
Serial.print("Adresse IP: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
|
||||
// Clignotement spécial LED pour indiquer le mode configuration
|
||||
for (int i = 0; i < 5; i++) {
|
||||
digitalWrite(STATUSLED, LOW);
|
||||
delay(200);
|
||||
digitalWrite(STATUSLED, HIGH);
|
||||
delay(200);
|
||||
}
|
||||
}
|
||||
|
||||
void audio_info(const char *info) {
|
||||
@@ -131,3 +356,292 @@ void audio_lasthost(const char *info) { //stream URL played
|
||||
void audio_eof_speech(const char *info) {
|
||||
Serial.print("eof_speech "); Serial.println(info);
|
||||
}
|
||||
|
||||
// WiFi credentials EEPROM functions
|
||||
void saveWiFiCredentials() {
|
||||
int addr = WIFI_CREDS_ADDR;
|
||||
EEPROM.put(addr, wifiCreds);
|
||||
EEPROM.commit();
|
||||
Serial.println("WiFi credentials saved to EEPROM");
|
||||
}
|
||||
|
||||
void loadWiFiCredentials() {
|
||||
int addr = WIFI_CREDS_ADDR;
|
||||
EEPROM.get(addr, wifiCreds);
|
||||
|
||||
// If not configured, use defaults from wifiinfo.h
|
||||
if (!wifiCreds.isConfigured) {
|
||||
strcpy(wifiCreds.ssid, WIFI_SSID);
|
||||
strcpy(wifiCreds.password, WIFI_PASSWD);
|
||||
strcpy(wifiCreds.hostname, HOSTNAME);
|
||||
wifiCreds.isConfigured = true;
|
||||
saveWiFiCredentials();
|
||||
}
|
||||
|
||||
Serial.print("Loaded WiFi credentials - SSID: ");
|
||||
Serial.println(wifiCreds.ssid);
|
||||
}
|
||||
|
||||
void resetWiFiCredentials() {
|
||||
strcpy(wifiCreds.ssid, "");
|
||||
strcpy(wifiCreds.password, "");
|
||||
strcpy(wifiCreds.hostname, HOSTNAME);
|
||||
wifiCreds.isConfigured = false;
|
||||
saveWiFiCredentials();
|
||||
Serial.println("WiFi credentials reset");
|
||||
}
|
||||
|
||||
// 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><meta charset='UTF-8'><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>";
|
||||
|
||||
if (configMode) {
|
||||
html += "<p><strong>🔧 Mode Configuration</strong></p>";
|
||||
html += "<p>IP: " + WiFi.softAPIP().toString() + "</p>";
|
||||
html += "<p>Réseau: " + String(AP_SSID) + "</p>";
|
||||
html += "<p><a href='/wifi-config'>🌐 Configurer WiFi</a></p>";
|
||||
} else {
|
||||
html += "<p><strong>📡 Mode Normal</strong></p>";
|
||||
html += "<p>IP: " + WiFi.localIP().toString() + "</p>";
|
||||
html += "<p>Réseau: " + String(wifiCreds.ssid) + "</p>";
|
||||
html += "<p><a href='/wifi-config'>🌐 Modifier WiFi</a></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; charset=utf-8", 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();
|
||||
}
|
||||
|
||||
void handleWiFiConfig() {
|
||||
String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Configuration WiFi</title>";
|
||||
html += "<style>body{font-family:Arial;margin:20px}input[type=text],input[type=password]{width:300px;padding:10px;margin:5px 0}";
|
||||
html += "button{padding:10px 20px;margin:10px 5px;font-size:16px}";
|
||||
html += ".back-btn{background-color:#6c757d;color:white;text-decoration:none;padding:10px 20px;display:inline-block;margin:10px 0}</style></head><body>";
|
||||
html += "<h1>Configuration WiFi</h1>";
|
||||
|
||||
if (configMode) {
|
||||
html += "<p><strong>🔧 Mode Configuration Actif</strong></p>";
|
||||
html += "<p>Connectez-vous au réseau <strong>" + String(AP_SSID) + "</strong> pour accéder à cette page.</p>";
|
||||
}
|
||||
|
||||
html += "<form method='POST' action='/save-wifi'>";
|
||||
html += "<p>Nom du réseau (SSID):</p>";
|
||||
html += "<input type='text' name='ssid' value='" + String(wifiCreds.ssid) + "' maxlength='" + String(MAX_SSID_LENGTH-1) + "' required><br>";
|
||||
html += "<p>Mot de passe:</p>";
|
||||
html += "<input type='password' name='password' value='" + String(wifiCreds.password) + "' maxlength='" + String(MAX_PASSWORD_LENGTH-1) + "'><br>";
|
||||
html += "<p>Nom de l'appareil:</p>";
|
||||
html += "<input type='text' name='hostname' value='" + String(wifiCreds.hostname) + "' maxlength='" + String(MAX_HOSTNAME_LENGTH-1) + "' required><br><br>";
|
||||
html += "<button type='submit'>💾 Sauvegarder et Redémarrer</button>";
|
||||
html += "</form>";
|
||||
|
||||
html += "<form method='POST' action='/reset-wifi'>";
|
||||
html += "<button type='submit' onclick=\"return confirm('Êtes-vous sûr de vouloir effacer la configuration WiFi ?')\">🔄 Reset Configuration</button>";
|
||||
html += "</form>";
|
||||
|
||||
html += "<a href='/' class='back-btn'>⬅️ Retour</a>";
|
||||
html += "</body></html>";
|
||||
|
||||
server.send(200, "text/html; charset=utf-8", html);
|
||||
}
|
||||
|
||||
void handleSaveWiFi() {
|
||||
String newSSID = server.arg("ssid");
|
||||
String newPassword = server.arg("password");
|
||||
String newHostname = server.arg("hostname");
|
||||
|
||||
if (newSSID.length() == 0 || newHostname.length() == 0) {
|
||||
server.send(400, "text/plain", "SSID et nom d'appareil requis");
|
||||
return;
|
||||
}
|
||||
|
||||
// Sauvegarder les nouvelles credentials
|
||||
newSSID.toCharArray(wifiCreds.ssid, MAX_SSID_LENGTH);
|
||||
newPassword.toCharArray(wifiCreds.password, MAX_PASSWORD_LENGTH);
|
||||
newHostname.toCharArray(wifiCreds.hostname, MAX_HOSTNAME_LENGTH);
|
||||
wifiCreds.isConfigured = true;
|
||||
saveWiFiCredentials();
|
||||
|
||||
String response = "Configuration WiFi sauvegardée. Redémarrage en cours...<br>";
|
||||
response += "SSID: " + newSSID + "<br>";
|
||||
response += "Appareil: " + newHostname;
|
||||
|
||||
server.send(200, "text/html; charset=utf-8", response);
|
||||
|
||||
Serial.println("Nouvelles credentials WiFi sauvegardées, redémarrage...");
|
||||
delay(2000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void handleResetWiFi() {
|
||||
resetWiFiCredentials();
|
||||
server.send(200, "text/html; charset=utf-8", "Configuration WiFi effacée. Redémarrage en mode configuration...");
|
||||
delay(2000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
Reference in New Issue
Block a user