Browse Source

OSC implementation

connection to multicast udp
IP and port configurable through portal, saved in json
proof of concept of lessage filtering using ESP_NAME
Etienne Landon 8 years ago
parent
commit
9e87ef6ba0
4 changed files with 81 additions and 66 deletions
  1. 68 4
      osc.h
  2. 4 6
      tens_esp.ino
  3. 0 53
      udp.h
  4. 9 3
      wifimgr.h

+ 68 - 4
osc.h

@@ -5,8 +5,72 @@
 WiFiUDP UDP; //udp listener
 
 /* UDP CONFIGURATION */
-int UDP_In_Port = 9000;  //udp port input for ESP
+//int UDP_In_Port = 9000;  //udp port input for ESP
+//
+////default address and port to send to (IP read from config)
+//IPAddress UDP_Out_IP ;
+//int UDP_Out_Port = 8000 ;
+IPAddress ipMulti(239, 0, 0, 56);
+unsigned int portMulti = 12345;      // local port to listen on
+
+template <typename TYPE> void sendOSC(const char * adress, TYPE parameter);
+
+
+template <typename TYPE> void sendOSC(const char * adress, TYPE parameter) {
+  OSCMessage OSCmsg(adress);
+  OSCmsg.add(parameter);
+  UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
+  OSCmsg.send(UDP); // send the bytes to the SLIP stream
+  UDP.endPacket(); // mark the end of the OSC Packet
+  OSCmsg.empty(); // free space occupied by message
+}
+
+void printTest(OSCMessage &msg) {
+  
+    Serial.print("OSC type : ");
+    Serial.println (msg.getType(0));
+    sendOSC("/received", msg.getType(0));
+  
+}
+
+void setup_OSC(){
+  
+  ipMulti.fromString(UDP_IP);
+  
+  String UDP_PORT_string = UDP_PORT;
+  portMulti = UDP_PORT_string.toInt();
+
+  Serial.print("connecting udp to ");
+  Serial.print(ipMulti);
+  Serial.print(":");
+  Serial.println(portMulti);
+  
+  UDP.beginMulticast(WiFi.localIP(),  ipMulti, portMulti);
+  OSCMessage OSCmsg("/connected");
+  for (int i=0 ; i<4 ; i++){
+    OSCmsg.add(WiFi.localIP()[i]);
+  }
+  UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
+  OSCmsg.send(UDP); // send the bytes to the SLIP stream
+  UDP.endPacket(); // mark the end of the OSC Packet
+  OSCmsg.empty(); // free space occupied by message
+}
+
+void handle_OSC() {
+  OSCMessage OSCin;
+  int size;
+
+  if ( (size = UDP.parsePacket()) > 0) {
+    while (size--)
+    OSCin.fill(UDP.read());
+
+    //Declare valid OSC messages here 
+    if (!OSCin.hasError()) {
+      OSCin.dispatch("/test", printTest);
+      if (OSCin.match(ESP_NAME)){Serial.println("matched");}
+      
+      
+    }
+  }
+}
 
-//default address and port to send to (IP read from config)
-IPAddress UDP_Out_IP ;
-int UDP_Out_Port = 8000 ;

+ 4 - 6
tens_esp.ino

@@ -19,8 +19,6 @@
 #include "leds.h"     //config and functions relative to leds
 #include "ota.h"      //config and functions relative to ota firmware updates
 #include "wifimgr.h"   //config and functions relative to wifi and access point configuration and configuration permanent saving
-
-
 #include "osc.h"  //some helpers functions
 
 
@@ -36,9 +34,9 @@ void setup ( void ) {
   
   setupWifi(); 
 
-  //setupUDP() ;
+  setup_OSC() ;
   
-  //setupOTA();
+  setupOTA();
  
   blueLedState(-1, 500);
   
@@ -49,8 +47,8 @@ void setup ( void ) {
 void loop ( void ) {
   //Serial.println(WiFi.status());
   StartConfigAP();
-  //checkOTA();
-  //UDP_send_receive();
+  checkOTA();
+  handle_OSC();
 
   
 }

+ 0 - 53
udp.h

@@ -1,53 +0,0 @@
-
-
-//Ticker tkUKI;  // periodic send ADC to UDP
-int GSR_sensor;
-bool flag_UDP;
-int  UDP_tick = 50 ; //delay between to udp send
-
-
-void UDP_flag(){
-  flag_UDP = true;
-}
-
-
-void UDP_send_receive() {
-
-  if (flag_UDP) {
-    flag_UDP = false;
-    redLedState (1, 300);
-      
-       /*  UKI part  */
-    GSR_sensor = analogRead(A0);
-    IPAddress ipMulti(239, 0, 0, 57);
-    UDP.beginPacketMulticast( ipMulti, 12345, WiFi.localIP() );//
-    //UDP.beginPacket(UDP_Out_IP, UDP_Out_Port);
-    UDP.print(ESP_NAME);
-    UDP.print(" ");
-    UDP.print(GSR_sensor);
-    UDP.endPacket();
-      
-    //Check udp in
-    int packetSize = UDP.parsePacket();
-    
-    if(packetSize) {
-      UDP_Out_IP = UDP.remoteIP();
-      UDP.beginPacket(UDP_Out_IP, 8000);
-      UDP.print("new master ip");
-      UDP.endPacket();
-    }
-    redLedState (0, 300);
-  }
-  
-
- 
-}
-
-
-void setupUDP(){
-  UDP_Out_IP.fromString(UDP_IP);
-  Serial.print ("sending UDP to ");
-  Serial.println (UDP_Out_IP);
-  UDP.begin(UDP_In_Port); 
-  //tkUKI.attach_ms(UDP_tick, UDP_flag); //raises flag 
-}

+ 9 - 3
wifimgr.h

@@ -22,8 +22,8 @@ bool flag_ConfigPortal = false;
 int timeout = 30000;
 
 char ESP_NAME[40];
-char UDP_PORT[6] = "9000";
-char UDP_IP[16] = "192.168.10.100";
+char UDP_PORT[10] = "12345";
+char UDP_IP[16] = "239.0.0.57";
 
 
 
@@ -47,6 +47,12 @@ void ReadConfig() {
   Serial.println("mounting FS...");
 
   if (SPIFFS.begin()) {
+  
+  // UNCOMMENT TO FORMAT CONFIG FILE, UPLOAD, EXECUTE ONCE AND REUPLOAD WITH THIS COMMENTEND
+//  Serial.println("Please wait 30 secs for SPIFFS to be formatted");
+//  SPIFFS.format();
+//  Serial.println("Spiffs formatted");
+  
     Serial.println("mounted file system");
     if (SPIFFS.exists("/config.json")) {
       //file exists, reading and loading
@@ -133,7 +139,7 @@ void StartConfigAP(){
     // After connecting, parameter.getValue() will get you the configured value
     // id/name placeholder/prompt default length
     WiFiManagerParameter custom_ESP_NAME("name", "ESP NAME", ESP_NAME, 40);
-    WiFiManagerParameter custom_UDP_PORT("port", " UDP port", UDP_PORT, 5);
+    WiFiManagerParameter custom_UDP_PORT("port", " UDP port", UDP_PORT, 10);
     WiFiManagerParameter custom_UDP_IP("ip", "UDP IP", UDP_IP, 32);
   
     //Local intialization. Once its business is done, there is no need to keep it around