osc.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //OSC via udp
  2. #include <WiFiUdp.h>
  3. #include <OSCMessage.h>
  4. WiFiUDP UDP; //udp listener
  5. /* UDP CONFIGURATION */
  6. //int UDP_In_Port = 9000; //udp port input for ESP
  7. //
  8. ////default address and port to send to (IP read from config)
  9. //IPAddress UDP_Out_IP ;
  10. //int UDP_Out_Port = 8000 ;
  11. IPAddress ipMulti(239, 0, 0, 56);
  12. unsigned int portMulti = 12345; // local port to listen on
  13. template <typename TYPE> void sendOSC(const char * adress, TYPE parameter);
  14. template <typename TYPE> void sendOSC(const char * adress, TYPE parameter) {
  15. OSCMessage OSCmsg(adress);
  16. OSCmsg.add(parameter);
  17. UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
  18. OSCmsg.send(UDP); // send the bytes to the SLIP stream
  19. UDP.endPacket(); // mark the end of the OSC Packet
  20. OSCmsg.empty(); // free space occupied by message
  21. }
  22. void printTest(OSCMessage &msg) {
  23. Serial.print("OSC type : ");
  24. Serial.println (msg.getType(0));
  25. sendOSC("/received", msg.getType(0));
  26. }
  27. void setup_OSC(){
  28. ipMulti.fromString(UDP_IP);
  29. String UDP_PORT_string = UDP_PORT;
  30. portMulti = UDP_PORT_string.toInt();
  31. Serial.print("connecting udp to ");
  32. Serial.print(ipMulti);
  33. Serial.print(":");
  34. Serial.println(portMulti);
  35. UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
  36. OSCMessage OSCmsg("/connected");
  37. for (int i=0 ; i<4 ; i++){
  38. OSCmsg.add(WiFi.localIP()[i]);
  39. }
  40. UDP.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
  41. OSCmsg.send(UDP); // send the bytes to the SLIP stream
  42. UDP.endPacket(); // mark the end of the OSC Packet
  43. OSCmsg.empty(); // free space occupied by message
  44. }
  45. void handle_OSC() {
  46. OSCMessage OSCin;
  47. int size;
  48. if ( (size = UDP.parsePacket()) > 0) {
  49. while (size--)
  50. OSCin.fill(UDP.read());
  51. //Declare valid OSC messages here
  52. if (!OSCin.hasError()) {
  53. OSCin.dispatch("/test", printTest);
  54. if (OSCin.match(ESP_NAME)){Serial.println("matched");}
  55. }
  56. }
  57. }