Skip to content

Instantly share code, notes, and snippets.

@MrRocketman
Last active November 4, 2015 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrRocketman/84945c9adc0595e2f622 to your computer and use it in GitHub Desktop.
Save MrRocketman/84945c9adc0595e2f622 to your computer and use it in GitHub Desktop.
SYSTEM_THREAD(ENABLED); // This makes the system cloud connection run on a background thread so as to not delay our timing
#define E131_PACKET_SIZE 638
#define E131_PORT 5568
// An UDP instance to let us send and receive packets over UDP
UDP udp;
bool previousWiFiReadiness = false;
bool wiFiReadiness = false;
int16_t e131PacketUniverseNumber = 0;
int udpDataCount = 0;
uint8_t udpData[E131_PACKET_SIZE];
int universePacketsCount[12];
unsigned int lastStatusReportTime = 0;
// Change this to however frequently you want a report
const int statusReportTimeIntervalMilliseconds = 2000;
// Main setup functions
void setup()
{
// start the UDP listening
udp.begin(E131_PORT);
//udp.setBuffer(640, udpData);
// Print your device IP Address via serial
Serial.begin(115200);
Serial.println(WiFi.localIP());
}
void loop()
{
// Check to see if the WiFi connection was lost.
previousWiFiReadiness = wiFiReadiness;
wiFiReadiness = WiFi.ready();
// WiFi stopped. Release udp so we can reinit once it's ready again
if(wiFiReadiness == false && previousWiFiReadiness == true)
{
Serial.println("WiFi not ready");
udp.stop();
}
// WiFi is back. Open up udp port again
else if(wiFiReadiness == true && previousWiFiReadiness == false)
{
Serial.println("WiFi Back online");
Serial.println(WiFi.localIP());
udp.begin(E131_PORT);
}
// Parse any incoming UDP data
udpParse();
}
void udpParse()
{
int returnValue = udp.receivePacket(udpData, E131_PACKET_SIZE);
//Serial.print("ret:");
//Serial.println(returnValue);
if(returnValue == E131_PACKET_SIZE)
{
udpDataCount ++;
e131PacketUniverseNumber = udpData[114] | (udpData[113] << 8);
// Keep track which universe of data we just received
universePacketsCount[e131PacketUniverseNumber] ++;
// Print out a status report every couple of seconds
if(millis() > lastStatusReportTime + statusReportTimeIntervalMilliseconds)
{
Serial.print("pkts:");
Serial.println(udpDataCount);
Serial.print("pkts/sec:");
Serial.println((udpDataCount * 1000) / (millis() - lastStatusReportTime));
lastStatusReportTime = millis();
// Print out the data for each universe
for(int i = 1; i < 12; i ++)
{
Serial.print("u");
Serial.print(i);
Serial.print(":");
Serial.println(universePacketsCount[i]);
universePacketsCount[i] = 0;
}
udpDataCount = 0;
}
//Serial.print("universe:");
//Serial.println(e131PacketUniverseNumber);
}
else if(returnValue != -2)
{
Serial.println("strange packet");
}
}
void printUDPData(char *udpData, int size)
{
Serial.println("Packet");
for(int i = 0; i < size; i ++)
{
Serial.print(udpData[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment