|
#include <ESP8266WiFi.h> |
|
#include <WiFiUdp.h> |
|
#include <OSCMessage.h> |
|
#include <OSCBundle.h> |
|
#include <OSCData.h> |
|
#include "FastLED.h" |
|
|
|
// How many leds in your strip? |
|
#define NUM_LEDS 8 |
|
#define DATA_PIN 2 |
|
|
|
char ssid[] = "SSIDNAME"; // your network SSID (name) |
|
char pass[] = "SSIDPASSWORD"; // your network password |
|
|
|
// A UDP instance to let us send and receive packets over UDP |
|
WiFiUDP Udp; |
|
const unsigned int localPort = 9000; // local port to listen for UDP packets (here's where we send the packets) |
|
|
|
CRGB leds[8]; |
|
|
|
OSCErrorCode error; |
|
|
|
// string for received data |
|
char position[13]; |
|
// temp variable |
|
unsigned int newTick = 0; |
|
// variable for current led number |
|
unsigned int led = 0; |
|
|
|
void setup() { |
|
|
|
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: "); |
|
Serial.println(Udp.localPort()); |
|
|
|
// sanity check delay - allows reprogramming if accidently blowing power w/leds |
|
delay(2000); |
|
// initialize leds with FastLED |
|
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); |
|
|
|
} |
|
|
|
|
|
void handle(OSCMessage &msg) { |
|
// get position from OSC packet |
|
msg.getString(0, position, 13); |
|
// beat position is at index 5, minus 48 to offset back to 0 (initially an ASCII number) |
|
newTick = int(position[5]) - 48; |
|
|
|
// change on new beat |
|
if (newTick != led) { |
|
Serial.println(newTick); |
|
leds[led-1] = CRGB::Black; |
|
// show green color for beat 1 |
|
leds[newTick-1] = newTick == 1 ? CRGB::Green : CRGB::Red; |
|
FastLED.show(); |
|
// current variable is temp variable |
|
led = newTick; |
|
} |
|
} |
|
|
|
void loop() { |
|
OSCBundle bundle; |
|
int size = Udp.parsePacket(); |
|
|
|
if (size > 0) { |
|
while (size--) { |
|
bundle.fill(Udp.read()); |
|
} |
|
if (!bundle.hasError()) { |
|
// handle OSC message on /position with handle() |
|
bundle.dispatch("/position", handle); |
|
} else { |
|
error = bundle.getError(); |
|
Serial.print("error: "); |
|
Serial.println(error); |
|
} |
|
} |
|
} |
|
|
|
|