#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int capteur1 = 32;
const int capteur2 = 33;
const int capteur3 = 34;
// --- CONFIGURATION CLOUD (MÉMOIRE EXCEL) ---
// ⚠️ Mets ton lien Google Apps Script ici (en http:// pour Wokwi)
String urlScriptGoogle = "https://script.google.com/macros/s/AKfycbx05a-5MxB3dnxtuUZnW-ag7XnPyWRZnLvsWy-hBynwdEeMHOIYaoBvv2VeeoRANBO8dw/exec";
// --- CONFIGURATION ALERTES (TELEGRAM) ---
String tokenTelegram = "8996079973:AAExarp9KcoqkS3BnyTe_5in1Q6yqEOL_Ic";
String chatIDMien = "5366655937"; // Ton ID personnel
String chatIDProf = ""; // ⚠️ ID du prof (laisser vide "" si tu testes seul ce soir)
bool anomalieT1 = false;
bool anomalieT2 = false;
bool anomalieT3 = false;
void setup() {
Serial.begin(115200);
Serial.println("Initialisation du système de séchage cacao...");
WiFi.begin(ssid, password);
Serial.print("Connexion au WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connecté avec succès !");
}
void loop() {
int valeur1 = analogRead(capteur1);
int valeur2 = analogRead(capteur2);
int valeur3 = analogRead(capteur3);
float temp1 = (valeur1 / 4095.0) * 100.0;
float temp2 = (valeur2 / 4095.0) * 100.0;
float temp3 = (valeur3 / 4095.0) * 100.0;
Serial.printf("T1: %.1f°C | T2: %.1f°C | T3: %.1f°C\n", temp1, temp2, temp3);
// 1. SURVEILLANCE ET ALERTES TELEGRAM
if (temp1 < 10.0 || temp1 > 90.0) {
if (!anomalieT1) {
envoyerAlerteMultiDestinataires("🚨 [ALERTE SÉCHOIR] Anomalie critique Capteur 1 ! Température : " + String(temp1, 1) + "°C");
anomalieT1 = true;
}
} else { anomalieT1 = false; }
if (temp2 < 10.0 || temp2 > 90.0) {
if (!anomalieT2) {
envoyerAlerteMultiDestinataires("🚨 [ALERTE SÉCHOIR] Anomalie critique Capteur 2 ! Température : " + String(temp2, 1) + "°C");
anomalieT2 = true;
}
} else { anomalieT2 = false; }
if (temp3 < 10.0 || temp3 > 90.0) {
if (!anomalieT3) {
envoyerAlerteMultiDestinataires("🚨 [ALERTE SÉCHOIR] Anomalie critique Capteur 3 ! Température : " + String(temp3, 1) + "°C");
anomalieT3 = true;
}
} else { anomalieT3 = false; }
// 2. STOCKAGE DES DONNÉES (EXCEL / CLOUD)
envoyerVersGoogleSheet(temp1, temp2, temp3);
Serial.println("--------------------------------------------------");
delay(10000); // Boucle toutes les 10 secondes
}
// ====================================================================
// FONCTIONS DE COMMUNICATION
// ====================================================================
// Fonction d'enregistrement dans Google Sheets (La Mémoire / Le Lien)
void envoyerVersGoogleSheet(float t1, float t2, float t3) {
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
if(urlScriptGoogle.indexOf("https://") == 0) {
urlScriptGoogle.replace("https://", "http://");
}
String requete = urlScriptGoogle + "?temp1=" + String(t1, 1) + "&temp2=" + String(t2, 1) + "&temp3=" + String(t3, 1);
Serial.println(">>> [MÉMOIRE CLOUD] Sauvegarde des données dans Excel...");
http.begin(client, requete);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("✅ Données stockées avec succès !");
} else {
Serial.println("❌ Échec de la sauvegarde.");
}
http.end();
}
}
// Fonction de distribution des alertes Telegram
void envoyerAlerteMultiDestinataires(String msg) {
executerEnvoiTelegram(msg, chatIDMien, "Ton Téléphone");
if (chatIDProf != "") {
executerEnvoiTelegram(msg, chatIDProf, "Téléphone du Professeur");
}
}
// Fonction technique d'envoi Telegram
void executerEnvoiTelegram(String message, String chatID, String destinataire) {
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
String messageEncode = message;
messageEncode.replace(" ", "%20");
String url = "https://api.telegram.org/bot" + tokenTelegram + "/sendMessage?chat_id=" + chatID + "&text=" + messageEncode;
Serial.print(">>> [TELEGRAM] Envoi d'alerte vers " + destinataire + "... ");
http.begin(client, url);
int httpCode = http.GET();
if (httpCode == 200) {
Serial.println("✅ Reçu !");
} else {
Serial.printf("❌ Échec (Code : %d)\n", httpCode);
}
http.end();
}
}