Ethernet Shield and UDP problems

I'm having some problems getting the Ethernet Shield (R3) communicating with Processing using UDP. I've tried it both on an Arduino Due and an Uno, same result.

The code I'm using is from the UDPSendReceiveString tutorial here, with some minor modifications:

Things I've changed:

  • Added the MAC address on the back of my Ethernet Shield
  • Tried a version with a manual IP address and with none (using DHCP)
  • added a small Processing window so I can tell when the app is in focus

What happens is that the Arduino never receives the strings sent from Processing. Also, if I try sending strings from the Arduino, I never receive them from Processing.

Arduino code is here:

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender
 
 A Processing sketch is included at the end of file that can be used to send 
 and received messages for testing with a computer.
 
 created 21 Aug 2010
 by Michael Margolis
 
 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0x90, 0xA2, 0xDA, 0x0D, 0x9F, 0x29 };

// This matches IP in Ethernet.localIP
IPAddress ip(10,120,85,19);

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

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

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

void setup() {
 
  // start the Ethernet and UDP:
  Serial.begin(9600);
  Serial.println("UDP Ethernet Test");
  
 
   // Explit IP
  Ethernet.begin(mac,ip);
 
  // DHCP CONNECTION
  /*
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  */
    
  delay(1000);  // 1 sec delay was suggested in some posts
  
  // prints the Arduino IP address:
  Serial.print("My IP address: ");
  
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

Processing code is here:

/*
  Processing sketch to run with this example
 =====================================================
 
 // Processing UDP example to send and receive string data from Arduino 
 // press any key to send the "Hello Arduino" message
 */
 
 import hypermedia.net.*;
 
 UDP udp;  // define the UDP object
 
 
 void setup() {
   size(800,600);
   udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
   udp.log( true );         // <-- printout the connection activity
   udp.listen( true );           // and wait for incoming message  
 }
 
 void draw()
 {
   background(0);
 }
 
 void keyPressed() {
 String ip       = "10.120.85.19"; // the remote IP address
 int port        = 8888;        // the destination port
 
 println("sending Hello World");
 udp.send("Hello World", ip, port );   // the message to send
 }
 
 void receive( byte[] data ) {          // <-- default handler
 //void receive( byte[] data, String ip, int port ) {   // <-- extended handler
 
 for(int i=0; i < data.length; i++) 
 print(char(data[i]));  
 println();   
 }

Note: using the Ethernet->WebClient example does work on both boards, so the Shield is functioning correctly.

Other stuff I've trued:

  • using on a completely different network (home and at work), still not working
  • using port numbers other than 8888
  • using a different Mac
  • using Eclipse instead of Processing for host environment

Running on a Mac OS X, Mtn Lion (both machines).

Both Arduino boards I've tried are R3 boards.

Thanks,
Scott

p.s. Processing console output is:

sending Hello World
-- UDP session started at Tue Aug 06 16:17:38 PDT 2013 --
-- bound socket to host:null, port: 6000 --
[13-08-06 16:17:41.67 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11
sending Hello World
[13-08-06 16:17:44.67 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11
sending Hello World
[13-08-06 16:17:45.535 -0700] send packet -> address:/10.120.85.19, port:8888, length: 11

What is the ip/subnet mask of the Processing computer? Same subnet? Can you ping the Arduino from the Mac?

edit: ...and the Arduino serial output shows only the ip address of the ethernet shield? Is this all it ever shows?

UDP Ethernet Test
My IP address: 10.120.85.19

I have it working now, thanks to your response: the ping didn't respond, i.e. request timeout.

The subnet of the Processing/Mac computer and the Arduino computer are both the same, but I just re-read the documentation on Ethernet.begin() and saw that the default subnet is 255.255.255.0 — my two machines have a different subnet than this (thought are the same).

If I explicitly set the dns, gateway and the subnet, it's now working.

Thanks!

Hi,

i am getting problem in my project when i send command to arduino via ethernet shield to trigger relay.

normaly it works fine but some time when i send command then i got error of ping drop .

or else its totaly got unreachble from pinging.

component:

1.Arduino Uno R3 ATmega328P ATMEGA16U2 Compatible with USB Cable
2.Ethernet W5100 Shield Network Expansion Board W/
3. 2 CHANNEL 5V RELAY BOARD MODULE RELAY EXPANSION BOARD

#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h> // Ethernet board connected - Uses A0, A1, (D2 or D4), D10, D11, D12, D13
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xE0 }; // <------- PUT YOUR MAC Address Here
byte ip[] = { 10, 195, 82, 251 }; // <------- PUT YOUR IP Address Here
unsigned int localPort = 8888; // Local IP port to listen on
byte gateway[] = { 10, 195, 82, 1 }; // <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here
byte subnet[] = { 255, 255, 255, 0 }; // <------- It will be as it is in most of the cases
EthernetServer server(80); // <------- It's Defaulf Server Port for Ethernet Shield

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP

int PinA = 3; // Switch Pin 5
int PinB = 4;
int PinC = 5; // Switch Pin 6

void setup() { // run setup only once on boot

Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Udp.begin(localPort); // Start UDP Connection

pinMode(PinA, OUTPUT); // attach the pin
pinMode(PinB, OUTPUT);
pinMode(PinC, OUTPUT); // attach the pin
digitalWrite(PinA, LOW);
digitalWrite(PinB, HIGH);
digitalWrite(PinC, HIGH);

} // end of setup

void loop() // Start Running System
{
int packetSize = Udp.parsePacket(); // if there's data available, read a packet
if(packetSize)
{
int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
packetBuffer[read] = 0;
char ch = packetBuffer[0];

switch(ch)
{
case '0': // if 0 is sent via udp then do this -->>
digitalWrite(PinA, LOW); // Turn PinA Off
break; // end of case 0
case '1': // if 1 is sent via udp then do this -->>
digitalWrite(PinA, HIGH); // Turn PinA On
break; // end of case 1
case '2': // if 2 is sent via udp then do this -->>
digitalWrite(PinB, LOW); // Turn PinB Off
break; // end of case 2
case '3': // if 3 is sent via udp then do this -->>
digitalWrite(PinB, HIGH); // Turn PinB On
break; // end of case 3
case '4': // if 3 is sent via udp then do this -->>
digitalWrite(PinC, LOW); // Turn PinB On
break; // end of case 3
case '5': // if 3 is sent via udp then do this -->>
digitalWrite(PinC, HIGH); // Turn PinB On
break; // end of case 3

default : // if recived info via udp and not found in the case do this -->>

break; // end of case not found
}
}

delay(15); // waits 15ms for udp input wait ...
} // end system run, but now loop and start system run again​

I see you didn't take my advice on your previous post.