esp8266 opensound control

Here some example Arduino code for sending and receiving OSC via the cheap ESP8266 serial WiFi module.

Note that the Open Sound Control messages here are very basic - only 4 bytes packed into a single 32bit integer.

* upload the code below to an Arduino.

* connect ESP8266 TX pin to Arduino pin0.

* connect ESP8299 RX to Arduino pin1. It is safest to use a 3V3 lever converter for this line (or at least a voltage divider).

* power the ESP8266 (VCC and GND) from an external 3V source. Do not use the Arduino 3V3 pin as it cannot provide the required current. I used a LF33CV voltage regulator to get 3.3V from the 5V supply that also powers the Arduino.

* connect ESP8288 RESET pin to Arduino pin4.

* and last connect ESP8266 CH_PD to 3V3

Optional: connect a separate usb-serial (FTDI) chip to Arduino pins 2 and 3 to use software serial debugging. Start debugging in terminal with something like screen /dev/tty.usbserial-A4015TKA 115200

The Arduino code sits and waits for an incoming OSC message (/tap). It then replies by sending out a counter message (/sti).

//f0 150705
//sending and receiving udp osc with an esp8266
//for an arduino + esp8266 with firmare

#include <SoftwareSerial.h>

#define WLAN_SSID  "SSID"
#define WLAN_PASS  "PASS"
#define WLAN_ADDR  "" //laptop running sc
#define PORT  1112 //incoming osc port
String tag = "/tap"; //incoming osc addy

SoftwareSerial mySerial(2, 3);

uint8_t buf[16];
byte cnt;
byte id, on, hi, lo;
boolean resp;

void setup() {

  //--osc message
  buf[0] = 47;   // /
  buf[1] = 115;  // s
  buf[2] = 116;  // t
  buf[3] = 105;  // i
  buf[4] = 0;
  buf[5] = 0;
  buf[6] = 0;
  buf[7] = 0;
  buf[8] = 44;   // ,
  buf[9] = 105;  // i
  buf[10] = 0;
  buf[11] = 0;
  buf[12] = 4;   // a high   (id)
  buf[13] = 3;   // a low    (on)
  buf[14] = 2;   // b high   (hi)
  buf[15] = 0;   // b low    (lo)


  mySerial.print("hard reset...");
  digitalWrite(4, 0);
  pinMode(4, OUTPUT);
  pinMode(4, INPUT);
  resp = Serial.find("ready\r\n");

  resp = Serial.find("OK\r\n");

  do {
    resp = Serial.find("OK\r\n");
  } while (!resp);

  resp = Serial.find("OK\r\n");

  resp = Serial.find("OK\r\n");

void loop() {
  while (Serial.available()) {
    String abc = Serial.readStringUntil('\n');
    if (abc.startsWith("+IPD,4,16:" + tag)) {
      id = abc[22];
      on = abc[23];
      hi = abc[24];
      lo = abc[25];

      buf[15] = cnt++;
      Serial.write(buf, sizeof(buf));
      resp = Serial.find("OK\r\n");

SuperCollider test code:

var last= Main.elapsedTime;
OSCFunc({|msg, time, addr|
        [\id, msg[1]>>24, \on, (msg[1]>>16)&255, \hi, (msg[1]>>8)&255, \lo, msg[1]&255, time-last, addr].postln;
        last= time;
}, \sti);
n= NetAddr("", 1112); //esp8266 ip address
f= {|id, on, hi, lo| (id&255<<24)|(on&255<<16)|(hi&255<<8)|(lo&255)};
                n.sendMsg(\tap, f.value(4, 1, i.asInteger>>8&255, i.asInteger%256));

Note: my new and better way to do this is described here


For a project in collaboration with Stine Janvin Motland I built this 4-channel transducer bass shaker system.

The system has four transducers (Visaton bs 130, 4Ω), two class D stereo amplifiers (2x50W, TDA7492 chip) and a powerful ATX switching power supply (Codecom pm-350c).

I modified the power supply to only give out 12V (yellow&black cables) and also made it start up automatically by shorting the green cable (PS-ON) to ground (black).

transducers 1

transducers 2

There's no volume control so better take care - the system is very hot.

video documentation

pd on raspberry pi

Here is a quick tutorial on how to install and run Pure Data headless on a Raspberry Pi. The instructions assume you want to start with a new clean [Raspbian] system image and do it all from scratch.
The instructions also assume you have a Raspberry model B, an USB soundcard like Terratec's Aureon Dual USB and an ethernet cable with internet access (just connect the ethernet cable between your RPi and your home router).
What you get after following the below tutorial is a SD card with a Pd patch that automatically starts when the RPi is booted.

* Put the Raspbian image onto a +4Gb SD card (it is easily done with balenaEtcher).
* On newer versions of Rasbian, activate SSH by creating an empty file called 'ssh' directly on the SD card
* Insert the SD card+ethernet+usbsoundcard and power up the RPi
* Open the terminal application on your laptop and type...
$ ssh pi@raspberrypi.local #log in from laptop. Password is 'raspberry'. (See notes below if fail)

$ sudo raspi-config #run this on the RPi and do the following system configurations
* Select expand filesystem (only needed on Wheezy and older versions of Raspbian)
* Change user password
* Optionally lower the GPU memory under advanced / memory split
* Select finish and reboot

$ ssh pi@raspberrypi.local #log in again from laptop with your new password
$ sudo apt-get update #on the RPi. Check for new updates
$ sudo apt-get upgrade #update any old packages
$ sudo apt-get dist-upgrade #update the distribution

//--Test sound
$ lsusb #should list the USB soundcard
$ aplay -l #should also list the soundcard
$ sudo speaker-test -t sine -c 2 -Ddefault:CARD=Device #should sound if headphones connected. Stop with ctrl+c
# Note: this assume that your usb soundcard name is Device - check what aplay and edit the CARD= in the line above if needed.

//--Install Pd
$ sudo apt-get install puredata #download and install Pure Data + required packages

//--Test Pd patches
Copy the following two example Pd patches (or download the attachments below) and save them on your laptop (here assume on the desktop). To copy Pd patches just paste the cryptic text into a plain text editor and save with .pd file extension.


#N canvas 1068 88 450 300 10;
#X obj 238 159 dac~;
#X obj 235 73 osc~ 400;
#X obj 289 73 osc~ 404;
#X msg 126 154 \; pd dsp 1;
#X obj 126 83 loadbang;
#X obj 126 123 del 100;
#X text 42 122 important ->;
#X obj 238 111 *~ 0.2;
#X obj 280 111 *~ 0.2;
#X connect 1 0 7 0;
#X connect 2 0 8 0;
#X connect 4 0 5 0;
#X connect 5 0 3 0;
#X connect 7 0 0 0;
#X connect 8 0 0 1;


#N canvas 1068 88 450 300 10;
#X obj 238 230 dac~;
#X msg 126 154 \; pd dsp 1;
#X obj 126 83 loadbang;
#X obj 126 123 del 100;
#X text 42 122 important ->;
#X obj 238 24 adc~;
#X obj 238 53 delwrite~ del1 500;
#X obj 238 123 delread~ del1 500;
#X obj 259 80 delwrite~ del2 750;
#X obj 280 144 delread~ del2 750;
#X obj 238 182 *~ 0.2;
#X obj 280 182 *~ 0.2;
#X connect 2 0 3 0;
#X connect 3 0 1 0;
#X connect 5 0 6 0;
#X connect 5 1 8 0;
#X connect 7 0 10 0;
#X connect 9 0 11 0;
#X connect 10 0 0 0;
#X connect 11 0 0 1;

//--Copy Pd files to RPi
$ exit #log out from the RPi
# run the two lines below on your laptop to copy the two example patches to your RPi. (This is also how you can transfer more Pd patches later on.)
$ scp ~/Desktop/testsines.pd pi@raspberrypi.local:/home/pi/
$ scp ~/Desktop/testmic.pd pi@raspberrypi.local:/home/pi/

//--Run Pure Data
$ ssh pi@raspberrypi.local #log in from laptop again
$ pd -stderr -nogui -verbose -audiodev 4 testsines.pd #stop with ctrl+c
# Note: if no sound test with different audiodev - 4 is usually the USB soundcard
$ pd -stderr -nogui -verbose -audiodev 4 testmic.pd #stop with ctrl+c
# Note: you will need to connect headphones or speakers for the first example to work. And some kind of audio input (e.g. electret mic or line-in from MP3 player) for the second example patch to work.

$ nano #creates a new file. Copy the two lines below into this new file.

pd -nogui -audiodev 4 /home/pi/testsines.pd

# save and exit with ctrl+o, return, ctrl+x
$ chmod +x #make the file executable
$ crontab -e #and add at the end...

@reboot /bin/bash /home/pi/

# again save and exit with ctrl+o, return, ctrl+x
$ sudo reboot #restarts the RPi. After booting the sine tones patch should have started automatically.

$ ssh pi@raspberrypi.local #log in from laptop once more
$ sudo pkill pd #stop Pd
$ sudo halt -p #turn off the RPi safely

* If you cannot log in and you get ssh: Could not resolve hostname raspberrypi.local, you might need to replace raspberrypi.local with the exact IP address of the RPi (e.g. ssh pi@ The exact address will vary and can be found in your router setup.
* Note: if you get WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! then run the command $ ssh-keygen -R raspberrypi.local to reset the SSH key.
* When ready with everything and you have the correct Pd patch autostarting you can [physically] lock the SD card. This will put it in no-write mode and possibly prolong its life (specially if you cut the power without properly turning off the system with sudo halt)
* If you experience audio dropouts you might try the suggestions here... Most important force USB1.1 and set CPU governor to performance mode.
* If you get ALSA output error Device or resource busy when trying to start Pd, then delay the ';pd dsp 1' message in your Pd patch with about 100 milliseconds.
* To remove the autostart just delete the file and go into cron again and remove the last line you added with crontab -e

update 160109: also works great on a Raspberry Pi 2 with 2015-11-21-raspbian-jessie.img
update 180102: updated for 2017-11-29-raspbian-stretch.img and 2017-11-29-raspbian-stretch-lite.img

Binary Data testsines.pd370 bytes
Binary Data testmic.pd522 bytes

supercollider firmata

clean-up: #57

Just cleaned up an example for SuperCollider and Arduino that I found on my computer. It is demonstrating the SCFirmata class by Eirik Arthur Blekesaune.

//how to read pin A0 with SCFirmata...

//for Arduino1.0.6 and SC3.6.6
//first in Arduino IDE:
//  * select File / Examples / Firmata / StandardFirmata
//  * upload this example to an arduino
//then in SC install the SCFirmata classes
//  * download zip file
//  * extract files and put them in your sc application support directory
//  * recompile sc

d= SerialPort.devices[0]; // or d= "/dev/tty.usbserial-A1001NeZ" - edit number (or string) to match your arduino
f= FirmataDevice(d);//if it works it should post 'Protocol version: 2.3' after a few seconds

Ndef(\snd, {|freq= 400, amp= 0.5|[freq, freq+4].lag(0.08), 0, amp.lag(0.08)).tanh}).play;
f.reportAnalogPin(0, true)      //start reading A0
f.analogPinAction= ({|num, val| [num, val].postln; Ndef(\snd).set(\freq, val.linexp(0, 1023, 400, 800))})//control freq
f.analogPinAction= ({|num, val| [num, val].postln; Ndef(\snd).set(\amp, val.linexp(0, 1023, 0.001, 1))})//control amp instead

f.reportAnalogPin(0, false)     //stop reading A0


clean-up: #56

Compared to generating a serial bitstream in audio, analysing and extract serial data from audio is much harder. The SuperCollider code below does it, but the program has limitations and is quite sensitive for noise.

The code takes a string, chops it up into groups of six 8bit bytes and generates a serial audio bitstream from that. Another part listens to this sound and tries to decode it. If it finds six full bytes it sends the result back to sclang via OSC where it is printed.
To test the example connect an audio cable directly from your computer's output to its input (preferably via a small mixer), or change the audioSerial SynthDef to use an internal audio bus. I can also imagine it could function with a mic next to the speakers - but i didn't test this.
If it only prints gibberish try with a different threshold setting, different volume on you computer or use a lower baud rate.

        var baudrate= 9600;
        SynthDef(\serialAudio, {|out= 0, amp= -0.5|                     //for sending out serial via audio
                var data= Control.names([\data]).kr(Array.fill(60, 0)); //max 6 bytes
                var src=, 0, Dseq(data), 2);
      , src*amp);
        SynthDef(\audioSerial, {|in= 0, thresh= 0.05|           //for receiving serial via audio
                var raw=; //here change to if trying internal audio bus
                var src= raw>thresh;
                var reading=, 1/baudrate*9), 1/baudrate/2, 1/baudrate/2);
                var osc=, baudrate/;
                var clock= (<;
                var index=, reading);
                var stopTrig= index>7;
                var data=, index>=#[7, 6, 5, 4, 3, 2, 1]);
                var byte= (1-data).sum{|x, i| 2**(6-i)*x};
      , '/data', byte);
        OSCFunc({|msg| msg[3]}, '/data');

var str= "hello supercollider!";
var baudrate= 9600;
                var data= bytes.collect{|x| [1]++(1-x.asBinaryDigits.reverse)++[0]}.flat;
                        Synth(\serialAudio, [\data, data]);

One can use this technique to communicate with another computer via audio. To communicate with a microcontroller (e.g. an Arduino), one needs additional electronics (amplification, rectification). Here's schematics for a bi-directional circuit for talking to a 5V Arduino.

This audio-to-serial technique was used to get input from RFID, touch and bend sensors in our Reflect installation. i.e. SuperCollider is running on an iPod Touch and receives all sensor data via audio from an ATmega168 microcontroller.


clean-up: #55

Another way (compared to FSK in my previous blog entry) of sending data via audio is to directly generate the serial bit stream using SuperCollider.

To test and learn about these things I first wrote and uploaded a very simple program to an Arduino board. The program just transmitted the bytes 128, 10, 20, 30, 40 and 50.

//arduino testcode
void setup() {
void loop() {

Then I connected the Arduino serial TX pin (pin1) to the audio line-in of my laptop (via a 1k + 10k voltage divider) and recorded the sound of the serial transmission.

I then analysed the sound by hand and wrote a little program in SuperCollider that could generate similar waveforms.

o= {|chr| [1]++(1-chr.asBinaryDigits.reverse)++[0]};
SynthDef(\serialAudio, {|amp= -0.5|     //for sending out serial via audio
        var data= Control.names([\data]).kr(Array.fill(60, 0));//max 6 bytes
        var src=, 0, Dseq(data), 2);     //baudrate, src*amp);
Synth(\serialAudio, [\data, [128, 10, 20, 30, 40, 50].collect{|c| o.value(c)}.flat, \amp, -0.5]);

This screenshot show the signal recorded from the Arduino in the first channel, and the SuperCollider generated one in the second.

After all this I could reverse the process, generate any serial data and send it back to the Arduino RX pin (pin0). A small amplifier circuit in between helped to get a more stable communication going.

This serial-to-audio technique was used to control the 24 leds (6 PWM channels) in our Reflect installation. i.e. SuperCollider is running on an iPod Touch and sends out serial audio to an ATmega168 microcontroller.

Here is another example that can fade a single led by sending serial commands over audio. Includes schematics for an amplifier circuit plus SuperCollider and Pure Data example code.

And for a more advanced (actually using a much better technique) example see here


clean-up: #54

For a few projects in the past I had to communicate data bidirectionally via sound. It involved for example hooking up a microcontroller to an iPod Touch (running SuperCollider) so that the device could read sensors and/or control leds. One successful method was to do Frequency-shift keying. See here for another blog entry on that.
I've published FSK Pure Data code for that before, and also some SuperCollider code for generating/encoding FSK, but never the decoding part I think.
So below is some old code I've cleaned up a bit. It just demonstrates sending audio via internal SuperCollider busses at a very low baudrate. For good baudrate and min/max frequency settings see here

s.latency= 0.05;
        SynthDef(\redFSKdecode, {|in= 20, thresh= 0.1, baudrate= 126, lo= 882, hi= 1764|
                var sig=, 1);
                var sigActive=, 0.01, 0.01)>thresh;
                var fre= 1/*sigActive);
                var trg=, lo+(baudrate*0.5), hi-(baudrate*0.5)); //0= lo, 1= hi
                var trgLo= trg<0.5;
                var trgHi= 1-trgLo;
                var trgCarr=, 1)>(1/baudrate*16); //when found more than 16bits high in a row
                var trgData=*trgCarr, trgCarr);
                var imp=, baudrate/;
                var writeTrg= (<;
                var writePos=, trgData);
                var writeVal=, trg);
                var ok, done= 1-trgData;
                var buf= LocalBuf(13).clear;
      , 0, Dbufwr(writeVal, buf, writePos, 0));
                ok= done*(, 0, Dbufrd(buf, 1, 0)))*, 0, Dbufrd(buf, 11, 0));
      , '/data',, 0, Dbufrd(buf, (2..9), 0)));
        SynthDef(\redFSKencode, {|out= 0, amp= 0.6, minFreq= 4900, maxFreq= 7350, invBaudrate= 0.008|
                var data= Control.names([\data]).ir(Array.fill(8, 0));
                var env=[1, 1, 0], [invBaudrate*(16+11), 0]), doneAction:2);
                var parity= data.sum+1;
                var freq=[invBaudrate*16]++invBaudrate.dup(11)), 0, Dseq(#[1, 0]++data++[parity%2, 1]));
                var src=*(maxFreq-minFreq)+minFreq, 0, amp);
      , src*env);

//test sending a simple counter
var baudrate= 126;
var lo= 882;
var hi= 1764;
        var byte;
        byte= msg[3..].sum{|x, i| 2**i*x};
}, '/data');{
        Synth(\redFSKdecode, [\in, 20]); //bus 20{|i|
                var byte= (i%256).asInteger;
                var data= byte.asBinaryDigits.reverse;
                        ("sending :"+byte).postln;
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 20, \invBaudrate, 1/baudrate]); //bus 20
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 0, \invBaudrate, 1/baudrate]); //monitor

//test a string of text
var baudrate= 126;
var lo= 882;
var hi= 1764;
        var byte;
        byte= msg[3..].sum{|x, i| 2**i*x};
}, '/data');{
        Synth(\redFSKdecode, [\in, 20]); //bus 20
        "hello supercollider!".do{|x|
                var byte= x.ascii.clip(0, 255);
                var data= byte.asBinaryDigits.reverse;
                        //("sending :"+x+byte+data).postln;
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 20, \invBaudrate, 1/baudrate]); //bus 20
                        Synth(\redFSKencode, [\minFreq, lo, \maxFreq, hi, \data, data, \out, 0, \invBaudrate, 1/baudrate]); //monitor

This FSK technique was used in many projects to control leds. e.g. most of the 3rd gen projects listed here are running SuperCollider on an iPod Touch with the 6 channels led brightness data being sent via FSK on one of the audio output channels.


clean-up: #53

Below some code for reading and parsing data from the Android app AndroSensor.
With this free app you can record a lot of different sensors in your Android phone (like GPS, accelerometer, mic, battery, light etc etc) and save it to a comma separated file (CSV). Later one can copy over the data file from the Android phone to a computer and read it in SuperCollider. the CSV files are normally stored on the SD card in a folder called AndroSensor.

By default the app only save and display data at slow update rates, so go into the AndroSensor's settings and change update interval to very fast and recording interval to for example 0.05 seconds (the fastest).


//--read the data - edit the path to your csv file
var path= "~/Desktop/Sensor_record_20141018_115750_AndroSensor.csv"; //edit here
var data=, delimiter:$;, startRow:1);
var data2= data.flop;
var dict= ();{|x|
        var key= x[0];
        var val= x.copyRange(1, x.size-1);
        while({ or:{key.last==$:}}, {
                key= key.copyRange(0, key.size-2);
        if(val[0].any{|x| #[$:, $/].includes(x)}.not, {
                val= val.asFloat; //make single numbers into floats
                //}, { //else do nothing - keep date and satellites as strings
        dict.put(key.asSymbol, val);
~dict= dict; //handle

//--list all stored keys...{|x| x.postln}

//--access data stored at keys...
~dict['ACCELEROMETER X (m/s²)']
~dict['ACCELEROMETER Y (m/s²)']
~dict['ACCELEROMETER Z (m/s²)']
~dict['GRAVITY X (m/s²)']
~dict['GRAVITY Y (m/s²)']
~dict['GRAVITY Z (m/s²)']
~dict['GYROSCOPE X (rad/s)']
~dict['GYROSCOPE Y (rad/s)']
~dict['GYROSCOPE Z (rad/s)']
~dict['LIGHT (lux)']
~dict['MAGNETIC FIELD X (μT)']
~dict['MAGNETIC FIELD Y (μT)']
~dict['MAGNETIC FIELD Z (μT)']
~dict['ORIENTATION X (°)']
~dict['ORIENTATION Y (°)']
~dict['ORIENTATION Z (°)']
~dict['PROXIMITY (i)']
~dict['SOUND LEVEL (dB)']
~dict['LOCATION Latitude']
~dict['LOCATION Longitude']
~dict['LOCATION Altitude ( m)']
~dict['LOCATION Speed ( Kmh)']
~dict['LOCATION Accuracy ( m)']
~dict['Satellites in range'] //note as strings
~dict['Temperature (F)']
~dict['Level (%)']
~dict['Voltage (Volt)']
~dict['Time since start in ms'] //timestamps in milliseconds
~dict['YYYY-MO-DD HH-MI-SS_SSS'] //absolute timestamps - note as strings

//--plot the xyz accelerometer data...
        ~dict['ACCELEROMETER X (m/s²)'],
        ~dict['ACCELEROMETER Y (m/s²)'],
        ~dict['ACCELEROMETER Z (m/s²)']

//--plot the xyz orientation data...
        ~dict['ORIENTATION X (°)'],
        ~dict['ORIENTATION Y (°)'],
        ~dict['ORIENTATION Z (°)']

//--plot a lot of data (but not all)...
        'ACCELEROMETER X (m/s²)',
        'ACCELEROMETER Y (m/s²)',
        'ACCELEROMETER Z (m/s²)',
        'GRAVITY X (m/s²)',
        'GRAVITY Y (m/s²)',
        'GRAVITY Z (m/s²)',
        'GYROSCOPE X (rad/s)',
        'GYROSCOPE Y (rad/s)',
        'GYROSCOPE Z (rad/s)',
        'LIGHT (lux)',
        'MAGNETIC FIELD X (μT)',
        'MAGNETIC FIELD Y (μT)',
        'MAGNETIC FIELD Z (μT)',
        'ORIENTATION X (°)',
        'ORIENTATION Y (°)',
        'ORIENTATION Z (°)',
        'SOUND LEVEL (dB)'
].collect{|x| ~dict[x]}.plot;

//--sound example mapping accelerometer to simple sound
        var a= {|freq= 500, amp= 0, pan= 0|, 0.2, amp.lag(0.25)), pan.lag(0.25))}.play;
                var prevTime= 0;
                ~dict['Time since start in ms'].do{|x, i|
                                \freq, ~dict['ACCELEROMETER Y (m/s²)'][i].linexp(-20, 20, 100, 1000),
                                \amp, ~dict['ACCELEROMETER Z (m/s²)'][i].linlin(-20, 20, 0, 1),
                                \pan, ~dict['ACCELEROMETER X (m/s²)'][i].linlin(-20, 20, -1, 1)
                        prevTime= x;

Attached is a demo CSV file of me first keeping the phone still, and then shaking it a bit + turning it around. The data recording is only about eight seconds long.

If you plot for example the 3D accelerometer data it will look like this...


Subscribe to f0blog RSS