0%

透過OSC讓NodeMCU與Processing溝通

Open Sound Control

Open Sound Control(OSC),是一個讓電腦、混音器跟其他多媒體裝置利用網路溝通的通訊協定。把現代網路的優點帶入了電子音樂的世界,OSC 的優勢包含了互動性、準確、彈性以及強化的組織與文件。

這個簡單而強大的協議提供了實時控制聲音和其他媒體處理所需的一切,同時保持彈性和易於使用。

想要知道更詳細的介紹可以去官網

需要的材料

Nodemcu
Arduino IDE 1.8.3
Processing 3.3.5

Arduino IDE安裝OSC

  1. 在arduino IDE裡面要安裝OSC的library,我們需要先到Github上下載檔案,直接Download ZIP就可以了
    螢幕快照 2017-08-03 下午12.54.36
  2. 下載完畢之後,打開Arduino IDE>Sketch>Include Library>Add .ZIP Library,接著選擇剛剛下載下來的ZIP檔就完成Library的安裝了
    螢幕快照 2017-08-03 下午12.57.26

Processing安裝OSC

  1. 開啟Prcoessing>Sketch>Import Library>Add Library…
    螢幕快照 2017-08-03 下午1.02.50
  2. 進入之後搜尋”OSC”,點選oscP5接著Install就可以完成安裝了
    螢幕快照 2017-08-03 下午1.03.18

編譯並執行程式碼

Arduino的程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCBundle.h>

char ssid[] = "your SSID";
char pass[] = "your Password";

WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
const unsigned int localPort = 8000; // local port to listen for UDP packets at the NodeMCU (another device must send OSC messages to this port)
const unsigned int destPort = 9000; // remote port of the target device where the NodeMCU sends OSC to

int ledState = 0;

void setup() {

pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(115200);
//Set static IP
WiFi.config(IPAddress(192, 168, 1, 25), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
// Connect to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid,pass);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
}

void OSC() {
OSCMessage msgIN;
int size;
if ((size = Udp.parsePacket()) > 0) {
while (size--)
msgIN.fill(Udp.read());
if (!msgIN.hasError()) {
msgIN.route("/led", led); //if message is come from "/led",it will run function led
}
}
}

void led(OSCMessage &msg, int addrOffset) {
ledState = msg.getInt(0); //save recivied data to ledState
}

void loop() {
OSC();
digitalWrite(LED_BUILTIN,ledState);//if led State is 0,led will be light(nodemcu's LED_BUILTIN is reversed logic)
}

Processing的程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// OSC
import oscP5.*;
import netP5.*;

// OSC
OscP5 oscP5;
NetAddress NodeMCULocation;
String HOST = "192.168.1.25"; //nodemcu's IP
int HOST_PORT = 8000; //port for Ground_Control Computer
int LOCAL_PORT = 9000; //port for my incoming port

void setup() {
size(200, 200);
// OSC SETUP
oscP5 = new OscP5(this, LOCAL_PORT);
NodeMCULocation = new NetAddress(HOST, HOST_PORT);
}

void draw() {
background(0);
}

void sendControlInt(String tag, int val) {
OscMessage myOscMessage = new OscMessage(tag);
myOscMessage.add(val);
oscP5.send(myOscMessage, NodeMCULocation);
print(tag+",");
println(val);
}

void keyPressed() {
if (key =='l') {
sendControlInt("/led", 1);
}
if (key =='k') {
sendControlInt("/led", 0);
}
}

執行程式之後會如下方這樣工作
20624325_1631332766879578_302485918_n

reference

https://dotblogs.com.tw/rickyteng/2016/02/01/114431
http://opensoundcontrol.org/introduction-osc
https://github.com/CNMAT/OSC