simpleWebRadioBt.ino


// this is the same example as Webradio_I2S.ino only with an IR remote control
// Author: Wolle (schreibfaul1)
// Changed for use with datalink: Kurt Nielsen kurtn.nypost@hotmail.dk

#include <Arduino.h>
#include <Preferences.h>
#include <SPI.h>
#include <WiFiMulti.h>
#include "Audio.h" //see my repository at github "https://github.com/schreibfaul1/ESP32-audioI2S"
#include "IR.h"    //see my repository at github "https://github.com/schreibfaul1/ESP32-IR-Remote-Control"
#include "BluetoothA2DPSink.h"  //https://github.com/pschatzmann/ESP32-A2DP

BluetoothA2DPSink a2dp_sink;

#define I2S_DOUT      22
#define I2S_BCLK      14
#define I2S_LRC       15
#define IR_PIN        33


Preferences pref;

Audio audio;

WiFiMulti wifiMulti;

IR ir(IR_PIN);  // do not change the objectname, it must be "ir"


String ssid =     "********";
String password = "********";

String stations[] ={
        "",
        "live-icy.gslb01.dr.dk:8000/A/A03H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A04H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A05H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A08H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A25H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A29H.mp3",
        "live-icy.gslb01.dr.dk:80/A/A22H.mp3",
        "live-bauerdk.sharp-stream.com/nova128.mp3",
        "live-bauerdk.sharp-stream.com/myrock.mp3",
};

//some global variables

uint8_t max_volume   = 21;
uint8_t max_stations = 0;   //will be set later
uint8_t cur_station  = 0;   //current station(nr), will be set later
uint8_t cur_volume   = 0;   //will be set from stored preferences
int8_t  cur_btn      =-1;   //current button (, -1 means idle)
uint32_t res = 0;
uint8_t        _state          = 0;
boolean        _f_irNumberSeen = false;
boolean bluetooth = false;

enum action{VOLUME_UP=0, VOLUME_DOWN=1, STATION_UP=2, STATION_DOWN=3};
enum staus {RELEASED=0, PRESSED=1};

void volume_up(){
    if(cur_volume < max_volume){
        cur_volume++;
        audio.setVolume(cur_volume);
        pref.putShort("volume", cur_volume);} // store the current volume in nvs
}
void volume_down(){
    if(cur_volume>0){
        cur_volume-- ;
        audio.setVolume(cur_volume);
        pref.putShort("volume", cur_volume);} // store the current volume in nvs
}
void station_up(){
    if(cur_station < max_stations-1){
        cur_station++;
        audio.connecttohost(stations[cur_station].c_str());
        pref.putShort("station", cur_station);} // store the current station in nvs
}
void station_down(){
    if(cur_station > 0){
        cur_station--;
        audio.connecttohost(stations[cur_station].c_str());
        pref.putShort("station", cur_station);} // store the current station in nvs
}


//**************************************************************************************************
//                                           S E T U P                                             *
//**************************************************************************************************
void setup() {   
    max_stations= sizeof(stations)/sizeof(stations[0]); log_i("max stations %i", max_stations);
    Serial.begin(115200);
    pref.begin("WebRadio", false);  // instance of preferences for defaults (station, volume ...)
    if(pref.getShort("volume", 1000) == 1000){ // if that: pref was never been initialized
        pref.putShort("volume", 15);
        pref.putShort("station", 1);
    }
    else{ // get the stored values
        cur_station = pref.getShort("station");
        cur_volume = 20;//pref.getShort("volume");
    }
    if (pref.getShort("volume") > 0)
      a2dp_sink.start("MyMusic");
    wifiMulti.addAP(ssid.c_str(), password.c_str());
    if(psramFound()) WiFi.useStaticBuffers(true);
    wifiMulti.run();
    ir.begin();  // Init InfraredDecoder
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(cur_volume); // 0...21
    audio.connecttohost(stations[cur_station].c_str());
       
}
//**************************************************************************************************
//                                            L O O P                                              *
//**************************************************************************************************
void loop()
{
    audio.loop();
    ir.loop();
           
}
//**************************************************************************************************
//                                           E V E N T S                                           *
//**************************************************************************************************
void audio_info(const char *info){
    Serial.print("audio_info: "); Serial.println(info);
}

// Events from IR Library

void ir_res(uint32_t res){
    if(res < max_stations){
        cur_station = res;               
        audio.connecttohost(stations[cur_station].c_str());
        pref.putShort("station", cur_station);} // store the current station in nvs
    else{       
        audio.connecttohost(stations[cur_station].c_str());
    }
}

void ir_key(const char* key){
    switch(key[0]){
        case 'k':   {audio.connecttohost(stations[cur_station].c_str());   
                    pref.putShort("volume", 0);
                    ESP.restart();}       break; // OK
        case 'r':   {cur_station = 7;       
                    audio.connecttohost(stations[cur_station].c_str());}    break; // right
        case 'l':   {cur_station = 5;       
                    audio.connecttohost(stations[cur_station].c_str());}    break; // left
        case 'u':   station_up();   break; // up
        case 'd':   station_down(); break; // down
        case '#':   {audio.connecttohost(stations[0].c_str());
                    pref.putShort("volume", 1);
                    audio.stopSong();
                    ESP.restart();                   
                    }   break; // #
        case '*':   audio.connecttohost(stations[0].c_str());    break; // *
        default:    break;
    }
}

//Bemærk: dette er eksperimentel software der er ingen garanti for brugbarhed, det kan derimod sandsynligvis være skadeligt, kun til brug i Danmark.