OSC control of TouchDesigner. NYU

This project I used OSC to control an application on my computer, called TouchDesigner, from an Arduino ESP32 microcontroller . OSC data is sent via UDP packets between the device and the software over a local network.

Project layout in TouchDesigner:

Screenshot 2024-11-14 at 4.02.25 PM.png

Final Product:

https://drive.google.com/file/d/152VPuCT-ZVgy8oy7fMTsPUvQysie9E05/view?usp=sharing

Arduino Code

Asynchronous data sending UDP packets from physical device to TouchDesigner software.

3 Potentiometer to control particle x, y, and z forces

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>

// Replace with your network credentials
const char* ssid = "*****";
const char* pass = "*****";

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
OSCErrorCode error;
//const unsigned int localPort = 13001; // Local port to listen for UDP packets 
const IPAddress remoteIp(192, 168, 1, 155); // TouchDesigner IP (my local)
const unsigned int remotePort = 12000; // TouchDesigner port

void setup() {
  Serial.begin(115200);
  
  while(!Serial){;;}

  // Connect to WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // logon to network
  WiFi.begin(ssid, pass);

  // wait to get on
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  // print put local IP address and verify port
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

// commenting out because it is one way message sending.

//  Serial.println("Starting UDP Listening port");
//  udp.begin(localPort);
//  Serial.print("Local port: ");
//  Serial.println(localPort);
}

void loop() {
  // Read analog values from A0, A1, and A2 and convert to range 0.0–0.5
  float externalX = analogRead(A0) / 4095.0 * 0.5;
  float externalY = analogRead(A1) / 4095.0 * 0.5;
  float externalZ = analogRead(A2) / 4095.0 * 0.5;

  // Print the scaled values to the Serial Monitor
  Serial.print("A0 (ExternalX): ");
  Serial.print(externalX, 3);
  Serial.print(" | A1 (ExternalY): ");
  Serial.print(externalY, 3);
  Serial.print(" | A2 (ExternalZ): ");
  Serial.println(externalZ, 3);
  
  // Send data to TouchDesigner over UDP using OSC format
  OSCMessage msg("/particleGpu/forces");
  msg.add((float)externalX);
  msg.add((float)externalY);
  msg.add((float)externalZ);

  // Send the message
  udp.beginPacket(remoteIp, remotePort);
  msg.send(udp); // Send the OSC message
  udp.endPacket();
  msg.empty(); // Clear the message to send a new one next loop

  delay(100);
}

Thank you for the starter here: https://github.com/IDMNYU/Net-Devices/blob/main/ESP32OSC/OSCLED_TouchOSC/OSCLED_TouchOSC.ino