Send OSC data to ESP 32 (Wi-Fi module connected to Arduino board)

Hello.

I would like to send data from Processing to ESP32 ( a wifi module plugged in a Arduino)

The Processing program is

/**
 * oscP5sendreceive by andreas schlegel
 * example shows how to send and receive osc messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
 
import oscP5.*;
import netP5.*;
  
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400,400);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,12000);
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
}


void draw() {
  background(0);  
}

void mousePressed() {
  /* in the following different ways of creating osc messages are shown by example */
  OscMessage myMessage = new OscMessage("/test");
  
  myMessage.add(123); /* add an int to the osc message */

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


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
}

The Arduino program to receive OSC data is this one, but I can’ t use it

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

WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
//int LED_BUILTIN = 2;


// Options
int update_rate = 16;

// Network settings
char ssid[] = "Iphone de ben"; // your network SSID (name)


char pass[] = "cacaprout";  // your network password
unsigned int localPort = 8001; // local port to listen for OSC packets

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

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

   Serial.print ( " good ");

  Udp.begin(localPort);
}

  void ledtoggle(OSCMessage & msg) {
  switch (msg.getInt(0)) {
  case 0:
    digitalWrite(LED_BUILTIN, LOW);
    break;
  case 1:
    digitalWrite(LED_BUILTIN, HIGH);
    break;
  }
}

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

  if (size > 0) {
    while (size--) {
      inmsg.fill(Udp.read());
      Serial.print ( " a ");
    }
    if (!inmsg.hasError()) {
      inmsg.dispatch("/led", ledtoggle);
       Serial.print ( " b ");
    } 
    else 
    { auto error = inmsg.getError();
      Serial.print ( " c ");
    
     }
  }
}

void loop() {

  receiveMessage();
  // digitalWrite(LED_BUILTIN, HIGH); 
  delay(update_rate);
  //  digitalWrite(LED_BUILTIN, LOW); 
}

Thank you :slight_smile:

Hi

Thanks Jafal,
I have ever tested but it doesn’t work.
I don’t know how to find the address of the ESP 32.
??

Hi

Here is how

ESP32 Useful Wi-Fi Library Functions (Arduino IDE) | Random Nerd Tutorials.

1 Like

Thank you.
I’m gonna precise my question step by step.

HI @bking,

I’ve used ESP8266 a few times. Never ESP32 but I think it’s mostly the same.

IP Address? Most of the example programs print the IP address on serial, so at first when you are developing your program connected to PC you can see the IP address on serial. When you take the ESP32 to another WiFi there are tools for the Android phone e.g. NetCutPro or WUMWiFi that will help you find it. I think the best way is to put code in the ESP to send an occasional UDP message. A listening program your PC can catch the messages.

I’d like to post 2 programs. The first is ESP8266 code which has a web-page, you’ll see that it’s developed from the Ard IDE examples. The buttons on the web-page switch a 4-wire fan on/off, and control the speed. The ESP measures the speed and puts that on the web-page. You don’t need all that, the part you want is where it UDP broadcasts the fan speed. (There’s another part where if you UDP send it an ‘A’ it replies with data.) It doesn’t need the fan etc. to run. You might have to alter it if I’ve used a pin that does something else on the ESP32

The 2nd is a Processing sketch which listens for the broadcast messages, and shows them in a window.

I’ve tried posting, but the ESP sketch is full of html, quotes, asterisks, and the forum software is changing it.

1 Like

I found a way to control ESP32 led with Osc message. There is a library Osc message in Processing and Arduino as well :wink: .

3 Likes