Getting UDP Working

Hi. I’m trying to set up my UNO R3 using the EthernetUDP class so that I can send UDP packets through an ENC28J60 Ethernet shield to my PC. I have wired it all up according to the Arduino website as follows:

13 - SCK
12 - MISO
11 - MOSI
10 - CS

And used the following code, cut and pasted from the same webpage:

#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 10);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:

void loop() {
  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

The ip address in the code above has been changed, but only as a personal preference, and it’s still on my subnet. I have tried it using both a switch and straight to the network card, but can’t get any packets to flow. In fact, on the router setup page (I’m using the router as a switch though), it’s not even showing the Ethernet module as attached. I’ve tried some of the examples from the ethernet_master library (which is the library available for the shield itself), and they work, but they don’t have anything straightforward for sending and receiving UDP. I would honestly rather use the Arduino functions/library anyway, for ease of use. I’ve been using Wireshark to monitor the card and check for packets, but nothing seems to be working. Can anybody help walk me through this. I’ve checked all over online, but from everything I’ve found, it should be working. Thanks.

The Arduino libraries are for the W5100 chip. The ENC28J60 is not officially supported by the Arduino. For the ENC28J60 use the UIPethernet or the Ethercard library.

Ah that explains it. The Ethercard library was the one I was able to get a bunch of sketches working with, so that makes sense. What I'm trying to do is send a few bytes of data from my UNO to a GUI on my PC, and back. I was thinking of using Java on the PC end as it's pretty easy to use the Datagram class to read and write UDP. Do you have any suggestions as far as the Ethercard library goes for accomplishing this on the Arduino end? Thanks.

No, sorry, I never used the UDP functions of the Ethercard library.
This seems pretty straightforward : EtherCard/udpListener.ino at main · njh/EtherCard · GitHub
Assign a port and a function to ether.udpServerListenOnPort() and the data comes in.

Thanks. Isn't that sketch for reading data from the PC as opposed to sending it to it? I need both eventually but I would most likely start by sending some packets from the UNO.

It is receiving UDP. Sending should not be very hard. But I got lost in the little documentation for EtherCard. Perhaps you have to look into the source files to find the function that sends UDP data.

K so what I ended up doing was using the UIPethernet library. It has client/server UDP and TCP sketches. Worked really well. I found them a little clearer to understand too.

Okay, very good. Good to know.