Recently a user was trying to use http to post some audio to be played on a microcontroller with the help of my Audio Tools. This project contains some simple http server implementation, but only for playing audio that is generated by a microcontroller on a webbrowser.

However, quite some time ago, I also implemented some extended server functionality in my TinyHttp project and I am using that one now to demonstrate how this can be done.

Arduino Sketch

Here is the sketch that plays any posted mp3 file:

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

I2SStream i2s; // final output of decoded stream
EncodedAudioStream dec(&i2s, new MP3DecoderHelix()); // Decoding stream
StreamCopy copier;
const char *ssid = "SSID";
const char *password = "PASSWORD";
WiFiServer wifi;
HttpServer server(wifi);

void setup() {
  AudioLogger::instance().begin(Serial, AudioLogger::Info);  
  HttpLogger.begin(Serial, tinyhttp::Debug);


  auto playMP3 = [](HttpServer *server_ptr, const char *requestPath,
                   HttpRequestHandlerLine *hl) {
    LOGI("Playing mp3");
    copier.begin(dec, server_ptr->client());

  server.on("/play", T_POST, playMP3);
  server.begin(80, ssid, password);
  HttpLogger.log(tinyhttp::Info, "server was started...");

void loop() { server.doLoop(); }

I am starting the decoding stream, i2s and the configured server. The critical thing here is to get access to the client object which contains the file data as Arduino stream: thus we can just copy the data to the decoding stream which is very lean and memory efficient.

You can test e.g. with curl -F "file=@test.mp3;type=audio/mp3" -X POST

My server implementation is not really optimized for this scenario, so it is blocking until the whole file is played, but a similar processing logic should be possible in any other Arduino Server implementation…



Leave a Reply

Avatar placeholder

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