#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
// Configuration du DHT22
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Configuration du LCD 16x2 I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse I2C 0x27, écran 16x2
// Broche du potentiomètre
const int potPin = 36;
// Broche du buzzer
#define BUZZER_PIN 5
// Seuils d'alerte
const float TEMP_THRESHOLD = 38.0; // Seuil de température en °C
const float HUMIDITY_THRESHOLD = 70.0; // Seuil d'humidité en %
const int HEART_RATE_THRESHOLD = 100; // Seuil de fréquence cardiaque en bpm
const int SPO2_THRESHOLD = 95; // Seuil de SpO2 en %
// Informations Wi-Fi
const char* ssid = "Wokwi-GUEST"; // Remplacez par votre SSID Wi-Fi
const char* password = ""; // Remplacez par votre mot de passe Wi-Fi
// Configuration ThingSpeak
const char* thingspeakApiKey = "0JGRL5JSXVHZ3QEP"; // Remplacez par votre clé API ThingSpeak
const char* thingspeakUrl = "http://api.thingspeak.com/update";
void setup() {
// Initialisation du moniteur série
Serial.begin(115200);
// Initialisation du DHT22
dht.begin();
// Initialisation du LCD
lcd.begin(16, 2); // Initialisation avec 16 colonnes et 2 lignes
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initialisation...");
delay(2000);
lcd.clear();
// Configuration du buzzer
pinMode(BUZZER_PIN, OUTPUT);
// Connexion Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connexion au Wi-Fi...");
}
Serial.println("Connecté au Wi-Fi");
}
void loop() {
// Lecture de la température et de l'humidité
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Lecture de la valeur du potentiomètre (simulation de fréquence cardiaque et SpO2)
int potValue = analogRead(potPin);
int heartRate = map(potValue, 0, 4095, 60, 120); // Simule une fréquence cardiaque entre 60 et 120
int spo2 = map(potValue, 0, 4095, 90, 100); // Simule un SpO2 entre 90% et 100%
// Affichage des données sur le moniteur série
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Heart Rate: ");
Serial.print(heartRate);
Serial.print(" bpm, SpO2: ");
Serial.print(spo2);
Serial.println(" %");
// Affichage des données sur le LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("HR: ");
lcd.print(heartRate);
lcd.print(" SpO2: ");
lcd.print(spo2);
lcd.print("%");
// Vérification des seuils et déclenchement du buzzer si nécessaire
if (temperature > TEMP_THRESHOLD || humidity > HUMIDITY_THRESHOLD ||
heartRate > HEART_RATE_THRESHOLD || spo2 < SPO2_THRESHOLD) {
tone(BUZZER_PIN, 1000); // Activer le buzzer à 1000 Hz
} else {
noTone(BUZZER_PIN); // Désactiver le buzzer
}
// Envoyer les données à ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(thingspeakUrl) + "?api_key=" + String(thingspeakApiKey) +
"&field1=" + String(temperature) +
"&field2=" + String(humidity) +
"&field3=" + String(heartRate) +
"&field4=" + String(spo2);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("Données envoyées à ThingSpeak, code de réponse: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Erreur lors de l'envoi des données, code de réponse: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Connexion Wi-Fi perdue");
// Tentative de reconnexion Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Tentative de reconnexion au Wi-Fi...");
}
Serial.println("Reconnecté au Wi-Fi");
}
// Attendre 2 secondes avant la prochaine lecture
delay(2000);
}