#include "DHT.h"
#include <FastLED.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOT_TOKEN "7102346027:AAHBB08UBmTFrEPL-WXHagXWj6qEtECK0ic"
const unsigned long BOT_MTBS = 1000;
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;
String nn = "\n";
#define DHTPIN 26
#define DHTTYPE DHT22
int t, h;
DHT dht(DHTPIN, DHTTYPE);
uint32_t Fonttimer;
#define NUM_LEDS 10
CRGB leds[NUM_LEDS];
#define LED_PIN 18
#define COLOR_ORDER GRB
#define CHIPSET WS2812 // тип светодиодов
#define PH 27
int ph;
bool light;
bool but;
Servo servo;
Servo Servo;
uint32_t timer1,timer2, timer3;
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 == "/svet")
{
but = !but;
if(light == true && but == false){
bot.sendMessage(chat_id,"Свет был включен.", "");
}
if(light == false && but == true){
bot.sendMessage(chat_id,"Свет был включен.", "");
}
if(light == false && but == false){
bot.sendMessage(chat_id,"Свет был выключен.", "");
}
if(light == true && but == true){
bot.sendMessage(chat_id,"Свет был выключен.", "");
}
}
if (text == "/start")
{
String welcome = "Вас приветствует сити-ферма телеграмм бот. Список команд:" + nn;
welcome += "/svet: Нажми чтобы включить или выключить свет." + nn;
welcome += "/status_svet: Нажми чтобы узнать включен или выключен свет." + nn;
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
servo.attach(14);
Servo.attach(12);
pinMode(PH, INPUT);
Serial.begin(115200);
dht.begin();
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
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"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
h = dht.readHumidity();
t = dht.readTemperature();
ph = analogRead(PH);
if(t > 25){
if (millis() - timer1 >= 1000) {
timer1 = millis();
for( int i=0; i <= 180; i++){
servo.write(i);
}
}
if (millis() - timer2 >= 1000) {
timer2 = millis();
for( int i=180; i >= 0; i--){
servo.write(i);
}
}
}
if(t < 18){
Servo.write(90);
}
else{
Servo.write(180);
}
if (millis() - Fonttimer >= 3000) {
Fonttimer = millis();
if(h < 60){
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
Serial.println("💧Водичка буль, буль, хлюп, хлюп.💧");
}
}
int svet2 = map(ph, 32, 4507, 1000, 0);
if(svet2 < 500){
light = true;
}else{
light = false;
}
if(light == true && but == false){
fill_solid(leds, NUM_LEDS,CRGB(243, 168, 243));
FastLED.show();
}
if(light == false && but == true){
fill_solid(leds, NUM_LEDS,CRGB(243, 168, 243));
FastLED.show();
}
if(light == false && but == false){
fill_solid(leds, NUM_LEDS,CRGB(0, 0, 0));
FastLED.show();
}
if(light == true && but == true){
fill_solid(leds, NUM_LEDS,CRGB(0, 0, 0));
FastLED.show();
}
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();
}
}