#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 15 // GPIO15 for DHT11 in Wokwi
#define DHTTYPE DHT22 // DHT11 Sensor Type
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Wokwi-GUEST"; // WiFi for Wokwi
const char* password = ""; // No password required
const char* server = "http://api.thingspeak.com/update";
String apiKey = "Y8WXN5J9YM93JRE4T"; // Replace with your API Key
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
dht.begin();
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature in °C
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server + "?api_key=" + apiKey + "&field1=" + String(temperature);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.println("Failed to send data.");
}
http.end();
}
delay(15000); // Send data every 15 seconds
}