Advertisement
safwan092

Untitled

Dec 8th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5. #include "DHT.h"
  6.  
  7. // -------- Define Pins -----------
  8.  
  9. //Button Pin (pulled down to ground)
  10. #define BUTTON_PIN 35 // ✓
  11. #define GAS_1_PIN 34 // ✓
  12. #define GAS_2_PIN 39 // ✓
  13. #define dht11_PIN 4 // ✓
  14. #define redLED_PIN 16 // ✓
  15. #define greenLED_PIN 17 // ✓
  16. #define blueLED_PIN 18 // ✓
  17. #define buzzer1_PIN 21 // ✓
  18. #define buzzer2_PIN 22 // ✓
  19.  
  20. DHT dht(4, DHT11);
  21.  
  22. int old_hum = 0;
  23. int count = 0;
  24.  
  25. int GAS_1_Value;
  26. int GAS_2_Value;
  27. float temp_Value;
  28. float hum_Value;
  29.  
  30. // Wifi network station credentials
  31. #define WIFI_SSID "network"
  32. #define WIFI_PASSWORD "123456789"
  33. // Telegram BOT Token (Get from Botfather)
  34. #define BOT_TOKEN "5018567050:AAGiKQLOERxiFGlKYVZA5bI_bKz4vPeOp54"
  35.  
  36. // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
  37. // Also note that you need to click "start" on a bot before it can
  38. // message you
  39. #define CHAT_ID "473975732"
  40.  
  41. WiFiClientSecure secured_client;
  42. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  43.  
  44. void setup() {
  45. Serial.begin(115200);
  46. Serial.println();
  47.  
  48. pinMode(BUTTON_PIN, INPUT);
  49. //pinMode(GAS_1_PIN, INPUT);
  50. //pinMode(GAS_2_PIN, INPUT);
  51. //pinMode(dht11_PIN, INPUT);
  52. dht.begin();
  53. pinMode(redLED_PIN, OUTPUT);
  54. pinMode(greenLED_PIN, OUTPUT);
  55. pinMode(blueLED_PIN, OUTPUT);
  56. pinMode(buzzer1_PIN, OUTPUT);
  57. pinMode(buzzer2_PIN, OUTPUT);
  58.  
  59. digitalWrite(redLED_PIN, 0);
  60. digitalWrite(greenLED_PIN, 1);
  61. digitalWrite(blueLED_PIN, 1);
  62. digitalWrite(buzzer1_PIN, 0);
  63. digitalWrite(buzzer2_PIN, 0);
  64.  
  65. // attempt to connect to Wifi network:
  66. Serial.print("Connecting to Wifi SSID ");
  67. Serial.print(WIFI_SSID);
  68. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  69. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  70. while (WiFi.status() != WL_CONNECTED)
  71. {
  72. Serial.print(".");
  73. delay(500);
  74. }
  75. Serial.print("\nWiFi connected. IP address: ");
  76. Serial.println(WiFi.localIP());
  77.  
  78. Serial.print("Retrieving time: ");
  79. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  80. time_t now = time(nullptr);
  81. while (now < 24 * 3600)
  82. {
  83. Serial.print(".");
  84. delay(100);
  85. now = time(nullptr);
  86. }
  87. Serial.println(now);
  88.  
  89. bot.sendMessage(CHAT_ID, "Bot started up", "");
  90. }
  91.  
  92. int checkGAS_1() {
  93. int value1 = analogRead(GAS_1_PIN);
  94. //Serial.print("GAS[1] value: ");
  95. //Serial.println(value);
  96. return value1;
  97. }
  98. int checkGAS_2() {
  99. int value2 = analogRead(GAS_2_PIN);
  100. //Serial.print("GAS[2] value: ");
  101. //Serial.println(value2);
  102. return value2;
  103. }
  104. float checkTemp() {
  105. float value3 = dht.readTemperature();
  106. //Serial.print("temprature value: ");
  107. //Serial.println(value3);
  108. return value3;
  109. }
  110. float checkHum() {
  111. float value4 = dht.readHumidity();
  112. //Serial.print("humidity value: ");
  113. //Serial.println(value4);
  114. return value4;
  115. }
  116.  
  117. void loop() {
  118. GAS_1_Value = checkGAS_1();
  119. if (GAS_1_Value > 200) {
  120. bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
  121. digitalWrite(redLED_PIN, 1);
  122. digitalWrite(blueLED_PIN, 0);
  123. digitalWrite(buzzer1_PIN, 1);
  124. }
  125. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  126. digitalWrite(redLED_PIN, 0);
  127. digitalWrite(blueLED_PIN, 1);
  128. digitalWrite(buzzer1_PIN, 0);
  129. }
  130. delay(50);
  131. GAS_2_Value = checkGAS_2();
  132. if (GAS_2_Value > 800) {
  133. bot.sendMessage(CHAT_ID, "GAS[2] detected " + String(GAS_2_Value));
  134. digitalWrite(redLED_PIN, 1);
  135. digitalWrite(blueLED_PIN, 0);
  136. digitalWrite(buzzer1_PIN, 1);
  137. }
  138. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  139. digitalWrite(redLED_PIN, 0);
  140. digitalWrite(blueLED_PIN, 1);
  141. digitalWrite(buzzer1_PIN, 0);
  142. }
  143. delay(50);
  144. temp_Value = checkTemp();
  145. hum_Value = checkHum();
  146. if (temp_Value > 30) {
  147. bot.sendMessage(CHAT_ID, "HIGH Tempreture detected " + String(temp_Value));
  148. digitalWrite(redLED_PIN, 1);
  149. digitalWrite(greenLED_PIN, 0);
  150. digitalWrite(buzzer2_PIN, 1);
  151. }
  152. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  153. digitalWrite(redLED_PIN, 0);
  154. digitalWrite(greenLED_PIN, 1);
  155. digitalWrite(buzzer2_PIN, 0);
  156. }
  157. if (hum_Value > 93) {
  158. digitalWrite(redLED_PIN, 1);
  159. digitalWrite(greenLED_PIN, 0);
  160. if (old_hum != hum_Value) {
  161. count = 0;
  162. }
  163. if (count < 5) {
  164. old_hum = hum_Value;
  165. count = count + 1;
  166. bot.sendMessage(CHAT_ID, "HIGH Humidity detected " + String(hum_Value));
  167. digitalWrite(buzzer2_PIN, 1);
  168. }
  169. else {
  170. digitalWrite(buzzer2_PIN, 0);
  171. }
  172. }
  173. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  174. digitalWrite(redLED_PIN, 0);
  175. digitalWrite(greenLED_PIN, 1);
  176. digitalWrite(buzzer2_PIN, 0);
  177. }
  178. delay(50);
  179.  
  180. }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement