safwan092

Untitled

May 14th, 2022
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 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 13 // ✓
  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. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  33. unsigned long bot_lasttime; // last time messages' scan has been done
  34.  
  35. // Wifi network station credentials
  36. #define WIFI_SSID "network"
  37. #define WIFI_PASSWORD "123456789"
  38. // Telegram BOT Token (Get from Botfather)
  39. #define BOT_TOKEN "5396944689:AAF9-Ti_u2gW5jGeg0jbVJaMk3CPkPJl4ig"
  40. //"5360711622:AAGDERb3AC-MfQyCZkQwTtjNkQuaXKM1N3s"
  41. //"5047012559:AAH7kbV_cqOglOJxyBEctmevjoKS6Tp295g"
  42. //"5018567050:AAGiKQLOERxiFGlKYVZA5bI_bKz4vPeOp54"
  43.  
  44. // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
  45. // Also note that you need to click "start" on a bot before it can
  46. // message you
  47. #define CHAT_ID "-782928143"
  48. //"473975732"
  49. //"-697950614"
  50. //"473975732"
  51.  
  52. WiFiClientSecure secured_client;
  53. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  54.  
  55. //////////////////////////////////////////////////////////
  56.  
  57. void handleNewMessages(int numNewMessages)
  58. {
  59. Serial.println("handleNewMessages");
  60. Serial.println(String(numNewMessages));
  61.  
  62. for (int i = 0; i < numNewMessages; i++)
  63. {
  64. String chat_id = bot.messages[i].chat_id;
  65. String text = bot.messages[i].text;
  66.  
  67. String from_name = bot.messages[i].from_name;
  68. if (from_name == "")
  69. from_name = "Guest";
  70.  
  71. if (text == "/send_test_action")
  72. {
  73. bot.sendChatAction(chat_id, "typing");
  74. delay(4000);
  75. bot.sendMessage(chat_id, "Did you see the action message?");
  76. }
  77.  
  78. if (text == "/Sensor_Data") {
  79. int value1 = checkGAS_1();
  80. int value2 = checkGAS_2();
  81. float value3 = checkTemp();
  82. float value4 = checkHum();
  83. bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
  84. bot.sendMessage(chat_id, "GAS[2] reading is : " + String(value2));
  85. bot.sendMessage(chat_id, "Temprature reading is : " + String(value3));
  86. bot.sendMessage(chat_id, "Humidity reading is : " + String(value4));
  87. }
  88.  
  89. if (text == "/options") {
  90. String keyboardJson = "[[\"/Sensor_Data\"]]";
  91. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  92. }
  93.  
  94. if (text == "/start")
  95. {
  96. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  97. welcome += "This is Chat Action Bot example.\n\n";
  98. welcome += "/Sensor_Data : to return the current sensors data\n";
  99. welcome += "/options : returns a custom reply keyboard\n";
  100. bot.sendMessage(chat_id, welcome);
  101. }
  102. }
  103. }
  104. //////////////////////////////////////////////////////////
  105.  
  106.  
  107.  
  108. void setup() {
  109. Serial.begin(115200);
  110. Serial.println();
  111.  
  112. pinMode(BUTTON_PIN, INPUT);
  113. pinMode(GAS_1_PIN, INPUT);
  114. pinMode(GAS_2_PIN, INPUT);
  115. pinMode(dht11_PIN, INPUT);
  116. dht.begin();
  117. pinMode(redLED_PIN, OUTPUT);
  118. pinMode(greenLED_PIN, OUTPUT);
  119. pinMode(blueLED_PIN, OUTPUT);
  120. pinMode(buzzer1_PIN, OUTPUT);
  121. pinMode(buzzer2_PIN, OUTPUT);
  122.  
  123. digitalWrite(redLED_PIN, 0);
  124. digitalWrite(greenLED_PIN, 1);
  125. digitalWrite(blueLED_PIN, 1);
  126. digitalWrite(buzzer1_PIN, 0);
  127. digitalWrite(buzzer2_PIN, 0);
  128.  
  129. attachInterrupt(BUTTON_PIN, interuptButtonPressed, RISING);
  130.  
  131.  
  132. // attempt to connect to Wifi network:
  133. Serial.print("Connecting to Wifi SSID ");
  134. Serial.print(WIFI_SSID);
  135. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  136. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  137. while (WiFi.status() != WL_CONNECTED)
  138. {
  139. Serial.print(".");
  140. delay(500);
  141. }
  142. Serial.print("\nWiFi connected. IP address: ");
  143. Serial.println(WiFi.localIP());
  144.  
  145. Serial.print("Retrieving time: ");
  146. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  147. time_t now = time(nullptr);
  148. while (now < 24 * 3600)
  149. {
  150. Serial.print(".");
  151. delay(100);
  152. now = time(nullptr);
  153. }
  154. Serial.println(now);
  155.  
  156. bot.sendMessage(CHAT_ID, "Bot started up", "");
  157. delay(20000);
  158. Serial.println("delay finished");
  159. }
  160.  
  161. int checkGAS_1() {
  162. int value1 = analogRead(GAS_1_PIN);
  163. Serial.print("GAS[1] value: ");
  164. Serial.println(value1);
  165. return value1;
  166. }
  167. int checkGAS_2() {
  168. int value2 = analogRead(GAS_2_PIN);
  169. Serial.print("GAS[2] value: ");
  170. Serial.println(value2);
  171. return value2;
  172. }
  173. float checkTemp() {
  174. float value3 = dht.readTemperature();
  175. Serial.print("temprature value: ");
  176. Serial.println(value3);
  177. return value3;
  178. }
  179. float checkHum() {
  180. float value4 = dht.readHumidity();
  181. Serial.print("humidity value: ");
  182. Serial.println(value4);
  183. return value4;
  184. }
  185.  
  186. void interuptButtonPressed() {
  187. Serial.println("SOS Button was pressed!!");
  188. int button = digitalRead(BUTTON_PIN);
  189. if (button == HIGH)
  190. {
  191. buttonPressedFlag = true;
  192. }
  193. return;
  194. }
  195. void handleButtonPressed() {
  196. bot.sendMessage(CHAT_ID, "SOS Button was pressed!! HELP");
  197. buttonPressedFlag = false;
  198. }
  199.  
  200. void loop() {
  201.  
  202. ///////////////////////////////////////////
  203. if (millis() - bot_lasttime > BOT_MTBS)
  204. {
  205. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  206.  
  207. while (numNewMessages)
  208. {
  209. Serial.println("got response");
  210. handleNewMessages(numNewMessages);
  211. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  212. }
  213.  
  214. bot_lasttime = millis();
  215. }
  216. ///////////////////////////////////////////
  217. delay(50);
  218. if ( buttonPressedFlag ) {
  219. handleButtonPressed();
  220. }
  221. delay(50);
  222. GAS_1_Value = checkGAS_1();
  223. if (GAS_1_Value > 900) {
  224. bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
  225. digitalWrite(redLED_PIN, 1);
  226. digitalWrite(blueLED_PIN, 0);
  227. digitalWrite(buzzer1_PIN, 1);
  228. }
  229. else if (GAS_1_Value < 900 && GAS_2_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  230. digitalWrite(redLED_PIN, 0);
  231. digitalWrite(blueLED_PIN, 1);
  232. digitalWrite(buzzer1_PIN, 0);
  233. }
  234. delay(50);
  235. GAS_2_Value = checkGAS_2();
  236. if (GAS_2_Value > 1500) {
  237. bot.sendMessage(CHAT_ID, "GAS[2] detected " + String(GAS_2_Value));
  238. digitalWrite(redLED_PIN, 1);
  239. digitalWrite(blueLED_PIN, 0);
  240. digitalWrite(buzzer1_PIN, 1);
  241. }
  242. else if (GAS_1_Value < 900 && GAS_2_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  243. digitalWrite(redLED_PIN, 0);
  244. digitalWrite(blueLED_PIN, 1);
  245. digitalWrite(buzzer1_PIN, 0);
  246. }
  247. delay(50);
  248. temp_Value = checkTemp();
  249. hum_Value = checkHum();
  250. if (temp_Value > 30) {
  251. bot.sendMessage(CHAT_ID, "HIGH Tempreture detected " + String(temp_Value));
  252. digitalWrite(redLED_PIN, 1);
  253. digitalWrite(greenLED_PIN, 0);
  254. digitalWrite(buzzer2_PIN, 1);
  255. }
  256. else if (GAS_1_Value < 900 && GAS_2_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  257. digitalWrite(redLED_PIN, 0);
  258. digitalWrite(greenLED_PIN, 1);
  259. digitalWrite(buzzer2_PIN, 0);
  260. }
  261. if (hum_Value > 93) {
  262. digitalWrite(redLED_PIN, 1);
  263. digitalWrite(greenLED_PIN, 0);
  264. if (old_hum != hum_Value) {
  265. count = 0;
  266. }
  267. if (count < 5) {
  268. old_hum = hum_Value;
  269. count = count + 1;
  270. bot.sendMessage(CHAT_ID, "HIGH Humidity detected " + String(hum_Value));
  271. digitalWrite(buzzer2_PIN, 1);
  272. }
  273. else {
  274. digitalWrite(buzzer2_PIN, 0);
  275. }
  276. }
  277. else if (GAS_1_Value < 900 && GAS_2_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  278. digitalWrite(redLED_PIN, 0);
  279. digitalWrite(greenLED_PIN, 1);
  280. digitalWrite(buzzer2_PIN, 0);
  281. }
  282. delay(50);
  283.  
  284. }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment