UDP Transmission is not fast enough ?
Posted: Thu Feb 16, 2017 4:12 pm
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:
Receiver Code:
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 all
void 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);
}
}