想用osc 控制esp32每個腳位的高低電位 在arduino ide環境裡 其中一隻腳位控制pwm
Posted: Tue Mar 28, 2023 8:41 am
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <ESP32_Servo.h>
// WiFi settings
const char* ssid = "Timi";
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);
}
}
}
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <ESP32_Servo.h>
// WiFi settings
const char* ssid = "Timi";
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);
}
}
}