#include <FastBot.h>
#include <DHT.h>>
#include <ESP32Servo.h>
Servo myservo;
const int servoPin = 4;
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define BOT_TOKEN "7169690062:AAE-s3OxabZ0PdiXpYFmPA1EKeKUsG2qSak"
FastBot bot(BOT_TOKEN);
const int rele1=12;
const int rele2=13;
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h;
float t;
void setup() {
pinMode(rele1, OUTPUT);
pinMode(rele2, OUTPUT);
myservo.attach(servoPin);
connectWiFi();
bot.attach(newMsg);
dht.begin();
Serial.begin(9600);
}
void newMsg ( FB_msg& msg ) {
if ( msg. text == "/start" ){
bot.sendMessage ( "Привіт! Я твій бот для керування кімнатою." , msg. chatID ) ;
}
else if ( msg. text == "/led1_on" ){
digitalWrite ( rele1, HIGH ) ;
bot.sendMessage ( "Світло в кімнаті1 ввімкнено! " , msg. chatID );
}
else if ( msg. text == "/led1_off" ){
digitalWrite ( rele1, LOW) ;
bot.sendMessage ( "Світло в кімнаті1 вимкнено! " , msg. chatID );
}
else if ( msg. text == "/led2_on" ){
bot.sendMessage ( "Світло в кімнаті2 ввімкнено! " , msg. chatID );
digitalWrite ( rele2, HIGH ) ;
}
else if ( msg. text == "/led2_off" ){
digitalWrite ( rele2, LOW) ;
bot.sendMessage ( "Світло в кімнаті2 вимкнено! " , msg. chatID );
}
else if ( msg. text == "/servo_on" ){
bot.sendMessage ( "Штори відкрито. " , msg. chatID );
myservo.write(180);
}
else if ( msg. text == "/servo_off" ){
bot.sendMessage ( "Штори закрито. " , msg. chatID );
myservo.write(0);
}
else if ( msg. text == "/temperature" ){
String mess="Температура повітря кімнати: "+ String(t)+"°C";
bot.sendMessage (mess , msg. chatID );
}
else if ( msg. text == "/humidity" ){
String mess="Вологість повітря кімнати: "+ String(h)+"%";
bot.sendMessage (mess , msg. chatID );
}
}
void loop() {
h = dht.readHumidity();
t = dht.readTemperature();
bot.tick();
}
void connectWiFi() {
delay(2000);
Serial.begin(115200);
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (millis() > 15000) ESP.restart();
}
Serial.println("Connected");
}