This week I wanted to connect to my ESP32 over wifi to send OSC messages to Max/MSP for my final project- so I can use the hall effect sensors and IMU to control a synthesizer. I have already done something like this so thankfully it will be pretty straightforward!
Though I have used wifi with ESP32s in the past, I strongly considered trying bluetooth for this for the following pros: not relying on wifi and needing an IP address (so not needing to reupload code when IP address changes),
and lower power consumption (longer battery life which would be nice because I'd like to use a small battery and have long wear time between recharging).
While those benefits of BLE would be nice to have, I am going with wifi because it is longer range, lower latency (very relevant from a musical instrument perspective), and the process of getting information into Max/MSP is much more straightforward
(from what I read online, getting bluetooth to work with Max is a pain). And lastly, regarding time and practicality, I've been really busy with the physical fab for this project so implementing something I've already done will be a safer and more efficient use of time.
Step one here is writing code for my microcontroller, to send OSC messages over wifi. I used this example code
in the CNMAT OSC library on github to modify my input + output week code to achieve this. I broke out the IMU into a separate file so that the code is easier to navigate and edit. The process for this is:
- initialize wifi network password, and IP addresses (local, gateway, and subnet)
- in setup function, configure and begin wifi
- in main loop, read sensors, create and populate an OSC bundle
- begin a UDP packet, send the information, end the packet, and empty the bundle
This portion is actually super straightforward! CNMAT has a max package which makes it easy to recieve UDP messages sent to your IP address. I put together some code using the hall sensors to control a modified version of a past synthesizer patch I made- getting the control values from the microcontroller simply requires a udprecieve object with the port number and routing things using the names assigned to them in the OSC bundles.
The patch and mapping I am using here is just a simple way of visualizing the sensor inputs!
-- week12.ino
-- max patch