esp8266 opensound control teensy

today i ported my opensound control esp8266 example for arduino to run on a teensy 3.

the version below is a bit simpler but still works the same. it is just to show how to send and receive osc messages directly in sc or max.

teensy code:

//f0 150705 - modified for teensy3 160430
//sending and receiving udp osc with an esp8266
//for teensy + esp8266 with firmare
#define WLAN_SSID  "ssid"
#define WLAN_PASS  "pass"
#define WLAN_ADDR  "" //laptop running sc EDIT
#define ADDR "/tap" //incoming osc addy
#define PORT  1112  //incoming osc port
uint8_t buf[16];
char indata[12];
char inbuffer[256];
char OKrn[] = "OK\r\n";
byte wait_for_esp_response(int timeout, char* term = OKrn) {
  unsigned long t = millis();
  bool found = false;
  int i = 0;
  int len = strlen(term);
  while (millis() < (t + timeout)) {
    if (Serial1.available()) {
      inbuffer[i++] =;
      if (i >= len) {
        if (strncmp(inbuffer + i - len, term, len) == 0) {
          found = true;
  inbuffer[i] = 0;
  return found;
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
  buf[13] = 3;   // b
  buf[14] = 2;   // c
  buf[15] = 0;   // d
  pinMode(23, OUTPUT);
  Serial.begin(115200);   //usb serial for feedback
  Serial1.begin(115200);  //teensy hardware pins 0 and 1
  Serial.print("hard reset...");
  pinMode(4, OUTPUT);
  pinMode(4, INPUT);
  boolean resp = wait_for_esp_response(1000, "ready\r\n");
  resp = wait_for_esp_response(1000);
  do {
    resp = wait_for_esp_response(3000);
  } while (!resp);
  resp = wait_for_esp_response(1000);
  resp = wait_for_esp_response(1000);
  Serial.println("setup done");
void loop() {
  if (wait_for_esp_response(1000, "\r\n+IPD,4,16:")) {
    if (wait_for_esp_response(1000, ADDR)) {
      Serial1.readBytes(indata, 12);
      buf[12] = indata[8] + 1; //add one to incomming values
      buf[13] = indata[9] + 1;
      buf[14] = indata[10] + 1;
      buf[15] = indata[11] + 1;
      if (wait_for_esp_response(1000, "> ")) {
        Serial1.write(buf, sizeof(buf));
        if (wait_for_esp_response(1000)) {
          Serial.println("reply sent!");

supercollider code:

var send= 0, last= Main.elapsedTime;
OSCdef(\sti, {|msg, time, addr|
        //should receive the values you sent +1
        ([msg[1]>>24, (msg[1]>>16)&255, (msg[1]>>8)&255, msg[1]&255]).post;
        (" % sec since last: %, % sec since sent").format(addr, time-last, time-send).postln;
        last= time;
}, \sti);
n= NetAddr("", 1112); //esp8266 ip address EDIT
f= {|id, on, hi, lo| (id&255<<24)|(on&255<<16)|(hi&255<<8)|(lo&255)};
                n.sendMsg(\tap, f.value(4, 3, i.asInteger%256, 1));
                send= Main.elapsedTime;

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

update 180212: removed an unneeded wait for response. thanks Niklas!

Package icon maxmsp example patch1.56 KB


here's a handy class for supercollider. it's an audio clipping detector loosely based on Batuhan Bozkurt's StageLimiter.

to install it download and extract the zip file into your supercollider extensions folder. then recompile and type...


now as soon as you play sound that's clipping (i.e. exceeds -1.0 or 1.0), the class will warn you.

to turn it off type...

Package icon GreenPeace.zip3.67 KB

joule thieves

to make something useful out of 'dead' batteries i've been building joule thief circuits. these circuits are very easy, cheap and fun to build plus it gives me a little bit less bad conscious when going to the recycling bin with the batteries. watch to learn more.

below are pictures of one variant. it has a small 3mm green led (salvaged from broken printer), a hand wound coil, a resistor and a transistor.

joulethief 0

joulethief 1

the batteries here came with my first ever (analogue) multimeter. they are 32 years old!. see the date code: 84-04. they still can drive the little led. amazing. i think running this little green led is a good way to use the last energy stored in these beautiful and truly long life batteries.

joulethief 2


just some hypnotic graphics...

the javascript code above is this...

<div style="background-color:black;">
<canvas id="can" width="800" height="600"></canvas>
var width, height;
var ctx, frameCount= 0;
(function() {
    var can= document.getElementById('can');
    ctx= can.getContext('2d');
    width= can.width;
    height= can.height;
    ctx.fillStyle= '#FFF';
function draw() {
    ctx.clearRect(0, 0, width, height);;
    ctx.translate(width*0.5, height*0.5);
    var theta= Math.sin(frameCount*0.001)*Math.PI*2*4;
    for(var y= 0; y<height; y++) {
        for(var i= 0; i<10; i++) {
            ctx.fillRect((Math.sin(y*0.1+theta+(i*2))*100), y, 2, 2);
    frameCount= frameCount+1;

originally this was a quick sketch made in processing...

//spiral.pde - processing
void setup() {
  size(800, 600);
void draw() {
  translate(width*0.5, height*0.5);
  float theta= sin(frameCount*0.001)*TWO_PI*4;
  for(int y= 0; y<height; y++) {
    for(int i= 0; i<10; i++) {
      rect((sin(y*0.1+theta+(i*2))*100), y, 2, 2);

and then ported to supercollider...

//spiral.scd - supercollider
var width= 800, height= 600;
var win= Window("spiral", Rect(100, 100, width, height), false);
var usr= UserView(win, Rect(0, 0, width, height));
usr.animate= true;
usr.drawFunc= {
        var theta= sin(usr.frame*0.001)*2pi*4;
        Pen.fillColor= Color.white;
        Pen.translate(width*0.5, height*0.5);{|y|
                        Pen.fillRect(Rect(sin(y*0.1+theta+(i*2))*100, y, 2, 2));

esp8266 opensound control

here some example arduino code for sending and receiving osc via the cheap esp8266 serial wifi module.

note that the opensound 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 3v3pin 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.

pd on raspberry pi

here is a quick tutorial on how to install and run puredata 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 pi and your home router).
what you get after following the below tutorial is a sdcard with a pd patch that automatically starts when the pi is booted.

* put the raspbian image onto a +4gb card (it is easily done with etcher).
* on newer versions of rasbian, activate ssh by creating an empty file called 'ssh' directly on the sd card
* insert the sdcard+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 puredata + 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 puredata
$ 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 raspberrypi2 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


Subscribe to f0blog RSS