I want to build a racing timer. I built a prototype with a button to simulate breaking an IR beam. Every 1 second it sends a keep-alive broadcast and when the beam if broken it sends another small message. Problem is that even just sending the keep alive every one second makes the core reset occasionally. I researched that not receiving your own broadcast causes a buffer overflow, so I read my own broadcast, then I tried Udp.stop to flush buffers. Still it causes a core reset. Here’s the code.
#include "application.h"
// Define the pins we're going to call pinMode on
int led = D1; // This LED will flash every 1 second as to signify that a keepalive USP broadcast was sent.
int onboardLED = D7; // This built-in, tiny LED will flash when a beam break is sent
int BUTTON = D0; // This is the input for the button pressed.
uint8_t buf[2]; // buffer to send beam break
uint8_t ping[2]; // buffer to send keepalive
UDP Udp; // UDP control
char discard[64]; // buffer to throw away the broadcasts we have received.
// This routine runs only once upon reset
void setup() {
pinMode(led, OUTPUT); // keepalive LED indicater
pinMode(onboardLED, OUTPUT); // signal break was sent
pinMode(BUTTON, INPUT_PULLDOWN); // signal when button pressed signaling beam break
attachInterrupt(BUTTON, beamBroken, RISING); // This sets microswitch as an Interrupt.
Udp.begin (6948); // use this port
ping[0] = 0x1; // Initialize keep alive...
ping[1] = 0x1; // buffer
}
// This routine gets called repeatedly, like once every 5-15 milliseconds.
// Spark firmware interleaves background CPU activity associated with WiFi + Cloud activity with your code.
// Make sure none of your code delays or blocks for too long (like more than 5 seconds), or weird things can happen.
void loop() {
Udp.beginPacket(IPAddress(255,255,255,255),6948); // Create a broadcast UDP packet
Udp.write(ping,2); // It contains the two bites of the keep alive
Udp.endPacket(); // End the packet - I beleive it is sent at this time.
delay(250); // Wait 1/4 of a second to make sure the message went
digitalWrite(led, HIGH); // Turn on the LED to indicate the keep alive was sent
digitalWrite(onboardLED, LOW); // Make sure the LED that indicates a button press is off
// Udp.stop is another way to flush the buffer
Udp.stop(); // This should flush the buffer that would have the keep alive and button press delivered back to us
SPARK_WLAN_Loop(); // Allow background task to run
delay(250); // wait another 1/4 second to let things flush out
Udp.begin(6948); // Start Udp once again.
delay(500); // Wait 1/2 second to complete sending a keep alive every 1 second
digitalWrite(led, LOW); // Turn OFF the LED that indicates a keep alive send
//
// THis code is commented out... I tried this to fush the broadcast and the system kept resetting anyway
//
// Flush the buffer since the packet was a broadcast, it needs to read it's own packet
// int32_t retries = 0;
// int32_t bytesrecv = Udp.parsePacket();
// while(bytesrecv == 0 && retries < 1000) {
// bytesrecv = Udp.parsePacket();
// retries++;
// }
// if (bytesrecv>0) {
// Udp.read(discard,bytesrecv);
// digitalWrite(onboardLED, HIGH);
// }
}
//
// This function sends a broadcast message each time the button is pressed
void beamBroken(){
buf[0] = 0x1;
buf[1] = 0x2;
Udp.beginPacket(IPAddress(255,255,255,255),6948);
Udp.write(buf,2);
Udp.endPacket();
}