#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// ===================================================
// CONFIGURACIÓN DE RED Y MQTT
// ===================================================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
// ===================================================
// CONFIGURACIÓN DE PINES DE SENSORES
// ===================================================
#define PIN_PRESION 34
#define PIN_CAUDAL 35
#define PIN_CONDUCT 32
#define PIN_TEMP 22
// ===================================================
// OBJETO DEL SENSOR DE TEMPERATURA
// ===================================================
OneWire oneWire(PIN_TEMP);
DallasTemperature sensors(&oneWire);
// ===================================================
// VARIABLES GLOBALES DE FILTRADO SUAVE
// ===================================================
float caudalFiltrado = 0;
float presionFiltrada = 0;
float tempFiltrada = 0;
float conductFiltrada = 0;
// ===================================================
// FUNCIONES DE LECTURA DE SENSORES
// ===================================================
float leerCaudal() {
int lectura = analogRead(PIN_CAUDAL);
// Escala de 0 a 10 L/min
return (lectura / 4095.0) * 10.0;
}
float leerPresion() {
int lectura = analogRead(PIN_PRESION);
// Escala de 0 a 120 psi
return (lectura / 4095.0) * 120.0;
}
float leerTemperatura() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
if (tempC == DEVICE_DISCONNECTED_C) tempC = 25.0; // valor por defecto
return constrain(tempC, 0.0, 50.0);
}
float leerConductividad() {
int lectura = analogRead(PIN_CONDUCT);
// Escala de 0 a 35 µS/cm
return (lectura / 4095.0) * 35.0;
}
float compensarConductividad(float conductividad, float tempC) {
// Compensación por temperatura (factor 2%/°C)
return conductividad * (1 + 0.02 * (tempC - 25.0));
}
// ===================================================
// CONEXIÓN WiFi
// ===================================================
void setup_wifi() {
Serial.println("Conectando a WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi conectado");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
// ===================================================
// RECONEXIÓN AL BROKER MQTT
// ===================================================
void reconnect() {
while (!client.connected()) {
Serial.print("Conectando a MQTT...");
if (client.connect("ESP32UnidadRenal")) {
Serial.println("conectado ✅");
} else {
Serial.print("❌ fallo, rc=");
Serial.print(client.state());
Serial.println(" -> reintentando en 5 s");
delay(5000);
}
}
}
// ===================================================
// SETUP
// ===================================================
void setup() {
Serial.begin(115200);
sensors.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
}
// ===================================================
// LOOP PRINCIPAL
// ===================================================
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// === Lecturas actuales ===
float caudal = leerCaudal();
float presion = leerPresion();
float temperatura = leerTemperatura();
float conductividad = leerConductividad();
float conductividadComp = compensarConductividad(conductividad, temperatura);
// === Filtrado progresivo (promedio exponencial) ===
const float alpha = 0.1; // cuanto más pequeño, más suave el cambio
caudalFiltrado = (1 - alpha) * caudalFiltrado + alpha * caudal;
presionFiltrada = (1 - alpha) * presionFiltrada + alpha * presion;
tempFiltrada = (1 - alpha) * tempFiltrada + alpha * temperatura;
conductFiltrada = (1 - alpha) * conductFiltrada + alpha * conductividadComp;
// === Construir mensaje CSV ===
// Formato: caudal;presion;temperatura;conductividad
char msg[80];
snprintf(msg, sizeof(msg), "%.2f;%.2f;%.2f;%.2f",
caudalFiltrado, presionFiltrada, tempFiltrada, conductFiltrada);
// === Publicar en topic MQTT ===
client.publish("unidadrenal/datos", msg);
// === Mostrar en monitor serie ===
Serial.print("MQTT -> ");
Serial.println(msg);
delay(1000); // cada 1 segundo
}