#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
const char* serverName = "https://webhook.site/2e960431-b5fe-4c17-ab34-8ed356d2221f";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi...");
}
Serial.println("Conectado a WiFi");
}
void loop() {
float humedad = dht.readHumidity();
float temperatura = dht.readTemperature();
if (isnan(humedad) || isnan(temperatura)) {
Serial.println("Error al leer del sensor DHT!");
} else {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
StaticJsonDocument<200> doc;
doc["humedad"] = humedad;
doc["temperatura"] = temperatura;
String requestBody;
serializeJson(doc, requestBody);
int httpResponseCode = http.POST(requestBody);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Código de respuesta HTTP: " + String(httpResponseCode));
Serial.println(response);
} else {
Serial.print("Error en la petición HTTP: ");
Serial.println(httpResponseCode);
Serial.print("Descripción del error: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
} else {
Serial.println("Error en la conexión WiFi");
}
}
delay(5000);
}