The following classes can be used as HTTP client and implement the AbstractURLStream API.
All these classes are dependent on the Arduino Networking API:

  • URLStream: Supports HTTP GET, POST, PUT and DELETE
  • ICYStream: Supports the ICY protocol
  • URLStreamBuffered: Using buffer that is filled by a task
  • ICYStreamBuffered: Using buffer that is filled by a task

I thought that it would be cool to have this functionality also working for the ESP32 outside of Arduino. The following new implementations just depend on the ESP32 IDF Framework:

  • URLStreamESP32
  • ICYStreamESP32
  • URLStreamBufferedESP32
  • ICYStreamBufferedESP32

I restructured the code quite a bit to avoid any code duplication…

Example HTTP Sketch

HTTP is recommended over HTTPS because it is leaner and much more efficient.
Here is a simple example sketch:

#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"

URLStreamESP32 url("ssid","password");  // or replace with ICYStream to get metadata
I2SStream i2s; // final output of decoded stream
MP3DecoderHelix mp3;
EncodedAudioStream dec(&i2s, &mp3); // Decoding stream
StreamCopy copier(dec, url); // copy url to decoder

void setup(){
  Serial.begin(115200);
  AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);  

  // setup i2s
  auto config = i2s.defaultConfig(TX_MODE);
  i2s.begin(config);

  // setup I2S based on sampling rate provided by decoder
  dec.begin();

  // mp3 radio
  if (!url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3")){
    stop();
  }
}

void loop(){
  copier.copy();
}

Example HTTPS Sketch

In order to support https, just change the url and provide the top level certificate

  // mp3 https radio
  url.setCACert(root_ca);
  if (!url.begin("https://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3")){
    stop();
  }

Determining the Certificate

To determine the cetificate execute the following command:

openssl s_client -showcerts -connect stream.srg-ssr.ch:443 </dev/null

You can include all certificates, but the relevant one is only the last!

Format the Certificate

This needs to be converted into the following format so that it can be provided as a C++ variable:

// openssl s_client -showcerts -connect stream.srg-ssr.ch:443 </dev/null
const char root_ca[] = 
 "-----BEGIN CERTIFICATE-----\n" 
 "MIIEADCCAuigAwIBAgIBADANBgkqhkiG9w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dEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQAD\n" 
 "ggEBADJL87LKPpH8EsahB4yOd6AzBhRckB4Y9wimPQoZ+YeAEW5p5JYXMP80kWNy\n" 
 "OO7MHAGjHZQopDH2esRU1/blMVgDoszOYtuURXO1v0XJJLXVggKtI3lpjbi2Tc7P\n" 
 "TMozI+gciKqdi0FuFskg5YmezTvacPd+mSYgFFQlq25zheabIZ0KbIIOqPjCDPoQ\n" 
 "HmyW74cNxA9hi63ugyuV+I6ShHI56yDqg+2DzZduCLzrTia2cyvk0/ZM/iZx4mER\n" 
 "dEr/VxqHD3VILs9RaRegAhJhldXRQLIQTO7ErBBDpqWeCtWVYpoNz4iCxTIM5Cuf\n" 
 "ReYNnyicsbkqWletNw+vHX/bvZ8=\n" 
 "-----END CERTIFICATE-----\n\0";

Make sure that you add a traling null (with \0).
By declaring this as const, the data will end up in PROGMEM!

Deactivating SSL Checks in IDF

To completely disable the certificate check, you will need to go to ESP-TLS
in menuconfig, enable “Allow potentially insecure options” and then enable
“Skip server certificate verification by default” (accepting risks).

Dependencies

You need to install the following libraries:


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *