I am new to the Spark, seems like a great idea, but I’m getting really frustrated with it.
First issue, is it seems to try to reconnected to WiFi and Cloud (I guess?) after less than a minute of being powered on. My program runs for a short while then stops, the LED flashes cyan until it has reconnected, but the program does not function quite correctly afterwards.
My program is (aiming to) broadcast a UDP packet. This is the beginning, it will build up to more later, but for now this is it. This works, up until the issue described above. Once the apparent disconnect, reconnect happens, no more successful UDP packets are received by my other Windows program, even though the Spark appears to be functioning correctly (I see the serial comms and the D7 LED flash).
I have seen something about the cyan flash of death, but mine does reconnect.
// UDP Port used for two way communication
unsigned int localPort = 6767;
IPAddress bcastip(255, 255, 255, 255);
// An UDP instance to let us send and receive packets over UDP
UDP Udp;
void setup()
{
pinMode(D7, OUTPUT); // DEV BOARD LED
Serial.begin(9600);
digitalWrite(D7, HIGH); // sets the LED on
delay(5000);
digitalWrite(D7, LOW); // sets the LED off
Serial.println("Serial begin");
for (int i = 0; i < 10; i++)
{
digitalWrite(D7, HIGH); // sets the LED on
delay(50); // waits for 200mS
digitalWrite(D7, LOW); // sets the LED off
delay(50);
}
delay(5000);
for (int i = 0; i < 5; i++)
{
digitalWrite(D7, HIGH); // sets the LED on
delay(250); // waits for 200mS
digitalWrite(D7, LOW); // sets the LED off
delay(250);
}
delay(5000);
Serial.println("Starting UDP");
// start the UDP
Udp.begin(localPort);
// Print your device IP Address via serial
Serial.println(WiFi.localIP());
}
void loop()
{
Serial.println("Broadcasting");
Udp.beginPacket(bcastip, localPort);
Udp.write("Hello");
Udp.endPacket();
digitalWrite(D7, HIGH); // sets the LED on
delay(250); // waits for 200mS
digitalWrite(D7, LOW); // sets the LED off
delay(250);
delay(2500);
}
Since you are broadcasting, all hosts, including your own Spark core, are seeing your packets if they are looking for them. You need to read (and dump if you like) the received packets. If you don’t pick them up, the Ti CC3000 runs out of packet buffers and the core will reset. You could also set your local receive port to be different from your transmit port to avoid receiving them at all by changing the ports used for Udp.begin() and Udp.beginPacket().
You are still just masking the issue. Calling Udp.stop() will reset the socket and does throw away received data.
Udp.flush() just clears the software packet buffer but does not clear any data in the TI CC3000.
I think your read loop would work if you read more data at one time, rather than character by character. Udp.read(*buf, packet_length) is the form you want.
Tried removing changing my method from Stop to reading the full buffer in one go as suggested, but it went straight back to the crashing situation again.