philsson 9302eadae7 Touch OSC and NodeMCU
1) A layout for the mobile phone
2) A sketch to receive the data on a NodeMCU over Wifi
3) A sketch to send a RC package to the stm32 from the NodeMCU

TODO: Merge these two sketches later
2020-06-13 15:45:43 +02:00

184 lines
4.6 KiB
C++

/*---------------------------------------------------------------------------------------------
Open Sound Control (OSC) library for the ESP8266/ESP32
Example for receiving open sound control (OSC) bundles 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.
Inspired from https://trippylighting.com/teensy-arduino-ect/touchosc-and-arduino-oscuino/
--------------------------------------------------------------------------------------------- */
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char ssid[] = "FaRgO2G4"; // your network SSID (name)
char pass[] = "Johansson85"; // your network password
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
const IPAddress outIp(192,168,1,72); // 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*
#ifndef BUILTIN_LED
#ifdef LED_BUILTIN
#define BUILTIN_LED LED_BUILTIN
#else
#define BUILTIN_LED 13
#endif
#endif
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(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: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
}
void OSCMsgReceive(){
OSCMessage msgIN;
int size;
if((size = Udp.parsePacket())>0){
while(size--)
msgIN.fill(Udp.read());
if(!msgIN.hasError()){
msgIN.route("/Robot/Enable",toggleEnabled);
msgIN.route("/Fader/Throttle",funcThrottle);
msgIN.route("/Fader/Steering",funcSteering);
msgIN.route("/Gain/Kp",funcKp);
//msgIN.route("/Gain/Ki",funcKi);
//msgIN.route("/Gain/Kd",funcKd);
}
}
}
void toggleEnabled(OSCMessage &msg, int addrOffset){
ledState = (boolean) msg.getFloat(0);
OSCMessage msgOUT("/Robot/Enable");
digitalWrite(BUILTIN_LED, !ledState);
msgOUT.add(ledState);
if (ledState) {
Serial.println("Robot enabled");
}
else {
Serial.println("Robot disabled");
}
//send osc message back to controll object in TouchOSC
//Local feedback is turned off in the TouchOSC interface.
//The button is turned on in TouchOSC interface whe the conrol receives this message.
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcThrottle(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Throttle");
Serial.print("Throttle = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcSteering(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Steering");
Serial.print("Steering = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcKp(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Gain/Kp");
Serial.print("Kp = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
// Redo this for label
OSCMessage msgLABEL("/Gain/KpOut");
msgLABEL.add(value);
Udp.beginPacket(Udp.remoteIP(), outPort);
msgLABEL.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgLABEL.empty(); // free space occupied by message
}
void loop() {
OSCMsgReceive();
}