Skip to content

Instantly share code, notes, and snippets.

@darrenmothersele
Created October 8, 2013 16:35
Show Gist options
  • Save darrenmothersele/6887486 to your computer and use it in GitHub Desktop.
Save darrenmothersele/6887486 to your computer and use it in GitHub Desktop.
A simple sketch to demonstrate control of LED pixels via UDP.
#include <EtherCard.h>
#include <IPAddress.h>
#include "FastSPI_LED2.h"
#define NUM_LEDS 8
#define DATA_PIN 6
CRGB pixels[NUM_LEDS];
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// gateway ip address
static byte gwip[] = { 192,168,1,254 };
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
if (len == NUM_LEDS * 3) {
memcpy(pixels, data, len);
FastLED.show();
}
}
void sendReady() {
char collBuf[1];
collBuf[0] = 0x75;
static byte destIp[] = { 192,168,1,81 };
ether.sendUdp((char*) collBuf, 1, 23456, destIp, 6000);
}
void sendRandom() {
char collBuf[1];
collBuf[0] = 0x75;
static byte destIp[] = { 192,168,1,81 };
ether.sendUdp("Random", 1, 23456, destIp, 6000);
}
void setup(){
FastLED.addLeds<WS2812, DATA_PIN, GRB>(pixels, NUM_LEDS);
Serial.begin(57600);
Serial.println("\n[UDP Listener]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip, gwip);
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.udpServerListenOnPort(&udpSerialPrint, 6000);
sendReady();
}
void loop(){
ether.packetLoop(ether.packetReceive());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment