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?