Advertisement
safwan092

Project_11222_Code

Apr 27th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 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. #define GAS_1_PIN 34 // ✓
  10. #define dht11_PIN 4 // ✓
  11. #define redLED_PIN 18 // ✓
  12. #define buzzer_PIN 22 // ✓
  13.  
  14. DHT dht(dht11_PIN, DHT11);
  15.  
  16. int old_hum = 0;
  17. int count = 0;
  18.  
  19. int GAS_1_Value;
  20. float temp_Value;
  21. float hum_Value;
  22.  
  23. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  24. unsigned long bot_lasttime; // last time messages' scan has been done
  25.  
  26. // Wifi network station credentials
  27. #define WIFI_SSID "network"
  28. #define WIFI_PASSWORD "123456789"
  29. #define BOT_TOKEN "5952106242:AAFsYvq64G7MoxITtXxdN7VTFt8B7rEHOZY"
  30. #define CHAT_ID "473975732"
  31.  
  32. WiFiClientSecure secured_client;
  33. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  34.  
  35. //////////////////////////////////////////////////////////
  36.  
  37. void handleNewMessages(int numNewMessages)
  38. {
  39. Serial.println("handleNewMessages");
  40. Serial.println(String(numNewMessages));
  41.  
  42. for (int i = 0; i < numNewMessages; i++)
  43. {
  44. String chat_id = bot.messages[i].chat_id;
  45. String text = bot.messages[i].text;
  46.  
  47. String from_name = bot.messages[i].from_name;
  48. if (from_name == "")
  49. from_name = "Guest";
  50.  
  51. if (text == "/send_test_action")
  52. {
  53. bot.sendChatAction(chat_id, "typing");
  54. delay(4000);
  55. bot.sendMessage(chat_id, "Did you see the action message?");
  56. }
  57.  
  58. if (text == "/Sensor_Data") {
  59. int value1 = checkGAS_1();
  60. float value3 = checkTemp();
  61. float value4 = checkHum();
  62. bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
  63. bot.sendMessage(chat_id, "Temprature reading is : " + String(value3));
  64. bot.sendMessage(chat_id, "Humidity reading is : " + String(value4));
  65. }
  66.  
  67. if (text == "/options") {
  68. String keyboardJson = "[[\"/Sensor_Data\"]]";
  69. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  70. }
  71.  
  72. if (text == "/start")
  73. {
  74. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  75. welcome += "This is Chat Action Bot example.\n\n";
  76. welcome += "/Sensor_Data : to return the current sensors data\n";
  77. welcome += "/options : returns a custom reply keyboard\n";
  78. bot.sendMessage(chat_id, welcome);
  79. }
  80. }
  81. }
  82. //////////////////////////////////////////////////////////
  83.  
  84.  
  85.  
  86. void setup() {
  87. Serial.begin(115200);
  88. Serial.println();
  89.  
  90. pinMode(GAS_1_PIN, INPUT);
  91. dht.begin();
  92. pinMode(redLED_PIN, OUTPUT);
  93. pinMode(buzzer_PIN, OUTPUT);
  94.  
  95. digitalWrite(redLED_PIN, 0);
  96. digitalWrite(buzzer_PIN, 0);
  97.  
  98. // attempt to connect to Wifi network:
  99. Serial.print("Connecting to Wifi SSID ");
  100. Serial.print(WIFI_SSID);
  101. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  102. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  103. while (WiFi.status() != WL_CONNECTED)
  104. {
  105. Serial.print(".");
  106. delay(500);
  107. }
  108. Serial.print("\nWiFi connected. IP address: ");
  109. Serial.println(WiFi.localIP());
  110.  
  111. Serial.print("Retrieving time: ");
  112. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  113. time_t now = time(nullptr);
  114. while (now < 24 * 3600)
  115. {
  116. Serial.print(".");
  117. delay(100);
  118. now = time(nullptr);
  119. }
  120. Serial.println(now);
  121.  
  122. bot.sendMessage(CHAT_ID, "Bot started up", "");
  123. }
  124.  
  125. int checkGAS_1() {
  126. int value1 = analogRead(GAS_1_PIN);
  127. Serial.print("GAS[1] value: ");
  128. Serial.println(value1);
  129. return value1;
  130. }
  131. float checkTemp() {
  132. float value3 = dht.readTemperature();
  133. Serial.print("temprature value: ");
  134. Serial.println(value3);
  135. return value3;
  136. }
  137. float checkHum() {
  138. float value4 = dht.readHumidity();
  139. Serial.print("humidity value: ");
  140. Serial.println(value4);
  141. return value4;
  142. }
  143.  
  144. void loop() {
  145.  
  146. ///////////////////////////////////////////
  147. if (millis() - bot_lasttime > BOT_MTBS)
  148. {
  149. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  150.  
  151. while (numNewMessages)
  152. {
  153. Serial.println("got response");
  154. handleNewMessages(numNewMessages);
  155. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  156. }
  157.  
  158. bot_lasttime = millis();
  159. }
  160. ///////////////////////////////////////////
  161. delay(50);
  162. GAS_1_Value = checkGAS_1();
  163. if (GAS_1_Value > 1500) {
  164. bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
  165. alert_ON();
  166. }
  167. else if (GAS_1_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  168. alert_OFF();
  169. }
  170. delay(50);
  171. temp_Value = checkTemp();
  172. hum_Value = checkHum();
  173. if (temp_Value > 30) {
  174. bot.sendMessage(CHAT_ID, "HIGH Tempreture detected " + String(temp_Value));
  175. alert_ON();
  176. }
  177. else if (GAS_1_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  178. alert_OFF();
  179. }
  180. if (hum_Value > 93) {
  181. alert_ON();
  182. if (old_hum != hum_Value) {
  183. count = 0;
  184. }
  185. if (count < 5) {
  186. old_hum = hum_Value;
  187. count = count + 1;
  188. bot.sendMessage(CHAT_ID, "HIGH Humidity detected " + String(hum_Value));
  189. alert_ON();
  190. }
  191. else {
  192. alert_OFF();
  193. }
  194. }
  195. else if (GAS_1_Value < 1500 && temp_Value < 30 && hum_Value < 93) {
  196. alert_OFF();
  197. }
  198. delay(50);
  199.  
  200. }//end of Loop
  201.  
  202. void alert_ON() {
  203. digitalWrite(redLED_PIN, 1);
  204. digitalWrite(buzzer_PIN, 1);
  205. }
  206. void alert_OFF() {
  207. digitalWrite(redLED_PIN, 0);
  208. digitalWrite(buzzer_PIN, 0);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement