// Variation of the standard ESP32 radio configured specifically for Planet Rock which needs a key
// that expires in 24 hours. This downloads the current Planet Rock playlist from radiofeeds.co.uk
// The raw html response includes headers etc so the required url+key is parsed, 6 characters in
// Wifi presently uses fixed SSID - next job is to reinstate wifimulti (or code it to switch SSID's from a list)
// Phil_G Nov 25
//
#include "Arduino.h"
//#include "WiFiMulti.h"
#include "Audio.h"
#include "SPI.h"
#include "SD.h"
#include "FS.h"
#include <HTTPClient.h>
#include <WiFi.h>

// Digital I/O used
#define SD_CS          5
#define SPI_MOSI      23
#define SPI_MISO      19
#define SPI_SCK       18
#define I2S_DOUT      25
#define I2S_BCLK      27
#define I2S_LRC       26
#define BLUE           2
#define volumePin     33

Audio audio;
WiFiMulti wifiMulti;
WiFiClient client;

const char* url      = "http://www.radiofeeds.net/playlists/bauer.pls?station=planetrock-mp3";
int linecount        = 0;
const char* ssid     = "My-wifi-SSID";
const char* password = "My-wifi-password";

const char* host     = "www.radiofeeds.net";                           // Domain only, no http://
const int port       = 80;                                             // Use 443 for HTTPS (with WiFiClientSecure)
const char* path     = "/playlists/bauer.pls?station=planetrock-mp3";  // Path from the URL
String line, encryptString;

void setup() {
  pinMode(BLUE, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
  digitalWrite(BLUE, HIGH);

  Serial.print("Connecting to ");
  Serial.print(host);
  Serial.print(":");
  Serial.println(port);

  if (client.connect(host, port)) {
    Serial.println("Connected to host");

    // pull the html page (the playlist file)
    client.println(String("GET ") + path + " HTTP/1.0");
    client.println(String("Host: ") + host);
    client.println("Connection: close");
    client.println();  // Blank line ends headers

    Serial.println("\nResponse to HTTP GET:");

    // Read response line by line, around line 14 is the url+key
    while (client.connected() || client.available()) {
      ++linecount;
      line = client.readStringUntil('\n');
      Serial.print("Line:");
      Serial.print(linecount);
      Serial.print(" ");
      Serial.println(line);
      if (line.indexOf("=http") != -1) encryptString = line.substring(6); // from character 6 onwards is the url
    }

    client.stop();
    Serial.println("Connection closed");
  } else {
    Serial.println("Cannot connect");
  }

  Serial.println(encryptString);

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.connecttohost(encryptString.c_str()); //  128k mp3 stream
}

void loop()
{
  audio.setVolume(analogRead(volumePin) / 188); // *** NOTE HOT END OF VOLUME POT IS 3v3 ***
  audio.loop();
}
