Currently, for testing only, I have 2 identical devices, directly connected via LAN cable ( no Internet connection ) in the following configuration: ESP32 <-> W5500 <-> RJ45 ( 1st device ) <-> LAN cable <-> ( 2nd device ) RJ45 <-> W5500 <-> ESP32. I want to be able to send and receive UDP broadcast messages between the devices. The problem is the device never receives the UDP packet. The packet seems to be sent correctly because if I connect my PC in place of the receiving device, and start capturing the Ethernet interface with Wireshark, the packet gets detected and displayed by Wireshark, so I assume the problem must actually be at the receiving side (either the way I use the Ethernet library or the way W5500 is set).
The code for the 2 devices is identical, I initialize the Ethernet on both devices in the same way:
#include <Arduino.h>
#include "Ethernet/Ethernet.h"
...
uint8_t ethMac [ 6 ] ;
esp_read_mac ( ethMac, ESP_MAC_ETH ) ; // 1. get the MAC address
IPAddress bcAddress ( 192, 168, 1, 255 ) ; // 2. set IP to the broadcast address in the local network
IPAddress adrGw ( 192, 168, 1, 1 ) ; // 3. set IP for gateway
IPAddress adrMask ( 255, 255, 255, 0 ) ; // 4. set subnet mask
Ethernet . begin ( ethMac, bcAddress, adrGw, adrGw, adrMask ) ;
An EthernetUDP instance is created:
EthernetUDP udp;
Then, in my UDP handling task, I initialize UDP by:
udp . begin ( 6881 ) ;
and then, within the while-loop of the task, I check for UDP packets with:
while ( true ) {
...
int UDP_packet_size = udp . parsePacket () ; // NEVER receives an UDP packet, size ALWAYS stays 0, even when the other device sends the UDP packet
if ( UDP_packet_size ) {
Serial . println ( "UDP data" ) ;
} else {
Serial . println ( "no UDP data" ) ; // ALWAYS executed
}
}
but UDP data is never received.
I checked several links:
- https://www.arduino.cc/reference/en/libraries/ethernet/
- https://forum.arduino.cc/t/broadcast-with-ethernetudp/287997
- https://forum.arduino.cc/t/arduino-accepts-udp-unicast-messages-but-not-broadcast/212966
- https://forum.arduino.cc/t/receiving-a-udp-broadcast/300160
and also the w5500 datasheet, but I still cannot figure out and track the correct way to get the UDP packets received.
Hence, may I ask for help in case somebody spots the mistake I am doing ?