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;
- // Wifi network station credentials
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- // Telegram BOT Token (Get from Botfather)
- #define BOT_TOKEN "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 "473975732"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- 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);
- // 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(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;
- }
- void loop() {
- 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