#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/adc.h"
// ==========================
// 📌 Pins
// ==========================
#define VIBRATION_PIN 4 // GPIO4
#define PRESSURE_PIN ADC1_CHANNEL_6 // GPIO34
#define CURRENT_PIN ADC1_CHANNEL_7 // GPIO35
// ==========================
// Variables température
// ==========================
float temp_history[5] = {0};
int index_temp = 0;
// ==========================
// Init capteurs
// ==========================
void init_sensors() {
// Vibration sensor
gpio_set_direction(VIBRATION_PIN, GPIO_MODE_INPUT);
gpio_pullup_en(VIBRATION_PIN); // important
// ADC
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(PRESSURE_PIN, ADC_ATTEN_DB_11);
adc1_config_channel_atten(CURRENT_PIN, ADC_ATTEN_DB_11);
}
// ==========================
// Température simulée
// ==========================
float read_temperature() {
static float temp = 25.0;
temp += 0.8;
if (temp > 75)
temp = 25.0;
return temp;
}
// ==========================
// Vibration
// ==========================
int read_vibration() {
return gpio_get_level(VIBRATION_PIN);
}
// ==========================
// Pression
// ==========================
float read_pressure() {
int raw = adc1_get_raw(PRESSURE_PIN);
return (raw / 4095.0) * 10.0; // 0 → 10 bar
}
// ==========================
// Courant
// ==========================
float read_current() {
int raw = adc1_get_raw(CURRENT_PIN);
return (raw / 4095.0) * 20.0; // 0 → 20 A
}
// ==========================
// Moyenne température
// ==========================
float calculate_average() {
float sum = 0;
for (int i = 0; i < 5; i++) {
sum += temp_history[i];
}
return sum / 5.0;
}
// ==========================
// Main
// ==========================
void app_main(void) {
init_sensors();
while (1) {
float temp = read_temperature();
int vibration = read_vibration();
float pressure = read_pressure();
float current = read_current();
// historique température
temp_history[index_temp] = temp;
index_temp = (index_temp + 1) % 5;
float avg_temp = calculate_average();
// ==========================
// Affichage
// ==========================
printf("Temp: %.2f C | Avg: %.2f C | Vib: %d | Pressure: %.2f Bar | Current: %.2f A\n",
temp, avg_temp, vibration, pressure, current);
// ==========================
// Diagnostic
// ==========================
if (temp > 65) {
printf("🔥 ALERTE : Température critique !\n");
}
else if (avg_temp > 55) {
printf("⚠️ Echauffement progressif détecté\n");
}
else if (vibration == 0) {
printf("⚠️ Vibration détectée !\n");
}
else if (pressure < 2.0) {
printf("⚠️ Pression faible détectée\n");
}
else if (current > 15.0) {
printf("⚠️ Surconsommation électrique détectée\n");
}
else {
printf("✅ Pompe en bon état\n");
}
printf("--------------------------------------------------\n");
vTaskDelay(pdMS_TO_TICKS(2000));
}
}