Regarding the buffer, I don´t know how else to write it then, because it has only worked now that i used the receivebuffer part. Using the part of code that you posted I get the same problem as before, it looses the connection.
//Se incluye librería para el WiFi
#include <WiFi.h>
#define NUM_SENSORS 4
#define NUM_LEDS 4
const byte SensorPins[NUM_SENSORS] = {36,39,34,35};
const byte LedPins[NUM_LEDS] = {23,22,21,19};
//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);
unsigned long lastSample = 0;
int intervalSample = 5000;
int SensorVals[NUM_SENSORS];
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();
for (int i = 0; i < NUM_SENSORS;i++){
pinMode(SensorPins[i], INPUT);
}
for (int i = 0; i < NUM_LEDS;i++){
pinMode(LedPins[i], 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()) {
bool haveChange = false;
if (millis()-lastSample >= intervalSample){
lastSample = millis();
int hum;
int por;
for (int i = 0; i < NUM_SENSORS;i++){
hum = analogRead(SensorPins[i]);
//int humedad1=334*humedad-81;
por = map(hum, 579, 252, 0, 100);
if (SensorVals[i]!=por){
SensorVals[i] = por;
haveChange = true;
}
}
}
//quick case for single char commands
if(client.available()){
switch(client.read()){
case '0': digitalWrite(LedPins[0], LOW); break;
case '1': digitalWrite(LedPins[0], HIGH); break;
case '2': digitalWrite(LedPins[1], LOW); break;
case '3': digitalWrite(LedPins[1], HIGH); break;
case '4': digitalWrite(LedPins[2], LOW); break;
case '5': digitalWrite(LedPins[2], HIGH); break;
case '6': digitalWrite(LedPins[3], LOW); break;
case '7': digitalWrite(LedPins[3], HIGH); break;
default: break;
}
}
//Mostrar en el puerto serial el valor obtenido por el sensor
//Enviar al cliente el valor obtenido por el sensor
if (haveChange){
for (int i = 0;i<NUM_SENSORS;i++){
Serial.println(SensorVals[i]);
client.println(SensorVals[i]);
}
}
}
}
}
TLDR. If you only need one value, @Delta_G's method is probably the way to go. If you need multiple values in either direction, best to get acquainted with the array format used in the buffer declaration.
The buffer is the number of bytes you're sending. So say you wanted to send "50" as a value, that would go in the first buffer array position, which starts at zero. If it's only one value then yes, you don't need an array. If you wanted to send multiple values, say humidity and temperature, you could send "50" in the first array value buffer[0], and then the temp in the second buffer[1]. For the example we tried, you have an array with a entry that starts at [0].
The amount of bytes sent in Labview should also be specified as well. If not, I'm assuming it just sends one byte. One of the issues that can crop up is that if Labview or other TCP source keeps sending packets faster than the Arduino can clear them out, they back up and can cause a potential board crash.
I suggested removing the first while statement and making it an if statement. The reason for the while checking the client.available length is to make sure the entire buffer is cleared out each cycle. I ran into an issue where the number of bytes I was communicating was sometimes more than the Arduino could keep up with and it would freeze, crash, or malfunction. I doubt it's an issue with such a small payload here, but like I said I was trying to gut, copy, and paste some code I new worked as quickly as I could.
Thank you so so much. This works perfectly. It´s been running for almost 24 hours with no problems at all. I wanted tu ask you what the last sample and interval sample were for, is it to control the speed to read the sensors? and if so, if i change it to a lower number will the values show faster in labview or could that crash the whole thing?
You're welcome, happy to hear it has worked well for you..
and Yes, intervalSample currently set to 5000 (5 seconds) is how often the sensor is read.
Most temp sensors have a limit as to how fast you can read them, not sure what you are using, so yes, of course experiment and try lower values if you like. Not sure what Labview will have to say about, so again, experiment some and see..