#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <ESP32Servo.h>
// Librerías de Adafruit para manejo de MQTT
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
// Credenciales para la conexión al servidor de Adafruit IO
#define AIO_SERVIDOR "io.adafruit.com" // Dirección del servidor MQTT de Adafruit IO
#define AIO_PUERTOSERVIDOR 1883 // Puerto estándar para conexión MQTT
#define AIO_USUARIO "Adelin" // Usuario de Adafruit IO
#define AIO_KEY "aio_jkNe15bDsXGQ9z83S6GJaHMDMW9E" // Llave API para autenticación
#define DHTPIN1 15
#define DHTPIN2 4
#define PIRPIN1 14
#define PIRPIN2 5
#define LED1 12
#define LED2 13
#define LED3 25
#define LED4 26
#define LED5 32
#define LED6 33
#define BUZZER 27
#define DOOR_SERVO_PIN1 18
#define WINDOW_SERVO_PIN1 19
#define FAN_SERVO_PIN1 21
#define DOOR_SERVO_PIN2 22
#define WINDOW_SERVO_PIN2 23
#define FAN_SERVO_PIN2 17
#define TRIG_PIN1 2
#define ECHO_PIN1 3
#define TRIG_PIN2 16
#define ECHO_PIN2 27
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
Servo doorServo1, windowServo1, fanServo1;
Servo doorServo2, windowServo2, fanServo2;
const int DHTPIN = 15;
float temperatura, humedad;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverUrl = "http://tu-api-rest.com/api";
WiFiClient client;
// Cliente MQTT de Adafruit, configurado con los datos de conexión
Adafruit_MQTT_Client mqtt(&client, AIO_SERVIDOR, AIO_PUERTOSERVIDOR, AIO_USUARIO, AIO_KEY);
// Suscripciones a los feeds de Adafruit IO
Adafruit_MQTT_Subscribe susc_amarillo = Adafruit_MQTT_Subscribe(&mqtt, AIO_USUARIO "/feeds/led_amarillo");
Adafruit_MQTT_Subscribe susc_azul = Adafruit_MQTT_Subscribe(&mqtt, AIO_USUARIO "/feeds/led_azul");
// Publicadores para los feeds
Adafruit_MQTT_Publish pub_temperatura = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO "/feeds/temperatura");
Adafruit_MQTT_Publish pub_humedad = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO "/feeds/humedad");
const int Led_amarillo=12;
const int Led_azul=13;
bool motionDetected1 = false;
bool motionDetected2 = false;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi...");
}
Serial.println("Conectado a WiFi");
dht1.begin();
dht2.begin();
pinMode(PIRPIN1, INPUT);
pinMode(PIRPIN2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
doorServo1.attach(DOOR_SERVO_PIN1);
windowServo1.attach(WINDOW_SERVO_PIN1);
fanServo1.attach(FAN_SERVO_PIN1);
doorServo2.attach(DOOR_SERVO_PIN2);
windowServo2.attach(WINDOW_SERVO_PIN2);
fanServo2.attach(FAN_SERVO_PIN2);
doorServo1.write(0);
windowServo1.write(0);
fanServo1.write(0);
doorServo2.write(0);
windowServo2.write(0);
fanServo2.write(0);
}
void loop() {
checkMotion();
controlDevices();
checkUltrasonicSensors();
sendDataToApi();
delay(2000);
}
void checkMotion() {
motionDetected1 = digitalRead(PIRPIN1);
motionDetected2 = digitalRead(PIRPIN2);
if (motionDetected1 || motionDetected2) {
digitalWrite(BUZZER, HIGH);
Serial.println("Movimiento detectado");
} else {
digitalWrite(BUZZER, LOW);
}
}
void controlDevices() {
float temp1 = dht1.readTemperature();
float hum1 = dht1.readHumidity();
float temp2 = dht2.readTemperature();
float hum2 = dht2.readHumidity();
Serial.print("Cuarto 1 - Temp: ");
Serial.print(temp1);
Serial.print("C, Hum: ");
Serial.print(hum1);
Serial.println("%");
Serial.print("Cuarto 2 - Temp: ");
Serial.print(temp2);
Serial.print("C, Hum: ");
Serial.print(hum2);
Serial.println("%");
if (motionDetected1) {
digitalWrite(LED1, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
doorServo1.write(90);
windowServo1.write(90);
fanServo1.write(90);
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
doorServo1.write(0);
windowServo1.write(0);
fanServo1.write(0);
}
if (motionDetected2) {
digitalWrite(LED2, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
doorServo2.write(90);
windowServo2.write(90);
fanServo2.write(90);
} else {
digitalWrite(LED2, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
doorServo2.write(0);
windowServo2.write(0);
fanServo2.write(0);
}
}
void checkUltrasonicSensors() {
long duration1, distance1;
long duration2, distance2;
// Sensor ultrasónico del cuarto 1
digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
duration1 = pulseIn(ECHO_PIN1, HIGH);
distance1 = duration1 * 0.034 / 2;
// Sensor ultrasónico del cuarto 2
digitalWrite(TRIG_PIN2, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN2, LOW);
duration2 = pulseIn(ECHO_PIN2, HIGH);
distance2 = duration2 * 0.034 / 2;
if (distance1 < 50) {
Serial.println("Alarma activada en el cuarto 1");
digitalWrite(BUZZER, HIGH);
}
if (distance2 < 50) {
Serial.println("Alarma activada en el cuarto 2");
digitalWrite(BUZZER, HIGH);
}
}
void sendDataToApi() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String apiUrl = String(serverUrl) + "/sensor-data";
String payload = "{\"room1\":{\"temp\":" + String(dht1.readTemperature()) +
",\"hum\":" + String(dht1.readHumidity()) +
",\"motion\":" + String(motionDetected1) +
"},\"room2\":{\"temp\":" + String(dht2.readTemperature()) +
",\"hum\":" + String(dht2.readHumidity()) +
",\"motion\":" + String(motionDetected2) + "}}";
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Datos enviados: " + response);
} else {
Serial.println("Error al enviar datos: " + String(httpResponseCode));
}
http.end();
}
}