/*
In Wokwi gateway, made forwarding of ports UDP
/Users/MacBookII/Desktop/wokwigw-darwin --forward udp:5555:10.13.37.2:5555 ; exit;
Sintaxis: --forward udp:[Puerto del localhost]:10.13.37.2:[Puerto del simulador] ; exit;
---------------------------------------------------------------------------------------------
Open Sound Control (OSC) library for the ESP8266/ESP32
--------------------------------------------------------------------------------------------- */
#include <WiFi.h>
#include <OSCMessage.h>
char ssid[] = "Wokwi-GUEST"; // 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 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 = 5555; // local port to listen for UDP packets (here's where we send the packets)
// Funcion para convertir el rando de los faders de o a 255
int dentrorango(float x) {
int resultado; // Se declara la variable resultado, esta es interna de la función
resultado = ((255*x)/1); //Es la operación que realiza la función
return resultado;
}
OSCErrorCode error;
float ledState = 0;
#define BUILTIN_LED 15
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, ledState); // turn *on* led
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
digitalWrite(BUILTIN_LED, HIGH);
Serial.print(".");
delay(250);
digitalWrite(BUILTIN_LED, LOW);
Serial.print("/");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
}
}
void recibirmensaje();
void led(OSCMessage &msg) {
ledState = msg.getFloat(0);
//digitalWrite(BUILTIN_LED, ledState);
Serial.print("/led: ");
Serial.println(ledState);
}
void brillo(OSCMessage &msg) {
ledState = msg.getFloat(0);
//digitalWrite(BUILTIN_LED, ledState);
Serial.print("/iman: ");
Serial.println(ledState);
Serial.println(dentrorango(ledState));
analogWrite(BUILTIN_LED, dentrorango(ledState));
}
void loop() {
OSCMessage msg;
int size = Udp.parsePacket();
if (size > 0) {
while (size--) {
msg.fill(Udp.read());
}
if (!msg.hasError()) {
msg.dispatch("/led", led); ///llama a las fuciones
msg.dispatch("/brillo", brillo);
} else {
error = msg.getError();
Serial.print("error: ");
Serial.println(error);
}
}
}