#include <Wire.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
// =====================================================
// SOLNDOX 6.0 - STATION D'IRRIGATION INTELLIGENTE
// ESP32 - WOKWI + COMMUNICATION MQTT
// =====================================================
// WIFI WOKWI
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// MQTT
const char* MQTT_SERVER = "broker.hivemq.com";
const int MQTT_PORT = 1883;
const char* MQTT_TOPIC = "solndox/demo/station-niayes-143";
const char* MQTT_CLIENT_ID = "SolNdox-ESP32-143";
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// CAPTEURS
const int PIN_HUMIDITE = 34;
const int PIN_DEBIT = 35;
const int PIN_PRESSION_AMONT = 32;
const int PIN_PRESSION_AVAL = 33;
// HC-SR04
const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
// LED
const int LED_VERTE = 25;
const int LED_ROUGE = 26;
// Bouton pluie
const int BOUTON_PLUIE = 27;
// SEUILS
const int SEUIL_SOL_SEC = 30;
const int SEUIL_SOL_HUMIDE = 80;
const int DEBIT_MIN = 20;
const int DEBIT_FUITE = 80;
const int DEBIT_BOUCHAGE = 35;
const int ECART_PRESSION_MIN = 30;
const float DISTANCE_CRITIQUE = 80.0;
const float DISTANCE_MIN = 2.0;
const float DISTANCE_MAX = 400.0;
// TEMPORISATIONS
const unsigned long DUREE_SCENARIO = 8000;
const unsigned long DUREE_DEMO = 24000;
const unsigned long INTERVALLE_LCD = 500;
const unsigned long INTERVALLE_SERIE = 1000;
const unsigned long INTERVALLE_MQTT = 2000;
const unsigned long INTERVALLE_WIFI = 5000;
const unsigned long ANTI_REBOND = 50;
// HORLOGES
unsigned long debutDemo = 0;
unsigned long dernierLCD = 0;
unsigned long dernierRapport = 0;
unsigned long dernierChangementBouton = 0;
unsigned long dernierePublicationMQTT = 0;
unsigned long derniereTentativeMQTT = 0;
// ETATS
bool modeDemo = true;
bool pausePluie = false;
bool arrosageActif = false;
bool alerte = false;
bool niveauDisponible = false;
bool niveauCritique = true;
bool boutonStable = HIGH;
bool derniereLectureBouton = HIGH;
// MESURES
int humidite = 0;
int debit = 0;
int pressionAmont = 0;
int pressionAval = 0;
int ecartPression = 0;
float distanceEau = -1;
String diagnostic = "INITIALISATION";
String scenario = "DEMARRAGE";
// =====================================================
// LECTURE ANALOGIQUE
// =====================================================
int lirePourcentage(int broche) {
int valeur = analogRead(broche);
valeur = constrain(valeur, 0, 4095);
return map(valeur, 0, 4095, 0, 100);
}
// =====================================================
// AFFICHAGE LCD
// =====================================================
void afficherLigneLCD(int ligne, String texte) {
if (texte.length() > 16) {
texte = texte.substring(0, 16);
}
lcd.setCursor(0, ligne);
lcd.print(texte);
for (int i = texte.length(); i < 16; i++) {
lcd.print(" ");
}
}
// =====================================================
// MESURE ULTRASON
// =====================================================
float mesurerDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duree = pulseIn(ECHO_PIN, HIGH, 30000UL);
if (duree == 0) {
niveauDisponible = false;
return -1;
}
float distance = duree * 0.0343 / 2.0;
if (distance < DISTANCE_MIN || distance > DISTANCE_MAX) {
niveauDisponible = false;
return -1;
}
niveauDisponible = true;
return distance;
}
// =====================================================
// SCENARIOS AUTOMATIQUES
// =====================================================
void chargerScenarioAutomatique() {
unsigned long temps = millis() - debutDemo;
int numero = temps / DUREE_SCENARIO;
if (numero == 0) {
scenario = "RESEAU NORMAL";
humidite = 20;
debit = 50;
pressionAmont = 60;
pressionAval = 55;
}
else if (numero == 1) {
scenario = "BOUCHAGE";
humidite = 20;
debit = 10;
pressionAmont = 80;
pressionAval = 20;
}
else {
scenario = "FUITE";
humidite = 20;
debit = 90;
pressionAmont = 80;
pressionAval = 20;
}
}
// =====================================================
// MODE MANUEL
// =====================================================
void lireCapteursManuels() {
scenario = "MODE MANUEL";
humidite = lirePourcentage(PIN_HUMIDITE);
debit = lirePourcentage(PIN_DEBIT);
pressionAmont = lirePourcentage(PIN_PRESSION_AMONT);
pressionAval = lirePourcentage(PIN_PRESSION_AVAL);
}
// =====================================================
// BOUTON PLUIE
// =====================================================
void lireBoutonPluie() {
bool lecture = digitalRead(BOUTON_PLUIE);
if (lecture != derniereLectureBouton) {
dernierChangementBouton = millis();
derniereLectureBouton = lecture;
}
if (millis() - dernierChangementBouton >= ANTI_REBOND) {
if (lecture != boutonStable) {
boutonStable = lecture;
if (boutonStable == LOW) {
pausePluie = !pausePluie;
Serial.print("PAUSE PLUIE : ");
Serial.println(pausePluie ? "ACTIVE" : "DESACTIVEE");
}
}
}
}
// =====================================================
// DIAGNOSTIC INTELLIGENT
// =====================================================
void analyserSysteme() {
ecartPression = abs(pressionAmont - pressionAval);
arrosageActif = false;
alerte = false;
niveauCritique = false;
if (!niveauDisponible || distanceEau < 0) {
diagnostic = "CAPTEUR NIVEAU HS";
alerte = true;
}
else if (distanceEau >= DISTANCE_CRITIQUE) {
niveauCritique = true;
diagnostic = "PUITS CRITIQUE";
alerte = true;
}
else if (humidite >= SEUIL_SOL_HUMIDE) {
diagnostic = "SOL TROP HUMIDE";
}
else if (pausePluie) {
if (debit > 10) {
diagnostic = "FUITE A VERIFIER";
alerte = true;
}
else {
diagnostic = "PAUSE PLUIE";
}
}
else if (humidite >= SEUIL_SOL_SEC) {
diagnostic = "SOL ASSEZ HUMIDE";
}
else if (
ecartPression >= ECART_PRESSION_MIN &&
debit <= DEBIT_BOUCHAGE
) {
diagnostic = "SUSPICION BOUCHAGE";
alerte = true;
}
else if (
ecartPression >= ECART_PRESSION_MIN &&
debit >= DEBIT_FUITE
) {
diagnostic = "SUSPICION FUITE";
alerte = true;
}
else if (debit < DEBIT_MIN) {
diagnostic = "DEBIT TROP FAIBLE";
alerte = true;
}
else if (ecartPression >= ECART_PRESSION_MIN) {
diagnostic = "PRESSION ANORMALE";
alerte = true;
}
else {
diagnostic = "RESEAU NORMAL";
arrosageActif = true;
}
if (alerte || !niveauDisponible || niveauCritique) {
arrosageActif = false;
}
digitalWrite(LED_VERTE, arrosageActif ? HIGH : LOW);
digitalWrite(LED_ROUGE, alerte ? HIGH : LOW);
}
// =====================================================
// AFFICHAGE LCD
// =====================================================
void afficherLCD() {
afficherLigneLCD(
0,
modeDemo ? "SOLNDOX 6.0 DEMO" : "SOLNDOX MANUEL"
);
if (diagnostic == "RESEAU NORMAL") {
afficherLigneLCD(1, "ARROSAGE ACTIF");
}
else if (diagnostic == "SUSPICION BOUCHAGE") {
afficherLigneLCD(1, "ALERTE BOUCHAGE");
}
else if (diagnostic == "SUSPICION FUITE") {
afficherLigneLCD(1, "ALERTE FUITE");
}
else if (diagnostic == "FUITE A VERIFIER") {
afficherLigneLCD(1, "FUITE A VERIFIER");
}
else if (diagnostic == "PUITS CRITIQUE") {
afficherLigneLCD(1, "PUITS CRITIQUE");
}
else if (diagnostic == "CAPTEUR NIVEAU HS") {
afficherLigneLCD(1, "CAPTEUR NIVEAU HS");
}
else if (diagnostic == "DEBIT TROP FAIBLE") {
afficherLigneLCD(1, "DEBIT TROP FAIBLE");
}
else if (diagnostic == "PRESSION ANORMALE") {
afficherLigneLCD(1, "PRESSION ANORMALE");
}
else if (diagnostic == "PAUSE PLUIE") {
afficherLigneLCD(1, "PAUSE PLUIE");
}
else if (diagnostic == "SOL TROP HUMIDE") {
afficherLigneLCD(1, "SOL TROP HUMIDE");
}
else if (diagnostic == "SOL ASSEZ HUMIDE") {
afficherLigneLCD(1, "SOL ASSEZ HUMIDE");
}
else {
afficherLigneLCD(1, "ARROSAGE ARRETE");
}
}
// =====================================================
// CONNEXION WIFI
// =====================================================
void connecterWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, 6);
Serial.println();
Serial.println("CONNEXION AU WIFI WOKWI...");
}
// =====================================================
// CONNEXION MQTT
// =====================================================
void connecterMQTT() {
if (WiFi.status() != WL_CONNECTED) {
return;
}
if (mqttClient.connected()) {
return;
}
if (millis() - derniereTentativeMQTT < 5000) {
return;
}
derniereTentativeMQTT = millis();
Serial.println("CONNEXION AU BROKER MQTT...");
if (mqttClient.connect(MQTT_CLIENT_ID)) {
Serial.println("MQTT CONNECTE !");
Serial.print("Topic : ");
Serial.println(MQTT_TOPIC);
}
else {
Serial.print("ECHEC MQTT. Code : ");
Serial.println(mqttClient.state());
}
}
// =====================================================
// CREATION DU JSON
// =====================================================
String creerJSON() {
String json;
json.reserve(450);
json = "{";
json += "\"app\":\"SolNdox\",";
json += "\"version\":\"6.0\",";
json += "\"mode\":\"";
json += modeDemo ? "demo" : "manuel";
json += "\",";
json += "\"scenario\":\"";
json += scenario;
json += "\",";
json += "\"humidite\":";
json += String(humidite);
json += ",\"debit\":";
json += String(debit);
json += ",\"pression_amont\":";
json += String(pressionAmont);
json += ",\"pression_aval\":";
json += String(pressionAval);
json += ",\"ecart_pression\":";
json += String(ecartPression);
json += ",\"distance_eau_cm\":";
if (niveauDisponible) {
json += String(distanceEau, 1);
}
else {
json += "null";
}
json += ",\"niveau_eau_disponible\":";
json += niveauDisponible ? "true" : "false";
json += ",\"pause_pluie\":";
json += pausePluie ? "true" : "false";
json += ",\"arrosage_actif\":";
json += arrosageActif ? "true" : "false";
json += ",\"alerte\":";
json += alerte ? "true" : "false";
json += ",\"diagnostic\":\"";
json += diagnostic;
json += "\"}";
return json;
}
// =====================================================
// PUBLICATION MQTT
// =====================================================
void publierMQTT() {
if (!mqttClient.connected()) {
return;
}
if (millis() - dernierePublicationMQTT < INTERVALLE_MQTT) {
return;
}
dernierePublicationMQTT = millis();
String message = creerJSON();
bool resultat = mqttClient.publish(
MQTT_TOPIC,
message.c_str(),
true
);
if (resultat) {
Serial.println();
Serial.println("DONNEES ENVOYEES AU DASHBOARD :");
Serial.println(message);
}
else {
Serial.println("ERREUR : PUBLICATION MQTT IMPOSSIBLE");
}
}
// =====================================================
// MONITEUR SERIE
// =====================================================
void afficherRapportSerie() {
Serial.println();
Serial.println("========== SOLNDOX 6.0 ==========");
Serial.print("Mode : ");
Serial.println(modeDemo ? "DEMO AUTOMATIQUE" : "MANUEL");
Serial.print("Scenario : ");
Serial.println(scenario);
Serial.print("Humidite : ");
Serial.print(humidite);
Serial.println(" %");
Serial.print("Debit : ");
Serial.print(debit);
Serial.println(" %");
Serial.print("Pression amont : ");
Serial.println(pressionAmont);
Serial.print("Pression aval : ");
Serial.println(pressionAval);
Serial.print("Ecart pression : ");
Serial.println(ecartPression);
Serial.print("Distance eau : ");
if (niveauDisponible) {
Serial.print(distanceEau, 1);
Serial.println(" cm");
}
else {
Serial.println("INDISPONIBLE");
}
Serial.print("WiFi : ");
Serial.println(
WiFi.status() == WL_CONNECTED ? "CONNECTE" : "NON CONNECTE"
);
Serial.print("MQTT : ");
Serial.println(
mqttClient.connected() ? "CONNECTE" : "NON CONNECTE"
);
Serial.print("Pause pluie : ");
Serial.println(pausePluie ? "OUI" : "NON");
Serial.print("Diagnostic : ");
Serial.println(diagnostic);
Serial.print("Irrigation simulee : ");
Serial.println(arrosageActif ? "ACTIVE" : "ARRETEE");
Serial.println("=================================");
}
// =====================================================
// INITIALISATION
// =====================================================
void setup() {
Serial.begin(115200);
analogReadResolution(12);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_VERTE, OUTPUT);
pinMode(LED_ROUGE, OUTPUT);
pinMode(BOUTON_PLUIE, INPUT_PULLUP);
digitalWrite(TRIG_PIN, LOW);
digitalWrite(LED_VERTE, LOW);
digitalWrite(LED_ROUGE, LOW);
Wire.begin(21, 22);
lcd.init();
lcd.backlight();
afficherLigneLCD(0, "SOLNDOX 6.0");
afficherLigneLCD(1, "INITIALISATION");
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setBufferSize(512);
Serial.println();
Serial.println("SOLNDOX 6.0 INITIALISE");
Serial.println("MONITEUR SERIE : 115200");
Serial.println("DEBUT DES 3 TESTS");
connecterWiFi();
debutDemo = millis();
dernierLCD = millis();
dernierRapport = millis();
}
// =====================================================
// BOUCLE PRINCIPALE
// =====================================================
void loop() {
// WIFI
static bool messageWiFiAffiche = false;
if (WiFi.status() != WL_CONNECTED) {
if (!messageWiFiAffiche) {
Serial.println("ATTENTE DU WIFI...");
messageWiFiAffiche = true;
}
}
else {
connecterMQTT();
messageWiFiAffiche = false;
}
// MQTT
if (mqttClient.connected()) {
mqttClient.loop();
}
// FIN DES TROIS TESTS AUTOMATIQUES
if (
modeDemo &&
millis() - debutDemo >= DUREE_DEMO
) {
modeDemo = false;
pausePluie = false;
Serial.println();
Serial.println("=================================");
Serial.println("LES 3 TESTS SONT TERMINES");
Serial.println("PASSAGE EN MODE MANUEL");
Serial.println("LES 4 POTENTIOMETRES SONT ACTIFS");
Serial.println("=================================");
}
// LECTURE DES DONNEES
if (modeDemo) {
chargerScenarioAutomatique();
}
else {
lireCapteursManuels();
lireBoutonPluie();
}
// MESURE DU NIVEAU
distanceEau = mesurerDistance();
// ANALYSE
analyserSysteme();
// LCD
if (millis() - dernierLCD >= INTERVALLE_LCD) {
afficherLCD();
dernierLCD = millis();
}
// RAPPORT SERIE
if (millis() - dernierRapport >= INTERVALLE_SERIE) {
afficherRapportSerie();
dernierRapport = millis();
}
// PUBLICATION MQTT
publierMQTT();
delay(40);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4