Advertisement
safwan092

Project_11458_ESP32_Gas_Buzzer_DHT11_Telegram_Leds_Helmet_FULL

Apr 27th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 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.  
  19. int Gas1ActivationValue = 2000;//900
  20. int Gas2ActivationValue = 2000;//1500
  21. int TempActivationValue = 30;
  22. int HumActivationValue = 93;
  23.  
  24. DHT dht(dht11_PIN, DHT11);
  25.  
  26. int old_hum = 0;
  27. int count = 0;
  28.  
  29. int GAS_1_Value;
  30. int GAS_2_Value;
  31. float temp_Value;
  32. float hum_Value;
  33.  
  34. volatile bool buttonPressedFlag = false;
  35.  
  36. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  37. unsigned long bot_lasttime; // last time messages' scan has been done
  38.  
  39. // Wifi network station credentials
  40. #define WIFI_SSID "network"
  41. #define WIFI_PASSWORD "123456789"
  42. // Telegram BOT Token (Get from Botfather)
  43. #define BOT_TOKEN "5997202065:AAG7_248qw99mYarJC4DnWysCJjRReIfoBQ"
  44.  
  45. // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
  46. // Also note that you need to click "start" on a bot before it can
  47. // message you
  48. #define CHAT_ID "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.  
  120. digitalWrite(redLED_PIN, 0);
  121. digitalWrite(greenLED_PIN, 1);
  122. digitalWrite(blueLED_PIN, 1);
  123. digitalWrite(buzzer1_PIN, 0);
  124.  
  125.  
  126. attachInterrupt(BUTTON_PIN, interuptButtonPressed, RISING);
  127.  
  128.  
  129. // attempt to connect to Wifi network:
  130. Serial.print("Connecting to Wifi SSID ");
  131. Serial.print(WIFI_SSID);
  132. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  133. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  134. while (WiFi.status() != WL_CONNECTED)
  135. {
  136. Serial.print(".");
  137. delay(500);
  138. }
  139. Serial.print("\nWiFi connected. IP address: ");
  140. Serial.println(WiFi.localIP());
  141.  
  142. Serial.print("Retrieving time: ");
  143. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  144. time_t now = time(nullptr);
  145. while (now < 24 * 3600)
  146. {
  147. Serial.print(".");
  148. delay(100);
  149. now = time(nullptr);
  150. }
  151. Serial.println(now);
  152.  
  153. bot.sendMessage(CHAT_ID, "Bot started up", "");
  154. delay(20000);
  155. Serial.println("delay finished");
  156. }
  157.  
  158. int checkGAS_1() {
  159. int value1 = analogRead(GAS_1_PIN);
  160. Serial.print("GAS[1] value: ");
  161. Serial.println(value1);
  162. return value1;
  163. }
  164. int checkGAS_2() {
  165. int value2 = analogRead(GAS_2_PIN);
  166. Serial.print("GAS[2] value: ");
  167. Serial.println(value2);
  168. return value2;
  169. }
  170. float checkTemp() {
  171. float value3 = dht.readTemperature();
  172. Serial.print("temprature value: ");
  173. Serial.println(value3);
  174. return value3;
  175. }
  176. float checkHum() {
  177. float value4 = dht.readHumidity();
  178. Serial.print("humidity value: ");
  179. Serial.println(value4);
  180. return value4;
  181. }
  182.  
  183. void interuptButtonPressed() {
  184. Serial.println("SOS Button was pressed!!");
  185. int button = digitalRead(BUTTON_PIN);
  186. if (button == HIGH)
  187. {
  188. buttonPressedFlag = true;
  189. }
  190. return;
  191. }
  192. void handleButtonPressed() {
  193. bot.sendMessage(CHAT_ID, "SOS Button was pressed!! HELP");
  194. buttonPressedFlag = false;
  195. }
  196.  
  197. void loop() {
  198.  
  199. ///////////////////////////////////////////
  200. if (millis() - bot_lasttime > BOT_MTBS)
  201. {
  202. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  203.  
  204. while (numNewMessages)
  205. {
  206. Serial.println("got response");
  207. handleNewMessages(numNewMessages);
  208. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  209. }
  210.  
  211. bot_lasttime = millis();
  212. }
  213. ///////////////////////////////////////////
  214. delay(50);
  215. if ( buttonPressedFlag ) {
  216. handleButtonPressed();
  217. }
  218. delay(50);
  219. GAS_1_Value = checkGAS_1();
  220. if (GAS_1_Value > Gas1ActivationValue) {
  221. bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
  222. digitalWrite(redLED_PIN, 1);
  223. digitalWrite(blueLED_PIN, 0);
  224. digitalWrite(buzzer1_PIN, 1);
  225. }
  226. else if (GAS_1_Value < Gas1ActivationValue && GAS_2_Value < Gas2ActivationValue && temp_Value < TempActivationValue && hum_Value < HumActivationValue) {
  227. digitalWrite(redLED_PIN, 0);
  228. digitalWrite(blueLED_PIN, 1);
  229. digitalWrite(buzzer1_PIN, 0);
  230. }
  231. delay(50);
  232. GAS_2_Value = checkGAS_2();
  233. if (GAS_2_Value > Gas2ActivationValue) {
  234. bot.sendMessage(CHAT_ID, "GAS[2] detected " + String(GAS_2_Value));
  235. digitalWrite(redLED_PIN, 1);
  236. digitalWrite(blueLED_PIN, 0);
  237. digitalWrite(buzzer1_PIN, 1);
  238. }
  239. else if (GAS_1_Value < Gas1ActivationValue && GAS_2_Value < Gas2ActivationValue && temp_Value < TempActivationValue && hum_Value < HumActivationValue) {
  240. digitalWrite(redLED_PIN, 0);
  241. digitalWrite(blueLED_PIN, 1);
  242. digitalWrite(buzzer1_PIN, 0);
  243. }
  244. delay(50);
  245. temp_Value = checkTemp();
  246. hum_Value = checkHum();
  247. if (temp_Value > TempActivationValue) {
  248. bot.sendMessage(CHAT_ID, "HIGH Tempreture detected " + String(temp_Value));
  249. digitalWrite(redLED_PIN, 1);
  250. digitalWrite(greenLED_PIN, 0);
  251. digitalWrite(buzzer1_PIN, 1);
  252. }
  253. else if (GAS_1_Value < Gas1ActivationValue && GAS_2_Value < Gas2ActivationValue && temp_Value < TempActivationValue && hum_Value < HumActivationValue) {
  254. digitalWrite(redLED_PIN, 0);
  255. digitalWrite(greenLED_PIN, 1);
  256. digitalWrite(buzzer1_PIN, 0);
  257. }
  258. if (hum_Value > HumActivationValue) {
  259. digitalWrite(redLED_PIN, 1);
  260. digitalWrite(greenLED_PIN, 0);
  261. if (old_hum != hum_Value) {
  262. count = 0;
  263. }
  264. if (count < 5) {
  265. old_hum = hum_Value;
  266. count = count + 1;
  267. bot.sendMessage(CHAT_ID, "HIGH Humidity detected " + String(hum_Value));
  268. digitalWrite(buzzer1_PIN, 1);
  269. }
  270. else {
  271. digitalWrite(buzzer1_PIN, 0);
  272. }
  273. }
  274. else if (GAS_1_Value < Gas1ActivationValue && GAS_2_Value < Gas2ActivationValue && temp_Value < TempActivationValue && hum_Value < HumActivationValue) {
  275. digitalWrite(redLED_PIN, 0);
  276. digitalWrite(greenLED_PIN, 1);
  277. digitalWrite(buzzer1_PIN, 0);
  278. }
  279. delay(50);
  280.  
  281. }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement