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

float hum=10;
float temp=10;
float lim;

//thingspeak details
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;


#define DHTPIN 12            
#define DHTTYPE DHT22        

#define LDRPIN 2 
#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 = 0;
float temperatureThreshold = 30;

void setup() {
  Serial.begin(9600);
  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 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(0); // Servo ouvre complètement les fenêtres
    //delay(5000);      // Attendre 5 secondes
    //servo.write(90);   // Revenir à la position fermée
  }else{
    servo.write(90);
  }

  // Scenario 2: Température
  float 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é
  int lightLevel = analogRead(LDRPIN);
  Serial.print("Luminosité: ");
  Serial.println(lightLevel);
  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(100);

  sendDataToThingSpeak();
}

void sendDataToThingSpeak(){
  ThingSpeak.setField(1,temp);
    delay(500);
  ThingSpeak.setField(2,hum);
  
  
  int writeResult = ThingSpeak.writeFields(myChannelNumber,write_API_Key);
  
  Serial.println("Temp: " + String(temp, 2) + "°C");
  Serial.println("Humidity: " + String(hum, 1) + "%");
  
  if(writeResult == 200){
    Serial.println("Data pushed successfull");
  }else{
    Serial.println("Push error" + String(writeResult));
  }
  Serial.println("---");
}