For an upcoming installation, we need a few speakers hanging on the walls playing back sound files in a loop. I found the DFPlayer Mini to work well. It's a small and cheap board that can play sound files from an SD card and it has a built-in 3W amplifier. With a decent 5V power bank (here 4400mAh) it can run for ~4 days continuously.
Today I ported my Open Sound 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 SuperCollider or MaxMSPJitter.
Teensy code:
//f0 150705 - modified for Teensy3 160430
//sending and receiving UDP OSC with an ESP8266
//for Teensy + ESP8266 with firmware 0.9.5.2
#define WLAN_SSID "ssid"
#define WLAN_PASS "pass"
#define WLAN_ADDR "192.168.1.3" //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++] = Serial1.read();
if (i >= len) {
if (strncmp(inbuffer + i - len, term, len) == 0) {
found = true;
break;
}
}
}
}
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
delay(400);
Serial1.begin(115200); //Teensy hardware pins 0 and 1
Serial.println("starting");
Serial.print("hard reset...");
pinMode(4, OUTPUT);
delay(10);
pinMode(4, INPUT);
Serial.print("ready...");
boolean resp = wait_for_esp_response(1000, "ready\r\n");
Serial.println(resp);
Serial.print("mode1...");
Serial1.println("AT+CWMODE=1");
resp = wait_for_esp_response(1000);
Serial.println(resp);
Serial.print("connecting...");
do {
Serial1.print("AT+CWJAP=\"");
Serial1.print(WLAN_SSID);
Serial1.print("\",\"");
Serial1.print(WLAN_PASS);
Serial1.println("\"");
resp = wait_for_esp_response(3000);
Serial.print(resp);
} while (!resp);
Serial.print("\nmux1...");
Serial1.println("AT+CIPMUX=1");
resp = wait_for_esp_response(1000);
Serial.println(resp);
Serial.print("udp...");
Serial1.print("AT+CIPSTART=4,\"UDP\",\"");
Serial1.print(WLAN_ADDR);
Serial1.print("\",57120,");
Serial1.print(PORT);
Serial1.println(",0");
resp = wait_for_esp_response(1000);
Serial.println(resp);
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;
Serial.println(int(indata[8]));
Serial.println(int(indata[9]));
Serial.println(int(indata[10]));
Serial.println(int(indata[11]));
Serial1.println("AT+CIPSEND=4,16");
if (wait_for_esp_response(1000, "> ")) {
Serial1.write(buf, sizeof(buf));
if (wait_for_esp_response(1000)) {
Serial.println("reply sent!");
}
}
}
}
}
SuperCollider code:
(
//--call&response
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("192.168.1.4", 1112); //esp8266 ip address EDIT
f= {|id, on, hi, lo| (id&255<<24)|(on&255<<16)|(hi&255<<8)|(lo&255)};
r= Routine.run({
inf.do{|i|
n.sendMsg(\tap, f.value(4, 3, i.asInteger%256, 1));
send= Main.elapsedTime;
0.5.wait;
};
});
)
Note: my new and better way to do this is described in this post: /f0blog/f0led/
Updates:
180212: removed an unneeded wait for response. Thanks Niklas!
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 this BigClive video to learn more.
Below are pictures of one variant. It has a small 3mm green led (salvaged from a broken printer), a hand-wound coil, a resistor and a transistor.
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.
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 an 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 a 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 firmware 0.9.5.2
#include <SoftwareSerial.h>
#define WLAN_SSID "SSID"
#define WLAN_PASS "PASS"
#define WLAN_ADDR "192.168.1.51" //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.begin(115200);
Serial.begin(115200);
Serial.setTimeout(10000);
mySerial.println("");
mySerial.println("starting");
mySerial.print("hard reset...");
digitalWrite(4, 0);
pinMode(4, OUTPUT);
delay(10);
pinMode(4, INPUT);
resp = Serial.find("ready\r\n");
mySerial.println(resp);
mySerial.print("mode1...");
Serial.println("AT+CWMODE=1");
resp = Serial.find("OK\r\n");
mySerial.println(resp);
mySerial.print("connecting...");
do {
Serial.print("AT+CWJAP=\"");
Serial.print(WLAN_SSID);
Serial.print("\",\"");
Serial.print(WLAN_PASS);
Serial.println("\"");
resp = Serial.find("OK\r\n");
mySerial.println(resp);
} while (!resp);
mySerial.print("mux1...");
Serial.println("AT+CIPMUX=1");
resp = Serial.find("OK\r\n");
mySerial.println(resp);
mySerial.print("udp...");
Serial.print("AT+CIPSTART=4,\"UDP\",\"");
Serial.print(WLAN_ADDR);
Serial.print("\",57120,");
Serial.print(PORT);
Serial.println(",0");
resp = Serial.find("OK\r\n");
mySerial.println(resp);
Serial.setTimeout(1000);
}
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];
mySerial.print("id:");
mySerial.println(id);
mySerial.print("on:");
mySerial.println(on);
mySerial.print("hi:");
mySerial.println(hi);
mySerial.print("lo:");
mySerial.println(lo);
buf[15] = cnt++;
Serial.println("AT+CIPSEND=4,16");
Serial.find(">");
Serial.write(buf, sizeof(buf));
resp = Serial.find("OK\r\n");
mySerial.print("send...");
mySerial.println(resp);
}
}
}
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).
There's no volume control so better take care - the system is very hot.