#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Configuración LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Configuración DHT11
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LEDs WiFi
#define LED_WIFI_OK 12
#define LED_WIFI_FAIL 13
// WiFi (simulado en Wokwi)
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Servidor web simulado (ejemplo: API de ThingSpeak)
const String apiKey = "TU_API_KEY"; // Cambiar si usas un servicio real
const String server = "api.thingspeak.com";
void setup() {
Serial.begin(115200);
pinMode(LED_WIFI_OK, OUTPUT);
pinMode(LED_WIFI_FAIL, OUTPUT);
dht.begin();
lcd.init();
lcd.backlight();
// Mensaje inicial
lcd.setCursor(0, 0);
lcd.print("Conectando...");
WiFi.begin(ssid, password);
int intentos = 0;
while (WiFi.status() != WL_CONNECTED && intentos < 10) {
delay(500);
Serial.print(".");
intentos++;
}
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(LED_WIFI_OK, HIGH); // LED verde ON
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi OK! IP:");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
delay(2000);
} else {
digitalWrite(LED_WIFI_FAIL, HIGH); // LED rojo ON
lcd.clear();
lcd.print("Error WiFi!");
while (1); // Detener programa
}
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Mostrar en LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(hum);
lcd.print("%");
delay(2000);
// Enviar datos a servidor (simulado)
if (WiFi.status() == WL_CONNECTED) {
enviarDatosServidor(temp, hum);
} else {
digitalWrite(LED_WIFI_OK, LOW);
digitalWrite(LED_WIFI_FAIL, HIGH);
}
delay(5000); // Esperar 5 segundos
}
// Función para "enviar" datos (simulación)
void enviarDatosServidor (float temp, float hum) {
Serial.println("=== Enviando datos ===");
Serial.print("Temperatura: ");
Serial.println(temp);
Serial.print("Humedad: "); Serial.println(hum);
Serial.println("¡Datos enviados a la nube!");
Serial.println("----------");
}