-->
Page 1 of 1

UDP transmission slow?

PostPosted: Tue Oct 31, 2017 9:06 am
by funtastic
Hi,
i am communicating between different ESP-8266 by WIFI UDP broadcasts. If i send a 17 byte message to a client that just echos it back i receive the response not before 200ms which is to slow for my usecase.
If i do the same on a raspberry communicating with a WIFI PC the response is received within 300 mikroseconds.
Is there something that i might do wrong or is there something that i have to configure?

I already tried to disable wifi encryption without big impact.
Using wireshark i also see a delay of around 200ms between first message and the response.

Then i tried to send 5 messages in a row. In Wireshark i saw a delay of only 3ms between the messages.
So does receiving cause the problem? Any other hint? Thank you!

Here is a test sketch that shows the problem.
Just fill out the WIFI ssid and password and start it once with client=true and once with client=false

#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <FS.h>

IPAddress myLocalIp;
IPAddress broadcastIp;
byte inBuffer[512];
int udpPort = 15555;
WiFiUDP Udp;

boolean client=true;
int start=0;
int counter=0;

void setup()
{
String ssidString = "PUT_YOUR_SSID";
String passwordString = "OUT_YOUR_PASSWORD";
client=true; // configure once as client and once as not client

Serial.begin(115200);
connectWifi(ssidString, passwordString);

if (client) Serial.println("I am client");
}

void loop()
{
if (!client)
{
client=true;
Serial.println("Preparing first message");
delay(2000);
Udp.beginPacketMulticast(broadcastIp, udpPort,WiFi.localIP());
Udp.write(inBuffer,10);
Udp.endPacket();
start=millis();
Serial.println("send!");
}

int noBytes = Udp.parsePacket();
if ( noBytes>0)
{
Udp.read(inBuffer,noBytes); // read the packet into the buffer
int duration = millis()-start;
counter++;

printf ("Round %d duration %d ms\n", counter, duration);

Udp.beginPacketMulticast(broadcastIp, udpPort,WiFi.localIP());
Udp.write(inBuffer,noBytes);
Udp.endPacket();
start=millis();
Serial.println("written");
}
}

void connectWifi(String ssidString, String passwordString)
{
char ssid[ssidString.length()+1];
ssidString.toCharArray(ssid, ssidString.length()+1);

char password[passwordString.length()+1];
passwordString.toCharArray(password, passwordString.length()+1);

WiFi.mode(WIFI_STA);
if (WiFi.status() != WL_CONNECTED) WiFi.begin(ssid, password);

// Wait for connection
int i=0;
while (WiFi.status() != WL_CONNECTED && i<60)
{
delay(500);
Serial.print(".");
i++;
}

if (WiFi.status() == WL_CONNECTED)
{
myLocalIp = WiFi.localIP();
Serial.println(myLocalIp);
broadcastIp = IPAddress(myLocalIp[0], myLocalIp[1], myLocalIp[2], 255);
Serial.print("UDP broadcast ip is ");
Serial.println(broadcastIp);
Udp.begin(udpPort);
}
else Serial.print("timeout: wlan not connected!");
}


My output:
Preparing first message
send!
Round 1 duration 146 ms
written
Round 2 duration 210 ms
written
Round 3 duration 194 ms
written
Round 4 duration 210 ms
written
Round 5 duration 195 ms
written
Round 6 duration 204 ms
written
Round 7 duration 200 ms
written
Round 8 duration 212 ms
written
Round 9 duration 199 ms
written
Round 10 duration 196 ms
written
Round 11 duration 203 ms
written
Round 12 duration 204 ms
written
Round 13 duration 201 ms
written
Round 14 duration 202 ms
written
Round 15 duration 204 ms
written
Round 16 duration 201 ms
written
Round 17 duration 202 ms
written
Round 18 duration 202 ms
written
Round 19 duration 211 ms
written
Round 20 duration 193 ms

Re: UDP transmission slow?

PostPosted: Sun Nov 05, 2017 2:11 am
by gdsports
Broadcast/multicast over WiFi is always slow. Try unicast.