Advertisement
safwan092

Untitled

Dec 7th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. #include <UniversalTelegramBot.h>
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. //#include <ArduinoJson.h>
  5. #include "DHT.h"
  6.  
  7.  
  8. // -------- Define Pins -----------
  9.  
  10. //Button Pin (pulled down to ground)
  11. #define BUTTON_PIN 35 // ✓
  12. #define GAS_1_PIN 34 // ✓
  13. #define GAS_2_PIN 39 // ✓
  14. #define dht11_PIN 4 // ✓
  15. #define redLED_PIN 16 // ✓
  16. #define greenLED_PIN 17 // ✓
  17. #define blueLED_PIN 18 // ✓
  18. #define buzzer1_PIN 21 // ✓
  19. #define buzzer2_PIN 22 // ✓
  20.  
  21. DHT dht(4, DHT11);
  22.  
  23. // -------- ---------- -----------
  24.  
  25. // Initialize Telegram BOT
  26. #define botToken "5018567050:AAGiKQLOERxiFGlKYVZA5bI_bKz4vPeOp54" // your Bot Token (Get from Botfather)
  27. WiFiClientSecure client;
  28. UniversalTelegramBot bot(botToken, client);
  29. long checkTelegramDueTime;
  30. int checkTelegramDelay = 1000; // 1000 (1 second)
  31. int old_hum = 0;
  32. int count = 0;
  33.  
  34. // The person/group you want the alert messages to go to.
  35. // You must start a chat with a bot first before it can send you messages
  36. String defaultChatId = "473975732"; //This can be got by using a bot called "myIdBot"
  37.  
  38. char ssid[] = "network"; // your network SSID (name)
  39. char password[] = "123456789"; // your network key
  40.  
  41. volatile bool buttonPressedFlag = false;
  42.  
  43. long GAS_1_DueTime;
  44. int check_GAS_1_Delay = 250; // 1000/4 (1/4 of a second)
  45.  
  46. long GAS_2_DueTime;
  47. int check_GAS_2_Delay = 250; // 1000/4 (1/4 of a second)
  48.  
  49. long temp_DueTime;
  50. int check_temp_Delay = 250; // 1000/4 (1/4 of a second)
  51.  
  52. int GAS_1_Value;
  53. int GAS_2_Value;
  54. float temp_Value;
  55. float hum_Value;
  56.  
  57. void setup() {
  58.  
  59. Serial.begin(115200);
  60. pinMode(BUTTON_PIN, INPUT);
  61. //pinMode(GAS_1_PIN, INPUT);
  62. //pinMode(GAS_2_PIN, INPUT);
  63. //pinMode(dht11_PIN, INPUT);
  64. dht.begin();
  65. pinMode(redLED_PIN, OUTPUT);
  66. pinMode(greenLED_PIN, OUTPUT);
  67. pinMode(blueLED_PIN, OUTPUT);
  68. pinMode(buzzer1_PIN, OUTPUT);
  69. pinMode(buzzer2_PIN, OUTPUT);
  70.  
  71. digitalWrite(redLED_PIN, 0);
  72. digitalWrite(greenLED_PIN, 1);
  73. digitalWrite(blueLED_PIN, 1);
  74. digitalWrite(buzzer1_PIN, 0);
  75. digitalWrite(buzzer2_PIN, 0);
  76.  
  77. attachInterrupt(BUTTON_PIN, interuptButtonPressed, RISING);
  78.  
  79. // Attempt to connect to Wifi network:
  80. Serial.print("Connecting Wifi: ");
  81. Serial.println(ssid);
  82. WiFi.begin(ssid, password);
  83. while (WiFi.status() != WL_CONNECTED) {
  84. Serial.print(".");
  85. delay(500);
  86. }
  87. Serial.println("");
  88. Serial.println("WiFi connected");
  89. Serial.println("IP address: ");
  90. IPAddress ip = WiFi.localIP();
  91. Serial.println(ip);
  92. delay(10000);
  93. }
  94.  
  95. void interuptButtonPressed() {
  96. Serial.println("SOS Button was pressed!!");
  97. int button = digitalRead(BUTTON_PIN);
  98. if (button == HIGH)
  99. {
  100. buttonPressedFlag = true;
  101. }
  102. return;
  103. }
  104. void handleButtonPressed() {
  105. bot.sendMessage(defaultChatId, "SOS Button was pressed!! HELP");
  106. buttonPressedFlag = false;
  107. }
  108.  
  109. int checkGAS_1() {
  110. int value1 = analogRead(GAS_1_PIN);
  111. //Serial.print("GAS[1] value: ");
  112. //Serial.println(value);
  113. return value1;
  114. }
  115.  
  116. int checkGAS_2() {
  117. int value2 = analogRead(GAS_2_PIN);
  118. //Serial.print("GAS[2] value: ");
  119. //Serial.println(value2);
  120. return value2;
  121. }
  122.  
  123. float checkTemp() {
  124. float value3 = dht.readTemperature();
  125. //Serial.print("temprature value: ");
  126. //Serial.println(value3);
  127. return value3;
  128. }
  129.  
  130. float checkHum() {
  131. float value4 = dht.readHumidity();
  132. //Serial.print("humidity value: ");
  133. //Serial.println(value4);
  134. return value4;
  135. }
  136.  
  137. // --- Telgram functions ---
  138.  
  139. void handleNewMessages(int numNewMessages) {
  140. for (int i = 0; i < numNewMessages; i++) {
  141. String chat_id = String(bot.messages[i].chat_id);
  142. String text = bot.messages[i].text;
  143.  
  144. String from_name = bot.messages[i].from_name;
  145. if (from_name == "") from_name = "Guest";
  146.  
  147. if (text == "/Sensor_Data") {
  148. int value1 = checkGAS_1();
  149. int value2 = checkGAS_2();
  150. float value3 = checkTemp();
  151. float value4 = checkHum();
  152. bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
  153. bot.sendMessage(chat_id, "GAS[2] reading is : " + String(value2));
  154. bot.sendMessage(chat_id, "Temprature reading is : " + String(value3));
  155. bot.sendMessage(chat_id, "Humidity reading is : " + String(value4));
  156. }
  157.  
  158. if (text == "/options") {
  159. String keyboardJson = "[[\"/Sensor_Data\"]]";
  160. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  161. }
  162.  
  163. if (text == "/start" || text == "/help") {
  164. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  165. welcome += "The following options are available.\n\n";
  166. welcome += "/Sensor_Data : to return the current sensors data\n";
  167. welcome += "/options : returns a custom reply keyboard\n";
  168. welcome += "/help : displays this message again\n";
  169. bot.sendMessage(chat_id, welcome, "Markdown");
  170. }
  171. }
  172. }
  173.  
  174.  
  175. // --- ----------------- ---
  176.  
  177. void loop() {
  178.  
  179. //************************************************************************************************
  180. //-----------------------------------------------------------------------------
  181. //1.Read SOS button status
  182. if ( buttonPressedFlag ) {
  183. handleButtonPressed();
  184. }
  185. //-----------------------------------------------------------------------------
  186. //************************************************************************************************
  187. //-----------------------------------------------------------------------------
  188. //2.handel messages from telegram
  189. long now = millis();
  190. if (now >= checkTelegramDueTime) {
  191. Serial.println("---- Checking Telegram -----");
  192. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  193. while (numNewMessages) {
  194. Serial.println("Bot recieved a message");
  195. handleNewMessages(numNewMessages);
  196. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  197. }
  198. checkTelegramDueTime = now + checkTelegramDelay;
  199. }
  200. //-----------------------------------------------------------------------------
  201. //************************************************************************************************
  202. //-----------------------------------------------------------------------------
  203. //3.Read gas sensor #1
  204. now = millis();
  205. if (now >= GAS_1_DueTime) {
  206. GAS_1_Value = checkGAS_1();
  207. if (GAS_1_Value > 200) {
  208. bot.sendMessage(defaultChatId, "GAS[1] detected " + String(GAS_1_Value));
  209. digitalWrite(redLED_PIN, 1);
  210. digitalWrite(blueLED_PIN, 0);
  211. digitalWrite(buzzer1_PIN, 1);
  212. }
  213. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  214. digitalWrite(redLED_PIN, 0);
  215. digitalWrite(blueLED_PIN, 1);
  216. digitalWrite(buzzer1_PIN, 0);
  217. }
  218. GAS_1_DueTime = now + check_GAS_1_Delay;
  219. }
  220. //-----------------------------------------------------------------------------
  221. //************************************************************************************************
  222. //-----------------------------------------------------------------------------
  223. //4.Read gas sensor #2
  224. now = millis();
  225. if (now >= GAS_2_DueTime) {
  226. GAS_2_Value = checkGAS_2();
  227. if (GAS_2_Value > 800) {
  228. bot.sendMessage(defaultChatId, "GAS[2] detected " + String(GAS_2_Value));
  229. digitalWrite(redLED_PIN, 1);
  230. digitalWrite(blueLED_PIN, 0);
  231. digitalWrite(buzzer1_PIN, 1);
  232. }
  233. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  234. digitalWrite(redLED_PIN, 0);
  235. digitalWrite(blueLED_PIN, 1);
  236. digitalWrite(buzzer1_PIN, 0);
  237. }
  238. GAS_2_DueTime = now + check_GAS_2_Delay;
  239. }
  240. //-----------------------------------------------------------------------------
  241. //************************************************************************************************
  242. //-----------------------------------------------------------------------------
  243. //5.Read temp/humidity sensor
  244. now = millis();
  245. if (now >= temp_DueTime) {
  246. temp_Value = checkTemp();
  247. hum_Value = checkHum();
  248. if (temp_Value > 30) {
  249. bot.sendMessage(defaultChatId, "HIGH Tempreture detected " + String(temp_Value));
  250. digitalWrite(redLED_PIN, 1);
  251. digitalWrite(greenLED_PIN, 0);
  252. digitalWrite(buzzer2_PIN, 1);
  253. }
  254. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  255. digitalWrite(redLED_PIN, 0);
  256. digitalWrite(greenLED_PIN, 1);
  257. digitalWrite(buzzer2_PIN, 0);
  258. }
  259. if (hum_Value > 93) {
  260. digitalWrite(redLED_PIN, 1);
  261. digitalWrite(greenLED_PIN, 0);
  262. if (old_hum != hum_Value) {
  263. count = 0;
  264. }
  265. if (count < 5) {
  266. old_hum = hum_Value;
  267. count = count + 1;
  268. bot.sendMessage(defaultChatId, "HIGH Humidity detected " + String(hum_Value));
  269. digitalWrite(buzzer2_PIN, 1);
  270. }
  271. else {
  272. digitalWrite(buzzer2_PIN, 0);
  273. }
  274. }
  275. else if (GAS_1_Value < 200 && GAS_2_Value < 800 && temp_Value < 30 && hum_Value < 93) {
  276. digitalWrite(redLED_PIN, 0);
  277. digitalWrite(greenLED_PIN, 1);
  278. digitalWrite(buzzer2_PIN, 0);
  279. }
  280. temp_DueTime = now + check_temp_Delay;
  281. }
  282. //-----------------------------------------------------------------------------
  283. //************************************************************************************************
  284. }//end of LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement