#include <DHT.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <RTClib.h>
// ==================== CONFIGURATION ====================
#define DHTPIN 4
#define DHTTYPE DHT22
#define POT_ACOU 35
#define POT_ELEC 32
#define MQ2_PIN 33
struct Thresholds {
float temp_warning = 40.0, temp_critical = 60.0;
float hum_warning = 70.0, hum_critical = 85.0;
float vib_warning = 12.0, vib_critical = 20.0;
float acou_warning = 50.0, acou_critical = 70.0;
float elec_warning = 60.0, elec_critical = 80.0;
float gaz_warning = 40.0, gaz_critical = 60.0;
};
struct KalmanFilter {
float estimate = 0, error = 1, process_noise = 0.01, measurement_noise = 1.0;
};
struct SensorData {
float temperature, humidity, vibration, acoustic, electric, gaz;
DateTime timestamp;
};
// ==================== GLOBAL ====================
DHT dht(DHTPIN, DHTTYPE);
Adafruit_MPU6050 mpu;
RTC_DS1307 rtc;
TwoWire i2c_mpu = TwoWire(0);
TwoWire i2c_rtc = TwoWire(1);
Thresholds thresholds;
KalmanFilter kf_temp, kf_hum, kf_vib, kf_acou, kf_elec, kf_gaz;
// ==================== KALMAN ====================
float kalmanFilter(float m, KalmanFilter &kf) {
kf.error += kf.process_noise;
float k = kf.error / (kf.error + kf.measurement_noise);
kf.estimate += k * (m - kf.estimate);
kf.error = (1 - k) * kf.error;
return kf.estimate;
}
// ==================== INIT ====================
bool initMPU6050() {
if (!mpu.begin(0x68, &i2c_mpu)) { Serial.println("[ERREUR] MPU6050"); return false; }
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("[OK] MPU6050");
return true;
}
bool initRTC() {
if (!rtc.begin(&i2c_rtc)) { Serial.println("[ERREUR] RTC"); return false; }
if (!rtc.isrunning()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.println("[OK] RTC");
return true;
}
bool initDHT() {
dht.begin(); delay(2000);
if (isnan(dht.readTemperature())) { Serial.println("[ERREUR] DHT22"); return false; }
Serial.println("[OK] DHT22");
return true;
}
SensorData readAllSensors() {
SensorData d;
d.timestamp = rtc.now();
d.temperature = dht.readTemperature(); if (isnan(d.temperature)) d.temperature = 0;
d.humidity = dht.readHumidity(); if (isnan(d.humidity)) d.humidity = 0;
sensors_event_t a, g, t;
mpu.getEvent(&a, &g, &t);
d.vibration = sqrt(a.acceleration.x*a.acceleration.x + a.acceleration.y*a.acceleration.y + a.acceleration.z*a.acceleration.z);
d.acoustic = analogRead(POT_ACOU) / 40.95;
d.electric = analogRead(POT_ELEC) / 40.95;
d.gaz = analogRead(MQ2_PIN) / 40.95;
d.acoustic = constrain(d.acoustic, 0, 100);
d.electric = constrain(d.electric, 0, 100);
d.gaz = constrain(d.gaz, 0, 100);
d.temperature = kalmanFilter(d.temperature, kf_temp);
d.humidity = kalmanFilter(d.humidity, kf_hum);
d.vibration = kalmanFilter(d.vibration, kf_vib);
d.acoustic = kalmanFilter(d.acoustic, kf_acou);
d.electric = kalmanFilter(d.electric, kf_elec);
d.gaz = kalmanFilter(d.gaz, kf_gaz);
return d;
}
int calculateScore(const SensorData &d) {
int s = 0;
if (d.temperature >= thresholds.temp_critical) s+=2; else if (d.temperature >= thresholds.temp_warning) s+=1;
if (d.humidity >= thresholds.hum_critical) s+=2; else if (d.humidity >= thresholds.hum_warning) s+=1;
if (d.vibration >= thresholds.vib_critical) s+=2; else if (d.vibration >= thresholds.vib_warning) s+=1;
if (d.acoustic >= thresholds.acou_critical) s+=2; else if (d.acoustic >= thresholds.acou_warning) s+=1;
if (d.electric >= thresholds.elec_critical) s+=2; else if (d.electric >= thresholds.elec_warning) s+=1;
if (d.gaz >= thresholds.gaz_critical) s+=2; else if (d.gaz >= thresholds.gaz_warning) s+=1;
int final = s * 10; if (final > 100) final = 100; return final;
}
String getState(int s) {
if (s < 20) return "NORMAL";
if (s < 40) return "SURVEILLANCE";
if (s < 70) return "MAINTENANCE";
return "ARRET URGENT";
}
// ==================== AFFICHAGE ====================
void printDateTime(const DateTime &dt) {
Serial.print("π
");
Serial.print(dt.day());
Serial.print("/");
Serial.print(dt.month());
Serial.print("/");
Serial.print(dt.year());
Serial.print(" | β° ");
Serial.print(dt.hour());
Serial.print("h ");
Serial.print(dt.minute());
Serial.print("min ");
Serial.print(dt.second());
Serial.println("s");
}
void printSensorValue(const String &name, float value, float warning, float critical, const String &unit) {
Serial.print(name);
Serial.print(": ");
Serial.print(value, 1);
Serial.print(unit);
Serial.print(" ");
if (critical > 0 && value >= critical) {
Serial.println("[CRITIQUE]");
} else if (warning > 0 && value >= warning) {
Serial.println("[ATTENTION]");
} else {
Serial.println("[OK]");
}
}
void printScoreAndState(int score, String etat) {
// Barre de progression
Serial.print(" ");
int barLength = map(constrain(score, 0, 100), 0, 100, 0, 30);
for (int i = 0; i < 30; i++) {
if (i < barLength) {
if (score >= 70) Serial.print("β");
else if (score >= 40) Serial.print("β");
else if (score >= 20) Serial.print("β");
else Serial.print("β");
} else {
Serial.print("β");
}
}
Serial.println();
// Score sur une ligne
Serial.print(" π SCORE: ");
Serial.print(score);
Serial.print("/100 (");
Serial.print(score);
Serial.println("%)");
// Γtat sur une autre ligne
Serial.print(" βοΈ ETAT: ");
if (etat == "NORMAL") {
Serial.println("π’ NORMAL");
} else if (etat == "SURVEILLANCE") {
Serial.println("π‘ SURVEILLANCE");
} else if (etat == "MAINTENANCE") {
Serial.println("π MAINTENANCE");
} else {
Serial.println("π΄ ARRET URGENT");
}
}
void printDiagnostic(const SensorData &data, String state) {
Serial.println("\nπ DIAGNOSTIC:");
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ");
if (state == "SURVEILLANCE") {
Serial.println(" β οΈ Voir de temps en temps");
}
else if (state == "MAINTENANCE") {
Serial.println(" π§ Anomalies significatives !");
Serial.println(" Actions recommandees :");
Serial.println(" - Planifier une intervention dans les 24h");
Serial.println(" - Contacter le service maintenance");
if (data.gaz >= thresholds.gaz_critical)
Serial.println(" - FUITE GAZ DANGEREUSE β EVACUER !");
if (data.temperature >= thresholds.temp_critical)
Serial.println(" - SURCHAUFFE CRITIQUE β arreter la machine");
if (data.vibration >= thresholds.vib_critical)
Serial.println(" - VIBRATIONS DANGEREUSES β risque de casse");
}
else if (state == "ARRET URGENT") {
Serial.println(" π¨ DANGER IMMINENT - ARRET TOTAL REQUIS !");
Serial.println(" Actions urgentes :");
Serial.println(" - COUPER L'ALIMENTATION GENERALE");
Serial.println(" - EVACUER LA ZONE");
Serial.println(" - ALERTER LES SECOURS");
if (data.gaz >= thresholds.gaz_critical)
Serial.println(" - FUITE GAZ MASSIVE β danger d'explosion");
if (data.temperature >= thresholds.temp_critical)
Serial.println(" - SURCHAUFFE EXTREME β risque d'incendie");
if (data.vibration >= thresholds.vib_critical)
Serial.println(" - CASSE MECANIQUE IMMINENTE");
}
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ");
}
// ==================== SETUP ====================
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββββ");
Serial.println("β SYSTEME DE MONITORING INDUSTRIEL v2.0 β");
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββββ");
Serial.println();
i2c_mpu.begin(21, 22);
i2c_rtc.begin(25, 26);
initDHT();
initMPU6050();
initRTC();
Serial.println("\nβ
SYSTEME PRET");
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ");
}
// ==================== LOOP ====================
void loop() {
SensorData data = readAllSensors();
int score = calculateScore(data);
String etat = getState(score);
printDateTime(data.timestamp);
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ");
printSensorValue("π‘οΈ Temperature", data.temperature,
thresholds.temp_warning, thresholds.temp_critical, "Β°C");
printSensorValue("π§ Humidite", data.humidity,
thresholds.hum_warning, thresholds.hum_critical, "%");
printSensorValue("π³ Vibration", data.vibration,
thresholds.vib_warning, thresholds.vib_critical, "m/sΒ²");
printSensorValue("π€ Acoustique", data.acoustic,
thresholds.acou_warning, thresholds.acou_critical, "%");
printSensorValue("β‘ Electrique", data.electric,
thresholds.elec_warning, thresholds.elec_critical, "%");
printSensorValue("π¨ Gaz", data.gaz,
thresholds.gaz_warning, thresholds.gaz_critical, "%");
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ");
printScoreAndState(score, etat);
if (etat != "NORMAL") {
printDiagnostic(data, etat);
}
Serial.println("\nβ±οΈ Prochaine mesure dans 3 secondes...");
Serial.println("ββββββββββββββββββββββββββββββββββββββββββββββββ\n");
delay(3000);
}