ESP8266 latency via UDP

Viewed 754

I create a simple setup of a master (ESP8266-12F) and a slave (ESP8266-01) and use UDP for data transmission. A message contains 4 bytes. I measured the time between the Master sends a request, and the Master gets the answer from the Slave via micros().

The Problem is there is a latency increase from 2-4 [ms] up to 25 [ms] and more.

  • To confirm that I remove all Serial.prints() perhaps a Serial Buffer problem, and I use as Signalindicator a digital Pin (High on Request and LOW if Answerpacket arrived) the Oscilloscope shows the same behaviour.

  • Next Step I thought maybe a intern Buffer increase? So I watch the Heap to each signal but there seems no problem. Perhaps a static buffer only for the WiFi???.

  • Next step I changed the interval-time of Masters request from 50 [ms] to 1000 [ms], but still the same picture. Also a different CPU Clockfreq. (80-160 MHz) brings no changes. Pictures:

  • Afterwards I created a new nodemcu Firmeware (Modules: adc, file, gpio, i2c, net, node, spi, tmr, uart, wifi), but same behaviour…

  • I thought it is a WiFi-Signal problem, so I tried some Metalshields around the ESPs, but this brings not the latency in it…

  • Maybe a general WiFi-Modul problem, so I tried to write commands on an SD card and measure the time… And there was the same behaviour, but not so random. Now I think there are more than one or two reasons for the UDP latency.


QUESTION:

Has anyone the same problems or have anyone experienced a behaviour similar to this?

enter image description here enter image description here enter image description here enter image description here


Edit : Added code

// Master (Send Request and waits for answer)
void loop(void) 
{
    delay(50);
    CallSensor(); 
    do{
        ;
    }while(!ListenClients());
}

void CallSensor(void)
{
    static long counter = 0;
    char     requestPacket[_SIZE_PACKET_] = {0x50,0x00,0x00,0x00};
    APlocal_IP[3] = SENSOR_FIELD[0][0];
    
    if(SENSOR_FIELD[0][0] != 1)
    {
        TIME_UDP_START = micros();
        digitalWrite(_PIN_LED_B_, HIGH);
        UDP.beginPacket(APlocal_IP, UDPPort);
        UDP.write(requestPacket);
        UDP.endPacket();
    }
}

bool ListenClients()
{
    static uint16_t     remport = 0;
    static IPAddress    remIP   = {0,0,0,0}; 
    char                incomingPacket[_SIZE_PACKET_] = {0x00,0x00,0x00,0x00};

    if( UDP.parsePacket() && (UDP.read(incomingPacket, _SIZE_PACKET_) > 0) )
    {
        remIP   = UDP.remoteIP();
        remport = UDP.remotePort();
        
        if (SENSOR_FIELD[0][0] == remIP[3])
        {
            TIME_UDP_STOP = micros();
            digitalWrite(_PIN_LED_B_, LOW);
            Serial.printf(":%d\n",(TIME_UDP_STOP-TIME_UDP_START));
            return true;
        }
        else{return false;}
    }
    else{return false;}
}

// Slave (sends Value on Masters Request)
void loop(void) 
{
    ListenHost();
}

void ListenHost()
{
    static uint16_t     remport     = 0;
    static IPAddress    remIp       = {0,0,0,0};
    char    incomingPacket[_SIZE_PACKET_] = {0x00,0x00,0x00,0x00};
    
    if( UDP.parsePacket() && (UDP.read(incomingPacket, _SIZE_PACKET_) > 0))
    {
        SendAnswer();
    }
}

void SendAnswer(void)
{
    char     requestPacket[_SIZE_PACKET_] = {0x26,0x00,0x00,0x00};
    UDP.beginPacket(APlocal_IP, UDPPort);
    UDP.write(requestPacket);
    UDP.endPacket();    
}
0 Answers
Related