Client.read(); doesn´t work as I want it to

So I´ve been struggling with a code that works with LabVIEW trough the TCP/IP protocol. I think I figured out where the problem is but I haven´t been able to solve it. So I have a TCP write block in labVIEW that sends the numer 50 to my controller to compare some values inside the Arduino code. I used the serial.print to visualize what is recieved and for each loop the value shifts from 53 to 48 to -1 and between those three numbers (its never 50 as I want it to) until labview reports an error that says that the connection was closed by the peer. So my guess is that the client.read function doesn´t work as I tought it did but I don´t know what I could use instead to get that number from LabVIEW. If I use the TCP write block without sending any data the client doesn´t disconnect and everyhting else works fine.

//Se incluye librería para el WiFi
#include <WiFi.h>

//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(8000);

//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;

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 inicia el ESP32 como servidor
  server.begin();

  // 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() {

// Mostrar al cliente que el ESP32 como servidor está disponible
  WiFiClient client = server.available();

//Mostrar en el puerto serial cuando el cliente se haya conectado
  if (client) {
    Serial.println("Cliente conectado");

//Ejecutar mientras el cliente esté conectado
    while (client.connected()) {
      
      //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 (client.available() > 0) {

        //Se define si se enciende o apaga LED1**************************************
        int labview = client.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);
        }
     // }

//Mostrar en el puerto serial el valor obtenido por el sensor
//Enviar al cliente el valor obtenido por el sensor
      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);
    }
  
  //Verificar si el servidor tiene un cliente conectado
  if(server.hasClient()){
  //Si no hay cliente salir del loop y esperar otro cliente
  return;
  }  
      
  }      
}

PLC = Programmable Logic Controller. A fancy automation computer.

I use 200 bytes to/from the Arduino/PLC which is why my values are what they are. This is just a small portion. I've also found that the PLC can send TCP information faster than the board checks and clears it from the buffers so I use "while (EIPlen >= 200) {" to read it until flushed.

byte InBuffer[200];
byte OutBuffer[200];

  EIPlen = ethClient.available();

  while (EIPlen >= 200) {
    if (EIPlen > 200) {
      Serial.print(F("EIPlen: "));
      Serial.println(EIPlen);
    }
    ethClient.read(InBuffer, 200);
    EIPlen = ethClient.available();
  }

  ethClient.flush();
  ethClient.write(OutBuffer, 200);

  if (InBuffer[38] == 0) {
    LINBaud = 19200;
  } else {
    LINBaud = InBuffer[38];
  }

  switch (InBuffer[40]) {
      LINprotocol = "2.1";
    case 1:
      LINprotocol = "1.0";
      break;
    case 2:
      LINprotocol = "2.0";
      break;
    case 3:
      LINprotocol = "2.1";
      break;
  }

If I recieve it in ascii format when I compare the values in a line like this

if (labview == '2')

Does Arduino IDE interpret is as 50? or as the other numbers?

Already did that but the Labview code still disconnects :frowning:

Is there any way to get it to read the whole "50" at the same time? insteade of doing 2 different reads?

//Se incluye librería para el WiFi
#include <WiFi.h>

//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(8000);

//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;

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 inicia el ESP32 como servidor
  server.begin();

  // 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() {

// Mostrar al cliente que el ESP32 como servidor está disponible
  WiFiClient client = server.available();

//Mostrar en el puerto serial cuando el cliente se haya conectado
  if (client) {
    Serial.println("Cliente conectado");

//Ejecutar mientras el cliente esté conectado
    while (client.connected()) {
      //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);

      while (client.available()) {

        //Se define si se enciende o apaga LED1**************************************
        int labview = client.read();
        Serial.println(labview);
        
        if (porcentaje2 > labview && porcentaje2==0) {
          digitalWrite(led1, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led1, HIGH && porcentaje2 !=0);
        }

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

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

        //Se define si se enciende o apaga LED4****************************************
        //int labview=Serial.read();
        if (porcentaje5 > labview) {
          digitalWrite(led4, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led4, HIGH);
        }
      }

//Mostrar en el puerto serial el valor obtenido por el sensor
//Enviar al cliente el valor obtenido por el sensor
      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);
    }
  
  //Verificar si el servidor tiene un cliente conectado
  if(server.hasClient()){
  //Si no hay cliente salir del loop y esperar otro cliente
  return;
  }  
      
  }      
}

This is the code that prints so I can see what is going on. I get a bunch of values from my sensors and in between I get the 53 and 48 as you said

Yes, there's an example in the code I posted.

Thank you, I´m gonna try it

This is the LabVIEW code that Im using to test this

The idea was to use a write function for each sensor but as I mentioned I can´t even get it to work with just one. I´m going to try the conversion that you explained to see if it helps.

I'm trying to put it in your code now but I don't have the wifi library on this machine and can't compile it. It crashed my last session and I had to reboot.

I'm unable to compile and test it. Check lines 23, 24, 95 - 110.

//Se incluye librería para el WiFi
#include <WiFi.h>

//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(8000);

//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;

byte InBuffer[1];
byte OutBuffer[1];

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 inicia el ESP32 como servidor
  server.begin();

  // 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() {

  // Mostrar al cliente que el ESP32 como servidor está disponible
  WiFiClient client = server.available();

  //Mostrar en el puerto serial cuando el cliente se haya conectado
  if (client) {
    Serial.println("Cliente conectado");

    //Ejecutar mientras el cliente esté conectado
    while (client.connected()) {
      //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);

      while (client.available()) {

        // Read out and clear buffer data if more than 1 byte is in memory
        // client.available() gives the amount of bytes available
        EIPlen = client.available();
        while (EIPlen >= 1) {
          ethClient.read(InBuffer, 1);
          EIPlen = ethClient.available();
        }

        Serial.println(F("Byte read from Labview: "));
        // Array starts at 0
        Serial.print(InBuffer[0]);

        // To send information back to LV
        // flush() is used to prevent writes until any previous write operations have been performed
        //ethClient.flush();
        //ethClient.write(OutBuffer, 1);

        //Se define si se enciende o apaga LED1**************************************
        int labview = client.read();
        Serial.println(labview);

        if (porcentaje2 > labview && porcentaje2 == 0) {
          digitalWrite(led1, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led1, HIGH && porcentaje2 != 0);
        }

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

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

        //Se define si se enciende o apaga LED4****************************************
        //int labview=Serial.read();
        if (porcentaje5 > labview) {
          digitalWrite(led4, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led4, HIGH);
        }
      }

      //Mostrar en el puerto serial el valor obtenido por el sensor
      //Enviar al cliente el valor obtenido por el sensor
      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);
    }

    //Verificar si el servidor tiene un cliente conectado
    if (server.hasClient()) {
      //Si no hay cliente salir del loop y esperar otro cliente
      return;
    }

  }
}

it says:
Compilation error: 'EIPlen' was not declared in this scope

Derp. Yeah, throw this in as a global.

int EIPlen;

Compilation error: 'ethClient' was not declared in this scope

//Se incluye librería para el WiFi
#include <WiFi.h>

//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(8000);

//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;

byte InBuffer[1];
byte OutBuffer[1];
int EIPlen;

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 inicia el ESP32 como servidor
  server.begin();

  // 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() {

  // Mostrar al cliente que el ESP32 como servidor está disponible
  WiFiClient client = server.available();

  //Mostrar en el puerto serial cuando el cliente se haya conectado
  if (client) {
    Serial.println("Cliente conectado");

    //Ejecutar mientras el cliente esté conectado
    while (client.connected()) {
      //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);

      while (client.available()) {

        // Read out and clear buffer data if more than 1 byte is in memory
        // client.available() gives the amount of bytes available
        EIPlen = client.available();
        while (EIPlen >= 1) {
          client.read(InBuffer, 1);
          EIPlen = client.available();
        }

        Serial.println(F("Byte read from Labview: "));
        // Array starts at 0
        Serial.print(InBuffer[0]);

        // To send information back to LV
        // flush() is used to prevent writes until any previous write operations have been performed
        //ethClient.flush();
        //ethClient.write(OutBuffer, 1);

        //Se define si se enciende o apaga LED1**************************************
        int labview = client.read();
        Serial.println(labview);

        if (porcentaje2 > labview && porcentaje2 == 0) {
          digitalWrite(led1, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led1, HIGH && porcentaje2 != 0);
        }

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

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

        //Se define si se enciende o apaga LED4****************************************
        //int labview=Serial.read();
        if (porcentaje5 > labview) {
          digitalWrite(led4, LOW);
        }
        if (porcentaje2 < labview) {
          digitalWrite(led4, HIGH);
        }
      }

      //Mostrar en el puerto serial el valor obtenido por el sensor
      //Enviar al cliente el valor obtenido por el sensor
      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);
    }

    //Verificar si el servidor tiene un cliente conectado
    if (server.hasClient()) {
      //Si no hay cliente salir del loop y esperar otro cliente
      return;
    }

  }
}

I'm trying to copy/paste some stuff I have working is all. It used to be 200 bytes. Feel free to improve and make suggestions.

imagen

This is what I get from the serial monitor. And LabVIEW is still disconnecting :cry:

I have to leave for the day but on line 73, instead of while(), try using if() instead. Maybe it's getting stuck in that statement so the TCP stack isn't being maintained and the connection is dropping out. Not sure about your data format. If I think of something I'll pop in though.

I changed it and it seems like it only does one loop and the sensor values stop showing up.

Oh sorry I forgot to mention it. Im doing an automated irrigation so the 50 is the humidity percentage that the soil should have. If the sensor value is less than 50 the valves should be turned on, if it is greater than 50 the valves are turned off. That is one of the ideas that I had.

I don´t just write the 50 in the Arduino code because the user is supposed to be able to change the number if they need to without having to code anything, just by entering the value in the interface of labview.

The other thing that I wanted to do instead of this was to compare the value of each sensor (I have 4 of them) with the humidity percentage specified by tyhe user. If the number is lower or higher send x number to arduino, and that part for the arduino code would look like this

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);
        }

But since it has been so hard to just get it to work with only one TCP write I was thinking that the first option would be better. Also I was using the code that I posted in this comment and I was using the TCP write with the numbers that are used to turn the valves on (1, 3, 5, 7) and it does work (only for 2 minutes), so that means that arduino does "understand" when these numbers are sent as they are. But still the same problem. It disconnects after a while.

Also I was testing some lines of code that I found and now it seem like it mantains the connection, but it isn´t reading the number as it should I guess because of the size of buffer that i put but I´m confused about how to know what the buffer size should be, the bytes part is really confusing for me. Im trying to send the number "1"

//Se incluye librería para el WiFi
#include <WiFi.h>

//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(8000);

//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;

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 inicia el ESP32 como servidor
  server.begin();

  // 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() {

// Mostrar al cliente que el ESP32 como servidor está disponible
  WiFiClient client = server.available();

//Mostrar en el puerto serial cuando el cliente se haya conectado
  if (client) {
    Serial.println("Cliente conectado");

uint8_t ReceiveBuffer[8];

//Ejecutar mientras el cliente esté conectado
    while (client.connected()) {
      
      //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 (client.available() > 0) {

        //Se define si se enciende o apaga LED1**************************************
        int labview = client.read(ReceiveBuffer, sizeof(ReceiveBuffer));

        Serial.print(labview);
        
        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);
        }
     // }

//Mostrar en el puerto serial el valor obtenido por el sensor
//Enviar al cliente el valor obtenido por el sensor
      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);
    }
  
  //Verificar si el servidor tiene un cliente conectado
  if(server.hasClient()){
  //Si no hay cliente salir del loop y esperar otro cliente
  return;
  }  
      
  }      
}

The lines I added are 68 and 96