Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Buffalochill
#62660 hi guys !

I have two ESP8266, station mode, connected to my home WiFi.
One gets data from I2C and transmitts this data (Broadcast) over UDP.
The other one just listens to those UDP packages and prints them serial.

somehow the UDP is pretty slow.
Sometimes i have 100 packages (containing 32byte) per second.
then it gets stuck for some seconds where the transmission is very slow.
I need a fast transmission because its Lightorgan Data, and my LED devices run out of Data the whole time.

Can you please have a look at my Code and tell me if everything is ok ?
How many packages can be send per second and why is mine getting stuck all the time ?


Transmitter Code:
Code: Select all/ WIFI UDP MULTICASTER I2C


#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

#define   sda   D1    // D1 on NodeMCU Board
#define   scl   D2    // D2 on NodeMCU Board
#define   LED   D4    // D4 on NodeMCU Board = GPIO2

byte I2Cadress = 8;   // LiOR I2C Slave adress from Arduino

void setup()
{
    pinMode(LED, OUTPUT);          // WIFI OnBoard LED Light 
    Wire.begin(sda, scl);
    Wire.setClockStretchLimit(40000);    // in µs
    Wire.setClock(400000);
    Serial.begin(115200);
    WiFi_setup();
}

void loop(){
  Request_Data_from_I2CSlave();  // get Bytes from Arduino Slave over I2C
  Send_inData_to_WiFi();      // Sends  inData over WiFi UDP...
  Receive_WiFi_response();    // If a WIFI device sends us UDP data we can react in this function on this event
  WiFi_setup();               // Here we check if we are still connected to the WiFi server
}

void Request_Data_from_I2CSlave()
{
      int i = 0;
      //inData[32];
      Wire.requestFrom(I2Cadress, 32);       // request 32 bytes from slave device adress (I2C LiOr)
      while (Wire.available()>0) {    // slave may send less than requested
          inData[i] = Wire.read();     // receive a byte as character
          if (inData[i] == '>'){
            break;
          }
          i++;
          inData[i] = '\0';
          }
      i++;
      inData[i] = '\0';   
      Serial.println(inData);  // zum Debuggen
      UDP_String = inData;
}

void Send_inData_to_WiFi()
{
      Udp.beginPacket(ipMulti, 2390);
      Udp.print(UDP_String);      // Sends the char Array over WiFi which we got from I2C
      Udp.endPacket();   
}

void WiFi_setup()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("connection lost... reconnecting");
    WiFi.disconnect();      // Disconnect any previous WiFi
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);  //Start Wifi as Station
    Serial.println();
    Serial.println("[Connecting] to");
    Serial.println(ssid);
    int tries=0;
    while (WiFi.status() != WL_CONNECTED)
    {
      digitalWrite(LED, HIGH);
      delay(250);
      digitalWrite(LED, LOW);
      delay(250);
      Serial.print(".");
      tries++;
        if (tries > 10)
        {
        Serial.println();
        tries = 0;
        }
    }
    Serial.println();
    printWifiStatus();
    Serial.println("Connected to wifi");
    Serial.print("Udp server started at port ");
    Serial.println(localPort);
    Udp.begin(localPort);               // Start UDP Server at PORT
    multicast_esp8266_udpserver();
  } //end if 
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void multicast_esp8266_udpserver()     //When ESP8266 starts, Broadcast presence of ESP8255
{
  Udp.beginPacket(ipMulti, 2390);   
  Udp.write("<ESP8266_UDP_Server startet! IP: ");
  Udp.print(WiFi.localIP());
  Udp.write(">");
  Udp.endPacket();
}


void Receive_WiFi_response(){
  int noBytes = Udp.parsePacket();      //anzahl der erhaltenen Bytes steht in noBytes
  if ( noBytes ) {                 
}




Receiver Code:

Code: Select allvoid setup()
{
  pinMode(LED, OUTPUT);          // WIFI OnBoard LED Light
  pinMode(EnablePin, INPUT);     //
  Serial.begin(115200);
 
  WiFi.disconnect();       // Stop Any Previous WIFI
  WiFi.mode(WIFI_STA);      // Station mode kein eigenes Wifi hosten
  // setting up Station
  WiFi.begin(ssid, pass);
 
  Serial.println();
  Serial.println("[Connecting] to");
  Serial.println(ssid);
  int tries=0;
  while (WiFi.status() != WL_CONNECTED) {
  digitalWrite(LED, HIGH);
  delay(250);
  digitalWrite(LED, LOW);
  delay(250);
  Serial.print(".");
    tries++;
    if (tries > 10){
    Serial.println();
    tries = 0;
    }
  }
  Serial.println();
  Serial.println("Connected to WiFi");
  Serial.print("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);

}

void loop()
{
    Receive_UDP_print_Serial();
}

void Receive_UDP_print_Serial(){
  int noBytes = Udp.parsePacket();
  if ( noBytes )
  {
    Udp.read(packetBuffer,noBytes); // read the packet into the buffer
    char inChar[noBytes-2];
    int i;
    for ( i=1;i<=noBytes;i++){
        inChar[i-1] = packetBuffer[i-1];
        if (inChar[i] == '>'){
        break; 
        }

    } // end for
        i++;
        inChar[i] = '\0'; 
    //Wifi_Daten_Auswerten();
    Serial.println(inChar);
    SoftSerial.print(inChar);
  }


User avatar
By picstart
#62663 I'm not saying that some have not had reasonable throughput using UDP. I suspect it is not true all the time and not true in general. I accept that packet delivery is not guaranteed but very very slow delivery does appear to be guaranteed or at least sporadic performance. It may just be an issue with the Arduino IDE and UDP. Maybe some pared down working code with a benchmark would be more convincing that useful speeds can be achieved. Perhaps there is some algorithm that controls when packets are put in the air and that Udp.endPacket(); doesn't mean the packet is soon to go into the air.
User avatar
By Buffalochill
#62666 When i use UART instead of WIFI everything is fast and good.
If i use wifi udp. its slow and the lightogan is totaly asynchronous to the music. Its getting stuck for seconds and stuff. I mean if i would lose some packages i wouldnt mind, but in this case i dont even reach an accaptable level of data transfer performance :(

Is anything wrong with my code ?
I made the buffersize bigger but still bad transferrate