#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST"; /// wifi ssid
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";// mosquitto server url
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define DHTPIN 12 // Utiliser la broche 12 pour le capteur DHT11
#define DHTTYPE DHT22
#define Bouton_1 14
#define Bouton_2 27
#define Bouton_3 26
#define Relais_1 16
#define Relais_2 17
#define Relais_3 5
#define Alarme 2
#define Ventillo 35
#define Led_Orange 25
#define Led_Bleu 33
const int servoPin = 4; // Utiliser la broche 4 pour le servomoteur
const int voltagePin = 32; // Broche analogique pour la mesure de la tension
const int currentPin = 34; // Broche analogique pour la mesure du courant
int i = 0;
int j = 0;
int k = 0;
int Sortie_1 = 0;
int Sortie_2 = 0;
int Sortie_3 = 0;
int Danger = 0;
int Test = 0;
int Lecture_TEST = 0;
Servo servoMotor; // Créer une instance de la classe Servo
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Remplacez 0x27 par l'adresse I2C de votre écran LCD
/*********************************************************************************************/
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
String string;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
string+=((char)payload[i]);
}
Serial.println(string);
if (topic ="/Thinkitive/Consigne/Sortie_1")//if (topic ="/ThinkIOT/Servo-nodered") /// esp32 subscribe topic
{
//Serial.print(" ");
Lecture_TEST = string.toInt();
= string.toInt();
delay(15);
}
if (topic ="/Thinkitive/Consigne/Sortie_2")//if (topic ="/ThinkIOT/Servo-nodered") /// esp32 subscribe topic
{
//Serial.print(" ");
Sortie_2 = string.toInt();
delay(15);
}
}
void reconnect()
{
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESPClient")) {
Serial.println("connected");
client.subscribe("/Thinkitive/Consigne/Sortie_1");
client.subscribe("/Thinkitive/Consigne/Sortie_2");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}}
}
/******************************** Void setup ****************************************/
void setup() {
Serial.begin(115200);
Wire.begin(22, 18);
pinMode(Alarme, OUTPUT);
pinMode(Led_Bleu, OUTPUT);
pinMode(Led_Orange, OUTPUT);
pinMode(Ventillo, OUTPUT);
pinMode(Relais_1, OUTPUT);
pinMode(Relais_2, OUTPUT);
pinMode(Relais_3, OUTPUT);
pinMode(Bouton_1, INPUT);
pinMode(Bouton_2, INPUT);
pinMode(Bouton_3, INPUT);
dht.begin();
servoMotor.attach(servoPin);
lcd.begin(20, 4); // Écran LCD 20x4
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
/******************************** Void loop ****************************************/
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
//delay(100);//
unsigned long now = millis();
if (now - lastMsg > 2000)
{ //perintah publish data
lastMsg = now;
delay(10); // Attendre 10ms entre les mesures
//Appel fonctions dans le sous programme !
Lecture_Boutons();
Affichage();
//
}
}
/************************************************* SOUS PROGRAMME *************************************************/
void Lecture_Boutons()
{
/************ Lecture Boutons ***********/
int Lecture_Bouton_1 = digitalRead(Bouton_1);
int Lecture_Bouton_2 = digitalRead(Bouton_2);
int Lecture_Bouton_3 = digitalRead(Bouton_3);
//Lecture Bouton 1
if((Lecture_Bouton_1 == 1)||(Sortie_1 == 1))
{
i+=1;
}
if(Danger == 0)
{
if(i%2)
{
digitalWrite(Relais_1, HIGH);
Sortie_1 = 1;
//SW_SORTIE_1 = 1;
}
else
{
digitalWrite(Relais_1, LOW);
Sortie_1 = 0;
//SW_SORTIE_1 = 0;
}
if(i>=1000)// Par précaution pour ne pas atteindre la valeur max de 'int'
{
i = 0;
}
}
//Lecture Bouton 2
if(Lecture_Bouton_2 == 1)
{
j+=1;
}
if(Danger == 0)
{
if(j%2)
{
digitalWrite(Relais_2, HIGH);
Sortie_2 = 1;
//SW_SORTIE_2 = 1;
}
else
{
digitalWrite(Relais_2, LOW);
Sortie_2 = 0;
//SW_SORTIE_2 = 0;
}
if(j>=1000)// Par précaution pour ne pas atteindre la valeur max de 'int'
{
j = 0;
}
}
//Lecture Bouton 3
if(Lecture_Bouton_3 == 1)
{
k+=1;
}
if(Danger == 0)
{
if(k%2)
{
digitalWrite(Relais_3, HIGH);
Sortie_3 = 1;
//SW_SORTIE_3 = 1;
}
else
{
digitalWrite(Relais_3, LOW);
Sortie_3 = 0;
//SW_SORTIE_3 = 0;
}
if(k>=1000)// Par précaution pour ne pas atteindre la valeur max de 'int'
{
k = 0;
}
}
}
void Affichage()
{
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
/*** Mesure de la température et de l'humidité ***/
// Mesure de la tension
int voltageValue = analogRead(voltagePin);
float voltage = 75*voltageValue * (3.3 / 4095.0); // Convertir la valeur analogique en tension
// Mesure du courant
int currentValue = analogRead(currentPin);
float current = 7*currentValue * (3.3 / 4095.0); // Convertir la valeur analogique en courant
if((voltage>=230)||(current>=20))
{
Danger = 1;//Activer le mode Danger pour déactiver toutes les sorties 230V
// Activer l'alarme
digitalWrite(Alarme, HIGH);
delay(300);
digitalWrite(Alarme, LOW);
delay(300);
// Déactiver toutes les sorties 230V
digitalWrite(Relais_1, LOW);
digitalWrite(Relais_2, LOW);
digitalWrite(Relais_3, LOW);
}
else
{
Danger = 0;//Déactiver le mode Danger
digitalWrite(Alarme, LOW);
}
Serial.print("Lecture_TEST : ");
Serial.print(Lecture_TEST);
Serial.print(" Temp : ");
Serial.print(temperature);
Serial.print("C Humid : ");
Serial.print(humidity);
Serial.print("% Tension : ");
Serial.print(voltage);
Serial.print("V Courant : ");
Serial.print(current);
Serial.println("A");
// Affichage sur l'écran LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("Tension: ");
lcd.print(voltage);
lcd.print("V");
lcd.setCursor(0, 3);
lcd.print("Courant: ");
lcd.print(current);
lcd.print("A");
if (temperature > 40.0)
{
//Faire clignoter la led orange
digitalWrite(Led_Orange, HIGH);
delay(300);
digitalWrite(Led_Orange, LOW);
delay(300);
// Faire tourner le servomoteur continuellement remplacement ventillo
for (int angle = 0; angle <= 180; angle++)
{
servoMotor.write(angle);
delay(15);
}
}
else
{
digitalWrite(Led_Orange, LOW); // Éteindre la LED ORANGE
servoMotor.write(0); // Ramener le servomoteur à la position initiale
}
if (humidity > 60.0)
{
//Faire clignoter la led Bleu
digitalWrite(Led_Bleu, HIGH);
delay(300);
digitalWrite(Led_Bleu, LOW);
delay(300);
}
else
{
digitalWrite(Led_Bleu, LOW); // Éteindre la LED BLEU
}
// Les fonctions pour l'interface homme machine avec NodeRed en utilisant le protocole mqtt
client.publish("/Thinkitive/temp", String(temperature).c_str()); //
client.publish("/Thinkitive/hum", String(humidity).c_str()); //
//client.publish("/Thinkitive/TEST_RELAIS2", String(test_relais2).c_str()); //
client.publish("/Thinkitive/voltage", String(voltage).c_str());
client.publish("/Thinkitive/current", String(current).c_str());
client.publish("/Thinkitive/Sortie_1", String(Sortie_1).c_str());
client.publish("/Thinkitive/Sortie_2", String(Sortie_2).c_str());
client.publish("/Thinkitive/Sortie_3", String(Sortie_3).c_str());
}