#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 15 // Pin donde está conectado el sensor DHT
#define DHTTYPE DHT22
#define PIR_PIN 13 // Pin para el sensor de movimiento
#define LIGHT_PIN 14 // Pin para controlar una luz
#define DOOR_PIN 12 // Pin para simular la puerta
#define WINDOW_PIN 27 // Pin para simular la ventana
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "TU_SSID"; // Cambia esto por el SSID de tu WiFi
const char* password = "TU_PASSWORD"; // Cambia esto por la contraseña de tu WiFi
const char* apiUrl = "http://api.example.com/control"; // URL de tu API
void setup() {
Serial.begin(115200);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(DOOR_PIN, OUTPUT);
pinMode(WINDOW_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Conectando a WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConectado a WiFi!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
enviarDatos();
recibirComandos();
} else {
Serial.println("Reconectando a WiFi...");
WiFi.begin(ssid, password);
}
delay(5000); // Esperar 5 segundos entre cada ciclo
}
void enviarDatos() {
float temperatura = dht.readTemperature();
float humedad = dht.readHumidity();
bool movimiento = digitalRead(PIR_PIN);
if (isnan(temperatura) || isnan(humedad)) {
Serial.println("Error leyendo del sensor DHT!");
return;
}
Serial.print("Temperatura: ");
Serial.print(temperatura);
Serial.print(" °C - Humedad: ");
Serial.print(humedad);
Serial.print(" % - Movimiento: ");
Serial.println(movimiento);
// Enviar datos a la API
HTTPClient http;
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
String payload = "{ \"temperatura\": " + String(temperatura) +
", \"humedad\": " + String(humedad) +
", \"movimiento\": " + String(movimiento) + " }";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Datos enviados correctamente!");
} else {
Serial.print("Error enviando datos: ");
Serial.println(httpResponseCode);
}
http.end();
}
void recibirComandos() {
HTTPClient http;
http.begin(apiUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Comando recibido: " + response);
ejecutarComando(response);
} else {
Serial.print("Error recibiendo comando: ");
Serial.println(httpResponseCode);
}
http.end();
}
void ejecutarComando(String comando) {
if (comando == "ENCENDER_LUZ") {
digitalWrite(LIGHT_PIN, HIGH);
Serial.println("Luz encendida.");
} else if (comando == "APAGAR_LUZ") {
digitalWrite(LIGHT_PIN, LOW);
Serial.println("Luz apagada.");
} else if (comando == "ABRIR_PUERTA") {
digitalWrite(DOOR_PIN, HIGH);
Serial.println("Puerta abierta.");
} else if (comando == "CERRAR_PUERTA") {
digitalWrite(DOOR_PIN, LOW);
Serial.println("Puerta cerrada.");
} else if (comando == "ABRIR_VENTANA") {
digitalWrite(WINDOW_PIN, HIGH);
Serial.println("Ventana abierta.");
} else if (comando == "CERRAR_VENTANA") {
digitalWrite(WINDOW_PIN, LOW);
Serial.println("Ventana cerrada.");
} else {
Serial.println("Comando no reconocido.");
}
}