I have an ESP8266 breakout connected to a Teensy 3.2
The ESP is set up to receive OSC messages over UDP and then send them to the Teensy over serial with SLIP.
The teensy is set up to output the received OSC messages to the USB serial that my computer is connected to.
When I send an OSC message the Teensy receives it, and routes it to the correct function (in other words, the address is correct), but when I try to read the value of the message it always comes out as 0.
I tested what would happen if I had the ESP route and read the OSC messages, and print the values to serial (that my computer now is connected to).
In this scenario everything works great! I get the correct values printed in the serial monitor.
So I'm wondering if sending the messages from the ESP to the Teensy by SLIP destroys the packets somehow?
Heres the code I'm running on the ESP when everything works fine:
Heres the code I'm running when I'm trying to use SLIP to send messages to the Teensy
ESP-code:
Teensy code
The ESP is set up to receive OSC messages over UDP and then send them to the Teensy over serial with SLIP.
The teensy is set up to output the received OSC messages to the USB serial that my computer is connected to.
When I send an OSC message the Teensy receives it, and routes it to the correct function (in other words, the address is correct), but when I try to read the value of the message it always comes out as 0.
I tested what would happen if I had the ESP route and read the OSC messages, and print the values to serial (that my computer now is connected to).
In this scenario everything works great! I get the correct values printed in the serial monitor.
So I'm wondering if sending the messages from the ESP to the Teensy by SLIP destroys the packets somehow?
Heres the code I'm running on the ESP when everything works fine:
Code:
// This program converts UDP OSC messages into SLIP Serial OSC messages on the ESP8266 wifi board
// At the moment there is no OSC bundle support, not sure I need it at the moment
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <OSCMessage.h>
//#include <SLIPEncodedSerial.h>
int status = WL_IDLE_STATUS;
const char* ssid = "xxxxx"; // your network name (SSID)
const char* pass = "xxxxx"; // your network password
int localPort = 8000;
int destPort = 9000;
IPAddress outIp(10, 0, 1, 215); //default IP, will change with received udp
IPAddress espip(10, 0, 1, 14);
IPAddress gateway(10,0,1,1);
IPAddress subnet(255,255,255,0);
WiFiUDP Udp;
//SLIPEncodedSerial SLIPSerial(Serial);
void setup()
{
//SLIPSerial.begin(115200);
Serial.begin(115200);
WiFi.begin(ssid, pass);
WiFi.config(espip,gateway,subnet);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tries++;
if (tries > 30){
break;
}
}
Udp.begin(localPort);
}
void loop(){
OSCMsgReceive();
}
void OSCMsgReceive(){
OSCMessage msgIN;
int size;
if((size = Udp.parsePacket())>0){
while(size--)
msgIN.fill(Udp.read());
if(!msgIN.hasError()){
msgIN.route("/OnOff/toggle1",toggleOnOff);
msgIN.route("/Fader/Value",funcValue);
msgIN.route("/XY/xy",xy);
}
}
}
void xy(OSCMessage &msg, int addrOffset)
{
float xValue;
float yValue;
xValue = msg.getFloat(0);
yValue = msg.getFloat(1);
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
}
void toggleOnOff(OSCMessage &msg, int addrOffset)
{
float ledState;
ledState = msg.getFloat(0);
//OSCMessage msgOUT("/OnOff/toggle1");
//msgOUT.add(ledState);
//digitalWrite(ledPin, ledState);
if (ledState) {
Serial.println("LED on");
Serial.println(ledState);
}
else {
Serial.println("LED off");
Serial.println(ledState);
}
//ledState = !ledState; // toggle the state from HIGH to LOW to HIGH to LOW ...
//send osc message back to control 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.
//SLIPSerial.beginPacket();
//msgOUT.send(SLIPSerial); // send the bytes
//SLIPSerial.endPacket(); // mark the end of the OSC Packet
//msgOUT.empty(); // free space occupied by message
}
void funcValue(OSCMessage &msg, int addrOffset ){
float value = msg.getFloat(0);
//OSCMessage msgOUT("/Fader/Value");
Serial.print("Value = : ");
Serial.println(value);
//msgOUT.add(value);
//SLIPSerial.beginPacket();
//msgOUT.send(SLIPSerial); // send the bytes
//SLIPSerial.endPacket(); // mark the end of the OSC Packet
//msgOUT.empty(); // free space occupied by message
}
Heres the code I'm running when I'm trying to use SLIP to send messages to the Teensy
ESP-code:
Code:
// This program converts UDP OSC messages into SLIP Serial OSC messages on the ESP8266 wifi board
// At the moment there is no OSC bundle support, not sure I need it at the moment
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <OSCMessage.h>
#include <SLIPEncodedSerial.h>
int status = WL_IDLE_STATUS;
const char* ssid = "xxxxx"; // your network name (SSID)
const char* pass = "xxxxx"; // your network password
int localPort = 8000;
int destPort = 9000;
IPAddress outIp(10, 0, 1, 215); //default IP, will change with received udp
IPAddress espip(10, 0, 1, 14);
IPAddress gateway(10,0,1,1);
IPAddress subnet(255,255,255,0);
WiFiUDP Udp;
SLIPEncodedSerial SLIPSerial(Serial);
void setup()
{
SLIPSerial.begin(115200);
WiFi.begin(ssid, pass);
WiFi.config(espip,gateway,subnet);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tries++;
if (tries > 30){
break;
}
}
Udp.begin(localPort);
}
void loop(){
// read=udp->serial, write=serial->udp
int rd,wr;
OSCMessage rMsg, wMsg;
//static unsigned int bp = 0;
static bool packet = 0;
static unsigned long tr = 0, tw = 0;
if((rd = Udp.parsePacket())>0){
if(tr - micros() > 10000)
{
outIp = Udp.remoteIP();
while (rd--)
rMsg.fill(Udp.read());
if(!rMsg.hasError())
{
SLIPSerial.beginPacket();
rMsg.send(SLIPSerial);
SLIPSerial.endPacket();
}
rMsg.empty();
}
else
{
while (rd--)
Udp.read();
}
tr = micros();
}
while(!SLIPSerial.endofPacket()) {
if(wr = SLIPSerial.available()>0){
tw = micros();
if(!packet)
packet = 1;
while(wr--) { //this needs a byte limit
wMsg.fill(SLIPSerial.read());
//if(++bp >= 512) break; //packets seem to get truncated about here anyway
}
}
if((micros() - tw) > 10000) break; //Timeout for no eoP()
//if(bp >= 512) break; //break again
}
if(packet) {
if(!wMsg.hasError()) {
Udp.beginPacket(outIp, destPort);
wMsg.send(Udp);
Udp.endPacket();
packet = 0;
}
}
}
Teensy code
Code:
//DHCP-based OSC server test code
//#include <SPI.h>
#include <SLIPEncodedSerial.h>
#include <OSCMessage.h>
SLIPEncodedSerial SLIPSerial(Serial1);
void setup(){
Serial.begin(9600); //Teensy <=> Computer
SLIPSerial.begin(115200); //Teensy <=> ESP
delay(1000);
Serial.println("Started");
}
void loop(){
OSCMsgReceive();
}
void xy(OSCMessage &msg, int addrOffset)
{
float xValue;
float yValue;
xValue = msg.getFloat(1);
yValue = msg.getFloat(2);
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
}
void toggleOnOff(OSCMessage &msg, int addrOffset)
{
float ledState;
ledState = msg.getFloat(0);
OSCMessage msgOUT("/OnOff/toggle1");
msgOUT.add(ledState);
//digitalWrite(ledPin, ledState);
if (ledState) {
Serial.println("LED on");
Serial.println(ledState);
}
else {
Serial.println("LED off");
Serial.println(ledState);
}
//ledState = !ledState; // toggle the state from HIGH to LOW to HIGH to LOW ...
//send osc message back to control 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.
SLIPSerial.beginPacket();
msgOUT.send(SLIPSerial); // send the bytes
SLIPSerial.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcValue(OSCMessage &msg, int addrOffset ){
float value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Value");
Serial.print("Value = : ");
Serial.println(value);
msgOUT.add(value);
SLIPSerial.beginPacket();
msgOUT.send(SLIPSerial); // send the bytes
SLIPSerial.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void OSCMsgReceive()
{
OSCMessage msgIN;
int size;
while(!SLIPSerial.endofPacket())
{
if((size = SLIPSerial.available()) > 0)
{
while(size--)
{
msgIN.fill(SLIPSerial.read());
}
if(!msgIN.hasError())
{
msgIN.route("/OnOff/toggle1",toggleOnOff);
msgIN.route("/Fader/Value",funcValue);
msgIN.route("/XY/xy",xy);
}
}
}
}