#include <FastBot.h>
#define BOT_TOKEN "6557649707:AAGsM-li8sW6YTJxHn58oYgRX2Y1uQ6O5Ms"
#define CHAT_ID "1405302752"
FastBot bot(BOT_TOKEN);
WiFiClient wifiClient;
int door_pin = 14;
char* ssid[] = {"Wokwi-GUEST", "GalaxyA30s", "HS6247", "RT-GPON-D322_EXT" }; //list a necessary wifi networks
char* pass[] = { "", "1234567890", "1234567890", "qYNbRN64" }; //list a passwords
void setup() {
pinMode(door_pin ,OUTPUT);
sendMessage("Hello, ESP32!", CHAT_ID);
}
void loop() {
if (wifiConnected() ) {
} else {
delay(1000);
};
bot.tick();
delay(1000);
}
void newMsg(FB_msg& msg) {
if ((msg.OTA) and (msg.chatID=="1405302752") ) {
bot.update();
} else {
Serial.println(msg.text);
//sendMessage(msg.text);
if ((msg.text == "/opendoor" ) and ( (msg.chatID=="1405302752") or (msg.chatID=="5176067754") )) {
digitalWrite(door_pin,HIGH);
delay(1000);
digitalWrite(door_pin,LOW);
delay(500);
digitalWrite(door_pin,HIGH);
delay(1000);
digitalWrite(door_pin,LOW);
delay(500);
digitalWrite(door_pin,HIGH);
delay(1000);
digitalWrite(door_pin,LOW);
delay(500);
sendMessage("Входите",msg.chatID);
} else {
sendMessage(msg.text,msg.chatID);
}
}
}
bool wifiConnected() {
if (WiFi.status() != WL_CONNECTED) {
int i;
for (i = 0; i < 3; i++) {
WiFi.begin(ssid[i], pass[i]);
int WiFi_timeout_count = 0;
while (WiFi.status() != WL_CONNECTED && WiFi_timeout_count < 50) { //waiting 10 sec
delay(200);
++WiFi_timeout_count;
}
if (WiFi.status() == WL_CONNECTED) {
break;
}
}
delay(500);
bot.attach(newMsg);
sendMessage("Door esp start",CHAT_ID);
sendMessage("WiFi connected, obtained IP address: ",CHAT_ID);
sendMessage(WiFi.localIP().toString(),CHAT_ID);
sendMessage(ssid[i],CHAT_ID);
return true;
}
return true;
}
void sendMessage(const String& msg, const String& chatid) {
bot.sendMessage(msg, chatid);
//delay(1000);
//bot.showMenu("Open \t Open \t Open", CHAT_ID);
//bot.inlineMenu("MyMenu", "Menu1 \t Menu2 \t Menu3 \n Menu4", CHAT_ID);
}