#include <DHT.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include "ThingSpeak.h"

const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASS = "";
const int myChannelNumber = 2547816;
const char* write_API_Key = "JPQN2MZMRXZ2SH80";
const char* server = "api.thingspeak.com";
WiFiClient client;

float temperature = 0;

#define DHTPIN 12            
#define DHTTYPE DHT22        

#define LDRPIN 33 
#define LED_BAR_GRAPH_1 23   
#define LED_BAR_GRAPH_2 22   
#define LED_BAR_GRAPH_3 21   
#define LED_BAR_GRAPH_4 19   
#define RGB_R 25             
#define RGB_G 27             
#define RGB_B 26             
#define SERVO_PIN 13         
#define POTENTIOMETER_PIN 35 

DHT dht(DHTPIN, DHTTYPE);
Servo servo;

float co2Threshold = 350;
float temperatureThreshold = 30;

void setup() {
  Serial.begin(9600);

  connecte_WIFI();

  pinMode(LDRPIN, INPUT);

  pinMode(RGB_R, OUTPUT);
  pinMode(RGB_G, OUTPUT);
  pinMode(RGB_B, OUTPUT);

  pinMode(LED_BAR_GRAPH_1, OUTPUT);
  pinMode(LED_BAR_GRAPH_2, OUTPUT);
  pinMode(LED_BAR_GRAPH_3, OUTPUT);
  pinMode(LED_BAR_GRAPH_4, OUTPUT);

  servo.attach(SERVO_PIN);
}

void connecte_WIFI(){
  WiFi.begin(WIFI_NAME, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Wifi not connected");
  }
  Serial.println("Wifi connected!");
  Serial.println("Local IP: " + String(WiFi.localIP()));
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);

  Serial.println("Setup finished");
  Serial.println("---");
}

void loop() {

  // Scenario 1: CO2 élevé
  int potentiometerValue = analogRead(POTENTIOMETER_PIN);
  co2Threshold = map(potentiometerValue, 0, 4095, 0, 1000);
  Serial.print("Valeur de CO2 : ");
   Serial.println(potentiometerValue);
  float co2Level = analogRead(35);
  if (co2Level > co2Threshold) {
    servo.write(180); 
  }else{
    servo.write(90);
  }

  // Scenario 2: Température
  temperature = dht.readTemperature();
  Serial.print("Temperature: ");
  Serial.println(temperature);
  if (temperature > temperatureThreshold) {
    digitalWrite(RGB_R, HIGH);
    digitalWrite(RGB_G, HIGH);
    digitalWrite(RGB_B, LOW);
  } else if (temperature < temperatureThreshold) {
    digitalWrite(RGB_R, LOW);
    digitalWrite(RGB_G, HIGH);
    digitalWrite(RGB_B, HIGH);
  }

  // Scenario 3: Luminosité
  noInterrupts();
  int lightLevel = analogRead(LDRPIN);
  Serial.print("Luminosité: ");
  Serial.println(lightLevel);
  interrupts();

  if (lightLevel < 1000) {
    digitalWrite(LED_BAR_GRAPH_1, HIGH);
    digitalWrite(LED_BAR_GRAPH_2, LOW);
    digitalWrite(LED_BAR_GRAPH_3, LOW);
    digitalWrite(LED_BAR_GRAPH_4, LOW);
  } else if (lightLevel < 2500) {
    digitalWrite(LED_BAR_GRAPH_1, HIGH);
    digitalWrite(LED_BAR_GRAPH_2, HIGH);
    digitalWrite(LED_BAR_GRAPH_3, LOW);
    digitalWrite(LED_BAR_GRAPH_4, LOW);
  } else if (lightLevel < 4000) {
    digitalWrite(LED_BAR_GRAPH_1, HIGH);
    digitalWrite(LED_BAR_GRAPH_2, HIGH);
    digitalWrite(LED_BAR_GRAPH_3, HIGH);
    digitalWrite(LED_BAR_GRAPH_4, LOW);
  } else {
    digitalWrite(LED_BAR_GRAPH_1, HIGH);
    digitalWrite(LED_BAR_GRAPH_2, HIGH);
    digitalWrite(LED_BAR_GRAPH_3, HIGH);
    digitalWrite(LED_BAR_GRAPH_4, HIGH);
  }

  delay(1000);
}