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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.