#include <FastBot.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <DallasTemperature.h>
#include <OneWire.h>
const int oneWireBus = 19;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
const int servoPin = 4;
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define BOT_TOKEN "7347018904:AAFbu5xuTzh0JrG5lKLj3dN4hBU3qw0WSuo"
FastBot bot(BOT_TOKEN);
const int rele1=12;
const int rele2=13;
#define LDR_PIN 34
int lightLevel = 0;
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const float GAMMA = 0.7;
const float RL10 = 50;
float h, t, lux, temperatureC;
void setup() {
lcd.init();
lcd.backlight();
pinMode(rele1, OUTPUT);
pinMode(rele2, OUTPUT);
myservo.attach(servoPin);
connectWiFi();
bot.attach(newMsg);
dht.begin();
sensors.begin();
}
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 );
}
else if ( msg. text == "/temperature2" ){
String mess="Температура повітря на вулиці: "+ String(temperatureC)+"°C";
bot.sendMessage (mess , msg. chatID );
}
else if ( msg. text == "/lux" ){
String mess="Освітленість в кімнаті: "+ String(lux)+" lux";
bot.sendMessage (mess , msg. chatID );
}
}
void loop() {
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
h = dht.readHumidity();
t = dht.readTemperature();
int analogValue = analogRead(LDR_PIN);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(0,0);
lcd.print("Temperature: ");
lcd.print(t);
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.setCursor(0,2);
lcd.print("Light Level: ");
lcd.print(lux);
lcd.setCursor(0,3);
lcd.print("temperatureC: ");
lcd.print(temperatureC);
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");
}