Open Sound Control (OSC) library for Plaquette
GitHub repo : https://github.com/SofaPirate/PqOsc
To receive the data, your software must be able to process OSC SLIP. Here are a few demonstrations that show how to receive the OSC Slip data in Pure Data and Max (in French):
- OSC SLIP dans Pd @ AidE
- Pd : Relais OSC SLIP ⇄ UDP @ AidE
- Max : Relais OSC SLIP ⇄ UDP @ AidE
- There is also a Pure Data patch included in the
extrasdirectory.
You can test these examples with the Pure Data patch included in the extras directory.
This code sends an OSC SLIP message with the OSC address "/wave" containing the value of a sine wave (10 times per second):
#include <Plaquette.h>
#include <PqOsc.h>
// The input signal core wave.
Wave wave(SINE);
// Create the input and output node
OscSlip<128> oscSlip(115200); // use Serial by default
// Link an output address to the node
OscOut oscOutWave(oscSlip, "/wave");
// Used to slow down message transmission
Metronome ticker(0.1f); // 10 Hz
void step()
{
if (ticker)
{
wave >> oscOutWave;
}
}Using Plaquette's event management callbacks you can replace step() by:
void begin() {
ticker.onBang([]() // inline callback
{
wave >> oscOutWave;
});
}This code echoes the value of the OSC message received on the OSC address "/alpha" to the OSC address "/beta" (i.e. /alpha → board → /beta):
#include <Plaquette.h>
#include <PqOsc.h>
// Create the input and output node
OscSlip<128> oscSlip(115200);
// Link an input address to the node
OscIn oscInAlpha(oscSlip, "/alpha");
// Link an output address to the node
OscOut oscOutBeta(oscSlip, "/beta");
void step()
{
if (oscInAlpha.updated())
{
// Send message only when new message received.
oscInAlpha >> oscOutBeta;
}
}Event callback:
void begin() {
oscInAlpha.onUpdate([]() // inline callback
{
oscInAlpha >> oscOutBeta;
});
}OscUdp sends and receives OSC over a network. It works with any UDP object (WiFiUDP, EthernetUDP, ...), so you can connect to the network however you like. The examples show two approaches:
- MicroNet (recommended): a helper library that takes care of the network connection for you (ESP32 only).
- Plain WiFi: uses only the WiFi library that comes with your board. Nothing extra to install.
Why MicroNet is recommended. Plain WiFi is fine for a quick test on your own network. Once a project leaves your desk (a performance, an installation, a workshop), it runs into problems that MicroNet solves:
- No WiFi password in your code. You enter the credentials once from a web page served by the board. You can share or publish your sketch without exposing a password, and move to a new venue without editing and re-uploading the code.
- No IP addresses to manage. Most networks hand out IP addresses automatically (DHCP), so a computer's address can change from one day to the next. With plain WiFi you must look up the destination IP and re-upload whenever it changes. MicroNet finds the destination computer by its name (e.g. "m3-air") and gives the board its own name ("plaquette.local"), so your software can find it too.
- WiFi or Ethernet with the same code. You only change two declarations to switch to a wired connection, which is often more reliable for long-running installations. Some boards can also be powered by the Ethernet cable (Power over Ethernet).
Use plain WiFi if your board is not an ESP32 or if you want to avoid installing extra libraries.
In both cases the board listens on port 8888 and sends to port 7777. The Pure Data patch extras/osc_udp.pd uses these ports and sends to plaquette.local.
This code echoes the value of the OSC message received on the OSC address "/alpha" to the OSC address "/beta" (i.e. /alpha → board → /beta) using MicroNet (see the OscUdpEchoMicroNet example). Install these libraries from the Library Manager or from GitHub:
- WiFi: MicroNet and WiFiManager (by tzapu)
- Ethernet: MicroNet, Ethernet, and EthernetBonjour
The board shows up on the network as "plaquette". It looks for the destination computer by its name (remoteName, here "m3-air"). Use your computer's local hostname without ".local". On macOS you can find it in System Settings → General → Sharing → Local hostname. When you refer to the board from your software, add ".local" to its name. For example, in Pure Data you would connect to "plaquette.local".
WiFi setup (first time only). There are no WiFi credentials in the code. You enter them once, from a web page served by the board:
- Upload the sketch. Because the board does not know your network yet, it creates its own WiFi access point named "plaquette".
- On your phone or computer, join the "plaquette" WiFi network. A setup page should open by itself. If it doesn't, go to http://192.168.4.1 in a web browser.
- Click Configure WiFi, pick your network, type its password and click Save.
- The board joins your network and saves the credentials. After a restart it reconnects on its own. If it can't connect (for example, on a different network), the "plaquette" access point comes back so you can set it up again.
myMicroNet.begin() does not return until the board has a network connection, which includes the setup step above. So call it in prepare():
#include <Plaquette.h>
#include <PqOsc.h>
#include <MicroNetWiFi.h>
const char* remoteName = "m3-air"; // change for your own computer's local hostname
const unsigned int receivePort = 8888;
const unsigned int sendPort = 7777;
// MicroNet over WiFi
MicroNetWiFi myMicroNet;
// UDP over WiFi
WiFiUDP myUdp;
// Create the input and output node
OscUdp<128> myOsc(myUdp, receivePort);
// Link input and output addresses to the node
OscIn oscInAlpha(myOsc, "/alpha");
OscOut oscOutBeta(myOsc, "/beta");
// Serial monitor
Monitor monitor(115200);
void prepare()
{
// Blocks until connected (opens the WiFi setup portal if needed).
myMicroNet.begin("plaquette");
}
void begin()
{
print("My IP: ");
print(myMicroNet.getIP());
println();
print("Looking for: ");
println(remoteName);
// Keep trying until the destination computer is found.
IPAddress destinationIp;
do {
destinationIp = myMicroNet.resolveName(remoteName);
} while (destinationIp == IPAddress(0, 0, 0, 0));
myOsc.setDestination(destinationIp, sendPort);
print("Found it at IP: ");
print(destinationIp);
println();
}
void step()
{
myMicroNet.update(); // must be called often
if (oscInAlpha.updated())
{
oscInAlpha >> oscOutBeta;
}
}Ethernet. For a wired connection, replace the MicroNet and UDP declarations with their Ethernet versions. Ethernet has no setup portal: the board gets its address from the network automatically (DHCP).
#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet(MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOM_LITE);
EthernetUDP myUdp;ATOM_POE_WITH_ATOM_LITE is a MicroNet preset with the pin settings for one hardware combination: an M5Stack Atom PoE Ethernet base with an M5Stack Atom Lite (ESP32) plugged into it. For an AtomS3 on the same base, use ATOM_POE_WITH_ATOMS3. For other boards with a WIZnet Ethernet chip, create MicroNetEthernet myMicroNet; with no preset. Then set up SPI and Ethernet.init(csPin) in prepare() before calling myMicroNet.begin(), as shown in the MicroNet documentation.
This code echoes the value of the OSC message received on the OSC address "/alpha" to the OSC address "/beta" (i.e. /alpha → board → /beta) using the board's built-in WiFi, without MicroNet.
Before uploading, set your network's name (ssid) and password, and the IP address of the computer running your OSC software (destinationIp). After uploading, open the Serial Monitor at 115200 baud to see the board's IP address. Send your OSC messages to that IP. On ESP32 you can also use the name plaquette.local.
#include <Plaquette.h>
#include <PqOsc.h>
#include <WiFi.h> // ESP32 (see below for other boards)
#include <ESPmDNS.h> // ESP32 only: announces the board as "plaquette.local"
const char* ssid = "my-network"; // change for your own network
const char* password = "my-password"; // change for the network's password
IPAddress destinationIp(192, 168, 1, 100); // change for the IP of the computer receiving OSC
const unsigned int receivePort = 8888;
const unsigned int sendPort = 7777;
// UDP over WiFi
WiFiUDP myUdp;
// Create the input and output node
OscUdp<128> myOsc(myUdp, receivePort, destinationIp, sendPort);
// Link input and output addresses to the node
OscIn oscInAlpha(myOsc, "/alpha");
OscOut oscOutBeta(myOsc, "/beta");
// Connect to WiFi before the OSC node starts.
void prepare()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
MDNS.begin("plaquette");
Serial.print("My IP: ");
Serial.println(WiFi.localIP());
}
void step()
{
if (oscInAlpha.updated())
{
oscInAlpha >> oscOutBeta;
}
}Connect to WiFi in prepare() instead of begin(): prepare() runs before the units start, so the network is ready when OscUdp opens its UDP port.
Each board family has its own WiFi library. Replace the #include lines with the right one:
| Board | WiFi include |
|---|---|
| ESP32 | #include <WiFi.h> |
| ESP8266 | #include <ESP8266WiFi.h> and #include <WiFiUdp.h> |
| Arduino UNO R4 WiFi | #include <WiFiS3.h> |
| Raspberry Pi Pico W | #include <WiFi.h> |
| Nano 33 IoT, MKR WiFi 1010, UNO WiFi Rev2 | #include <WiFiNINA.h> |
On boards without mDNS (ESPmDNS.h), remove the MDNS lines and use the board's IP address instead of plaquette.local. The OscUdpEchoWiFi and OscUdpOutputWiFi examples choose the right include automatically.
You can change output type by specifying it at construction. For example, this will send the value as an integer:
OscOut oscOutput(oscSlip, "/output", 'i');Input types in OscIn are detected at reception and converted to float.
Supported OSC type tags:
| Tag | Type | Notes |
|---|---|---|
'f' |
float32 | default |
'i' |
int32 | |
'd' |
float64 (double) | cast to float on receive |
'b' |
blob | raw bytes; float extracted if blob size matches |
'T' |
true | received as 1.0 |
'F' |
false | received as 0.0 |
'N' |
nil | received as 1.0 |
'I' |
impulse | received as 1.0 |
Note: The string type ('s') is not supported in order to minimize flash memory usage.