#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <Servo.h>
// — بيانات الشبكة وتلجرام —
const char* ssid = “Redmi A1”;
const char* pass = “19041904”;
const char* BOT_TOKEN = “8767103482:AAH6Zn4FRJ-NQ9RwCrnt3OTDLqPZK-L7Fbg”;
const char* CHAT_ID = “-1003617183721”; // معرف المجموعة الخاص بك
// — تعريف الدبابيس —
const int BUZZER = D2;
const int SERVO_PIN = D1;
Servo myServo;
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
// دالة تنفيذ عملية الإطعام
void executeFeeding(String chat_id) {
for(int i=0; i<3; i++) {
digitalWrite(BUZZER, HIGH);
delay(150);
digitalWrite(BUZZER, LOW);
delay(100);
}
myServo.write(90); // وضعية الفتح
delay(3000);
myServo.write(0); // وضعية الغلق
bot.sendMessage(chat_id, “🐕 تم تقديم الوجبة لكلب الحراسة بنجاح!”, “”);
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text == “/start”) {
String welcome = “🛡️ نظام إطعام كلب الحراسة الذكي\n”;
welcome += “/feed : لإطعام كلب الحراسة الآن\n”;
bot.sendMessage(chat_id, welcome, “”);
}
if (text == “/feed”) {
bot.sendMessage(chat_id, “⌛ جاري تقديم الطعام لكلب الحراسة…”, “”);
executeFeeding(chat_id);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
myServo.attach(SERVO_PIN);
myServo.write(0);
WiFi.begin(ssid, pass);
client.setInsecure(); // لتجاوز التحقق من الشهادات وتبسيط الاتصال
Serial.print(“Connecting to WiFi…”);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“\n✅ WiFi Connected!”);
// — إرسال الرسالة الترحيبية تلقائياً عند التشغيل —
String welcomeMsg = “🛡️ مرحباً بك في نظام إطعام كلب الحراسة الذكي\n”;
welcomeMsg += “✅ النظام متصل الآن وجاهز لاستقبال الأوامر.”;
if(bot.sendMessage(CHAT_ID, welcomeMsg, “”)) {
Serial.println(“✅ Welcome message sent to Telegram Group.”);
} else {
Serial.println(“❌ Failed to send welcome message.”);
}
}
void loop() {
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}