#include <WiFi.h>
#include <DHT.h>
#include <ESP32Servo.h>
#include "ThingSpeak.h"
#include <UrlEncode.h>
#include <HTTPClient.h>
float hum=10;
float temp=10;
float lim;
// Define phone number and API key for the WhatsApp messaging service
String phoneNumber = "212705489106";
String apiKey = "2336824";
// Function to send a WhatsApp message
void sendMessage(String message){
// Construct the URL for the CallMeBot WhatsApp API with phone number, API key, and encoded message
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
// Create an HTTPClient object
HTTPClient http;
// Initialize the HTTPClient with the constructed URL
http.begin(url);
// Send a GET request and get the HTTP response code
int httpResponseCode = http.GET();
// Check if the message was sent successfully
if (httpResponseCode == 200) {
Serial.println("Message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Close the HTTP connection
http.end();
}
// 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 = 4;
DHT dht(DHTPIN, DHTTYPE);
// Servo details
Servo servo;
int SERVO_PIN = 17;
// Potentiometer details
int POTENTIOMETER_PIN = 35;
// LED pins
int Red = 25;
int Green = 33;
int Blue = 32;
// Luminosity
int LDRPIN = 34;
int led1 = 22;
int led2 = 21;
int led3 = 19;
int led4 = 5;
// CO2 and temperature thresholds
float co2Threshold = 350;
float temperatureThreshold = 30;
void setup() {
Serial.begin(115200);
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);
// Initialize DHT sensor
dht.begin();
// Read temperature and humidity data from the sensor
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Send a WhatsApp message with the temperature information
sendMessage("Temp: " + String(temperature, 2) + "C");
}
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
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) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
} else if (lightLevel < 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(100);
sendDataToThingSpeak();
}
void sendDataToThingSpeak() {
hum = dht.readHumidity();
temp = dht.readTemperature();
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 successfully");
} else {
Serial.println("Push error: " + String(writeResult));
}
Serial.println("---");
}