0% found this document useful (0 votes)
23 views

Arduino Esp32 Sensors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Arduino Esp32 Sensors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <WiFi.

h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <ESP32Servo.h>

// Configuration WiFi
const char* ssid = "VOTRE_SSID";
const char* password = "VOTRE_MOT_DE_PASSE";

// Configuration MQTT
const char* mqtt_server = "ADRESSE_BROKER_MQTT";
const int mqtt_port = 1883;
const char* mqtt_user = "UTILISATEUR_MQTT";
const char* mqtt_password = "MOT_DE_PASSE_MQTT";

// Définition des pins


#define SERVO_PIN 13
#define DHT_PIN 14
#define DHT_TYPE DHT22 // DHT22 (AM2302) ou DHT11
#define WATER_LEVEL_1 36
#define WATER_LEVEL_2 39
#define WATER_LEVEL_3 34
#define WATER_LEVEL_4 35

// Création des objets


WiFiClient espClient;
PubSubClient client(espClient);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse I2C: 0x27, 16 colonnes, 2 lignes
DHT dht(DHT_PIN, DHT_TYPE);
Servo myservo;

// Variables globales
unsigned long lastMsg = 0;
const long interval = 5000; // Intervalle de publication MQTT (5 secondes)

void setup_wifi() {
delay(10);
Serial.println("Connexion à " + String(ssid));
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connecté");
}

void reconnect() {
while (!client.connected()) {
Serial.print("Connexion MQTT...");
String clientId = "ESP32Client-" + String(random(0xffff), HEX);

if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {


Serial.println("connecté");
} else {
Serial.print("échec, rc=");
Serial.print(client.state());
Serial.println(" nouvel essai dans 5 secondes");
delay(5000);
}
}
}

int getWaterLevel() {
int level = 0;
if (analogRead(WATER_LEVEL_1) > 1000) level++;
if (analogRead(WATER_LEVEL_2) > 1000) level++;
if (analogRead(WATER_LEVEL_3) > 1000) level++;
if (analogRead(WATER_LEVEL_4) > 1000) level++;
return level;
}

void updateLCD(float temp, float hum, int waterLevel) {


lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp, 1);
lcd.print("C H:");
lcd.print(hum, 0);
lcd.print("%");

lcd.setCursor(0, 1);
lcd.print("Niveau eau: ");
lcd.print(waterLevel);
}

void callback(char* topic, byte* payload, unsigned int length) {


// Traitement des messages MQTT reçus
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}

// Si le message concerne le servo


if (String(topic) == "esp32/servo") {
int angle = message.toInt();
if (angle >= 0 && angle <= 180) {
myservo.write(angle);
}
}
}

void setup() {
Serial.begin(115200);

// Initialisation du LCD
Wire.begin();
lcd.init();
lcd.backlight();

// Initialisation des capteurs


dht.begin();
myservo.attach(SERVO_PIN);

// Configuration des pins pour le niveau d'eau


pinMode(WATER_LEVEL_1, INPUT);
pinMode(WATER_LEVEL_2, INPUT);
pinMode(WATER_LEVEL_3, INPUT);
pinMode(WATER_LEVEL_4, INPUT);

// Configuration WiFi et MQTT


setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);

lcd.print("System Ready!");
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

unsigned long now = millis();


if (now - lastMsg > interval) {
lastMsg = now;

// Lecture des capteurs


float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int waterLevel = getWaterLevel();

// Vérification si les lectures sont valides


if (!isnan(temperature) && !isnan(humidity)) {
// Mise à jour LCD
updateLCD(temperature, humidity, waterLevel);

// Publication MQTT
char tempString[8];
char humString[8];
char levelString[8];

dtostrf(temperature, 1, 2, tempString);
dtostrf(humidity, 1, 2, humString);
itoa(waterLevel, levelString, 10);

client.publish("esp32/temperature", tempString);
client.publish("esp32/humidity", humString);
client.publish("esp32/waterlevel", levelString);
}
}
}

You might also like