Compare commits

...

14 Commits

7 changed files with 1022 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
**/wifiinfo.h

26
Makefile Normal file
View 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
View File

@@ -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

167
esp32_radio/Makefile Normal file
View File

@@ -0,0 +1,167 @@
# Makefile for Arduino based scketches
#
# Copyright 2020 Valerio Di Giampietro http://va.ler.io v@ler.io
# MIT License - see License.txt file
#
# This Makefile uses the arduino-cli, the Arduino command line interface
# and has been designed and tested to run on Linux, not on Windows.
# Probably it will run on a Mac, but it has not been tested.
#
# Please note that:
#
# 1. each sketch must reside in his own folder with this Makefile
#
# 2. the main make targets are:
# - all compiles and upload
# - compile compiles only
# - upload upload via serial port, compile if the binary file is
# not available
# - ota upload Over The Air, automatically find the device
# IP address using the IOT_NAME (device hostname)
# - clean clean the build directory
# - find find OTA updatable devices on the local subnet
# - requirements it the file "requirements.txt" exists it will
# install the libraries listed in this file
#
# default is "all"
#
# 3. it gets the name of the sketch using the wildcard make command;
# the name is *.ino; this means that you must have ONLY a file
# with .ino extension, otherwise this makefile will break. This
# also means that you can use this Makefile, almost unmodified,
# for any sketch as long as you keep a single .ino file in each
# folder
#
# 4. you can split your project in multiple files, if you wish,
# using a single .ino file and multiple .h files, that you can
# include in the .ino file with an '#include "myfile.h"'
# directive
#
# Optionally some environment variables can be set:
#
# FQBN Fully Qualified Board Name; if not set in the environment
# it will be assigned a value in this makefile
#
# SERIAL_DEV Serial device to upload the sketch; if not set in the
# environment it will be assigned:
# /dev/ttyUSB0 if it exists, or
# /dev/ttyACM0 if it exists, or
# unknown
#
# IOT_NAME Name of the IOT device; if not set in the environment
# it will be assigned a value in this makefile. This is
# very useful for OTA update, the device will be searched
# on the local subnet using this name
#
# OTA_PORT Port used by OTA update; if not set in the environment
# it will be assigned the default value of 8266 in this
# makefile
#
# OTA_PASS Password used for OTA update; if not set in the environment
# it will be assigned the default value of an empty string
#
# V verbose flag; can be 0 (quiet) or 1 (verbose); if not set
# in the environment it will be assigned a default value
# in this makefile
MAKE_DIR := $(PWD)
#
# ----- setup wor Wemos D1 mini -----
FQBN ?= esp32:esp32:esp32
IOT_NAME ?= esp32-radio
OTA_PORT ?= 8266
OTA_PASS ?=
# ----- setup for Arduino Uno
#FQBN ?= arduino:avr:uno
# ----- ---------------------
V ?= 0
VFLAG =
ifeq "$(V)" "1"
VFLAG =-v
endif
ifndef SERIAL_DEV
ifneq (,$(wildcard /dev/ttyUSB0))
SERIAL_DEV = /dev/ttyUSB0
else ifneq (,$(wildcard /dev/ttyACM0))
SERIAL_DEV = /dev/ttyACM0
else
SERIAL_DEV = unknown
endif
endif
BUILD_DIR := $(subst :,.,build/$(FQBN))
SRC := $(wildcard *.ino)
HDRS := $(wildcard *.h)
BIN := $(BUILD_DIR)/$(SRC).bin
ELF := $(BUILD_DIR)/$(SRC).elf
$(info FQBN is [${FQBN}])
$(info IOT_NAME is [${IOT_NAME}])
$(info OTA_PORT is [${OTA_PORT}])
$(info OTA_PASS is [${OTA_PASS}])
$(info V is [${V}])
$(info VFLAG is [${VFLAG}])
$(info MAKE_DIR is [${MAKE_DIR}])
$(info BUILD_DIR is [${BUILD_DIR}])
$(info SRC is [${SRC}])
$(info HDRS is [${HDRS}])
$(info BIN is [${BIN}])
$(info SERIAL_DEV is [${SERIAL_DEV}])
all: $(ELF) upload
.PHONY: all
compile: $(ELF)
.PHONY: compile
$(ELF): $(SRC) $(HDRS)
arduino-cli compile -b $(FQBN) $(VFLAG)
@if which arduino-manifest.pl; \
then echo "---> Generating manifest.txt"; \
arduino-manifest.pl -b $(FQBN) $(SRC) $(HDRS) > manifest.txt; \
else echo "---> If you want to generate manifest.txt, listing used libraries and their versions,"; \
echo "---> please install arduino-manifest, see https://github.com/digiampietro/arduino-manifest"; \
fi
upload: compile
@if [ ! -c $(SERIAL_DEV) ] ; \
then echo "---> ERROR: Serial Device not available, please set the SERIAL_DEV environment variable" ; \
else echo "---> Uploading sketch\n"; \
arduino-cli upload -b $(FQBN) -p $(SERIAL_DEV) $(VFLAG); \
screen $(SERIAL_DEV) 115200;\
fi
ota:
@PLAT_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.platform.path' | awk -F= '{print $$2}'` ; \
PY_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.tools.python3.path' | awk -F= '{print $$2}'` ; \
IOT_IP=`avahi-browse _arduino._tcp --resolve --parsable --terminate|grep -i ';$(IOT_NAME);'|grep ';$(OTA_PORT);'| awk -F\; '{print $$8}'|head -1`; \
BINFILE=$(wildcard $(BUILD_DIR)/$(SRC)*bin); \
echo "PLAT_PATH is [$$PLAT_PATH]" ; \
echo "PY_PATH: is [$$PY_PATH]" ; \
echo "IOT_IP: is [$$IOT_IP]" ; \
echo "BINFILE: is [$$BINFILE]" ; \
if [ "$$IOT_IP" = "" ] ; \
then echo "Unable to find device IP. Check that the IOT_NAME environment variable is correctly set. Use 'make find' to search devices"; \
else echo "---> Uploading Over The Air"; \
$$PY_PATH/python3 $$PLAT_PATH/tools/espota.py -i $$IOT_IP -p $(OTA_PORT) --auth=$(OTA_PASS) -f $$BINFILE ;\
fi
clean:
@echo "---> Cleaning the build directory"
rm -rf build
find:
avahi-browse _arduino._tcp --resolve --parsable --terminate
requirements:
@if [ -e requirements.txt ]; \
then while read -r i ; do echo ; \
echo "---> Installing " '"'$$i'"' ; \
arduino-cli lib install "$$i" ; \
done < requirements.txt ; \
else echo "---> MISSING requirements.txt file"; \
fi

647
esp32_radio/esp32_radio.ino Normal file
View File

@@ -0,0 +1,647 @@
#include "wifiinfo.h"
#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"
#include "WebServer.h"
#include "EEPROM.h"
#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRC 26
#define STATUSLED 32
#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};
String ssid = WIFI_SSID;
String password = WIFI_PASSWD;
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();
// 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.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");
}
void loop()
{
audio.loop();
server.handleClient();
volumeAdjust();
powerButton();
// Indicateur LED pour mode configuration
if (configMode) {
configModeLEDIndicator();
}
}
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);
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() {
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() {
Serial.println(".");
Serial.println("WiFi connecte");
Serial.println("Adresse IP: ");
Serial.println(WiFi.localIP());
}
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(wifiCreds.hostname);
WiFi.begin(wifiCreds.ssid, wifiCreds.password);
Serial.print("Connexion au reseau ");
Serial.println(wifiCreds.ssid);
int timeout_counter = 0;
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(STATUSLED, LOW);
delay(500);
Serial.print(".");
digitalWrite(STATUSLED, HIGH);
delay(500);
timeout_counter++;
if (timeout_counter > CONNECTION_TIMEOUT){
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) {
Serial.print("info "); Serial.println(info);
}
void audio_id3data(const char *info) { //id3 metadata
Serial.print("id3data "); Serial.println(info);
}
void audio_eof_mp3(const char *info) { //end of file
Serial.print("eof_mp3 "); Serial.println(info);
}
void audio_showstation(const char *info) {
Serial.print("station "); Serial.println(info);
}
void audio_showstreaminfo(const char *info) {
Serial.print("streaminfo "); Serial.println(info);
}
void audio_showstreamtitle(const char *info) {
Serial.print("streamtitle "); Serial.println(info);
}
void audio_bitrate(const char *info) {
Serial.print("bitrate "); Serial.println(info);
}
void audio_commercial(const char *info) { //duration in sec
Serial.print("commercial "); Serial.println(info);
}
void audio_icyurl(const char *info) { //homepage
Serial.print("icyurl "); Serial.println(info);
}
void audio_lasthost(const char *info) { //stream URL played
Serial.print("lasthost "); Serial.println(info);
}
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();
}

View File

@@ -0,0 +1,8 @@
#ifndef CREDENTIALS_H
#define CREDENTIALS_H
// Replace with your actual SSID and password:
#define WIFI_SSID "Your SSID here"
#define WIFI_PASSWD "WLAN AP password here"
#endif

9
wifiinfo.h.sample Normal file
View File

@@ -0,0 +1,9 @@
#ifndef CREDENTIALS_H
#define CREDENTIALS_H
// Replace with your actual SSID and password:
#define WIFI_SSID "Your SSID here"
#define WIFI_PASSWD "WLAN AP password here"
#define HOSTNAME "hostname"
#endif