#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHTesp.h>
#include "HX711.h"
// -------------------- Pines --------------------
#define DHTPIN 4
#define OLED_SDA 21
#define OLED_SCL 22
#define LED_OK 32 // verde
#define LED_LOW 19 // baja temperatura
#define LED_HIGH 25 // alta temperatura
#define FAN_PIN 33 // ventilador
#define RELAY_PIN 13 // bombillo por rele
#define HX_DT 26
#define HX_SCK 27
// -------------------- OLED --------------------
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// -------------------- Sensores --------------------
DHTesp dhtSensor;
HX711 scale;
// -------------------- Umbrales --------------------
const float TEMP_LOW = 36.0;
const float TEMP_HIGH = 37.5;
// Control calefactor
const float HEATER_ON_TEMP = 36.0;
const float HEATER_OFF_TEMP = 37.5;
// Control ventilador
const float FAN_ON_TEMP = 37.6;
const float FAN_OFF_TEMP = 37.2;
// Relay module Wokwi: normalmente activa en LOW
#define RELAY_ON_STATE LOW
#define RELAY_OFF_STATE HIGH
// -------------------- HX711 --------------------
float calibration_factor = 420.0;
// -------------------- Variables --------------------
float temperature = NAN;
float humidity = NAN;
float weightKg = 0.0;
bool fanState = false;
bool heaterState = false;
bool hxReady = false;
// -------------------- Funciones auxiliares --------------------
void setLEDs(bool okLed, bool lowLed, bool highLed) {
digitalWrite(LED_OK, okLed ? HIGH : LOW);
digitalWrite(LED_LOW, lowLed ? HIGH : LOW);
digitalWrite(LED_HIGH, highLed ? HIGH : LOW);
}
void heaterOn() {
heaterState = true;
digitalWrite(RELAY_PIN, RELAY_ON_STATE);
}
void heaterOff() {
heaterState = false;
digitalWrite(RELAY_PIN, RELAY_OFF_STATE);
}
const char* estadoTermico(float t) {
if (isnan(t)) return "ERROR";
if (t < TEMP_LOW) return "BAJA";
if (t > TEMP_HIGH) return "ALTA";
return "OK";
}
void actualizarLEDs(float t) {
if (isnan(t)) {
setLEDs(false, false, false);
} else if (t < TEMP_LOW) {
setLEDs(false, true, false);
} else if (t > TEMP_HIGH) {
setLEDs(false, false, true);
} else {
setLEDs(true, false, false);
}
}
void actualizarVentilador(float t) {
if (isnan(t)) {
fanState = false;
} else {
if (t >= FAN_ON_TEMP) {
fanState = true;
} else if (t <= FAN_OFF_TEMP) {
fanState = false;
}
}
digitalWrite(FAN_PIN, fanState ? HIGH : LOW);
}
void actualizarBombillo(float t) {
if (isnan(t)) {
heaterOff();
return;
}
if (t <= HEATER_ON_TEMP) {
heaterOn();
} else if (t >= HEATER_OFF_TEMP) {
heaterOff();
}
// Entre 36.0 y 37.5 conserva el estado
}
void leerDHT() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if (!isnan(data.temperature)) temperature = data.temperature;
if (!isnan(data.humidity)) humidity = data.humidity;
}
void iniciarHX711() {
scale.begin(HX_DT, HX_SCK);
if (scale.is_ready()) {
hxReady = true;
scale.set_scale(calibration_factor);
scale.tare();
} else {
hxReady = false;
}
}
void leerPeso() {
if (hxReady && scale.is_ready()) {
float w = scale.get_units(5);
if (w < 0) w = 0;
weightKg = w;
}
}
void mostrarOLED() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Incubadora neonatal");
display.println("-------------------");
display.print("Temp: ");
if (isnan(temperature)) display.println("ERROR");
else {
display.print(temperature, 1);
display.println(" C");
}
display.print("Hum : ");
if (isnan(humidity)) display.println("ERROR");
else {
display.print(humidity, 1);
display.println(" %");
}
display.print("Peso: ");
if (hxReady) {
display.print(weightKg, 2);
display.println(" kg");
} else {
display.println("HX ERR");
}
display.print("Estado: ");
display.println(estadoTermico(temperature));
display.print("Heat: ");
display.print(heaterState ? "ON " : "OFF");
display.print(" Fan: ");
display.println(fanState ? "ON" : "OFF");
display.display();
}
void mostrarSerial() {
Serial.print("Temp: ");
if (isnan(temperature)) Serial.print("ERROR");
else {
Serial.print(temperature, 2);
Serial.print(" C");
}
Serial.print(" | Hum: ");
if (isnan(humidity)) Serial.print("ERROR");
else {
Serial.print(humidity, 2);
Serial.print(" %");
}
Serial.print(" | Peso: ");
if (hxReady) {
Serial.print(weightKg, 3);
Serial.print(" kg");
} else {
Serial.print("HX ERROR");
}
Serial.print(" | Estado: ");
Serial.print(estadoTermico(temperature));
Serial.print(" | Bombillo: ");
Serial.print(heaterState ? "ON" : "OFF");
Serial.print(" | Ventilador: ");
Serial.println(fanState ? "ON" : "OFF");
}
// -------------------- Setup --------------------
void setup() {
Serial.begin(115200);
delay(500);
pinMode(LED_OK, OUTPUT);
pinMode(LED_LOW, OUTPUT);
pinMode(LED_HIGH, OUTPUT);
setLEDs(false, false, false);
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
pinMode(RELAY_PIN, OUTPUT);
heaterOff();
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
while (true) delay(1000);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("INCUBADORA UMNG");
display.println("Inicializando...");
display.display();
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
iniciarHX711();
delay(1000);
}
// -------------------- Loop --------------------
void loop() {
leerDHT();
leerPeso();
actualizarLEDs(temperature);
actualizarBombillo(temperature);
actualizarVentilador(temperature);
mostrarOLED();
mostrarSerial();
delay(800);
}INCUBADORA NEONATAL - ESP32
Simulacion del sistema presentado: OLED + temperatura + peso + rele + ventilador
VISUALIZACION / SENSORES
CONTROL
ACTUADORES
PANEL DE ESTADO
Loading
ssd1306
ssd1306
ETAPA DE POTENCIA REAL (REPRESENTADA):\nTransformador 120VAC -> 12VDC\nAlimenta rele + ventilador + bombillo\nEn Wokwi se muestra de forma conceptual
PINES USADOS:\nDHT22 -> GPIO4\nOLED -> GPIO21 / GPIO22\nHX711 -> GPIO26 / GPIO27\nRele -> GPIO13\nVentilador -> GPIO33\nLEDs -> GPIO19 / GPIO32 / GPIO25
Bombillo 120V\n(controlado por rele)
Ventilador 12V\n(representacion)
Rojo: baja T\nVerde: rango OK\nAmarillo: alta T