#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOT_TOKEN "6606731930:AAE1MCVs8nhMaoBiIrsvdLtvkoFtQJkcFw0"
#include "RTClib.h"
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
#define LDR_PIN 4
String t;
String td;
String t2;
String dok;
String dok2;
String t3;
String t4;
String t5;
LiquidCrystal_I2C lcd(0x27, 20, 4);
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 16
#define NUMPIXELS 16
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 200
RTC_DS1307 rtc;
#define DS1307_ADDRESS 0x68
int inputPin = 5;
int pirState = LOW;
int val = 0;
String s = "<Initialization>";
int h;
const unsigned long BOT_MTBS = 1000;
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
if (text == "/doklad")
{
bot.sendMessage(chat_id,dok2, "");
}
if (text == "/sekret")
{
bot.sendMessage(chat_id,"Разработчик Матвей Котенко 28.10.23 20:23", "");
}
if (text == "/chelovek")
{
bot.sendMessage(chat_id,t3, "");
}
if (text == "/start")
{
String welcome = "Добро пожаловать в систему контроля офиса, " + from_name + ".\n";
welcome += "/doklad : Нажми чтобы узнать время начало работы и количество срабатываний датчика движения.\n";
welcome += "/chelovek : Нажми чтобы узнать человек на работе или нет.";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
pinMode(inputPin, INPUT);
pixels.begin();
Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(2, 2);
lcd.print(s);
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
pixels.clear();
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
lcd.setCursor(0, 3);
lcd.print(dok);
Serial.println(dok2);
DateTime now = rtc.now();
int year = now.year();
int mon = now.month();
int day = now.day();
int hour = now.hour();
int minut = now.minute();
int sec = now.second();
td= String(day) + "-" + mon + "-" + year + " "+ hour + ":"+ minut + ":" +sec;
t4= String(hour) + ":"+ minut + ":" +sec;
t = String("Начало работы >") + day + "-" + mon + "-" + year + " "+ hour + ":"+ minut + ":" +sec;
if(h==0){
t2=t4;
dok ="Start Work: " + t2;
}else{
dok = "Start Work: " + t2;
}
if(h==0){
t5=td;
dok2 = "Начало работы: " + t5 + " Срабатывания: " + h ;
}else{
dok2 = "Начало работы: " + t5 + " Срабатывания: " + h ;
}
if(digitalRead(LDR_PIN) == LOW){
t3 = "Человек на работе";
}else{
t3 = "Человека нет на работе";
}
Serial.println (t);
delay(1000);
if(hour==7 && minut==0){
delay(60000);
ESP.restart();
}
val = digitalRead(inputPin);
if (val == HIGH) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 250, 0));
pixels.show();
delay(DELAYVAL);
}
if (pirState == LOW) {
// we have just turned on
Serial.println("Начало задержки!");
++h;
pirState = HIGH;
}
} else {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(250, 0, 0));
pixels.show();
delay(DELAYVAL);
}
if (pirState == HIGH) {
Serial.println("Конец задержки!");
pirState = LOW;
}
lcd.setCursor(0, 0);
if (digitalRead(LDR_PIN) == LOW) {
s= "Man in the office";
lcd.print(s);
} else {
s= " No one ";
lcd.print(s);
}
lcd.setCursor(0, 1);
lcd.print("Triggers:");
lcd.setCursor(10, 1);
lcd.print(h);
lcd.setCursor(0, 2);
lcd.print(td);
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
}
}