#include <WiFi.h>
#include "DHT.h"
#include <HTTPClient.h>
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// بيانات ThingSpeak
const char* server = "http://api.thingspeak.com/update";
String apiKey = "EKVRKDHCSFTCC0HJ"; // مفتاح الكتابة من القناة
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
Serial.printf("Temp: %.2f °C Humidity: %.2f %%\n", t, h);
sendToThingSpeak(t, h);
} else {
Serial.println("فشل قراءة الحساس!");
}
delay(20000); // ThingSpeak يقبل تحديث كل 15 ثانية على الأقل
}
void sendToThingSpeak(float t, float h) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server;
url += "?api_key=" + apiKey;
url += "&field1=" + String(t);
url += "&field2=" + String(h);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("ThingSpeak response: %d\n", httpCode);
} else {
Serial.println("Error sending to ThingSpeak!");
}
http.end();
}
}