Advertisement
safwan092

Untitled

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