// --- Luis Angel Guanilo Esteves - Internet de las Cosas (IoT)
// --- Semana 10 - Plataforma ThingSpeak
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
// adicionar para l2c
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //
//gddd
// --- Configuración del sensor DHT22 ---
#define DHTPIN 16
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// --- Pin analógico para leer voltaje simulado ---
#define VOLT_PIN 34
// --- Pines de LEDs ---
const int led1 = 2; // LED 1 rojo
const int led2 = 4; // LED 2 azul
// --- Configuración WiFi ---
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// --- Configuración ThingSpeak ---
String writeAPIKey = "Q0BYJP5SMVAKFWZ6"; // API Key de escritura
String readAPIKey = "9ZYXLG0JTKW2QQRK"; // API Key de lectura
String channelID = "3140330"; // Channel ID
const char* server = "http://api.thingspeak.com/update";
void setup() {
//---
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Sistema Listo!");
delay(1000);
lcd.clear();
//---
Serial.begin(115200);
dht.begin();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(VOLT_PIN, INPUT);
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() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
int sensorValue = analogRead(VOLT_PIN);
float voltaje = (sensorValue / 4095.0) * 3.3; // voltaje simulado 0–3.3V
if (isnan(temp) || isnan(hum)) {
Serial.println("Error al leer el sensor DHT22");
return;
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// --- Enviar datos a ThingSpeak ---
String url = String(server) + "?api_key=" + writeAPIKey +
"&field1=" + String(temp) +
"&field2=" + String(hum) +
"&field3=" + String(voltaje);
http.begin(url);
int httpResponseCode = http.GET();
Serial.println("Datos enviados a ThingSpeak: " + url);
http.end();
// --- Leer estados de LEDs desde ThingSpeak ---
// LED 1 -> Field4
String readUrl1 = "http://api.thingspeak.com/channels/" + channelID +
"/fields/4/last.txt?api_key=" + readAPIKey;
http.begin(readUrl1);
int httpCode1 = http.GET();
if (httpCode1 > 0) {
String payload1 = http.getString();
int estadoLED1 = payload1.toInt();
digitalWrite(led1, estadoLED1 == 1 ? HIGH : LOW);
Serial.println("LED 1: " + payload1);
}
http.end();
// LED 2 -> Field5
String readUrl2 = "http://api.thingspeak.com/channels/" + channelID +
"/fields/5/last.txt?api_key=" + readAPIKey;
http.begin(readUrl2);
int httpCode2 = http.GET();
if (httpCode2 > 0) {
String payload2 = http.getString();
int estadoLED2 = payload2.toInt();
digitalWrite(led2, estadoLED2 == 1 ? HIGH : LOW);
Serial.println("LED 2: " + payload2);
}
http.end();
// --- Mostrar datos en LCD ---
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp, 1);
lcd.print("C H:");
lcd.print(hum, 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("V:");
lcd.print(voltaje, 2);
lcd.print(" L1:");
lcd.print(digitalRead(led1)); // muestra estado real del LED1
lcd.print(" L2:");
lcd.print(digitalRead(led2)); // muestra estado real del LED2
}
delay(15000); // Límite de actualización gratuito de ThingSpeak
}