#include <WiFi.h>


// Déclaration des broches pour ESP32
int trigPin = 23; // GPIO23
int echoPin = 22; // GPIO22
float photoresistorPin1 = 34; // GPIO34 (ADC1)
int LED_40W_PIN = 25; // GPIO25
int LED_100W_PIN = 26; // GPIO26

const char WiFiPassword[] = "1234567890";
const char AP_NameChar[] = "GOGETASSJ4";

WiFiServer server(80);

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON'></form><br>";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF'></form><br>";
String html_4 = "<form id='F3' action='CMD_MAN_ON'><input class='button' type='submit' value='CMD_MAN_ON'></form><br>";
String html_5 = "<form id='F4' action='CMD_MAN_OFF'><input class='button' type='submit' value='CMD_MAN_OFF'></form><br>";
String html_6 = "</div></body></html>";

String request = "";

// Variables d'état pour les lampes
bool manualControl = false;
unsigned long timer = 0;
const unsigned long timeoutDuration = 300000; // 5 minutes

void setup() {
  // Configuration des broches des capteurs
  pinMode(photoresistorPin1, INPUT);
  pinMode(LED_40W_PIN, OUTPUT);
  pinMode(LED_100W_PIN, OUTPUT);
  // Configuration des broches du capteur ultrasonique
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(115200);
  boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
  server.begin();
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    // Read the first line of the request
    request = client.readStringUntil('\r');
    client.flush();

    // Traitement de la requête
    if (request.indexOf("LED_40W_PIN_A") > 0) {
      Serial.println("ALLUME 40 W");
      digitalWrite(LED_40W_PIN, LOW);
      manualControl = true;
      timer = millis(); // Reset timer
    } else if (request.indexOf("LED_40W_PIN_E") > 0) {
      Serial.println("ETEINT 40 W");
      digitalWrite(LED_40W_PIN, HIGH);
      manualControl = true;
      timer = millis(); // Reset timer
    } else if (request.indexOf("LED_100W_PIN_A") > 0) {
      Serial.println("ALLUME 100 W");
      digitalWrite(LED_100W_PIN, LOW);
      manualControl = true;
      timer = millis(); // Reset timer
    } else if (request.indexOf("LED_100W_PIN_E") > 0) {
      Serial.println("ETEINT 100 W");
      digitalWrite(LED_100W_PIN, HIGH);
      manualControl = true;
      timer = millis(); // Reset timer
    } else if (request.indexOf("CMD_MAN_ON") > 0) {
      Serial.println("COMMANDE MANUELLE ACTIVE");
      manualControl = true;
      timer = millis(); // Reset timer
    } else if (request.indexOf("CMD_MAN_OFF") > 0) {
      Serial.println("COMMANDE AUTO ACTIVE");
      manualControl = false;
    }

    client.print(header);
    client.print(html_1);
    client.print(html_2);
    client.print(html_3);
    client.print(html_4);
    client.print(html_5);
    client.print(html_6);
    delay(5);
  }

  // Lire les valeurs analogiques des photorésistances
  int value1 = analogRead(photoresistorPin1);

  // Mesure de la distance avec le capteur ultrasonique
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  // Afficher les valeurs lues sur le moniteur série
  Serial.println("Valeur de la photorésistance 1 : ");
  Serial.println(value1);
  Serial.println("Distance : ");
  Serial.println(distance);

  // Conditions pour les lampes basées sur les capteurs
  unsigned long currentMillis = millis();

  // Vérification du temporisateur pour le passage en mode automatique
  if (manualControl && (currentMillis - timer >= timeoutDuration)) {
    manualControl = false;
    Serial.println("Mode automatique pour les deux lampes");
  }

  // Exécution des actions automatiques uniquement si nous ne sommes pas en mode manuel
  if (!manualControl) {
    if ((distance < 100) && (value1 < 490)) {
      Serial.println("Mouvement détecté !");
      digitalWrite(LED_40W_PIN, HIGH);
      digitalWrite(LED_100W_PIN, HIGH);
    } else if (value1 <= 90) {
      Serial.println("ON EST LE JOUR");
      digitalWrite(LED_40W_PIN, LOW);
      digitalWrite(LED_100W_PIN, LOW);
    } else if (value1 < 490 && value1 > 90) {
      Serial.println("on est entre le jour et la nuit");
      digitalWrite(LED_40W_PIN, HIGH);
      digitalWrite(LED_100W_PIN, LOW);
    } else {
      Serial.println("on est la nuit");
      digitalWrite(LED_100W_PIN, HIGH);
      digitalWrite(LED_40W_PIN, LOW);
    }
  }

  delay(5000);
}