Hi community!
I have some problem with the code below. It works fine for several hours but suddenly it stop to replay on my broadcasts.
I could send several UDP broadcast/ min Spark replay everything works fin. Then I stop broadcast for some hours and come back to broadcast again and this time Spark don´t replay.
I have put in Serial output in the loop() just to see if its still run my code and not have booted up in Tinker app but it seems correct. But the Serial in the if (Udp.parsePacket() > 0) don´t seems to be running. So Udp seems to have stopped working.
Could this be related to TI CC3000 UDP problems or is it my code? I have installed latest CC3000 patch.
// An UDP instance to let us send and receive packets over UDP
UDP Udp;
// UDP Broadcast msg
const char UDP_ALL[] = "SPARK"; // Broadcast msg to all units
const char UDP_SET_ADDRESS[] = "SET_ADDRESS";
const int RX_BUFFER_SIZE = 20;
unsigned char mRxBuffer[RX_BUFFER_SIZE];
const int UNIQUE_ID_SIZE = 12;
unsigned char *pUniqueID = (unsigned char *)0x1FFFF7E8;
const int UNIT_INFO_SIZE = 23;
unsigned char mUnitInfo[UNIT_INFO_SIZE] = { 'S', 'P', 'A', 'R', 'K', 1, 1, 1, 0, 0, 0, pUniqueID[0], pUniqueID[1], pUniqueID[2], pUniqueID[3], pUniqueID[4], pUniqueID[5], pUniqueID[6], pUniqueID[7], pUniqueID[8], pUniqueID[9], pUniqueID[10], pUniqueID[11]};
uint8_t mUnitAddress = 0;
IPAddress ipAddressRemote;
unsigned int portRemote;
// UDP Port
unsigned int localPort = 8888;
enum unit_info {
UNIT_INFO_TYPE = 5,
UNIT_INFO_HW,
UnIT_INFO_FW,
UNIT_INFO_ADDRESS,
UNIT_INFO_DATA1,
UNIT_INFO_DATA2
};
void setup() {
// start the UDP
Udp.begin(localPort);
// Print your device IP Address via serial
Serial.begin(9600);
}
void loop() {
Serial.print("loop is running ");
// Check if data has been received
if (Udp.parsePacket() > 0) {
Serial.println("");
Serial.print("parsePacket: Ok ");
Udp.read(mRxBuffer, RX_BUFFER_SIZE);
Udp.flush();
// Store sender ip and port
ipAddressRemote = Udp.remoteIP();
portRemote = Udp.remotePort();
if (strcmp((char*)mRxBuffer, UDP_ALL) == 0) {
Serial.println("UDP Broadcast: Ok");
mUnitInfo[UNIT_INFO_ADDRESS] = mUnitAddress;
// Send UnitInfo data to sender
Udp.beginPacket(ipAddressRemote, portRemote);
Udp.write(mUnitInfo, UNIT_INFO_SIZE);
Udp.endPacket();
} else if (strcmp((char*)mRxBuffer, UDP_SET_ADDRESS) == 0) {
Serial.println("Set address: Ok");
// Send data to sender
Udp.beginPacket(ipAddressRemote, portRemote);
Udp.write("OK");
Udp.endPacket();
} else {
Serial.print("UDP Broadcast: Error");
}
}
}