OSC in Arduino
Version vom 3. Mai 2022, 15:55 Uhr von HannahPernerWilson (Diskussion | Beiträge) (→3 OPEN CODE EXAMPLE)
Example for sending data from an Arduino ESP Dev Module to COMPUTER in the same network with OSC.
1 DOWNLOAD OSCuino LIBRARY
Download OSCuino library from github: https://github.com/CNMAT/OSC Code –> Download ZIP
2 INSTALL OSCuino LIBRARY
Arduino Menu: Sketch –> Include Library –> Add .ZIP Library select .ZIP file
3 OPEN CODE EXAMPLE
/////////////////////////////////////////////////
// Code for
// ESP32 DEVKIT V1 38pin version
/////////////////////////////////////////////////
// reads an analog value and sends it as OSC message
/////////////////////////////////////////////////
// when uploading code: sometimes necessairy
// to presss&hold the "BOOT" button
// when you see the following lines:
// "Connecting........_____....._____....."
/////////////////////////////////////////////////
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCBundle.h>
#define SENSOR_PIN_1 36
// OSC-Stuff ----------------------------------------------------------
///////////////////// VARIABLES TO EDIT /////////////////////////////
IPAddress outIp = {192, 168, 236, 90}; // IP Adress of Receiver PC
const char *ssid = "Ladenlokal"; // wifi name
const char *password = "puppe2010"; // wifi password (min. 8 characters)
const char *hostName = "myName"; // choose a name for your device
const char *oscAddress = "/myAddress/mySubAddress"; // choose an OSC address for sending your messages to
int sendFrequencyMillis = 40; // send new OSC message every somany milliseconds
/////////////////////////////////////////////////////////////////////
WiFiUDP Udp;
const unsigned int localPort = 9000; // not needed
const unsigned int destPort = 6448;
float s1; // variablee for storing sensor value
unsigned long millisOld; // variable for keeping track of time passed
void setup() {
Serial.begin(115200); // begin serial connection
WiFi.setHostname(hostName);
WiFi.mode(WIFI_STA); // WiFi mode station (connect to wifi router only)
WiFi.begin(ssid, password); // begin wifi connection
// Wait for connection:
uint16_t timeOut = 0;
while ((WiFi.status() != WL_CONNECTED) && (timeOut < 10)) {
delay(500);
timeOut++;
Serial.println(timeOut);
}
Serial.print("local IP address: ");
Serial.println(WiFi.localIP());
Udp.begin(localPort);
Serial.print("Listening at port: ");
Serial.println(localPort);
}
void loop() {
s1 = analogRead(SENSOR_PIN_1); // read sensor value
if (millis() >= (millisOld + sendFrequencyMillis)) {
millisOld = millis(); // update millisOld to be able to keep track of time
Serial.println(s1); // print sensor value
// publish sensor value over OSC:
if (WiFi.status() == WL_CONNECTED)
{
OSCMessage msg(oscAddress);
msg.add(s1);
Udp.beginPacket(outIp, destPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
}
}
}
4 UPLOAD SKETCH
Tip: sometimes you need to press and hold the BOOT button on the ESP for 2 seconds while Arduino IDE is trying to program.
“Hard resetting via RTS pin…” = upload was successful