Skip to content

Arduino Ethernet Communication via UDP

NikuSirbu edited this page Feb 8, 2016 · 1 revision

    Our Arduino used in this project supports communication over Ethernet networks using the User Datagram or UDP protocol. UDP is a simple protocol which is supported by the Arduino Ethernet libraries. UDP can also be used with shields including the Arduino Ethernet and WiFi shields.

    The Arduino libraries makes it very easy to get started with Ethernet communication.

    The first part of the program sets up some configuration we need. Like any internet device, the Arduino ethernet shield will need a MAC address and its own IP address. In theory the MAC address should be globally unique, but many Arduino devices don’t come with built-in MAC addresses. However, as long as the device is on a private network just pick a MAC address that doesn’t match any other device on your network. You’ll also need an IP address; this should be in the same address range as the IP address of your PC running Arduino IDE. To find out your PC’s address, open the Command Prompt (you’ll find it in the start menu) and type ipconfig. Look for the IPv4 Address; its probably something like 192.168.15.100. Normally you’d setup the Arduino to request an address from a DHCP server, but to keep things simpler, we can just make one up. The first three numbers should be the same as you’re computer’s address and the last number should be different to any other device on your network and between 1 and 254. If you don’t have many devices on your network, you can probably just pick one at random!

    As well as its own IP address, the Arduino needs to know your computer’s IP address so it knows where to send the data. This is the destination address in the listing below.

    You’ll also need a service port for each end of the connection. The service port lets several programs all listen for traffic on a single computer (on the PC end anyway). Each program uses a different port and this port number needs to be shared between the sender and receiver so the two ends know they are talking to the right program. The port can be the same, or different for each end. In this program I’ve used 9876 for both ends.

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

// The MAC address and IP address for the controller. They will be dependent on local network.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xB6, 0x47 };
IPAddress ip(172, 17, 41, 54);
unsigned int localPort = 9876;

IPAddress remoteIP(172, 17, 41, 71);
unsigned int remotePort = 9876;

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

void setup(){    
  delay(200);
  Serial.begin(9600);

  setupEthernet();
  
  Serial.println();
  Serial.println("Ethernet Started");
  delay(50); 
}

void setupEthernet() {
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  // Start the Ethernet and UDP.
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  // Ethernet Shield IP.
  Serial.print("Device IP : ");
  Serial.println(Ethernet.localIP());
}

void loop(){     
  String someValue = "Text to send to server."; 
  sendUDP(someValue);   
}

void sendUDP(String value) {
  Udp.beginPacket(remoteIP, remotePort);
  Udp.print(value);
  Udp.endPacket();
}