#include <DHT.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include "ThingSpeak.h"
#include <HTTPClient.h>
#include <UrlEncode.h>
#include "DHTesp.h"

DHTesp sensor;

const char* ssid = "Wokwi-GUEST";
const char* password = ""; 

String phoneNumber = "212705489106";
String apiKey = "2663452";

void sendMessage(String message) {
  String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
  HTTPClient http;
  http.begin(url);
  int httpResponseCode = http.GET();  // Use GET instead of POST
  
  if (httpResponseCode == 200) {
    Serial.print("Message sent successfully");
  } else {
    Serial.println("Error sending the message");
    Serial.print("HTTP response code: ");
    Serial.println(httpResponseCode);
  }
  
  http.end();
}

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

float temperature = 0;
float humidity = 0;
int potentiometerValue = 0;

const float GAMMA = 0.7;
const float RL10 = 50;
int luminosite = 0;

#define DHTPIN 12            
#define DHTTYPE DHT22        

#define LDRPIN 33 
#define led1 23   
#define led2 22   
#define led3 21   
#define led4 19   
#define RED 25             
#define GREEN 27             
#define BLUE 26             
#define SERVO_PIN 13         
#define POTENTIOMETER_PIN 35 

DHT dht(DHTPIN, DHTTYPE);
Servo servo;

float co2_val = 350;
float temperatureThreshold = 30;

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

  connecte_WIFI();
  
  dht.begin();  // Initialize the DHT sensor
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();
  sendMessage("Temp:" + String(temperature, 2) + "C" + "\n" + "Humidity:" + String(humidity, 1) + "%");

  pinMode(LDRPIN, 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);
}

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é
  potentiometerValue = analogRead(POTENTIOMETER_PIN);
  potentiometerValue = map(potentiometerValue, 0, 4095, 0, 1000);

  Serial.print("Valeur de CO2 : ");
  Serial.print(potentiometerValue);
  Serial.println(" ppm");

  if (potentiometerValue > co2_val) {
    servo.write(180); 
  } else {
    servo.write(90);
  }

  // Scenario 2: Température
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  if (temperature > temperatureThreshold) {
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, LOW);
  } else {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, HIGH);
  }

  // Scenario 3: Luminosité
  luminosite = analogRead(LDRPIN);
  float voltage = luminosite * 5 / 4095.0;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  luminosite = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  Serial.print("Luminosité: ");
  Serial.print(luminosite);
  Serial.println(" lux");

  if (luminosite < 1000) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  } else if ((luminosite >= 1000) && (luminosite < 2500)) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  } else if ((luminosite >= 2500) && (luminosite < 4000)) {
    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(1000);

  sendDataToThingSpeak();
}

void sendDataToThingSpeak() {
  ThingSpeak.setField(1, temperature);
  delay(500);
  ThingSpeak.setField(2, humidity);
  delay(500);
  ThingSpeak.setField(3, potentiometerValue);
  delay(500);
  ThingSpeak.setField(4, luminosite);
  delay(500);

  int writeResult = ThingSpeak.writeFields(myChannelNumber, write_API_Key);

  if (writeResult == 200) {
    Serial.println("Data pushed successfully");
  } else {
    Serial.println("Push error: " + String(writeResult));
  }
  Serial.println("---");
}
$abcdeabcde151015202530354045505560fghijfghij