airpanther
Active member
Good day,
I've been using the Ethernet.h library for a while, and generally it works great! I can send and receive UDP broadcast messages to/from other devices. Challenge is, incoming UDP messages simply stop working after a random interval. Sometimes it happens after 1-2 minutes, and sometimes longer. Other times, I can run the sketch for hours without a single hiccup. I simplified the code and added a bunch of traps to see where the sketch was hanging up, and it always hangs at the same line:
int packetSize = Udp.parsePacket();
Adding a delay(1) helped, and adding a delay(10) helped even more, but eventually it still stops working. I also have a keep alive, which sends a small UDP message every 2 seconds. It still continues to send successfully when the incoming messages stop. I've tried running the code in a thread with the same results, I've tried using a long instead of an int, no luck.
Any idea why the incoming UDP comms randomly stop working, while the outgoing UDP continues working fine? I'm using a Teensy 3.5 and a W5500 Ethernet module.
Thanks much!
Robert
I've been using the Ethernet.h library for a while, and generally it works great! I can send and receive UDP broadcast messages to/from other devices. Challenge is, incoming UDP messages simply stop working after a random interval. Sometimes it happens after 1-2 minutes, and sometimes longer. Other times, I can run the sketch for hours without a single hiccup. I simplified the code and added a bunch of traps to see where the sketch was hanging up, and it always hangs at the same line:
int packetSize = Udp.parsePacket();
Adding a delay(1) helped, and adding a delay(10) helped even more, but eventually it still stops working. I also have a keep alive, which sends a small UDP message every 2 seconds. It still continues to send successfully when the incoming messages stop. I've tried running the code in a thread with the same results, I've tried using a long instead of an int, no luck.
Any idea why the incoming UDP comms randomly stop working, while the outgoing UDP continues working fine? I'm using a Teensy 3.5 and a W5500 Ethernet module.
Thanks much!
Robert
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <SPI.h>
#include <Ethernet.h>
#define UDP_TX_PACKET_MAX_SIZE 1000 //increase UDP buffer size
////// Begin Ethernet Stuff //////
IPAddress UDPServer(255, 255, 255, 255); // Use UDP Broadcast address so the IP address of the server doesn't have to be known. Works as long as the devices are on the same LAN as the server.
unsigned int UDPPort = 7772; // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
String UDPMessage;
char MessageBuffer[100]; // a string to send back
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
const String DeviceName = "IO_Test";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address and IP address for your controller
////// END Ethernet Stuff //////
// Timers
IntervalTimer KeepAlive; // Sends a small network package periodically to keep the connection alive
const long KeepAliveFreq = 2000000; // Keep Alive Frequency (2000000 = 2 sec)
void InitializeComms()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
// IMPORTANT!!!! Comment out for standalone UDP, Uncomment for tethered serial testing
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
Udp.begin(UDPPort);
Serial.print(" IP Address ");
Serial.println(Ethernet.localIP());
}
void KeepAlive_tick()
{
SendUDPMessage(DeviceName + "|" + "Server" + "|" + "KeepAlive" + "|" + DeviceName); // Sends a small datagram to keep the network connection alive
}
void setup()
{
InitializeComms(); // Initialize the serial port and the UDP Connection
KeepAlive.begin(KeepAlive_tick, KeepAliveFreq); // Start KeepAlive
}
void SendUDPMessage(String message)
{
// Send UDP Message
message.toCharArray(MessageBuffer,100); //
Udp.beginPacket(UDPServer, UDPPort);
Udp.write(MessageBuffer);
Udp.endPacket();
Serial.println(message); // Transmit message serially
}
void loop()
{
int packetSize = Udp.parsePacket(); // if there's data available, read a packet
if (packetSize)
{
Serial.print("Packet Size: "); Serial.print(packetSize);
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
UDPMessage = packetBuffer;
Serial.print(" Message: "); Serial.println(packetBuffer);
memset(packetBuffer, 0, sizeof(packetBuffer));
}
delay(10);
}
Last edited: