150 lines
3.5 KiB
C++
150 lines
3.5 KiB
C++
#include "wifiinfo.h"
|
|
#include "Arduino.h"
|
|
#include "WiFi.h"
|
|
#include "Audio.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;
|
|
|
|
#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;
|
|
|
|
void setup() {
|
|
pinMode(STATUSLED, OUTPUT);
|
|
pinMode(POWERBUTTON, INPUT);
|
|
|
|
esp_sleep_enable_ext0_wakeup(POWERBUTTON, 1);
|
|
|
|
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();
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
void loop()
|
|
|
|
{
|
|
audio.loop();
|
|
volumeAdjust();
|
|
powerButton();
|
|
|
|
}
|
|
|
|
void volumeAdjust () {
|
|
volumeresistor = analogRead(VOLUMERES);
|
|
// Serial.println(volumeresistor);
|
|
audio.setVolume(volumeresistor*30/4095);
|
|
}
|
|
|
|
void powerButton () {
|
|
powerButtonState = digitalRead(POWERBUTTON);
|
|
if (powerButtonState == 1) {
|
|
Serial.println("Deepsleep mod");
|
|
delay(500);
|
|
esp_deep_sleep_start();
|
|
}
|
|
}
|
|
|
|
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();
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.setHostname(hostname.c_str());
|
|
WiFi.begin(ssid.c_str(), password.c_str());
|
|
|
|
Serial.print("Connexion au reseau ");
|
|
Serial.println(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){
|
|
ESP.restart();
|
|
}
|
|
}
|
|
WiFi.config(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask(), IPAddress(192,168,2,1));
|
|
|
|
}
|
|
|
|
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);
|
|
}
|