Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #include "DHT.h"
- // -------- Define Pins -----------
- //Button Pin (pulled down to ground)
- #define BUTTON_PIN 35 // ✓
- #define GAS_1_PIN 34 // ✓
- #define GAS_2_PIN 39 // ✓
- #define dht11_PIN 4 // ✓
- #define redLED_PIN 16 // ✓
- #define greenLED_PIN 17 // ✓
- #define blueLED_PIN 18 // ✓
- #define buzzer1_PIN 21 // ✓
- #define buzzer2_PIN 22 // ✓
- DHT dht(4, DHT11);
- int old_hum = 0;
- int count = 0;
- int GAS_1_Value;
- int GAS_2_Value;
- float temp_Value;
- float hum_Value;
- volatile bool buttonPressedFlag = false;
- const unsigned long BOT_MTBS = 1000; // mean time between scan messages
- unsigned long bot_lasttime; // last time messages' scan has been done
- // Wifi network station credentials
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- // Telegram BOT Token (Get from Botfather)
- #define BOT_TOKEN "5047012559:AAH7kbV_cqOglOJxyBEctmevjoKS6Tp295g"
- //"5018567050:AAGiKQLOERxiFGlKYVZA5bI_bKz4vPeOp54"
- // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
- // Also note that you need to click "start" on a bot before it can
- // message you
- #define CHAT_ID "-697950614"
- //"473975732"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- //////////////////////////////////////////////////////////
- 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?");
- }
- if (text == "/Sensor_Data") {
- int value1 = checkGAS_1();
- int value2 = checkGAS_2();
- float value3 = checkTemp();
- float value4 = checkHum();
- bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
- bot.sendMessage(chat_id, "GAS[2] reading is : " + String(value2));
- bot.sendMessage(chat_id, "Temprature reading is : " + String(value3));
- bot.sendMessage(chat_id, "Humidity reading is : " + String(value4));
- }
- if (text == "/options") {
- String keyboardJson = "[[\"/Sensor_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 += "/Sensor_Data : to return the current sensors data\n";
- welcome += "/options : returns a custom reply keyboard\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- //////////////////////////////////////////////////////////
- void setup() {
- Serial.begin(115200);
- Serial.println();
- pinMode(BUTTON_PIN, INPUT);
- //pinMode(GAS_1_PIN, INPUT);
- //pinMode(GAS_2_PIN, INPUT);
- //pinMode(dht11_PIN, INPUT);
- dht.begin();
- pinMode(redLED_PIN, OUTPUT);
- pinMode(greenLED_PIN, OUTPUT);
- pinMode(blueLED_PIN, OUTPUT);
- pinMode(buzzer1_PIN, OUTPUT);
- pinMode(buzzer2_PIN, OUTPUT);
- digitalWrite(redLED_PIN, 0);
- digitalWrite(greenLED_PIN, 1);
- digitalWrite(blueLED_PIN, 1);
- digitalWrite(buzzer1_PIN, 0);
- digitalWrite(buzzer2_PIN, 0);
- attachInterrupt(BUTTON_PIN, interuptButtonPressed, RISING);
- // attempt to connect to Wifi network:
- 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);
- bot.sendMessage(CHAT_ID, "Bot started up", "");
- }
- int checkGAS_1() {
- int value1 = analogRead(GAS_1_PIN);
- Serial.print("GAS[1] value: ");
- Serial.println(value1);
- return value1;
- }
- int checkGAS_2() {
- int value2 = analogRead(GAS_2_PIN);
- Serial.print("GAS[2] value: ");
- Serial.println(value2);
- return value2;
- }
- float checkTemp() {
- float value3 = dht.readTemperature();
- Serial.print("temprature value: ");
- Serial.println(value3);
- return value3;
- }
- float checkHum() {
- float value4 = dht.readHumidity();
- Serial.print("humidity value: ");
- Serial.println(value4);
- return value4;
- }
- void interuptButtonPressed() {
- Serial.println("SOS Button was pressed!!");
- int button = digitalRead(BUTTON_PIN);
- if (button == HIGH)
- {
- buttonPressedFlag = true;
- }
- return;
- }
- void handleButtonPressed() {
- bot.sendMessage(CHAT_ID, "SOS Button was pressed!! HELP");
- buttonPressedFlag = false;
- }
- void loop() {
- ///////////////////////////////////////////
- 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();
- }
- ///////////////////////////////////////////
- delay(50);
- if ( buttonPressedFlag ) {
- handleButtonPressed();
- }
- delay(50);
- GAS_1_Value = checkGAS_1();
- if (GAS_1_Value > 200) {
- bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
- digitalWrite(redLED_PIN, 1);
- digitalWrite(blueLED_PIN, 0);
- digitalWrite(buzzer1_PIN, 1);
- }
- else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
- digitalWrite(redLED_PIN, 0);
- digitalWrite(blueLED_PIN, 1);
- digitalWrite(buzzer1_PIN, 0);
- }
- delay(50);
- GAS_2_Value = checkGAS_2();
- if (GAS_2_Value > 800) {
- bot.sendMessage(CHAT_ID, "GAS[2] detected " + String(GAS_2_Value));
- digitalWrite(redLED_PIN, 1);
- digitalWrite(blueLED_PIN, 0);
- digitalWrite(buzzer1_PIN, 1);
- }
- else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
- digitalWrite(redLED_PIN, 0);
- digitalWrite(blueLED_PIN, 1);
- digitalWrite(buzzer1_PIN, 0);
- }
- delay(50);
- temp_Value = checkTemp();
- hum_Value = checkHum();
- if (temp_Value > 30) {
- bot.sendMessage(CHAT_ID, "HIGH Tempreture detected " + String(temp_Value));
- digitalWrite(redLED_PIN, 1);
- digitalWrite(greenLED_PIN, 0);
- digitalWrite(buzzer2_PIN, 1);
- }
- else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
- digitalWrite(redLED_PIN, 0);
- digitalWrite(greenLED_PIN, 1);
- digitalWrite(buzzer2_PIN, 0);
- }
- if (hum_Value > 93) {
- digitalWrite(redLED_PIN, 1);
- digitalWrite(greenLED_PIN, 0);
- if (old_hum != hum_Value) {
- count = 0;
- }
- if (count < 5) {
- old_hum = hum_Value;
- count = count + 1;
- bot.sendMessage(CHAT_ID, "HIGH Humidity detected " + String(hum_Value));
- digitalWrite(buzzer2_PIN, 1);
- }
- else {
- digitalWrite(buzzer2_PIN, 0);
- }
- }
- else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
- digitalWrite(redLED_PIN, 0);
- digitalWrite(greenLED_PIN, 1);
- digitalWrite(buzzer2_PIN, 0);
- }
- delay(50);
- }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement