#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String apiKey = "A5V8FKSFOVJGRAYU";
const char* server = "http://api.thingspeak.com/update";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Conectando ao Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Conectado!");
}
void loop() {
float temperatura = dht.readTemperature();
float umidade = dht.readHumidity();
if (isnan(temperatura) || isnan(umidade)) {
Serial.println("Erro ao ler o DHT!");
return;
}
Serial.print("Temp: ");
Serial.print(temperatura);
Serial.print(" °C, Umidade: ");
Serial.print(umidade);
Serial.println(" %");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server;
url += "?api_key=" + apiKey;
url += "&field1=" + String(temperatura);
url += "&field2=" + String(umidade);
http.begin(url);
int httpResponseCode = http.GET();
http.end();
Serial.print("Código de resposta: ");
Serial.println(httpResponseCode);
}
delay(15000); // Espera 15 segundos por limitação do ThingSpeak
}