Multitrack recording remote control

Coder l’ESP32 pour l’envoi et l’écoute sur 2 port (port 51326 pour l’envoi des trames REC et STOP, et port 51325 pour l’écoute du statut de la console et controler la LED) est plus compliqué.
Si tu y arrives je suis preneur.

En attendant voici une optimisation du code avec gestion des handshake (sans reconnection auto):

#include <WiFi.h>

// Configuration Réseau
const char* ssid     = "*****";
const char* password = "******";
const char* fx_ip    = "*.*.*.*"; 
const uint16_t fx_port = 51326; // Retour sur le port stable qui accepte les commandes

// Configuration Hardware
const int BTN_REC_PIN  = 4;  
const int BTN_STOP_PIN = 5;  
const int LED_STAT_PIN = 2;  // Câblée sur GPIO 2 avec R=1kOhm

// Variables de debounce
const unsigned long debounceDelay = 50;
unsigned long lastRecDebounce  = 0;
unsigned long lastStopDebounce = 0;
int lastRecState  = HIGH;
int lastStopState = HIGH;

// Variable de KeepAlive
unsigned long d_KeepAlive = 0;

WiFiClient client;

// --- TRAMES D'ÉMISSION (PORT 51326) ---
uint8_t trameREC[9] = {0xF7, 0x11, 0x11, 0x11, 0x03, 0xFF, 0x00, 0x00, 0x00};
uint8_t trameSTOP_1[9] = {0xF7, 0x11, 0x11, 0x10, 0x01, 0xFF, 0x00, 0x00, 0x00};
uint8_t trameSTOP_2[9] = {0xF7, 0x11, 0x11, 0x11, 0x01, 0xFF, 0x00, 0x00, 0x00};

// Votre trame de KeepAlive TCP qui stabilise le port 51326
uint8_t pingTrame[8] = {0xF7, 0x00, 0x04, 0x0F, 0x00, 0x01, 0x03, 0x00}; 

void setup() {
    Serial.begin(115200);
    
    pinMode(BTN_REC_PIN, INPUT_PULLUP);
    pinMode(BTN_STOP_PIN, INPUT_PULLUP);
    pinMode(LED_STAT_PIN, OUTPUT);
    digitalWrite(LED_STAT_PIN, LOW); // LED éteinte au démarrage

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nWi-Fi connecté !");
}

void loop() {
    // S'assurer de la connexion TCP
    verifierConnexion();

    // ---- ANTI-DÉCONNEXION TCP (Toutes les 3 secondes) ----
    if (client.connected() && (millis() - d_KeepAlive > 3000)) {
        d_KeepAlive = millis();
        client.write(pingTrame, 8);
    }

    // ---- GESTION BOUTON REC ----
    int readingRec = digitalRead(BTN_REC_PIN);
    if (readingRec != lastRecState) lastRecDebounce = millis();
    if ((millis() - lastRecDebounce) > debounceDelay) {
        if (readingRec == LOW) { 
            actionREC();
            delay(200); 
        }
    }
    lastRecState = readingRec;

    // ---- GESTION BOUTON STOP ----
    int readingStop = digitalRead(BTN_STOP_PIN);
    if (readingStop != lastStopState) lastStopDebounce = millis();
    if ((millis() - lastStopDebounce) > debounceDelay) {
        if (readingStop == LOW) { 
            actionSTOP();
            delay(200); 
        }
    }
    lastStopState = readingStop;
}

void actionREC() {
    if (!client.connected()) return;
    Serial.println("[TX] Envoi commande REC...");
    client.write(trameREC, sizeof(trameREC));
    
    // Synchro locale : on allume la LED immédiatement
    digitalWrite(LED_STAT_PIN, HIGH);
    Serial.println("[LED] Statut : ENREGISTREMENT (ON)");
}

void actionSTOP() {
    if (!client.connected()) return;
    Serial.println("[TX] Envoi commande STOP...");
    client.write(trameSTOP_1, sizeof(trameSTOP_1));
    client.write(trameSTOP_2, sizeof(trameSTOP_2));
    
    // Synchro locale : on éteint la LED immédiatement
    digitalWrite(LED_STAT_PIN, LOW);
    Serial.println("[LED] Statut : ARRÊTÉ (OFF)");
}

bool verifierConnexion() {
    if (!client.connected()) {
        digitalWrite(LED_STAT_PIN, LOW); // Éteindre la LED en cas de perte réseau
        
        Serial.println("Connexion au port de commande stable 51326...");
        if (client.connect(fx_ip, fx_port)) {
            Serial.println("-> Connecté à la console.");
            d_KeepAlive = millis(); 
            return true;
        } else {
            Serial.println("Échec de la connexion.");
            delay(1000);
            return false;
        }
    }
    return true;
}