OSC

http://opensoundcontrol.org/

http://en.wikipedia.org/wiki/Open_Sound_Control

Control app per creare interfacce utente via OSC

Lib OSC utilizzata:

Documentazione Lib OSC:

Esempio di sola ricezione (send via app Sensors2OSC)

Sensors2OSC app

#include <ESP8266WiFi.h>
 
#include <WiFiUdp.h>
const byte MAX_MSG_SIZE PROGMEM=100;
byte packetBuffer[MAX_MSG_SIZE];  //buffer to hold incoming udp packet
WiFiUDP Udp;
 
#include <OSCBundle.h>
#include <OSCBoards.h>
float gX, gY;
 
// modificare le due righe sucessive se si vuole passare in modalità AP
const String ssid="ArduinoAfternoon";
const String pass="...";
 
void setup() {
    pinMode(LED_BUILTIN,OUTPUT);
 
    Serial.begin(115200);
 
	// modificare le righe sucessive se si vuole passare in modalità AP (check doc) da qui...
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), pass.c_str());
 
    Serial.print("Connecting...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
 
    Serial.print("Connected, IP address: ");
    Serial.println(WiFi.localIP());
    // ...fin qui
 
 
    //setup ethernet part
    Udp.begin(7400); // porta default OSC
 
    multiPingLed(10);
}
 
 
void loop() {
    receiveOSC();
 
    if(gX>0 && gY>0) // cambiare quadrante!!! (devo spiegare io purtroppo)
        pingLed();
 
    delay(50);
}
 
void receiveOSC() {
    Serial.println("osc...");
 
    //OSCBundle bundleIN; // NO
    OSCMessage messageIN;
    int size;
    if( (size = Udp.parsePacket())>0) {
        //pingLed();
        //oscLastPacketTime=millis();
 
        Serial.println("received");
        Serial.printf("Received %d bytes from %s, port %d\n", size, Udp.remoteIP().toString().c_str(), Udp.remotePort());
 
        Udp.read(packetBuffer,size);
        messageIN.fill(packetBuffer,size);
 
        if(!messageIN.hasError()) {
            //Serial.println("----------->  no error");
            //messageIN.route("/accelerometer/X", accelX);
            //messageIN.route("/accelerometer/Y", accelY);
            //messageIN.route("/accelerometer/Z", accelZ);
 
            //messageIN.route("/temperature", temp);
            //messageIN.route("/magnetometer", magn);
            //messageIN.route("/accelerometer", accel);
            //messageIN.route("/accelerometer", accel);
 
            //bundleIN.dispatch("/accelerometer/X", accel);
            messageIN.route("/gravity/X", gravityX);
            messageIN.route("/gravity/Y", gravityY);
            //messageIN.route("/gravity/Z", gravityZ);
        }
        Udp.flush();
    }
}
 
void gravityX(OSCMessage &msg, int addrOffset) {
    gX=msg.getFloat(0);
    Serial.print("gX:");
    Serial.println(gX);
}
 
void gravityY(OSCMessage &msg, int addrOffset) {
    gY=msg.getFloat(0);
    Serial.print("gY:");
    Serial.println(gY);
}
 
void pingLed() {
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)
    delay(10);
    digitalWrite(LED_BUILTIN, HIGH);    // turn the LED off by making the voltage LOW
}
 
void multiPingLed(int many) {
    for(int i=0; i<many; i++)
        pingLed();
}

Esempio send&receive

A cura di due studenti del corso (se volete pubblico i nomi, ditemi voi)

Il sender è collegato ad un sensore a ultrasuoni (SR04, libreria NewPing) e manda la distanza misurata da un ostacolo al receiver che usa quell'informazione per controllare la luminosità di un LED.

#include <ESP8266WiFi.h>
#include <NewPing.h>
 
#define TRIGGER_PIN  D7
#define ECHO_PIN     D1
#define MAX_DISTANCE 250
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
#include <WiFiUdp.h>
const byte MAX_MSG_SIZE PROGMEM=100;
byte packetBuffer[MAX_MSG_SIZE];  //buffer to hold incoming udp packet
WiFiUDP Udp;
 
#include <OSCBundle.h>
#include <OSCBoards.h>
//https://en.wikipedia.org/wiki/Open_Sound_Control
 
// modificare le due righe sucessive se si vuole passare in modalità AP
const String ssid="ArduinoAfternoon";
const String pass="lavagnamit";
IPAddress outIp(192, 168, 1, 11);
 
void setup() {
    Serial.begin(115200);
  // modificare le righe sucessive se si vuole passare in modalità AP (check doc) da qui...
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), pass.c_str());
 
    Serial.print("Connecting...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
 
    Serial.print("Connected, IP address: ");
    Serial.println(WiFi.localIP());
    // ...fin qui
 
 
    //setup ethernet part
    Udp.begin(8000); // porta default OSC
}
 
 
void loop() {
    sendOSC();
    delay(150);
}
 
void sendOSC() {
    OSCMessage msg("/address");
    int cm = sonar.ping_cm();
    msg.add(cm);
    Udp.beginPacket(outIp, 8000);
    msg.send(Udp); // send the bytes to the SLIP stream
    Udp.endPacket(); // mark the end of the OSC Packet
    msg.empty(); // free space occupied by message
}
#include <ESP8266WiFi.h>
 
#include <WiFiUdp.h>
const byte MAX_MSG_SIZE PROGMEM=100;
byte packetBuffer[MAX_MSG_SIZE];  //buffer to hold incoming udp packet
WiFiUDP Udp;
 
#include <OSCBundle.h>
#include <OSCBoards.h>
float gX, gY;
//https://en.wikipedia.org/wiki/Open_Sound_Control
 
// modificare le due righe sucessive se si vuole passare in modalità AP
const String ssid="ArduinoAfternoon";
const String pass="lavagnamit";
 
void setup() {
    pinMode(D0, OUTPUT);
 
    Serial.begin(115200);
 
  // modificare le righe sucessive se si vuole passare in modalità AP (check doc) da qui...
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), pass.c_str());
 
    Serial.print("Connecting...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
 
    Serial.print("Connected, IP address: ");
    Serial.println(WiFi.localIP());
    // ...fin qui
 
 
    //setup ethernet part
    Udp.begin(8000); // porta default OSC
}
 
 
void loop() {
    receiveOSC();
 
    delay(150);
}
 
void receiveOSC() {
    Serial.println("osc...");
 
    //OSCBundle bundleIN; // NO
    OSCMessage messageIN;
    int size;
    if( (size = Udp.parsePacket())>0) {
 
        Serial.println("received");
        Serial.printf("Received %d bytes from %s, port %d\n", size, Udp.remoteIP().toString().c_str(), Udp.remotePort());
 
        Udp.read(packetBuffer,size);
        messageIN.fill(packetBuffer,size);
 
        if(!messageIN.hasError()) {
            int data = messageIN.getInt(0);
            Serial.printf("Data refcieved: %d\n", data);
            if (data!= 0)
              analogWrite(D0, data);
        }
        Udp.flush();
    }
}
pub/sistemiembedded/osc.txt · Last modified: 2017/11/11 09:00 by atrent
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0