TCP Connection latency between two ESP32's

alwurts
Posts: 4
Joined: Sat Jun 30, 2018 2:49 pm

TCP Connection latency between two ESP32's

Postby alwurts » Wed Jul 04, 2018 7:08 am

Hi everyone i'm connecting two ESP32 directly to each other through wifi, one is an access point and the other is a client.

So far connection seems to be reliable however the interval in which the messages get sent is very slow sometimes up to 300ms, for my project i would like to send and receive values for motors and servos all concatenated in a single string such as (motor1Value)/(motor2Value)/(servoValue)\r and i would like to send this values with as low of a latency as possible.
For testing purposes i'm sending a value i from the client and sending it back from the access point.

I'm new to using TCP so any help would be greatly appreciated, maybe i'm doing something wrong or maybe it just the limitations of the microcontroller.

Below is my code for the two ESP32's.

Code for the Access point

Code: Select all


#include <WiFi.h>

WiFiServer server(80);                // Set a object server as a WiFiServer class
IPAddress IP(192,168,4,15);           // Select ip and mask
IPAddress mask = (255, 255, 255, 0);

// The devices that want to connect to our Access Point will need to use the next credentials.
char ssid[] = "****";           // SSID for the Access Point 
char pass[] = "****";         // password for the Access Point

byte EspLed = 2;                      // Built in lED on the ESP32

void setup() {
  
  Serial.begin(115200);
  
  WiFi.mode(WIFI_AP);                     // Set Wi-Fi as access point/server
  WiFi.softAP(ssid, pass);                // SSID and Password for the AP
  WiFi.softAPConfig(IP, IP, mask);        // Set our own desired IP address
  server.begin();                         // Begin the server
  
  pinMode(EspLed, OUTPUT);                // Set Buil-in LED as output

  Serial.println("Server started.");
  Serial.print("IP: ");     
  Serial.println(WiFi.softAPIP());        // .softAPIP calls for the IP of the access point which we set earlier
  Serial.print("MAC:");     
  Serial.println(WiFi.softAPmacAddress());  // Calls for the mac address
}

void loop() {
  
  WiFiClient client = server.available(); // Return a client object to the class if there is a client available
  if (!client) {return;}                  // Return cuts the function (loop) if client class is not connected
  
  digitalWrite(EspLed, LOW); 
  
  String request = client.readStringUntil('\r');    // Reads string received until \r and saves as string 
  Serial.println(request);
  
  client.flush();                                 
  client.println(request + " Aknowledge" + "\r");   // Send the data with the \r so the client knows when to stop
  Serial.println(request  + "Aknowledge");                          // Prints the data that we sent 
  
  digitalWrite(EspLed, HIGH);
}
Code for the client

Code: Select all

#include <WiFi.h>

int i = 0;

char ssid[] = "****";           // SSID of your AP
char pass[] = "****";         // password of your AP

IPAddress server(192,168,4,15);     // IP address of the AP
WiFiClient client;

byte EspLed = 2;                    // Built in LED on ESP32      

void setup() {
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);              // Wifi as client mode
  WiFi.begin(ssid, pass);           // Credentials to connect to the Access Point
  
  Serial.println("Connection to the AP");    
  while (WiFi.status() != WL_CONNECTED) {       
    Serial.print(".");
    delay(500);
  }
  
  Serial.println("Connected");
  
  Serial.print("LocalIP:");          
  Serial.println(WiFi.localIP());               
  Serial.println("MAC:" + WiFi.macAddress());
  Serial.print("Gateway:"); 
  Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC:"); 
  Serial.println(WiFi.BSSIDstr());
  
  pinMode(EspLed, OUTPUT);
}

void loop() {
  
  if(i > 100){
    i = 0;
  }
  
  if (!client.connect(server, 80)) {          // Connect to the server through the port 80
          Serial.println("connection failed");  // If server not connected run connection again
          return;                                 // return terminates the function (loop)
      }
  
  digitalWrite(EspLed, LOW);
  
  client.print(i);
  client.println("\r");                       // Send a value to the AP with \r so it knows when to stop
  Serial.print(i);
  Serial.println("\r");
  
  
  unsigned long timeout = millis();       // The next block of code handles disconnections from the AP
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {      // If its disconnected for more than 5 seconds then start again
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }
    
  String answer = client.readStringUntil('\r');   // Receive value from the server until \r
  Serial.println(answer);
  //client.flush();
  client.stop();
  
  digitalWrite(EspLed, HIGH);
  i++;
}

Who is online

Users browsing this forum: Google [Bot] and 62 guests