1

I'm using the Arduino WiFi Shield to make two Arduinos Due communicate using UDP. The first node to send a message (the Client) doesn't always receive a reply from the other node (the Server). The code for the Client:

#include <string.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#define BUFSZ 90
// variables for WiFi connection
char ssid[] = "ssid";
char pass[] = "pwd";
int status = WL_IDLE_STATUS;
char sendBuffer[BUFSZ];
char recvBuffer[BUFSZ];
WiFiUDP Udp;
unsigned int localPort = 51515;
IPAddress server(192,168,88,251);
unsigned int serverPort = 12345;
void printWifiData()
{
 // print your WiFi shield's IP address:
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);
 // print your MAC address:
 byte mac[6]; 
 WiFi.macAddress(mac);
 Serial.print("MAC address: ");
 Serial.print(mac[5],HEX);
 Serial.print(":");
 Serial.print(mac[4],HEX);
 Serial.print(":");
 Serial.print(mac[3],HEX);
 Serial.print(":");
 Serial.print(mac[2],HEX);
 Serial.print(":");
 Serial.print(mac[1],HEX);
 Serial.print(":");
 Serial.println(mac[0],HEX);
}
void setup()
{
 Serial.begin(115200);
 while (!Serial); // wait for serial port to connect
 // connect to WiFi network
 while (status != WL_CONNECTED) {
 Serial.print("Attempting to connect to WPA SSID: ");
 Serial.println(ssid); 
 status = WiFi.begin(ssid, pass);
 delay(10000);
 }
 // print connection details
 Serial.println("You're connected to the network");
 printWifiData();
 Udp.begin(localPort);
 strcpy(sendBuffer, "abcdefghijklmnopqrstuvwxyz123456789101112131415161718192021222324252627282930313233343536");
}
void loop()
{
 Udp.beginPacket(server, serverPort);
 int len = Udp.write(sendBuffer);
 Udp.endPacket();
 Serial.print("Sent ");
 Serial.print(len);
 Serial.println(" bytes.");
 delay(10);
 int packetSize = Udp.parsePacket();
 if (packetSize > 0) {
 Serial.print("\nReceived 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 recvBuffer
 int len = Udp.read(recvBuffer, BUFSZ);
 if (len > 0) {
 Serial.println("Contents:");
 Serial.println(recvBuffer);
 }
 else {
 Serial.println("Read 0 bytes.");
 }
 }
 else {
 Serial.println("\nNo packets yet.");
 }
}

Server:

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#define BUFSZ 90
// variables for WiFi connection
char ssid[] = "ssid";
char pass[] = "pwd";
int status = WL_IDLE_STATUS;
char sendBuffer[BUFSZ];
char recvBuffer[] = "acknowledged";
WiFiUDP Udp;
unsigned int localPort = 12345;
void printWifiData()
{
 // print your WiFi shield's IP address:
 IPAddress ip = WiFi.localIP();
 Serial.print("IP Address: ");
 Serial.println(ip);
 // print your MAC address:
 byte mac[6]; 
 WiFi.macAddress(mac);
 Serial.print("MAC address: ");
 Serial.print(mac[5],HEX);
 Serial.print(":");
 Serial.print(mac[4],HEX);
 Serial.print(":");
 Serial.print(mac[3],HEX);
 Serial.print(":");
 Serial.print(mac[2],HEX);
 Serial.print(":");
 Serial.print(mac[1],HEX);
 Serial.print(":");
 Serial.println(mac[0],HEX);
}
void setup()
{
 Serial.begin(115200);
 while (!Serial); // wait for serial port to connect
 // connect to WiFi network
 while (status != WL_CONNECTED) {
 Serial.print("Attempting to connect to WPA SSID: ");
 Serial.println(ssid); 
 status = WiFi.begin(ssid, pass);
 delay(10000);
 }
 // print connection details
 Serial.println("You're connected to the network");
 printWifiData();
 Udp.begin(localPort);
 Serial.println("Waiting...");
}
void loop()
{
 int packetSize = Udp.parsePacket();
 if (packetSize) {
 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 recvBuffer
 int len = Udp.read(recvBuffer, BUFSZ);
 if (len > 0) {
 Serial.println("\nContents:");
 Serial.println(recvBuffer);
 Udp.beginPacket(Udp.remoteIP(), 51515);
 Udp.write(sendBuffer);
 Udp.endPacket();
 }
 else {
 Serial.println("Read 0 bytes.");
 }
 }
}

I don't know if it's a timing problem... So I've tried to use delays after the Client send its message but the issue persists. So the Client sends many messages to the Server but receives few replies. I didn't want to put the part of the code in the Client when it's waiting for a response inside a loop in case the first message is lost (so the Client will never receive a response anyway). Did anyone have a simular problem or a suggestion to fix this issue?

asked Jan 29, 2018 at 15:40
2
  • 1
    duplicate of forum.arduino.cc/index.php?topic=525579 Commented Jan 29, 2018 at 17:30
  • UDP is not a protocol that guarantees delivery Commented Jan 29, 2018 at 19:33

1 Answer 1

1

I was able to solve the problem. First of all, I needed a bigger delay between the send and receive parts in the Client.

So I'm using delay(35);. This is important so the Client can have enough time to receive the reply before sending another message.

Also, inside the Server, the buffers to send and receive a packet were inverted.

So, the correct would be:

char sendBuffer[] = "acknowledged";
char recvBuffer[BUFSZ];

I understand the messages can stil be lost because UDP does not guarantee delivery, but with a larger delay in an isolated WiFi network all of the messages from the Client got a reply.

answered Jan 29, 2018 at 19:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.