#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHT_PIN 14 // Pin del ESP32 al que está conectado el pin DATA del DHT22
#define DHT_TYPE DHT22 // Definir el tipo de sensor DHT
const char* ssid = "Wokwi-GUEST"; // Reemplaza con tu SSID de WiFi
const char* password = ""; // Reemplaza con tu contraseña de WiFi
const char* server = "http://api.thingspeak.com/update";
String apiKey = "HE0S1Y6J3GGOOJBI"; // Reemplaza con tu API Key de ThingSpeak
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Conectando a red WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Conectado");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Error de lectura del sensor DHT!");
return;
}
HTTPClient http;
String url = server + "?api_key=" + apiKey + "&field1=" + String(temperature) + "&field2=" + String(humidity);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error de envio de datos: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Desconectado");
}
delay(20000); // Enviar datos cada 20 segundos
}