can't send/recieve UDP packets via Wi-Fi shield

Hi,
I’m trying to send information from the arduino board to my computer through the Wi-Fi network.
for my project’s purposes it has to be a UDP connection
I use the “Send and Receive UDP String” example (http://arduino.cc/en/Tutorial/WiFiSendReceiveUDPString)
with a few changes:

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
char ssid[] = "itay_net"; //  your network SSID (name) 
char pass[] = "0527414540";    // your network password (use for WPA, or use as key for WEP)

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

IPAddress remote_ip(192, 168, 1, 100);
unsigned int remote_port = 50505;


char  ReplyBuffer[] = "acknowledged";       // a string to send back

WiFiUDP Udp;

void setup() {
  //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);
  } 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  delay(10000);
  printWifiStatus();



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

void loop() {
  
  int bite_send;
  Udp.beginPacket(remote_ip, remote_port);
  bite_send = Udp.write("hello");
  Udp.endPacket();
  Serial.println("the packet was sent");
  Serial.println(bite_send);



  delay(1000);
}


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

It compiles and connects to the network just fine.
the only problem is that I can’t tell if the packet was sent because I see no trace of it on Wireshark.
I also wrote a socket on java that listens to the port (50505) and should display the message from the packet, but it didn’t work either.
(I can copy the java code here but i can assure you that it is not the problem 'cause I tested it with a different java server and it worked, so the problem should be on the Arduino side)

a few details to narrow it down:
I believe the “remote ip” is correct but even if it isn’t - I still should have seen it in the Wireshark, so it can’t be the problem.
I should mention that the Wi-Fi shield works, I successfully sent pings and ran other examples (such as SimpleWebServerWifi).

I’m using an original Arduino Uno R3 board and an original Wi-Fi shield.
The arduino IDE is the newest version.
I updated the Wi-Fi shield with the newest update I found on GitHub.

I also ran the same “Send and Receive UDP String” code (with the necessary changes) on my Ethernet shield and it did work.

I don’t know what else to try - please help.
any help will be appreciated.

Itay

Can you copy and paste the debugging messages please? Inside code tags.

HI Nick,
There are no debugging messages.
It compiles and runs as if it would send the packets.
I even get the serial print message "the packet was sent" (the relevant lines are copied below) but the Java socket doesn't receive anything and I can't find the packets on the Wireshark.

void loop() {
  
  int bite_send;
  Udp.beginPacket(remote_ip, remote_port);
  bite_send = Udp.write("hello");
  Udp.endPacket();
  Serial.println("the packet was sent");
  Serial.println(bite_send);

  delay(1000);
}

itaysteinberg:
There are no debugging messages.
...
I even get the serial print message "the packet was sent"

That's a debugging message. I am asking you to copy and paste the session output. Not the code that produces it (we have that) but what you see on the serial monitor.

There are two functions that return values that not many people use. Udp.begin() and Udp.endPacket() both return 1 on success and 0 on fail. Bear in mind that endPacket success does not mean the packet was accepted by the destination, but only your wifi router took the packet. Those two checks might help you figure out what is happening.

// in setup, add this error checking to the Udp.begin() call.
  Serial.print("\nStarting UDP...");
  if(!Udp.begin(localPort)) Serial.println(F("failed"));  
  else Serial.println(F("ok"));

void loop() {
  int bite_send;
  Udp.beginPacket(remote_ip, remote_port);
  bite_send = Udp.print("hello");

  if(!Udp.endPacket()) Serial.println("packet failed");
  else Serial.println("packet sent");

  Serial.println(bite_send);

  delay(1000);
}

edit: I changed Udp.write() to Udp.print(). The write() function called like that sends only one character.

SurferTim:
Udp.begin() and Udp.endPacket() both return 1 on success and 0 on fail. Bear in mind that endPacket success does not mean the packet was accepted by the destination, but only your wifi router took the packet.

Thank you Nick Gammon and SurferTim,
I had a little crisis at work, so i couldn't reply until now.

I've edited the code with your additions and ran it and found out that the error is in the endPacket function.
Here are the debugging messages:
"Attempting to connect to WPA SSID: -ITAY.NET-
You're connected to the networkSSID: -ITAY.NET-
IP Address: 192.168.1.103
signal strength (RSSI):-70 dBm

Starting connection to server...

Starting UDP...ok
packet failed
5
packet failed
5
packet failed
5
...."

I still haven't figured out the cause to this failure but now, thanks to you, I know where to look.
This is the first progress I had in a long time
I will keep you updated soon as I'll find out the cause.

Of course, If anyone already experienced this error and/or have any suggestions I would love to hear them.

Itay

I had exactly the same problem as you, I've now been successful by downgrading my IDE version back to 1.0.3 and copying the WIFI library across from 1.0.5 then upload and try again :slight_smile:
Hope this helps

Onamission71:
I had exactly the same problem as you, I've now been successful by downgrading my IDE version back to 1.0.3 and copying the WIFI library across from 1.0.5 then upload and try again :slight_smile:
Hope this helps

Thanks Onamission71 for your comment, but no such luck :confused:
I tried it and got the same error as before.

Itay

Can you ping the Arduino's ip from the receiving UDP device?
Are you sure the receiving device is 192.168.1.100?
Do you have the firewall open for udp port 50505 on the receiving device?

It compiles and connects to the network just fine.
the only problem is that I can’t tell if the packet was sent because I see no trace of it on Wireshark.

Wireshark monitors your PCs network interface (NIC). You would not normally see any trace of packets sent from the WiFi shield, as they are sent directly to your WiFi Access Point. It is possible to intercept traffic sent to/from the shield, by looping it back to the PCs NIC but you would need a hub or switch with port mirroring and possibly an additional Access Point.

Have you tried the WiFiUdpNtpExample, which you can find in the WiFi/Examples folder?

Sorry everyone,
I tried everything again from scratch and found that the problem was in the firmware update.
apparently it didn't go well at the first time (I can't recall what I've done differently) but now it works just fine.

Itay

Try opened port in the server Ip by testing them using in application for detecting what port opened