safwan092

Untitled

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