#include <WiFi.h>
#include <WiFiClientSecure.h> // 👈 هاد المكتبة هي اللي كانت ناقصة باش تخدم العميل الآمن
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ⚠️ الرابط ديال localtunnel ديالك
const char* serverUrl = "https://sad-mugs-pay.loca.lt/api/iot/data";
void setup() {
Serial.begin(115200);
Serial.println("\n===== ESP32 START =====");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (isnan(temp)) temp = 25.0;
if (isnan(hum)) hum = 60.0;
float vibration = random(10, 50) / 10.0;
String json = "{\"temperature\":" + String(temp, 2) +
",\"humidity\":" + String(hum, 2) +
",\"vibration\":" + String(vibration, 2) + "}";
Serial.println("\nSending to LocalTunnel:");
Serial.println(json);
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client; // دابا غا يتعرف عليها بدون مشاكل ✅
client.setInsecure(); // تخطي التحقق من الشهادة المعقدة لسهولة المحاكاة
HTTPClient http;
http.begin(client, serverUrl);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json);
Serial.print("HTTP CODE = ");
Serial.println(httpCode);
if (httpCode > 0) {
String response = http.getString();
Serial.print("SERVER RESPONSE = ");
Serial.println(response);
} else {
Serial.print("ERROR = ");
Serial.println(http.errorToString(httpCode));
}
http.end();
}
delay(5000);
}