Skip to content

Communication HTTP Server

pschatzmann edited this page Oct 22, 2026 · 1 revision

With an HTTP server on your microcontroller you can listen to its audio with any web browser, VLC, ffplay or a media player app - no wires, no special software. See Communication for the general considerations.

Contents

Which Server?

Class Header Provides Clients
AudioWAVServer AudioTools/Communication/AudioHttp.h PCM as WAV one at a time
AudioEncoderServer AudioTools/Communication/AudioHttp.h PCM encoded with any encoder (MP3, AAC, ADPCM, Opus/Ogg...) one at a time
AudioServer AudioTools/Communication/AudioHttp.h any data as it is, with the content type you define one at a time
AudioServerEx, AudioWAVServerEx AudioTools/Communication/AudioServerEx.h any data / WAV, via TinyHttp several at the same time

The first three are part of this library and use the WiFi library; AudioServerEthernet, AudioEncoderServerEthernet and AudioWAVServerEthernet (HTTP/AudioServerEthernet.h) do the same over Ethernet. They are templates (AudioServerT<Client, Server> ...), so other network stacks work as well.

AudioWAVServer

The simplest case: provide PCM audio as WAV, which every player understands. From streams-generator-webserver_wav.ino:

#include "AudioTools.h"
#include "AudioTools/Communication/AudioHttp.h"

AudioWAVServer server("ssid", "password");
SineGenerator<int16_t> sineWave;
GeneratedSoundStream<int16_t> in(sineWave);

void setup() {
  Serial.begin(115200);
  server.begin(in, 10000, 1);  // stream, sample rate, channels
  sineWave.begin(1, 10000, N_B4);
  in.begin();
}

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

The server connects to WiFi and prints its address; open http://<ip-address>/ in a browser. begin() also accepts an AudioInfo, or an AudioStream whose format it takes over.

AudioEncoderServer

WAV needs a lot of bandwidth (see Communication). An AudioEncoderServer encodes the PCM data with the encoder you pass to it. From streams-generator-webserver_mp3.ino:

#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3LAME.h"
#include "AudioTools/Communication/AudioHttp.h"

AudioInfo info(24000, 1, 16);
MP3EncoderLAME mp3;
AudioEncoderServer server(&mp3, "ssid", "password");
SineGenerator<int16_t> sineWave;
GeneratedSoundStream<int16_t> in(sineWave);

void setup() {
  server.begin(in, info);
  sineWave.begin(info, N_B4);
}

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

The content type comes from the encoder's mime(). There are examples for AAC, ADPCM, MP3, Ogg and WAV, with a generator, a microphone (AudioKit, I2S) or text to speech as source.

AudioServer

AudioServer sends whatever it gets, with the content type you define - e.g. MP3 data that is already encoded:

AudioServer server("ssid", "password");
...
server.begin(mp3_stream, "audio/mpeg");

Instead of a stream you can pass a callback void cb(Print *out) that writes the reply, e.g. a complete file: server.begin(cb, "audio/mpeg").

More options:

Method Description
copy() sends the next data to the client, and accepts a new client - call it in loop()
setChunked(bool) chunked transfer encoding (default true)
setMaxOutputSize(bytes) sends at most this many bytes, with a Content-Length header
setConverter(converter) converts the data before sending
isClientConnected() true while a client is connected

The server switches off WiFi power save when it connects to WiFi.

AudioServerEx: several clients

AudioServerEx and AudioWAVServerEx are based on TinyHttp (install it) and serve several clients at the same time with the same audio. They are AudioOutputs: you write the audio to them. From streams-generator-webserverex_wav.ino:

#include "AudioTools.h"
#include "AudioTools/Communication/AudioServerEx.h"

AudioInfo info(10000, 1, 16);
SineGenerator<int16_t> sineWave;
GeneratedSoundStream<int16_t> in(sineWave);
AudioWAVServerEx server;
StreamCopy copier(server, in);

void setup() {
  copier.setCheckAvailableForWrite(true);
  auto cfg = server.defaultConfig();
  cfg.copyFrom(info);
  cfg.ssid = "ssid";
  cfg.password = "password";
  server.begin(cfg);
  sineWave.begin(info, N_B4);
  in.begin();
}

void loop() {
  copier.copy();  // audio to the server
  server.copy();  // server to the clients
}

Things to consider

  • Browsers play WAV, MP3, AAC and Ogg/Opus. Some players start the playback only after they have buffered a few seconds.
  • A source that is faster than real time (generator, file) is only paced by the TCP connection: the client receives data as fast as it can play it. That's fine for one client; for a live source at the audio rate (microphone, I2S) nothing needs to be done.
  • Video: the same approach streams camera video with sound - see Streaming Video and Audio via a Webserver.
  • Receiving an HTTP stream on a microcontroller: see HTTP Client.

Clone this wiki locally