#include <WiFi.h>
#include <DHT.h>
#include <ESP32Servo.h>
#include "ThingSpeak.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;

//DHT details
#define DHTTYPE DHT22
int DHTPIN=12;
DHT dht(DHTPIN, DHTTYPE);

//Servo details
Servo servo;
int SERVO_PIN = 18;

//Potentiometer details
int POTENTIOMETER_PIN = 36;

//Pins des led
int Red = 27;
int Green = 26;
int Blue = 25;

//Luminosité
int LDRPIN = 33;

int led1 = 19;
int led2 = 21;
int led3 = 22;
int led4 = 23;

//valeurs de CO2 et température
float co2Threshold = 350;
float temperatureThreshold = 30;


void setup() {
   Serial.begin(115200);
  while(!Serial);
  Serial.println("Test");
  
  pinMode(LDRPIN, INPUT);
  pinMode(POTENTIOMETER_PIN, INPUT);

  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  servo.attach(SERVO_PIN);
  servo.write(90);

  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);

}

void loop() {
  // Scenario 1: CO2 élevé
  int potentiometerValue = analogRead(POTENTIOMETER_PIN);
  potentiometerValue = map(potentiometerValue,0, 1023, 0, 180);
   Serial.print("Valeur de CO2 : ");
   Serial.println(potentiometerValue);
  if (potentiometerValue > co2Threshold) {
    servo.write(0); // Servo ouvre complètement les fenêtres
    delay(10000);      // 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(Red, LOW);
    digitalWrite(Green, LOW);
    digitalWrite(Blue, HIGH);
  } else {
    digitalWrite(Red, HIGH);
    digitalWrite(Green, LOW);
    digitalWrite(Blue, LOW);
  }


  // Scenario 3: Luminosité
  int lightLevel = analogRead(LDRPIN);
  Serial.print("Luminosité: ");
  Serial.println(lightLevel);
  if (lightLevel < 1000) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  } else if (lightLevel < 2500) { // Changed condition to avoid overlap
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  } else if (lightLevel < 4000) { // Changed condition to avoid overlap
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
  } else {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, 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("---");
}
$abcdeabcde151015202530fghijfghij