ESP8266ReceiveBundle.ino 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*---------------------------------------------------------------------------------------------
  2. Open Sound Control (OSC) library for the ESP8266/ESP32
  3. Example for receiving open sound control (OSC) bundles on the ESP8266/ESP32
  4. Send integers '0' or '1' to the address "/led" to turn on/off the built-in LED of the esp8266.
  5. This example code is in the public domain.
  6. --------------------------------------------------------------------------------------------- */
  7. #ifdef ESP8266
  8. #include <ESP8266WiFi.h>
  9. #else
  10. #include <WiFi.h>
  11. #endif
  12. #include <WiFiUdp.h>
  13. #include <OSCMessage.h>
  14. #include <OSCBundle.h>
  15. #include <OSCData.h>
  16. char ssid[] = "*****************"; // your network SSID (name)
  17. char pass[] = "*******"; // your network password
  18. // A UDP instance to let us send and receive packets over UDP
  19. WiFiUDP Udp;
  20. const IPAddress outIp(10,40,10,105); // remote IP (not needed for receive)
  21. const unsigned int outPort = 9999; // remote port (not needed for receive)
  22. const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets)
  23. OSCErrorCode error;
  24. unsigned int ledState = LOW; // LOW means led is *on*
  25. #ifndef BUILTIN_LED
  26. #ifdef LED_BUILTIN
  27. #define BUILTIN_LED LED_BUILTIN
  28. #else
  29. #define BUILTIN_LED 13
  30. #endif
  31. #endif
  32. void setup() {
  33. pinMode(BUILTIN_LED, OUTPUT);
  34. digitalWrite(BUILTIN_LED, ledState); // turn *on* led
  35. Serial.begin(115200);
  36. // Connect to WiFi network
  37. Serial.println();
  38. Serial.println();
  39. Serial.print("Connecting to ");
  40. Serial.println(ssid);
  41. WiFi.begin(ssid, pass);
  42. while (WiFi.status() != WL_CONNECTED) {
  43. delay(500);
  44. Serial.print(".");
  45. }
  46. Serial.println("");
  47. Serial.println("WiFi connected");
  48. Serial.println("IP address: ");
  49. Serial.println(WiFi.localIP());
  50. Serial.println("Starting UDP");
  51. Udp.begin(localPort);
  52. Serial.print("Local port: ");
  53. #ifdef ESP32
  54. Serial.println(localPort);
  55. #else
  56. Serial.println(Udp.localPort());
  57. #endif
  58. }
  59. void led(OSCMessage &msg) {
  60. ledState = msg.getInt(0);
  61. digitalWrite(BUILTIN_LED, ledState);
  62. Serial.print("/led: ");
  63. Serial.println(ledState);
  64. }
  65. void loop() {
  66. OSCBundle bundle;
  67. int size = Udp.parsePacket();
  68. if (size > 0) {
  69. while (size--) {
  70. bundle.fill(Udp.read());
  71. }
  72. if (!bundle.hasError()) {
  73. bundle.dispatch("/led", led);
  74. } else {
  75. error = bundle.getError();
  76. Serial.print("error: ");
  77. Serial.println(error);
  78. }
  79. }
  80. }