Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <UniversalTelegramBot.h>
- #include <WiFi.h>
- #include <WiFiClientSecure.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);
- // -------- ---------- -----------
- // Initialize Telegram BOT
- #define botToken "5018567050:AAGiKQLOERxiFGlKYVZA5bI_bKz4vPeOp54" // your Bot Token (Get from Botfather)
- WiFiClientSecure client;
- UniversalTelegramBot bot(botToken, client);
- long checkTelegramDueTime;
- int checkTelegramDelay = 1000; // 1000 (1 second)
- int old_hum = 0;
- int count = 0;
- // The person/group you want the alert messages to go to.
- // You must start a chat with a bot first before it can send you messages
- String defaultChatId = "473975732"; //This can be got by using a bot called "myIdBot"
- char ssid[] = "network"; // your network SSID (name)
- char password[] = "123456789"; // your network key
- volatile bool buttonPressedFlag = false;
- long GAS_1_DueTime;
- int check_GAS_1_Delay = 250; // 1000/4 (1/4 of a second)
- long GAS_2_DueTime;
- int check_GAS_2_Delay = 250; // 1000/4 (1/4 of a second)
- long temp_DueTime;
- int check_temp_Delay = 250; // 1000/4 (1/4 of a second)
- int GAS_1_Value;
- int GAS_2_Value;
- float temp_Value;
- float hum_Value;
- void setup() {
- Serial.begin(115200);
- 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 Wifi: ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- IPAddress ip = WiFi.localIP();
- Serial.println(ip);
- delay(10000);
- }
- void interuptButtonPressed() {
- Serial.println("SOS Button was pressed!!");
- int button = digitalRead(BUTTON_PIN);
- if (button == HIGH)
- {
- buttonPressedFlag = true;
- }
- return;
- }
- void handleButtonPressed() {
- bot.sendMessage(defaultChatId, "SOS Button was pressed!! HELP");
- buttonPressedFlag = false;
- }
- int checkGAS_1() {
- int value1 = analogRead(GAS_1_PIN);
- //Serial.print("GAS[1] value: ");
- //Serial.println(value);
- 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;
- }
- // --- Telgram functions ---
- void handleNewMessages(int numNewMessages) {
- for (int i = 0; i < numNewMessages; i++) {
- String chat_id = String(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 == "/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" || text == "/help") {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "The following options are available.\n\n";
- welcome += "/Sensor_Data : to return the current sensors data\n";
- welcome += "/options : returns a custom reply keyboard\n";
- welcome += "/help : displays this message again\n";
- bot.sendMessage(chat_id, welcome, "Markdown");
- }
- }
- }
- // --- ----------------- ---
- void loop() {
- //************************************************************************************************
- //-----------------------------------------------------------------------------
- //1.Read SOS button status
- if ( buttonPressedFlag ) {
- handleButtonPressed();
- }
- //-----------------------------------------------------------------------------
- //************************************************************************************************
- //-----------------------------------------------------------------------------
- //2.handel messages from telegram
- long now = millis();
- if (now >= checkTelegramDueTime) {
- Serial.println("---- Checking Telegram -----");
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while (numNewMessages) {
- Serial.println("Bot recieved a message");
- handleNewMessages(numNewMessages);
- numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- }
- checkTelegramDueTime = now + checkTelegramDelay;
- }
- //-----------------------------------------------------------------------------
- //************************************************************************************************
- //-----------------------------------------------------------------------------
- //3.Read gas sensor #1
- now = millis();
- if (now >= GAS_1_DueTime) {
- GAS_1_Value = checkGAS_1();
- if (GAS_1_Value > 200) {
- bot.sendMessage(defaultChatId, "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);
- }
- GAS_1_DueTime = now + check_GAS_1_Delay;
- }
- //-----------------------------------------------------------------------------
- //************************************************************************************************
- //-----------------------------------------------------------------------------
- //4.Read gas sensor #2
- now = millis();
- if (now >= GAS_2_DueTime) {
- GAS_2_Value = checkGAS_2();
- if (GAS_2_Value > 800) {
- bot.sendMessage(defaultChatId, "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);
- }
- GAS_2_DueTime = now + check_GAS_2_Delay;
- }
- //-----------------------------------------------------------------------------
- //************************************************************************************************
- //-----------------------------------------------------------------------------
- //5.Read temp/humidity sensor
- now = millis();
- if (now >= temp_DueTime) {
- temp_Value = checkTemp();
- hum_Value = checkHum();
- if (temp_Value > 30) {
- bot.sendMessage(defaultChatId, "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(defaultChatId, "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);
- }
- temp_DueTime = now + check_temp_Delay;
- }
- //-----------------------------------------------------------------------------
- //************************************************************************************************
- }//end of LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement