#include "WiFi.h"
#include "ThingSpeak.h"
#include "DHT.h"
#include <HTTPClient.h>
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long myChannelNumber = 3054427;
const char* myWriteAPIKey = "481INPB7N26ZKY0P";
String phoneNumber = "+967777888258";
String apiKey = "VEVP3EFQCEOHMEG5";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
// دالة ترميز URL
String urlEncode(String str) {
String encodedString = "";
for (unsigned int i = 0; i < str.length(); i++) {
char c = str[i];
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encodedString += c;
} else if (c == ' ') {
encodedString += '+';
} else {
char hex[4];
sprintf(hex, "%%%02X", (unsigned char)c);
encodedString += hex;
}
}
return encodedString;
}
// التحقق من اتصال الإنترنت
bool checkInternetConnection() {
WiFiClient client;
if (client.connect("www.google.com", 80)) {
client.stop();
return true;
}
return false;
}
void setup() {
Serial.begin(115200);
pinMode(DHTPIN, INPUT_PULLUP);
dht.begin();
delay(2000);
connectToWiFi();
ThingSpeak.begin(client);
}
void connectToWiFi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\n✅ WiFi Connected!");
Serial.print("📡 IP Address: ");
Serial.println(WiFi.localIP());
// التحقق من الإنترنت
if (checkInternetConnection()) {
Serial.println("🌐 Internet access: OK");
} else {
Serial.println("❌ No internet access - Check wokwi.toml");
}
}
}
void sendWhatsAppMessage(String message) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("❌ No WiFi connection");
return;
}
// التحقق من الإنترنت أولاً
if (!checkInternetConnection()) {
Serial.println("❌ No internet access - Cannot send message");
return;
}
HTTPClient http;
String encodedMessage = urlEncode(message);
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber +
"&text=" + encodedMessage +
"&apikey=" + apiKey;
Serial.println("📤 Sending WhatsApp message...");
Serial.println("🔗 URL: " + url);
http.begin(url);
http.setTimeout(15000); // 15 ثانية timeout
int httpCode = http.GET();
if (httpCode > 0) {
String response = http.getString();
Serial.print("📨 HTTP Code: ");
Serial.println(httpCode);
Serial.print("📝 Response: ");
Serial.println(response);
if (httpCode == 200) {
Serial.println("✅ WhatsApp message sent successfully!");
}
} else {
Serial.print("❌ Connection failed: ");
Serial.println(http.errorToString(httpCode).c_str());
Serial.println("🔧 Possible solutions:");
Serial.println("1. Check wokwi.toml file");
Serial.println("2. Try HTTPS instead of HTTP");
Serial.println("3. Check internet connection");
}
http.end();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("❌ Sensor error");
delay(5000);
return;
}
// إرسال إلى ThingSpeak
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// إرسال تنبيه WhatsApp
if (t > 30.0) {
String message = "Warning High Temperature: " + String(t, 1) + "C";
sendWhatsAppMessage(message);
}
delay(20000);
}