Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "DHT.h"
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- int setTemprature = 23;
- #define BOT_TOKEN "6140528970:AAHV78G7miJ0Auw-uh7HYy8cVyvzYhh46jQ"G5OpIUQw0pl275pPGFeDfx2frQ6Ca4FH8"
- String chat_id = "895680550";
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- const unsigned long BOT_MTBS = 500; // mean time between scan messages
- unsigned long bot_lasttime; // last time messages' scan has been done
- bool Start = false;
- #define DHTPIN 18
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- int FlamePin = 35;
- int SoilDigitalPin = 34;
- const int relyFan = 4;
- const int relyLight = 32;
- const int relyPump = 21;
- int flag = 0;
- int sensorDigitalValue = 0;
- float t = 0;
- float h = 0;
- int sensorReading = 0;
- void handleNewMessages(int numNewMessages)
- {
- Serial.println("handleNewMessages");
- Serial.println(String(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 == "/send_test_action")
- {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, "Did you see the action message?");
- // You can't use own message, just choose from one of bellow
- //typing for text messages
- //upload_photo for photos
- //record_video or upload_video for videos
- //record_audio or upload_audio for audio files
- //upload_document for general files
- //find_location for location data
- //more info here - https://core.telegram.org/bots/api#sendchataction
- }
- ///////////////////////////////////////////////////
- if (text == "/fan_on")
- {
- bot.sendMessage(chat_id, "fan is on");
- //turn fan on
- digitalWrite( relyFan, HIGH);
- delay(500);
- }
- if (text == "/fan_off")
- {
- bot.sendMessage(chat_id, "fan is off");
- //turn fan off
- digitalWrite( relyFan, LOW);
- }
- //////////////////////////////////////////////////////////
- if (text == "/light_on")
- {
- bot.sendMessage(chat_id, "light is on");
- //turn light on
- digitalWrite( relyLight, HIGH);
- }
- if (text == "/light_off")
- {
- bot.sendMessage(chat_id, "light is off");
- //turn light off
- digitalWrite( relyLight, LOW);
- }
- //////////////////////////////////////////////////////////
- if (text == "/pump_on")
- {
- bot.sendMessage(chat_id, "pump is on");
- //turn pump on
- if (sensorDigitalValue < 50) {
- flag = 1;
- digitalWrite( relyPump, HIGH);
- }
- else {
- digitalWrite( relyPump, LOW);
- bot.sendMessage(chat_id, "High Humidity pump is off ");
- }
- }
- if (text == "/pump_off")
- {
- bot.sendMessage(chat_id, "pump is off");
- digitalWrite( relyPump, LOW);
- }
- //////////////////////////////////////////////////////////
- if (text == "/data")
- {
- String temp_str = "Temp: " + String(t) + "°C \n";
- String hum_str = "Hum: " + String(h) + "% \n";
- String Soil_str = "Soil Hum:" + String(sensorDigitalValue) + "% \n";
- bot.sendMessage(chat_id, temp_str );
- bot.sendMessage(chat_id, hum_str);
- bot.sendMessage(chat_id, Soil_str);
- }
- //////////////////////////////////////////////////////////
- if (text == "/options")
- {
- String keyboardJson = "[[\"/light_on\", \"/light_off\"],[\"/pump_on\", \"/pump_off\"],[\"/fan_on\", \"/fan_off\"],[\"/data\"]]";
- bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
- }
- if (text == "/start")
- {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- welcome += "/send_test_action : to send test chat action message\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Setup Start]
- void setup() {
- Serial.begin(9600);
- initSensorsAndOutputs();
- connectToWiFi();
- bot.sendMessage(chat_id, "Project Online");
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Setup end]
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Loop Start]
- void loop() {
- readSensorsAndSerialDisplay();
- receiveOrdersFromTelegramUsers();
- //****************************************************************
- //Flame Sensor Conditions
- if (sensorReading < 500 ) {
- bot.sendMessage(chat_id, "Fire detected");
- digitalWrite( relyPump, HIGH);
- delay(500);
- }
- else if (sensorReading > 500 && flag == 0) {
- digitalWrite( relyPump, LOW);
- }
- //****************************************************************
- //****************************************************************
- //Soil Moisture Sensor Conditions
- if (sensorDigitalValue < 50) {
- flag = 1;
- digitalWrite( relyPump, HIGH);
- }
- else if (sensorDigitalValue > 50 && flag == 1) {
- flag = 0;
- digitalWrite( relyPump, LOW);
- bot.sendMessage(chat_id, "High Humidity pump is off ");
- }
- //****************************************************************
- //****************************************************************
- //DHT11 Temprature Sensor Conditions
- if (t > setTemprature) {
- digitalWrite(relyFan, HIGH);
- delay(500);
- }
- else if (t <= setTemprature) {
- digitalWrite(relyFan, LOW);
- }
- //****************************************************************
- }//end of loop
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Loop end]
- ////////////////////////////////////////////////
- void connectToWiFi() {
- 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("\n");
- Serial.print("WiFi 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 readSensorsAndSerialDisplay() {
- sensorDigitalValue = analogRead(SoilDigitalPin);
- h = dht.readHumidity();
- t = dht.readTemperature();
- sensorReading = analogRead(FlamePin);
- sensorDigitalValue = map(sensorDigitalValue, 0, 4095, 100, 0);
- Serial.print("Soil sensor digital value: ");
- Serial.println(sensorDigitalValue);
- Serial.print(F("% Temperature: "));
- Serial.print(t);
- Serial.println(F("°C "));
- Serial.print(F("% Humidity: "));
- Serial.println(h);
- Serial.print(F("Flame sensor reading: "));
- Serial.println(sensorReading);
- }
- void receiveOrdersFromTelegramUsers() {
- 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();
- }
- }
- void initSensorsAndOutputs() {
- dht.begin();
- pinMode (FlamePin, INPUT);
- pinMode(SoilDigitalPin, INPUT);
- pinMode(relyFan, OUTPUT);
- pinMode(relyLight, OUTPUT);
- pinMode(relyPump, OUTPUT);
- digitalWrite(relyPump, LOW);
- digitalWrite(relyLight, LOW);
- digitalWrite(relyFan, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement