Arduino Uno Wifi Rev2 未通过本地网络接收 OSC 信号(OSC 由 Python 脚本提供)

Arduino Uno Wifi Rev2 not receiving OSC signals over Local Network (OSC provided by Python Script)

提问人:Redgar Pro 提问时间:11/15/2023 更新时间:11/15/2023 访问量:18

问:

您好 StackOverFlow 社区,我正在处理一个项目,我尝试通过本地网络将 OSC 信号从 Python 脚本发送到 Arduino Uno Wifi Rev2。Python 脚本将 Tkinter 用于 UI,并使用 python-osc 库来发送 OSC 消息。

我已经成功地在Arduino和本地网络之间建立了WiFi连接,Python脚本似乎正在发送OSC信号。但是,Arduino似乎没有像串行监视器输出所暗示的那样接收到这些信号。

用于发送 OSC 信号的简单 Tkinter UI Python 程序:

import tkinter as tk
from pythonosc import udp_client

class OSCSenderApp:
    def __init__(self, master):
        self.master = master
        master.title("OSC Sender")

        self.label = tk.Label(master, text="OSC Sender")
        self.label.pack()

        self.button = tk.Button(master, text="Send OSC Signal", command=self.send_osc)
        self.button.pack()

    def send_osc(self):
        # Change the IP address and port to match your Arduino's IP address and the port it's listening on
        client = udp_client.SimpleUDPClient("192.168.130.4", 80)  # Use port 80
        client.send_message("/example", 1)  # You can change "/example" to any OSC address and 1 to any value

if __name__ == "__main__":
    root = tk.Tk()
    app = OSCSenderApp(root)
    root.mainloop()

Arduino程序接收信号并在串行监视器中显示:

#include <SPI.h>
#include <WiFiNINA.h>
#include <OSCMessage.h>
#include <OSCData.h>

char ssid[] = "VRRobot";      // your network SSID (name)
char pass[] = "Tech120Test";  // your network password
int status = WL_IDLE_STATUS;   // the WiFi radio's status

WiFiServer server(80);
WiFiClient client;  // Declare the client variable

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.print("Connecting to WiFi");
  while (status != WL_CONNECTED) {
    Serial.print(".");
    status = WiFi.begin(ssid, pass);
    delay(5000);
  }
  Serial.println("WiFi connected");

  server.begin();
}

void loop() {
  Serial.println("Loop started");

  // Check if there is a new client
  WiFiClient newClient = server.available();
  if (newClient) {
    if (client && client.connected()) {
      client.stop(); // Close the previous connection if there was one
      Serial.println("Previous client disconnected");
    }
    client = newClient; // Assign the new client to the global variable
    Serial.println("New client connected");
  }

  if (client && client.connected()) {
    while (client.available()) {
      OSCMessage msg;
      char c = client.read();
      msg.fill(c);

      if (msg.hasError()) {
        Serial.println("Error in OSC message");
        break;
      }

      // Assuming the message contains a single float value
      if (msg.getAddress() == "/example" && msg.size() == 1 && msg.isFloat(0)) {
        float value = msg.getFloat(0);
        Serial.print("Received OSC message: ");
        Serial.println(value);
      } else {
        Serial.println("Invalid OSC message format");
      }
      msg.empty(); // Clear the message for the next iteration
    }
  } else {
    Serial.println("No client or client not connected");
  }

  Serial.println("Loop completed");
}

基于上述代码的串行监视器输出:

No client or client not connected
Loop completed
Loop started
No client or client not connected
Loop completed
Loop started

我基于 Python-OSC 和 Arduino-OSC 背后的文档

任何帮助将不胜感激,我相信这是非常简单的事情。

Python Arduino osc

评论


答: 暂无答案