Advertisement
safwan092

Untitled

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