Last active
November 4, 2015 06:43
-
-
Save MrRocketman/84945c9adc0595e2f622 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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