#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* server = "api.thingspeak.com";
const char* apiKey = "EV3D35I73YJHWZ4X";
WiFiClient wifi;
HTTPClient http;
#define DHTTYPE DHT22
#define DHTPIN 5
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
double temp = dht.readTemperature();
double humid = dht.readHumidity();
HTTPClient http;
String url = "http://" + String(server) + "/update?api_key=" + apiKey + "&field1=" + temp + "&field2=" + humid;
Serial.println("Sending request to: " + url);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println("Data sent to Thingspeak successfully");
} else {
Serial.println("Failed to send data to Thingspeak");
}
} else {
Serial.println("Error sending HTTP request");
}
http.end();
delay(1000);
}