Lab_interaccio/2019/GIGANTES-MERCE/ESP8266-OSC/ESP8266-OSC.ino
2025-02-25 21:29:42 +01:00

160 lines
4 KiB
C++

/*---------------------------------------------------------------------------------------------
Open Sound Control (OSC) library for the ESP8266/ESP32
Example for receiving open sound control (OSC) messages on the ESP8266/ESP32
Send integers '0' or '1' to the address "/led" to turn on/off the built-in LED of the esp8266.
This example code is in the public domain.
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char* ssid = "BRoomX-746"; // your network SSID (name)
char* pass = "Broomx88"; // your network password
//char* ssid = "ESP32"; // your network SSID (name)
//char* pass = ""; // your network password
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
const IPAddress local_IP(10, 100, 50, 76); // local IP (if is need it)
const IPAddress gateway(10, 100, 50, 1);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress outIp(10,40,10,105); // remote IP (not needed for receive)
const unsigned int outPort = 9999; // remote port (not needed for receive)
const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets)
OSCErrorCode error;
unsigned int ledState = LOW; // LOW means led is *on*
#define UDP_RX_PACKET_MAX_SIZE 4
#ifndef BUILTIN_LED
#ifdef LED_BUILTIN
#define BUILTIN_LED LED_BUILTIN
#else
#define BUILTIN_LED 13
#endif
#endif
String translateEncryptionType(wifi_auth_mode_t encryptionType) {
switch (encryptionType) {
case (WIFI_AUTH_OPEN):
return "Open";
case (WIFI_AUTH_WEP):
return "WEP";
case (WIFI_AUTH_WPA_PSK):
return "WPA_PSK";
case (WIFI_AUTH_WPA2_PSK):
return "WPA2_PSK";
case (WIFI_AUTH_WPA_WPA2_PSK):
return "WPA_WPA2_PSK";
case (WIFI_AUTH_WPA2_ENTERPRISE):
return "WPA2_ENTERPRISE";
}
}
void scanNetworks() {
int numberOfNetworks = WiFi.scanNetworks();
Serial.print("Number of networks found: ");
Serial.println(numberOfNetworks);
for (int i = 0; i < numberOfNetworks; i++) {
Serial.print("Network name: ");
Serial.println(WiFi.SSID(i));
Serial.print("Signal strength: ");
Serial.println(WiFi.RSSI(i));
Serial.print("MAC address: ");
Serial.println(WiFi.BSSIDstr(i));
Serial.print("Encryption type: ");
String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
Serial.println(encryptionTypeDescription);
Serial.println("-----------------------");
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, ledState); // turn *on* led
Serial.begin(115200);
//scanNetworks();
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
WiFi.setSleep(false);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(localPort);
}
void led(OSCMessage &msg)
{
ledState = msg.getFloat(0);
//ledState = msg.getInt(0);
digitalWrite(BUILTIN_LED, ledState);
Serial.print("led: ");
Serial.println(ledState);
}
void loop() {
OSCMessage msg;
int size = Udp.parsePacket();
if (size > 0) {
while (size--) {
//msg.fill(Udp.read());
uint8_t packetBuffer[UDP_RX_PACKET_MAX_SIZE];
Udp.read(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
msg.fill(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
}
if (!msg.hasError()) {
msg.dispatch("/1/led", led);
} else {
error = msg.getError();
Serial.print("error: ");
Serial.println(error);
}
}
}