Arduino UDP connection UNRELIABLE: Destination NOT Reachable

Hello,

I have a project where I have to send periodically data via UDP from Matlab to the Arduino DUE via WiFI.

The problem is that the Arduino receives the first request, but not the other requests. I have selected the 9090 port for communication, but the Arduino tells me that it receives the data on port 62181 (although the board is configured for the 9090 port). The program is based on the UDP WiFi example on the website.
I have used Wireshark to see what is going on: the host computer clearly sends the UDP request, the request is taken by the router and it returns ('Destination Not Reachable') message.

What should I do?

Here is the Arduino code:

/*
  WiFi UDP Send and Receive String

 This sketch wait an UDP packet on localPort using a WiFi shield.
 When a packet is received an Acknowledge packet is sent to the client on port remotePort

 Circuit:
 * WiFi shield attached

 created 30 December 2012
 by dlf (Metodo2 srl)

 */


#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
int PIN = 37;
int status = WL_IDLE_STATUS;
char ssid[] = "TP_LINK"; //  your network SSID (name)
char pass[] = "buckethead";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

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

char packetBuffer[255]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back

WiFiUDP Udp;

void setup() {
  
  pinMode(PIN, OUTPUT);  
  digitalWrite(PIN, LOW); 
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);
}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    digitalWrite(PIN, HIGH);
   delayMicroseconds(400);
  digitalWrite(PIN, LOW);  
  
  Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);
  
  
      }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Best regards,
Jean

I have selected the 9090 port for communication, but the Arduino tells me that it receives the data on port 62181 (although the board is configured for the 9090 port)

Where in your code do you determine it is received on port 62181? I show your code only prints the sender's port, and it may be port 62181.

The "Destination not reachable" message normally indicates there is no route to the ip you are returning the packet to. What IP is the wifi shield getting from the router, and what IP does it show as the sender?

Hello Tim,

Thank you for your reply. The program returns the port 62981 when I run the Serial.println(Udp.remotePort()); command and check the serial messages returned from the Arduino. the sender's port (computer) is also 9091. I do not understand why this is happening.

As for the IPs, the router assigns the computer the 192.168.0.101 IP and the Arduino gets 192.168.0.100.

Concerning the WiFi shield, the firmware version is 1.1.0 (I have upgrade it myself). The thing is that I need to communicate via UDP with 3 Arduino boards. is there another firmware version in the works? I use the antenna connector WiFi shield to which I have attached an antenna.

Thank you,
Jean

I noticed this bug a while ago. When the UDP function in the WiFi shield responds to a packet, it gets another socket, and that socket uses another port. That causes problems with the sender device if that device expects the packet from the port it sent the packet to.

It seems your computer is not using port 9091 to send packets as you expected. I have always seen the correct port from my computer when using the remoteIP() call in the Arduino. I use my own UDP C code for the PC (Linux) and it works well.

I have abandoned the wifi shield for an ethernet shield and a wireless router. Too many bugs in the wifi shield firmware.

Thank you Tim for your reply,

Talking about bugs in the WiFi shield, I have noticed two things: sometimes, the Arduino outputs: "WiFi Shield not present", and I have to reset the unit again. After that, the WiFi transfer is not very reliable, it crashes inexplicably after 10 or 20 minutes, which is not good especially if you want to develop something more serious and sell it.

I think the Arduno is very good when developing some lab/teaching systems, but for industrial/professional use I will need something else more reliable. Do you have any suggestions?

Best regards,
Jean

If you have a SD card in the shield's slot, insure you disable it before starting the wifi shield. That goes for the ethernet shield also. It can cause fails like that.

Like I said, I use an ethernet shield with a wireless router "backwards". The WAN interface is the wireless, and the localnet is the ethernet ports. The router I use is pricey tho.

or less expensive but not as versatile

edit: These routers allow me to connect to the Arduino usb port using ssh, and provide power to the Arduino using the usb port connectors, among other neat features.