#include <Esp32WifiManager.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <Adafruit_NeoPixel.h>
#include <SimpleWiFiClient.h>
#define WIFI_SSID "Keenetic-2.4"
#define WIFI_PASSWORD "password"
#define DHTPIN 14
DHT dht(DHTPIN, DHT11);
WiFiClientSecure secured_client;
//Конфигурация телеграм
#define BOT_TOKEN "1875231296:AAE7Z-XxLwqJ9tOUwt1IueRa8Ilszkcbptg"
#define CHAT_ID 828422873 //https://api.telegram.org/bot1875231296:AAE7Z-XxLwqJ9tOUwt1IueRa8Ilszkcbptg/getUpdates
const unsigned long BOT_MTBS = 3000;
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;
int temp;
int maxTemp = 40;
const unsigned long TEMP_MTBS = 60000;
unsigned long temp_lasttime;
#define PIN 4
#define NUMPIXELS 5
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
dht.begin();
pixels.begin();
configTime(0, 0, "pool.ntp.org");
secured_client.setTrustAnchors(&cert);
Serial.print("Connecting to WiFi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Hi! I'm online", "");
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS)
{
temp = int(dht.readTemperature());
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
criticalTemp();
}
}
void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
if (bot.messages[i].chat_id == CHAT_ID)
{
String text = bot.messages[i].text;
if (text == "/temp")
{
String str = "Temp: " + String(temp);
bot.sendMessage(CHAT_ID, str, "");
}
if (text == "/led")
{
ledPix(5);
}
if (text.length() > 4)
{
if (text[0] == '/' && text[1] == 'l' && text[2] == 'e' && text[3] == 'd')
{
if (text[4] == '0')
ledPix(0);
if (text[4] == '1')
ledPix(1);
if (text[4] == '2')
ledPix(2);
if (text[4] == '3')
ledPix(3);
if (text[4] == '4')
ledPix(4);
if (text[4] == '5')
ledPix(5);
}
}
if (text == "/start" || text == "/help")
{
String welcome = "Hi i'm ESP8266.\n";
welcome += "/temp - get the current temperature value\n";
welcome += "/led - light up all leds\n";
welcome += "/led0 - turn off all leds\n";
welcome += "/led1 - light up one led\n";
welcome += "/led2 - light up two leds\n";
welcome += "/led3 - light up three leds\n";
welcome += "/led4 - light up four leds\n";
welcome += "/led5 - light up five leds\n";
bot.sendMessage(CHAT_ID, welcome, "");
}
}
}
}
void criticalTemp()
{
if (millis() - temp_lasttime > TEMP_MTBS)
{
if (temp >= maxTemp)
{
String str = "Critical temp: " + String(temp);
bot.sendMessage(CHAT_ID, str, "");
}
temp_lasttime = millis();
}
}
void ledPix(int led)
{
for (int i = 0; i < 5; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
pixels.show();
for (int i = 0; i < led; i++)
{
pixels.setPixelColor(i, pixels.Color(200, 0, 0));
}
pixels.show();
}