Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// <Arduino.h> is needed so that "settings.h" can refer to the String type.
// It just so happens that "RNXV.h" will load <Arduino.h> so we can skip
// the call here if we load "RNXV.h" first.
// #include <Arduino.h>
#include "RNXV.h"
#include "settings.h"
int led = 13; // For Teensy 3.1
int ts = 0;
int delta = 0;
// This is the object that will wrap calls to the serial device handling the WiFly
RNXV rnxv = RNXV();
String rnvxContent = "";
String usbSerialBuffer = "";
char usbSerialChar;
// Used to building a float arg for an OSC message
float f = 0.0;
union u_tag {
byte b[4];
float fval;
} u;
String cmds1[] = {
"set ip dhcp 1", // 0 means don't get IP form DHCP server. 1 means grab one va DHCP
"set wlan phrase " + passPhrase,
"set wlan ssid " + ssid,
"set wlan auth 4",
"set wlan join 1",
"set ip proto 3" , // should cover both TCP adn UDP
"set ip host " + remoteHost, // The remote host address
"set ip remote_port " + remotePort, // the remote host port
"set sys autoconn 1",
"" // End marker
};
void blink() {
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
}
void sendCommands(String cmds[]) {
Serial.println("Send command strings ...");
int i = 0;
while( cmds[i].length() > 0 ) {
Serial.println( cmds[i] );
rnxv.sendCommand(cmds[i], "<4.00>");
i++;
}
Serial.println("Command strings are done.");
}
void setup() {
pinMode(led, OUTPUT); // THIS IS IMPORTANT, or else you never see the light
Serial3.begin(9600); // UART, RX (D0), TX (D1) are connected to WiFly
rnxv.setUart(&Serial3); // Set up the object that wraps interaction with the WiFly
Serial.begin(9600); // USB serial
Serial.println("10");
delay(5000);
Serial.println("5");
delay(5000);
blink();
Serial.println("Are we ready?");
blink();
Serial.println("Send dollars ..");
rnxv.enterCommandMode();
sendCommands(cmds1);
rnxv.exitCommandMode();
}
// Glommed from http://niltoid.com/blog/arduino-wifly-sends-osc/
void sendRawOsc() {
Serial.println("Send OSC ...");
char data[32] = "/oscillator/4/frequency\0,f\0\0";
f += 0.2;
if (f > 1.0) { f = 0.0; }
u.fval = f;
data[28] = u.b[3];
data[29] = u.b[2];
data[30] = u.b[1];
data[31] = u.b[0];
Serial.println("");
Serial.flush();
rnxv.sendChars(data, 32);
}
void loop() {
// Print a timestamp delta to track the interval between loop passes.
// The reason for this is that the OSC server seems to miss getting messages
// every so often. The question became, is the Teensy or WiFly just not
// sending it?
// Is the loop() method getting stuck somehow?
// The timestamp delta shows that loop() is executing at the
// expected intervals. The problem now is to determine if
// sendRawOsc() is failing to send anything, or if the OSC server
// is failing to pick up what is sent.
delta = (micros() - ts)/1000;
Serial.println(delta);
ts = micros();
sendRawOsc();
Serial.println("sent\n");
delay(2000); // This should more or less match the timestamp delta.
// What you'll see is a number that is a few milliseconds longer
// because of additional code execution time.
rnvxContent = rnxv.receiveData();
if (rnvxContent != "") { Serial.print(rnvxContent); }
// String handling: stackoverflow.com/questions/5697047/convert-serial-read-into-a-useable-string-using-arduino
while(Serial.available()) {
usbSerialChar = Serial.read();
usbSerialBuffer.concat(usbSerialChar);
}
if (usbSerialBuffer != "") { rnxv.println(usbSerialBuffer); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment