Want to control esp32 thru Osc Maxmsp contorlling some gpio outputs and one servo output, cant work!

#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <ESP32_Servo.h>

// WiFi settings
const char* ssid = "Forgg";
const char* password = "77777777";

// Server IP and port
const IPAddress outIp(172, 20, 10, 4); // Change to your Max MSP computer's IP address
const unsigned int outPort = 9999;

// Create WiFiUDP instance
WiFiUDP udp;

// Servo settings
const int servoPin = 14; // Change to the GPIO pin you connected the servo to
Servo servo;

void setup() {
// Initialize serial communication
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected succed");

// Initialize all GPIO pins as OUTPUT
for (int pin = 0; pin < 34; pin++) {
pinMode(pin, OUTPUT);
}

// Attach servo
servo.attach(servoPin);

// Begin listening for UDP packets
udp.begin(outPort);
}

void loop() {
// Read OSC message
OSCMessage oscMessage;
int packetSize = udp.parsePacket();
if (packetSize) {
while (packetSize--) {
oscMessage.fill(udp.read());
}

// Handle OSC messages
if (oscMessage.fullMatch("/gpio")) {
  int pin = oscMessage.getInt(0);
  int state = oscMessage.getInt(1);
  digitalWrite(pin, state);
} else if (oscMessage.fullMatch("/servo")) {
  int angle = oscMessage.getInt(0);
  servo.write(angle);
}

}
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the <CODE> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly.

Can you please give a proper description of your problem? "Can't work" is not giving people much to go on with. It might also be useful to post the MaxMsp 'code' so people who are familiar with maxMSP can load it and have a look at it; I think that the extension is maxpat (it's a little too long ago that I fiddeld with it).

The following code will likely crash the ESP32 because it programs the pins needed to access SPI flash. It might also disable the UART used for Serial. Try pins 13 to 33 instead. Take a look at an ESP32 pinout reference to see which pins are safe to use.

// Initialize all GPIO pins as OUTPUT
for (int pin = 0; pin < 34; pin++) {
  pinMode(pin, OUTPUT);
}
1 Like

Chang my esp32 pins to 13 to 33, and successfully connect to wifi thru looking at COM port, but the message from max still can't contol of the gpio, maybe my message is wrong? here's my max msp code part:

{
"boxes" : [ {
"box" : {
"maxclass" : "message",
"text" : "host 172.20.10.2",
"fontsize" : 13.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"id" : "obj-6",
"patching_rect" : [ 85.0, 79.0, 105.0, 23.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "host 255.255.255.0",
"fontsize" : 13.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"id" : "obj-1",
"patching_rect" : [ 150.0, 130.0, 120.0, 23.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "/gpio/D5 0",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-5",
"patching_rect" : [ 473.0, 204.0, 63.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "/gpio 4 0",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-4",
"patching_rect" : [ 394.0, 191.0, 55.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "toggle",
"parameter_enable" : 0,
"numinlets" : 1,
"numoutlets" : 1,
"id" : "obj-23",
"patching_rect" : [ 282.0, 117.0, 24.0, 24.0 ],
"outlettype" : [ "int" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "/gpio13 1",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-20",
"patching_rect" : [ 313.0, 156.0, 58.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "host 192.168.1.25",
"fontsize" : 13.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"id" : "obj-16",
"patching_rect" : [ 113.0, 104.0, 113.0, 23.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "host 127.0.0.1",
"fontsize" : 13.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"id" : "obj-21",
"patching_rect" : [ 56.0, 52.0, 93.0, 23.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "host localhost",
"fontsize" : 13.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"id" : "obj-18",
"patching_rect" : [ 28.0, 25.0, 90.0, 23.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "0",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-15",
"patching_rect" : [ 273.0, 20.0, 29.5, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "1",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-13",
"patching_rect" : [ 221.0, 20.0, 29.5, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "newobj",
"text" : "prepend /gpio 27",
"numinlets" : 1,
"numoutlets" : 1,
"id" : "obj-11",
"patching_rect" : [ 228.25, 62.0, 99.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "/gpio 27 1",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-7",
"patching_rect" : [ 446.0, 162.0, 157.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

, {
"box" : {
"maxclass" : "newobj",
"text" : "udpsend 172.20.10.2 9999",
"numinlets" : 1,
"numoutlets" : 0,
"id" : "obj-3",
"patching_rect" : [ 216.0, 230.0, 151.0, 22.0 ]
}

	}

, {
"box" : {
"maxclass" : "message",
"text" : "/digitalWrite D5 0",
"numinlets" : 2,
"numoutlets" : 1,
"id" : "obj-2",
"patching_rect" : [ 378.0, 62.0, 100.0, 22.0 ],
"outlettype" : [ "" ]
}

	}

],
"lines" : [ {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-3", 0 ],
"order" : 1
}

	}

, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-7", 1 ],
"order" : 0
}

	}

, {
"patchline" : {
"source" : [ "obj-13", 0 ],
"destination" : [ "obj-11", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-15", 0 ],
"destination" : [ "obj-11", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-1", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-5", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-4", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-23", 0 ],
"destination" : [ "obj-20", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-20", 0 ],
"destination" : [ "obj-7", 1 ],
"order" : 0
}

	}

, {
"patchline" : {
"source" : [ "obj-20", 0 ],
"destination" : [ "obj-3", 0 ],
"order" : 1
}

	}

, {
"patchline" : {
"source" : [ "obj-2", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-18", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

, {
"patchline" : {
"source" : [ "obj-16", 0 ],
"destination" : [ "obj-3", 0 ]
}

	}

],
"appversion" : {
"major" : 8,
"minor" : 5,
"revision" : 3,
"architecture" : "x64",
"modernui" : 1
}
,
"classnamespace" : "box"
}

I have never used max/msp so I have no idea what it is doing. But there are ways to debug these kinds of problems no matter what program is sending OSC.

Add debug output to your sketch should show what exactly the OSC sender is sending. Serial.print every byte of the UDP packet. If nothing is printed, the OSC sender probably sending to the wrong IP address and/or port number.

Or perhaps there is a network configuration problem. Try pinging the ESP32's IP address from the OSC sender. If ping fails, OSC will also fail.

Use Wireshark on the computer running the OSC sender to capture outgoing UDP packets. This can also show if the sender is sending to the wrong IP address or port number.

1 Like

Hi !
Did you find a solution ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.