currently happily under construction :)

ESP32 meets OSC Communication

This example shows a direct connection of both ESP32 and any computer running Processing in the same network. Both ESP needs the suitable C++ code uploaded with the Processing application.

you will need this lib to get it done 🙂 https://github.com/CNMAT/OSC

nice content: https://madskjeldgaard.dk/posts/esp32-simple-osc-receiver/

get realtime data from ESP32 to host

import oscP5.*;
import netP5.*;

ArrayList income_buffer_arr = new ArrayList();  
  
OscP5 oscP5;
NetAddress myRemoteLocation;

float lerpspeed = .1;


void setup() {
  size(400,400);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,7777);
  myRemoteLocation = new NetAddress("192.168.1.8",7777);
  frameRate(30);
}


void draw() {
 
  fill(0,12);
  rect(0,0,width,height);
   drawRealtimeData();
  
}

void mousePressed() {
  
  OscMessage myMessage = new OscMessage("/toggle_stream");
  myMessage.add(1); /* add an int to the osc message */
  oscP5.send(myMessage, myRemoteLocation); 
}


void mouseReleased() {
  
  OscMessage myMessage = new OscMessage("/toggle_stream");
  myMessage.add(0); /* add an int to the osc message */
  oscP5.send(myMessage, myRemoteLocation); 
}


PVector tpos1 = new PVector();
PVector tpos2 = new PVector();
PVector tpos3 = new PVector();
PVector tpos4 = new PVector();

PVector spos1 = new PVector();
PVector spos2 = new PVector();
PVector spos3 = new PVector();
PVector spos4 = new PVector();

void oscEvent(OscMessage theOscMessage) {
 
  
  spos1.lerp(tpos1, lerpspeed);
   spos2.lerp(tpos2, lerpspeed);
    spos3.lerp(tpos3, lerpspeed);
     spos4.lerp(tpos4, lerpspeed);
    
  tpos1.x =   theOscMessage.get(0).floatValue();
  tpos1.y =   theOscMessage.get(1).floatValue();
  
  tpos2.x = theOscMessage.get(2).floatValue();
  tpos2.y =   theOscMessage.get(3).floatValue();
 
 
   tpos3.x =   theOscMessage.get(4).floatValue();
  tpos3.y =   theOscMessage.get(5).floatValue();
  
    tpos4.x =   theOscMessage.get(6).floatValue();
  tpos4.y =   theOscMessage.get(7).floatValue();
  
}

// just visualize the incoming data
void drawRealtimeData(){
   
  noStroke();
  fill(255);
  ellipse(spos1.x*width,spos1.y*height,12,12);
  ellipse(spos2.x*width,spos2.y*height,12,12);
  ellipse(spos3.x*width,spos3.y*height,12,12);
  ellipse(spos4.x*width,spos4.y*height,12,12);
  
  
}
#include "Arduino.h"
#include "WiFi.h"
#include <OSCMessage.h>

WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
const IPAddress outIp(192,168,1,7); // your esp ip here

// Options
int update_rate = 16;

// Network settings
char ssid[] = " "; // your network SSID (name)
char pass[] = " ";  // your network password
unsigned int localPort = 7777; // local port to listen for OSC packets

bool is_streaming = false;

void setup() {
  /* setup wifi */
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  Udp.begin(localPort);
}

void sendOSC() {
  OSCMessage msg("/datafromesp");

  // send some random values from 0 to 1
  float _v1 = (sin(millis()*.01)+1.)*.5;
  float _v2 = (sin(millis()*.04)+1.)*.5;
  float _v3 = (sin(millis()*.001)+1.)*.5;
  float _v4 = (sin(millis()*.02)+1.)*.5;
  float _v5 = (sin(millis()*.0041)+1.)*.5;
  float _v6 = (sin(millis()*.0091)+1.)*.5;
  float _v7 = (sin(millis()*.021)+1.)*.5;
  float _v8 = (sin(millis()*.0031)+1.)*.5;
  msg.add( _v1);
  msg.add( _v2);
  msg.add( _v3);
  msg.add( _v4);
  msg.add( _v5);
  msg.add( _v6);
  msg.add( _v7);
  msg.add( _v8);
  
  Udp.beginPacket(outIp, 7777);
  msg.send(Udp); // Send the bytes to the SLIP stream
  Udp.endPacket();  // Mark the end of the OSC Packet
  msg.empty();   // Free space occupied by message
  delay(10);
}


void receiveMessage() {
  OSCMessage inmsg;
  int size = Udp.parsePacket();

  if (size > 0) {
    while (size--) {
      inmsg.fill(Udp.read());
    }
    if (!inmsg.hasError()) {
      inmsg.dispatch("/toggle_stream", stream_toggle);
    } 

   
  
    //else { auto error = inmsg.getError(); }
  }
}


void stream_toggle(OSCMessage &msg) {
  switch (msg.getInt(0)) {
  case 0:
   is_streaming=false;
    break;
  case 1:
    is_streaming=true;
    break;
  }
}




void loop() {

    
    if(is_streaming){sendOSC();}


    receiveMessage();
    delay(10);

   

}

get a data stream pack from ESP32 to host

import oscP5.*;
import netP5.*;

ArrayList income_buffer_arr = new ArrayList();  
  
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400,400);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,7777);
  myRemoteLocation = new NetAddress("192.168.1.8",7777);
}


void draw() {
  background(0);  
  
  drawInputBuffer();
  
}

void mousePressed() {
  
  OscMessage myMessage = new OscMessage("/send_sensor_stream");
  myMessage.add(1); /* add an int to the osc message */
   income_buffer_arr = new ArrayList(); // reset the temp buffer

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


void oscEvent(OscMessage theOscMessage) {
 
  // expect 8 float values from the osc message.
 float[] _c = new float[8]; 
 
 _c[0] = theOscMessage.get(0).floatValue();
 _c[1] = theOscMessage.get(1).floatValue();
 _c[2] = theOscMessage.get(2).floatValue();
 _c[3] = theOscMessage.get(3).floatValue();
 _c[4] = theOscMessage.get(4).floatValue();
 _c[5] = theOscMessage.get(5).floatValue();
 _c[6] = theOscMessage.get(6).floatValue();
 _c[7] = theOscMessage.get(7).floatValue();
  income_buffer_arr.add(_c);
 
}

// just visualize the incoming data
void drawInputBuffer(){
   
  for(int i=0;i<income_buffer_arr.size();i++){
  
    float[] cs = (float[])income_buffer_arr.get(i);
    
    for(int j=0;j<cs.length;j++){
      noStroke();
      fill(cs[j]);
      rect(i*3+10,j*40+10,3,38);
    }
  
  }
 
}
#include "Arduino.h"
#include "WiFi.h"
#include <OSCMessage.h>

WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
const IPAddress outIp(192,168,1,7); // put in IP of your ESP here!

// Options
int update_rate = 16;

// Network settings
char ssid[] = " "; // your network SSID (name)
char pass[] = " ";  // your network password
unsigned int localPort = 7777; // local port to listen for OSC packets

bool is_streaming = false;
int stream_tick = 0;
int stream_max = 128;

void setup() {
  /* setup wifi */
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  Udp.begin(localPort);
}

void sendOSC() {
  OSCMessage msg("/datafromesp");

  float _v1 = random(25500)*.01;
  float _v2 = random(25500)*.01;
  float _v3 = random(25500)*.01;
  float _v4 = random(25500)*.01;
  msg.add( _v1);
  msg.add( _v2);
  msg.add( _v3);
  msg.add( _v4);
  msg.add( _v1);
  msg.add( _v2);
  msg.add( _v3);
  msg.add( _v4);
  
  Udp.beginPacket(outIp, 7777);
  msg.send(Udp); // Send the bytes to the SLIP stream
  Udp.endPacket();  // Mark the end of the OSC Packet
  msg.empty();   // Free space occupied by message
  delay(10);
}


void receiveMessage() {
  OSCMessage inmsg;
  int size = Udp.parsePacket();

  if (size > 0) {
    while (size--) {
      inmsg.fill(Udp.read());
    }
    if (!inmsg.hasError()) {
      inmsg.dispatch("/led", ledtoggle);
    } 

   is_streaming = true;
   stream_tick = 0;
    //else { auto error = inmsg.getError(); }
  }
}

void loop() {

    
    if(is_streaming){
        if(stream_tick<stream_max){
            stream_tick++;
            sendOSC();
        }else{
         is_streaming = false; 
         }
    }else{

      receiveMessage();
      delay(update_rate);
     }

}