Arduino program for internet radio using VS1003 and ESP32

Arduino program for Internet radio using VS1003 and ESP32.



#include <VS1053.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#define VS1053_CS     D1
#define VS1053_DCS    D0
#define VS1053_DREQ   D3
#endif

//////////////////////////////////////////////////////
/////        VS1053 - ESP32 Connections         //////
//////////////////////////////////////////////////////
////////////  VS1053_CS - ESP32 G5     ///////////////
////////////  VS1053_DCS - ESP32 G16   ///////////////
////////////  VS1053_DREQ - ESP32 G4   ///////////////
////////////  VS1053_SCLK - ESP32 G18  ///////////////
////////////  VS1053_MOSI - ESP32 G23  ///////////////
////////////  VS1053_MISO - ESP32 G19  ///////////////
////////////  VS1053_XRST - ESP32 EN   ///////////////
//////////////////////////////////////////////////////

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#define VS1053_CS     5
#define VS1053_DCS    16
#define VS1053_DREQ   4
#endif

#define QSIZ 3000


// Default volume
#define VOLUME  80

uint8_t chunkReadSize;
uint8_t tmpBuff[6000];

uint8_t taskOneCount = 0;

uint32_t delayCounter = 0;

//uint32_t chunkSize = 32;
//uint32_t qSpace;

QueueHandle_t dataqueue ;

struct qdata_struct
{
  //int datatyp ;                                      // Identifier
  __attribute__((aligned(4))) uint8_t buf[32] ;        // Buffer for chunk
} ;

qdata_struct outchunk, inchunk ;

uint8_t* structBufPtr = outchunk.buf;

VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
WiFiClient client;

// WiFi settings example, substitute your own
const char *ssid = "HABEROCEAN";
const char *password = "Aquaticdaisy900@haberocean";

//  http://comet.shoutca.st:8563/1

//  High KBPS radio
/*
const char *host = "airspectrum.cdnstream1.com";
const char *path = "/1648_128";
int httpPort = 8114;
*/
//  Sehion radio
/*
const char *host = "199.195.194.140";
const char *path = "/stream";
int httpPort = 8012;
*/

// RADIO STATION 3

const char *host = "radiostreaming.ert.gr";
const char *path = "/ert-kosmos";
int httpPort = 80;


// The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
uint8_t mp3buff[32];

void taskOne( void * parameter )
{

  delay(5000);
  
  while(true) {
    if ( xQueueReceive ( dataqueue, &inchunk, 5 ) ) {
      player.playChunk(inchunk.buf, sizeof(inchunk.buf));
      uint32_t queueSpacesAvailable = uxQueueSpacesAvailable( dataqueue );
      Serial.println(queueSpacesAvailable);

/*
      if(delayCounter++ == 3000)
        delayCounter = 0;

      if(delayCounter % 10 ==  0)
        delayMicroseconds(3000);

      else
        delayMicroseconds(1700);
*/      

      if(queueSpacesAvailable < 850)
        delayMicroseconds(900); 
      else  
        delayMicroseconds(queueSpacesAvailable * 2);
    }
    //delay(1000);
  }
}

void handleByte(uint8_t recByte) {
  //Serial.println(recByte);

  *structBufPtr = recByte;
  structBufPtr++;

  if(structBufPtr == (outchunk.buf + sizeof(outchunk.buf))) {
    xQueueSend ( dataqueue, &outchunk, 200 );
    structBufPtr = outchunk.buf;
  }
}
 
void setup() {
 
    Serial.begin(115200);

    // Wait for VS1053 and PAM8403 to power up
    // otherwise the system might not start up correctly
    delay(3000);

    // This can be set in the IDE no need for ext library
    // system_update_cpu_freq(160);

    Serial.println("\n\nSimple Radio Node WiFi Radio");

    SPI.begin();

    player.begin();
    player.switchToMp3Mode();
    player.setVolume(VOLUME);

    Serial.print("Connecting to SSID ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.print("connecting to ");
    Serial.println(host);

    if (!client.connect(host, httpPort)) {
        Serial.println("Connection failed");
        return;
    }

    Serial.print("Requesting stream: ");
    Serial.println(path);

    // delay(1000);

    client.print(String("GET ") + path + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    dataqueue = xQueueCreate ( QSIZ, sizeof ( qdata_struct ) ) ;

    xTaskCreatePinnedToCore(
             taskOne,          
             "TaskOne",        
             10000,            
             NULL,             
             1,               
             NULL, 1);            
}
 
void loop() {
  


    //if (client.available() > 0) {

    uint32_t chunkSize = sizeof(tmpBuff);
    uint32_t bytesAvailable = client.available();
    uint32_t qSpace = uxQueueSpacesAvailable( dataqueue ) * sizeof(qdata_struct) ;

    if(bytesAvailable < chunkSize) {
      chunkSize = bytesAvailable;
    }

    if(chunkSize > qSpace) {
       chunkSize = qSpace;
    }

    if(chunkSize > 0) {
    int bytesRead = client.read(tmpBuff, chunkSize);

    //Serial.println(bytesAvailable);
    
    //player.playChunk(mp3buff, bytesread); 
    //outchunk.buf = mp3buff;

    //Serial.println(bytesread);
    //xQueueSend ( dataqueue, &outchunk, 200 );

    for(int i=0;i<bytesRead;i++) {
      //if(tmpBuff[i] != '\0') {
        handleByte(tmpBuff[i]);
        //delayMicroseconds(20);
        //delayMicroseconds(150); // for sehion radio
      //}
      //vTaskDelay(1 / portTICK_PERIOD_MS);
      
    }

        //delay(500);
    
/*
    Serial.print(bytesAvailable);
    Serial.print("\t");
    Serial.print(chunkSize);
    Serial.print("\t");
    Serial.println(qSpace);
*/
        //Serial.println(client.available());
    //}
    }
}
 
0 0 vote
Article Rating

Published by

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
X