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"
- int flag = 0;
- int flag2 = 0;
- // -------- Define Pins -----------
- #define GAS_1_PIN 34 // ✓
- #define redLED_PIN 26 // ✓
- #define buzzer1_PIN 21 // ✓
- int count = 0;
- int GAS_1_Value;
- 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 "6743180949:AAFE4lN6QQsvEY9U33Pv2a-ta45f-1iOrb4"
- // 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 "793949283"
- #define CHAT_ID1 "6458727643"
- 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();
- bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
- }
- 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";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- //////////////////////////////////////////////////////////
- void setup() {
- Serial.begin(115200);
- Serial.println();
- pinMode(GAS_1_PIN, INPUT);
- pinMode(redLED_PIN, OUTPUT);
- pinMode(buzzer1_PIN, OUTPUT);
- // 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", "");
- delay(20000);
- Serial.println("delay finished");
- }
- int checkGAS_1() {
- int value1 = analogRead(GAS_1_PIN);
- Serial.print("GAS[1] value: ");
- Serial.println(value1);
- return value1;
- }
- 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();
- }
- ///////////////////////////////////////////
- GAS_1_Value = checkGAS_1();
- if (GAS_1_Value > 900) {
- digitalWrite(redLED_PIN, 1);
- digitalWrite(buzzer1_PIN, 1);
- bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
- bot.sendMessage(CHAT_ID1, "GAS[1] detected " + String(GAS_1_Value));
- }
- else if (GAS_1_Value < 900)
- {
- digitalWrite(redLED_PIN, 0);
- digitalWrite(buzzer1_PIN, 0);
- }
- delay(50);
- }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement