Fab Academy
Arthur DEFRAIN

ESP32 Contrôleur - Code

ESP32 Contrôleur - Code

             #include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN    D7
#define BUTTON_PIN_1 D4
#define BUTTON_PIN_2 D5
#define BUTTON_PIN_3 D6
#define NUM_LEDS   3
const char* ssid = "XIAO_HOTSPOT";
const char* password = "12345678";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
WiFiServer server(80);
String currentState = "libre";
unsigned long lastStatusSendTime = 0;
const unsigned long statusSendInterval = 500; // Envoi d'état toutes les 500 ms
void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  strip.begin();
  strip.show();
  strip.setBrightness(38);
  createHotspot();
  server.begin();
  xTaskCreatePinnedToCore(
    handleClientRequests, /* Function to implement the task */
    "HandleClientRequests", /* Name of the task */
    10000,  /* Stack size in words */
    NULL,  /* Task input parameter */
    1,  /* Priority of the task */
    NULL,  /* Task handle. */
    0); /* Core where the task should run */
  xTaskCreatePinnedToCore(
    sendStatusTask, /* Function to implement the task */
    "SendStatusTask", /* Name of the task */
    10000,  /* Stack size in words */
    NULL,  /* Task input parameter */
    1,  /* Priority of the task */
    NULL,  /* Task handle. */
    1); /* Core where the task should run */
}
void loop() {
  checkButtons();
  updateLEDs();
  delay(1); // Vérification rapide pour améliorer la réactivité
}
void createHotspot() {
  Serial.println("Creating hotspot...");
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("Hotspot IP address: ");
  Serial.println(IP);
}
void checkButtons() {
  static unsigned long lastDebounceTime = 0;
  static unsigned long debounceDelay = 50;
  if (millis() - lastDebounceTime > debounceDelay) {
    if (digitalRead(BUTTON_PIN_1) == LOW) {
      currentState = "libre";
    } else if (digitalRead(BUTTON_PIN_2) == LOW) {
      currentState = "occupe";
    } else if (digitalRead(BUTTON_PIN_3) == LOW) {
      currentState = "ne_pas_deranger";
    }
    lastDebounceTime = millis();
  }
}
void updateLEDs() {
  if (currentState == "libre") {
    setLEDColor(0, 0, 255, 0); // LED 1 en vert
    setLEDColor(1, 0, 0, 0);   // Éteindre LED 2
    setLEDColor(2, 0, 0, 0);   // Éteindre LED 3
  } else if (currentState == "occupe") {
    setLEDColor(0, 0, 0, 0);   // Éteindre LED 1
    setLEDColor(1, 255, 165, 0); // LED 2 en orange
    setLEDColor(2, 0, 0, 0);   // Éteindre LED 3
  } else if (currentState == "ne_pas_deranger") {
    setLEDColor(0, 0, 0, 0);   // Éteindre LED 1
    setLEDColor(1, 0, 0, 0);   // Éteindre LED 2
    setLEDColor(2, 255, 0, 0); // LED 3 en rouge
  }
  strip.show();
}
void setLEDColor(int ledIndex, int red, int green, int blue) {
  strip.setPixelColor(ledIndex, strip.Color(red, green, blue));
}
void handleClientRequests(void * parameter) {
  for (;;) {
    WiFiClient client = server.available();
    if (client) {
      Serial.println("Client connected");
      while (client.connected()) {
        if (client.available()) {
          String request = client.readStringUntil('\r');
          Serial.println("Received request: " + request);
          if (request.indexOf("GET") != -1) {
            sendState(client);
          }
        }
      }
      client.stop();
      Serial.println("Client disconnected");
    }
    delay(10); // Ajuster la valeur en fonction des besoins
  }
}
void sendState(WiFiClient client) {
  String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
  response += currentState;
  response += "\r\n";
  client.print(response);
  Serial.println("Sent state: " + currentState);
}
void sendStatusTask(void * parameter) {
  for (;;) {
    sendStatusToClient();
    delay(statusSendInterval); // Attendre avant de renvoyer
  }
}
void sendStatusToClient() {
  WiFiClient client;
  if (client.connect("192.168.4.2", 80)) { // Assurez-vous que cette adresse IP correspond à votre deuxième XIAO ESP32
    Serial.println("Sending state to server: " + currentState);
    client.print("GET /update?state=" + currentState + " HTTP/1.1\r\nHost: 192.168.4.2\r\nConnection: close\r\n\r\n");
    client.stop();
  } else {
    Serial.println("Connection to server failed");
  }
}