Ethernet shield UDP example doesn't work

Hello everybody,

again I have a problem: Now that my wifi shield simply didn't want to work, now matter how often I upgraded the firmware or tried different codes to make it working, I decided to use an ethernet shield + wlan router combination. Till my wlan router arrives, I want to prepare everything else. What actually is only making my ethernet shield receive UDP packets. But I simply can't get it working, I used the example called UDPSendReceiveString that's provided by the IDE (my is 1.0.5 R2).

#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,0x0E,0xC9,0xBE};
IPAddress ip(192,168,2,126);

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:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

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);
}

The IP is the one I got when I used the DhcpAddressPrinter sketch (also an example), and the Mac is the one printed on the shield.

I simply can't figure out why this isn't working. I disabled the firewalls on my laptop (I use my own AutoIT software to send packets and some foreign software as well). The Arduino is connected with the ethernet shield and a lan cable to my router, while my laptop is connected via wlan to the router.

Can someone help me with this problem?

I use the ethernet shield and the wifi shield as a "client" with a Linux computer or Raspberry Pi as the "server", and it works really well. The Linux code is the same for both the ethernet shield and wifi shield. How convenient! Neither need any modification of your router firewall if the Linux or RPi is on the internet and the wifi or ethernet shield is behind a router.

Ethernet shield with Linux/Raspberry Pi code
http://forum.arduino.cc/index.php?topic=198259.msg1462983#msg1462983

Wifi shield with Linux/Raspberry Pi code
http://forum.arduino.cc/index.php?topic=210151.msg1543236#msg1543236

Maybe you have an idea why my wifi shield didn't work with UDP? I used the example code provided in the Arduino IDE and I also managed to upgrade the firmware of the shield...

Also I used to code you posted and it doesn't work with my ethernet shield. I simply don't know what's wrong.

Can you explain "Doesn't work" a little better?

For a communications link to work, all things in the path of the transmission must 'work.' When the link does not work, one or more problems are occurring along the path. By working systematically and methodically from one end of the path to the other, you should be able to first isolate, and then resolve, any problems.

There is some example code on my web site, which provides both ends of the path. An Arduino sketch to send UDP packets and a Processing sketch to receive them. I don't have a great deal of time available but maybe you could load up the code, so we can work our way along the path to see where things might be going wrong for you.
http://mssystems.emscom.net/helpdesk/knowledgebase.php?article=54

Have you tried the UdpNtpClient sketch in the Ethernet library examples?
Have you tried the WiFiUdpNtpClient sketch in the WiFi library examples?
Those are almost identical to my code examples, using an NTP server rather than my Linux/RPi C code. Maybe you should start with a UDP "server" that you know works.

SurferTim:
Maybe you should start with a UDP "server" that you know works.

Great idea Tim but UDP does not always play nicely with NAT.

Testing end to end on the local segment takes NAT out of the equation. Personally, I write my own UDP listener, so I can see the source code but a quick search turned up this tool which may also be useful
http://www.simplecomtools.com/productcart/pc/viewPrd.asp?idproduct=6&

The pedant in me wants to say, there is no such thing as a UDP 'server' :blush:

My router does fine with DNS and NTP going through my router with no problems, but I hear that not all play well that way. It depends on connection tracking and the firewall rules. If you use connection tracking, the "server" must return a packet immediately. If it waits any time at all, it will fail. My router's connection tracking holds the routing for only 10 seconds.

I don't like the term UDP "server" either, and that is why I put it in quotes. But it seems that NTP uses that term, so I do too. It is a term I use to determine which device sends the "request" and which returns a "response".

Damn. I'm not used to high quality answers in a forum. Thank you very much!

I'm using an AutoIT script to send UDP packets around my network. Here's the code, after compiling it should send a packet every 500ms to the IP of my Arduino (I tested it with both of them, neither my ethernet one nor my wifi shield one worked):

$g_IP = "192.168.2.126" ; IP-Addresse des Servers

UDPStartup() ;startet den UDP-Service

$aSocket = UDPOpen($g_IP,3178) ; verbindet sich mit dem offenen Socket des Servers mit der IP $g_IP und dem Port 65432
; $aSocket ist ein Array das wie folgt aufgebaut ist:
;    $aSocket[1]: real socket
;    $aSocket[2]: IP des Servers
;    $aSocket[3]: Port des Servers

If @error Then
    MsgBox(48, "Error", "Es kann keine Verbindung zum Server hergestellt werden!")
    Exit
EndIf

$sText="Banana"
While 1
UDPSend($aSocket,$sText); Sendet den Inhalt der InputBox an den Server
Sleep(500)
WEnd

UDPShutdown()

Also I just tested the UdpNtp sketches and they worked for both of my Arduinos. So I guess it's not a problem with the Arduino itself. I guess my client (code above) sucks, but I also tested another UDP client. Can you guys suggest a working client so I can test if the problem isn't my network?

If UdpNtpClient worked, then your router does fine with connection tracking. That is the whole premise of my code set. It uses the same connection tracking to allow "connections" through the router's NAT. That way you need not modify the router to allow "destination NATs" through the router. Connection tracking will handle that for you, but for a limited time only as I mentioned above.

I am running a test of the Mega 2560/wifi shield as a "client" and a Raspberry Pi with the Linux code as a "server". It has been running for almost a week at a packet every 5 seconds without failure. The packet count shows over 51,000 packets, and that is after rolling over once.

The major problem with the server code I posted is the "server" firewall. If you do not allow the UDP port through your Linux firewall, it will fail. I use UDP port 5005 in my test, so I allow that through my Debian Linux firewall. The Raspberry Pi (using Raspbian) "server" does not have a firewall by default, so it works fine with no additional mods.

I use Windows, and I disabled both of my firewalls (The standard Windows Firewall and Comodo). But it still isn't working. Do you know a working client that I can use to send stuff to my Arduino?

Fun fact: When I use the provided WiFiUdpNtpClient sketch, my Arduino connects to my wlan. But when I use the also provided WiFiUdpSendReceiveString sketch, it can't connect. Both of them are from the 1.0.5 R2 IDE.

That is because the "server" code is for a Linux box. You would need to use a UDP "server" program designed for a Windoze box.

Fun fact: When I use the provided WiFiUdpNtpClient sketch, my Arduino connects to my wlan. But when I use the also provided WiFiUdpSendReceiveString sketch, it can't connect. Both of them are from the 1.0.5 R2 IDE.

The difference is which is the "client" and which is the "server". The WiFiUdpNtpClient sketch is a "client". It sends the "request" packet to the NTP "server" and gets a "response" packet from that "server". The WiFiUdpSendReceiveString sketch is a "server". It actually is named incorrectly. It should be WiFiUdpReceiveSendString. It waits for a "request" packet from a "client" and sends a "response" packet. That will NOT go through a router's NAT. Connection tracking is a one-way street.

But why can't it even connect to the wlan? And how do I make it working? Okay that's a vague question, but I guess you get what I mean: How can I send UDP packets to my Arduino although connecting to the wlan doesn't even work?

If you want the Arduino to be the UDP "server" (using the word 'server' is a loose interpretation of its function), you must set your router to port forward the public IP and udp port to the localnet ip and port of the Arduino, then use the public IP and port of the router from the internet (WAN) interface.

edit: If you are trying to connect to the Arduino from your wireless LAN, that should not be a problem, considering you are using a UDP "client" on the WLAN interface. Insure the Arduino has the correct network settings for the interface.

One code doesn't even let my Arduino connect to my wlan, ond does. I can't even send UDP packets through my own network, at the moment I don't care about anywhere else.

lutan:
One code doesn't even let my Arduino connect to my wlan, ond does. I can't even send UDP packets through my own network, at the moment I don't care about anywhere else.

Which are you talking about? The ethernet shield or the wifi shield? Let's stay on one or the other. Mixing the two may be confusing you. Pick one and stay with that one.

At the moment I'm talking about the wifi shield. I use the SendReceive one and can't even connect to my network. But when I use the UdpNtp one, it can connect to my wlan and also get the time from the server.

What message does the WiFiUdpSendReceive sketch display when it fails to connect to the wifi network?

Attempting to connect to SSID: NerdtechCorporation
Attempting to connect to SSID: NerdtechCorporation

And so on.

Did you change the security type on the WiFiUdpSendReceiveString sketch?

    // this is for a secure network (WPA2) in WiFiUdpNtpClient
    status = WiFi.begin(ssid, pass);

    // this is for an open network in WiFiUdpSendReceiveString
    status = WiFi.begin(ssid);

I'm feeling dumb now. Yes, it can now connect to the network, but it still doesn't receive UDP packets. I guess my code is wrong (although I tested some other software too). Do you know a working windows application to send UDP packets?