Fab Academy
Arthur DEFRAIN

ESP32 Récepteur - Code

             #include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN    D1
#define NUM_LEDS   10
const char* ssid = "XIAO_HOTSPOT";
const char* password = "12345678";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
WiFiClient client;
void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.show();
  strip.setBrightness(38);
  connectToWiFi();
}
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connectToWiFi();
  } else {
    receiveStatus();
  }
  delay(500); // Tentative de réception d'état toutes les 500 ms
}
void connectToWiFi() {
  Serial.print("Connecting to Wi-Fi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("Connected to Wi-Fi");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void receiveStatus() {
  Serial.println("Attempting to connect to server...");
  if (!client.connect("192.168.4.1", 80)) { // Assurez-vous que cette adresse IP correspond à votre premier XIAO ESP32
    Serial.println("Connection to server failed");
    return;
  }
  client.print("GET / HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: close\r\n\r\n");
  String state = "";
  while (client.connected()) {
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println("Received line: " + line);
      if (line.length() == 1) {
        state = client.readStringUntil('\r');
        Serial.println("Received state: " + state);
        break; // Sortir de la boucle après avoir lu l'état
      }
    }
  }
  client.stop();
  if (state == "libre") {
    setAllLEDsColor(0, 255, 0); // Vert
    Serial.println("LEDs set to Vert");
  } else if (state == "occupe") {
    setAllLEDsColor(255, 165, 0); // Orange
    Serial.println("LEDs set to Orange");
  } else if (state == "ne_pas_deranger") {
    setAllLEDsColor(255, 0, 0); // Rouge
    Serial.println("LEDs set to Rouge");
  }
}
void setAllLEDsColor(int red, int green, int blue) {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(red, green, blue));
  }
  strip.show();
}