Client isn´t connecting to ESP32 server

I wrote a code that reads sensor data and sends it to LabVIEW and also writes data from labview to the controller. I´m doing it trough the TCP/IP protocol but I´m having trouble connecting the esp32 and labview. I´m supposed to be able to see on the serial monitor if a client is connected but nothing comes up and I also get a LabVIEW error that means either that the server refused the connection or that the server hasn't been started but I haven´t been able to figure out what is wrong with my code since I have followed multiple examples that I´ve found online. Could anyone help me figure out if something is missing?

//Se incluye librería para el WiFi
#include <WiFi.h>
//Se define el número del puerto como "8000"
#define SERVER_PORT 8000
//Se definen nombre de red y contraseña a la que se va a conectar el controlador
const char* ssid="";
const char* pass="";

//Se configura el servidor con el puerto 8000
WiFiServer server (SERVER_PORT);

//Se conectan los sensores a los pines 36, 39, 34 y 35
int sensor2=36;
int sensor3=39;
int sensor4=34;
int sensor5=35;

//Se conectan los relé a los pines 23, 22, 21 y 19
int led1=23;
int led2=22;
int led3=21;
int led4=19;

char labview=0;

void setup() {

Serial.begin(115200);

//Se inicia la conexión a la red WiFi definida
WiFi.begin (ssid,pass);
delay(2000);

//Mostrar asteriscos mientras se realiza la conexión
while (WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print("*");
}

//Se anuncia cuando ya se haya conectado a la red WiFi y se muestra la dirección IP que será usada para el código en LabVIEW
Serial.println("");
Serial.println("Se ha conectado a la red WiFi");
Serial.println("Dirección IP: ");
Serial.println(WiFi.localIP());



// Se establecen los sensores como entrada
pinMode(sensor2,INPUT);
pinMode(sensor3,INPUT);
pinMode(sensor4,INPUT);
pinMode(sensor5,INPUT);

//Se establecen los relé como salida
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);

}

void loop() {

//Conectar con cliente
WiFiClient client = server.available();

if (client){   
Serial.println("New Client");

while(client.available()){

  uint8_t data =client.read();  
  Serial.write(data);

//Calibración del sensor 2********************************
int humedad2=analogRead(sensor2);
//int humedad1=334*humedad-81;
int porcentaje2=map(humedad2, 579,252,0,100);

//Calibración del sensor 3**********************************
int humedad3=analogRead(sensor3);
//int humedad1=334*humedad-81;
int porcentaje3=map(humedad3, 579,252,0,100);

//Calibración del sensor 4**********************************
int humedad4=analogRead(sensor4);
//int humedad1=334*humedad-81;
int porcentaje4=map(humedad4, 579,252,0,100);

//Calibración del sensor 5**********************************
int humedad5=analogRead(sensor5);
//int humedad1=334*humedad-81;
int porcentaje5=map(humedad5, 579,252,0,100);

if (Serial.available()>0)
{

//Se define si se enciende o apaga LED1**************************************
 int labview=Serial.read();
if (labview=='0')
{ 
digitalWrite(led1,LOW); 
}
 if (labview=='1')
 {  
digitalWrite(led1, HIGH);
}

//Se define si se enciende o apaga LED2****************************************
//int labview=Serial.read();
if (labview=='2')
{  
digitalWrite(led2,LOW);
}
 if (labview=='3')
 {  
digitalWrite(led2, HIGH);
}

//Se define si se enciende o apaga LED3****************************************
//int labview=Serial.read();
if (labview=='4')
{ 
digitalWrite(led3,LOW); 
}
 if (labview=='5')
 {  
digitalWrite(led3, HIGH);
}

//Se define si se enciende o apaga LED4****************************************
//int labview=Serial.read();
if (labview=='6')
{ 
digitalWrite(led4,LOW); 
}
 if (labview=='7')
 { 
digitalWrite(led4, HIGH);
}

}
Serial.println(porcentaje2);
client.println(porcentaje2);

Serial.println(porcentaje3);
client.println(porcentaje3);

Serial.println(porcentaje4);
client.println(porcentaje4);

Serial.println(porcentaje5);
client.println(porcentaje5);
}



delay(1000);
}
}

I haven't used the ESP wireless capabilities, but if you're not hosting an access point, are you sure that all the relevant firewalls are allowing traffic? Maybe try a direct connection if you aren't already.

are you specifying both the IP and port (8000)?

I already made sure that the firewalls aren´t blocking the communication. When I use the serial port with the board the code works well

Yes, I already specified them in my LabVIEW code

You haven't posted your Labview code, but another user recently had an issue where not putting in a delay in between port open and read was causing issues. Can you post your labview code and test a delay in that code.

I can´t upload the LabVIEW code because of the format. I do have a 90000 ms timeout in the port open function but it still doesn´t work. Is that what you meant by the delay?

Maybe it was between read and write. I've been trying to search for the old post but haven't been able to find it yet.

So I kind of got it working now, I used the server.begin() command and it now connects to LabVIEW but I have a new problem. After around 2 minutes of the connection i get a LabVIEW error that says that the network connection was closed by the peer. For some reason the ESP32 is closing the connection but I haven´t figured out why, or how to connect it again automatically after the client disconnects

I use this for staying connected to a PLC. Maybe it'll help?

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Connection_LED_Red();
  } else {
    if (Ethernet.linkStatus() == 1) {
      if (!ethClient.connected()) {
        ethClient.stop();
        if (ethClient.connect(server, TCP_Client_Port)) {
          Serial.println(F("Connected to server"));
        }
      }
      if (ethClient.connected()) {
        Connection_LED_Green();
        TCP_Communications();
      }
      if (!ethClient.connected()) Connection_LED_White();
    } else {
      Connection_LED_Blue();
    }
  }

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