Wi-Fi transmission delays

Hi, I've been having problems with the transmission of data over Wi-Fi. In simple terms, I'm sending constant blocks of data to an ESP-32 via the App and having strong delays every 4/5 blocks received. Here I uploaded a photo that shows the problem, you can see that between the yellow to the orange part there is a 1 sec delay. This happens randomly, and I have no clue what is causing that.

If anyone has some insight on the problem, any help is greatly appreciated.

Photo of the problem:


Code:

//PINES DISPONIBLES 14 ,17, 19, 21, 22, 23.
/*
  SOBRE LA POSICION DE LOS MOTORES":
  ^^  M1  M2 ^^
  ^^  M3  M4 ^^
  SOBRE EL GIROSCOPIO
  X= ROLL
  Y=PITCH
*/

/*
Advertencias:
- Tener en cuenta que la posicion del giroscopio/*accelerometro y magnetometro hara que haya que 
  redefinir la configuracion de los motores y demas varables.

*/
/*
  INCLUIR:
  WiFi.RSSI()

  WiFi.setHostname(YOUR_NEW_HOSTNAME);

  IPAddress local_IP(192, 168, 1, 184);
  // Set your Gateway IP address
  IPAddress gateway(192, 168, 1, 1);

  IPAddress subnet(255, 255, 0, 0);
  IPAddress primaryDNS(8, 8, 8, 8);   // optional
  IPAddress secondaryDNS(8, 8, 4, 4); // optional
*/
#include <WiFi.h>
//#include "I2Cdev.h"
//#include "MPU6050.h"
#include "Wire.h"
WiFiServer server(80);

const char* ssid = "SSID";                  //REMPLAZAR POR SSID
const char* password = "PASSWORD";              //REMPLAZAR POR CONTRA
String msj;                                 //STRING QUE GUARDA EL MENSAJE RECIBIDO POR WIFI
int TimingVar=950;
float bat = 99.9;                           //VARIABLE DE ALAMACENAMIENTO DE NIVEL DE BATERIA
                                            //FALTA VARIABLE DE MAGNETOMETRO!
float AnglePitchX;
float AnglePitchY;
float ax, ay;
float DatosApp[5] = {0,0,0,0,0};                //VARIABLE DE DATOS DE DIRECCION Y POTENCIA DE LA APP


int PW[4] = {0,0,0,0};                      //VARIABLES FINALES DEL DUTY CICLE DEL PWM DE LOS MOTORES // ORDEN M1,M2,M3,M4
float PWRoll;                               //VARIABLES DE ROLL 
float PWPitch;                              //VARIABLES DE PITCH
float PWYaw;                                //VARIABLES DE YAW
float RpE = 0;
float PpE = 0;
float YpE = 0;
#define M1 12                               //DEFINICION DE MOTORES
#define M2 14                               //DEFINICION DE MOTORES
#define M3 26                               //DEFINICION DE MOTOREaS
#define M4 27                               //DEFINICION DE MOTORES
#define KpRoll   0                          //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KiRoll   0                          //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KdRoll   0                          //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KpPitch   0                         //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KiPitch   0                         //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KdPitch   0                         //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KpYaw   0                           //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KiYaw   0                           //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define KdYaw   0                           //DEFINICION DE CONSTANTE PARA AJUSTE PID
#define PRDiv 1                             //DEFINICION DE CONSTANTE PARA DIVIDIR LA ENTRADA DE LA POTENCIA
#define ConstProportional 25.992883792      //DEFINICION DE CONSTANTE PARA AJUSTAR LA SALIDA DEL PID

void WifiStart(){
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  server.begin();
}
void assign(){
  String var = "";
  int i;
  int cont=24;
  for(i=24; msj[i]!='&'; i++){var += msj[i];cont++;}
  Serial.print("Slider: "); Serial.print(var); Serial.print("  ");
  DatosApp[0]= var.toFloat();
  var = "";

  cont = cont + 7;
  for(i=cont; msj[i]!='&'; i++){var += msj[i];cont++;}
  Serial.print("Gyro X Axis: "); Serial.print(var); Serial.print("  "); 
  DatosApp[1]= var.toFloat();
  var = "";
  
  cont = cont + 7;
  for(i=cont; msj[i]!='&'; i++){var += msj[i];cont++;}
  Serial.print("Gyro Y Axis: "); Serial.print(var); Serial.print("  "); 
  DatosApp[2]= var.toFloat();
  var = "";
  
  cont = cont + 4;
  for(i=cont; msj[i]!='&'; i++){var += msj[i];cont++;}
  Serial.print("Zero Y Axis: "); Serial.print(var); Serial.print("  "); 
  DatosApp[2]= var.toFloat();
  var = "";
  
  cont = cont + 4;
  for(i=cont; msj[i]!='\0'; i++){var += msj[i];cont++;}
  Serial.print("Zero Y Axis: "); Serial.print(var); Serial.print("  "); 
  Serial.println("uT");
  DatosApp[2]= var.toFloat();
  var = "";
  
  }
void clasify(){
  /*GET /Res?ID=1205&Slider=165.75&XGyro=0.04875&YGyro=0.04875&0x=-0.06&0y=0.015 HTTP/1.1*/
  int temp;
  String var = "";
  for(int i=12; i!='&'; i++){var += msj[i];}
  temp = var.toInt();
  if(TimingVar<temp && temp<TimingVar + 8000){
    assign();
    TimingVar=temp;
  }
  else if(TimingVar>9950 && temp<1050){
    assign();
    TimingVar=temp;
  } 
}
void MotorStart(){
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);
  pinMode(M3,OUTPUT);
  pinMode(M4,OUTPUT);  
  //VVVV COMPLETAMENTE OPCIONAL VVVV
  tone(M1,1975, 250);
  tone(M2,1975, 250);
  tone(M3,1975, 250);
  tone(M4,1975, 250);
  delay(350);
  tone(M1,2349, 250);
  tone(M2,2349, 250);
  tone(M3,2349, 250);
  tone(27,2349, 250);
  delay(350);
  tone(M1,2637, 250);
  tone(M2,2637, 250);
  tone(M3,2637, 250);
  tone(M4,2637, 250);
  delay(350);
  tone(M1,1975, 400);
  tone(M2,1975, 400);
  tone(M3,1975, 400);
  tone(M4,1975, 400);
  delay(420);
  tone(M1,2637, 400);
  tone(M2,2637, 400);
  tone(M3,2637, 400);
  tone(M4,2637, 400);
  delay(500);
  // ^^ TERMINA OPCIONAL ^^
}
void WifiConection(){
  WiFiClient client = server.available();
  client.println("GET /Res?Slider=xx&XGyro=xx&YGyro=xx&0x=xx&0y=xx HTTP/1.1");
  client.println("Host: " + String(WiFi.localIP()));
  client.println("Connection: close");
  client.println();
  msj = client.readStringUntil('\n');
  //Serial.println(msj);
  clasify();
}
void MotorDriver(){ 
  for (int i = 0; i < 4; ++i) {
    analogWrite(M1 + i, PW[i]);
    }
 }/*
void PIDRoll(){
  float E = Giroscopio[0] - DatosApp[0];
  float IoutRoll = IoutRoll + (E * KiRoll);
  PWRoll = (E * KpRoll) + ((E - RpE) * KdRoll) + IoutRoll;
  RpE = Giroscopio[0] - DatosApp[0];
}
void PIDPitch(){
  float E = Giroscopio[1] - DatosApp[1];
  float IoutPitch = IoutPitch + (E * KiPitch);
  PWPitch = (E * KpPitch) + ((E - PpE) * KdPitch) + IoutPitch;
  PpE = Giroscopio[1] - DatosApp[1];
}
/*void PIDYaw(){
  float E = Magnetometro;1
  float IoutYaw = Ioutyaw + (E * KiYaw);
  PWYaw = (E * KpYaw) + ((E - YpE) * KdYaw) + IoutYaw;
  YpE = Magnetometro;
}*/

void setup() {
  Serial.begin(115200);
  WifiStart();                              //INICIO DE RECEPCION DE DATOS
  //incialicia_Gyro();
  //MotorStart();
  //pinMode(X, INPUT);                     //PIN A DEFINIR PARA CONTROLAR LA CARGA DE LA BATERIA

}

void loop() {                               //NO PONER DELAYS!!!!!!!
  WifiConection();                          //RECEPCION DE DATOS
  //Giro();                                 //INPUT DEL GIROSCOPIO
  //Magnetometro();                         //INPUT DEL MAGNETOMETRO
  /*PIDRoll();                              //PID ROLL
  PIDPitch();                             //PID PITCH
  PIDYaw();                               //PID YAW
  PIDconvert();                           //SUMA DE LOS OUTPUT DE LOS PID
  MotorDriver();*/
  //Serial.println(DatosApp[0]);
  delay(20);                                //UNICO DELAY PARA DEJA PROCESAR
}

App:

App.aia (1.4 MB)

Data generated is faster than the hardware gyroscope can respond perhaps.
How often is the gyroscope data sampled? Appears every 500 ms or faster. Perhaps that is too fast for your system memory on your esp-32 or Android.

It might be that I try that, but I don't think either my phone or the ESP32 lack memory. Im also verifying the Serial conection Im very suspicius about that.

Thanks.

I've done some testing and increasing the delay didn't help. I've also checked the transmision speed at wich the ESP-32 can recieve data (2 Mbps, plenty), that shouldn't be the bottle neck because I'm only sending a string of data every 100ms. So I'm in the look again for answers.

Something that I noticed is that the dalay is always 1sec, don't know if that means something.

only 100ms? What happens if you set it to 1000 ms? What you experience is probably a 'garbage collection' delay that automatically free up memory space when data is reset faster than the hardare can handle.

Hi, I've been testing further and found a completly another problem. Thanks for your idea.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.