Advertisement
safwan092

Untitled

Nov 19th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 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. int flag = 0;
  7. int flag2 = 0;
  8. // -------- Define Pins -----------
  9.  
  10. #define GAS_1_PIN 34 // ✓
  11. #define redLED_PIN 26 // ✓
  12. #define buzzer1_PIN 21 // ✓
  13.  
  14. int count = 0;
  15.  
  16. int GAS_1_Value;
  17.  
  18. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  19. unsigned long bot_lasttime; // last time messages' scan has been done
  20.  
  21. // Wifi network station credentials
  22. #define WIFI_SSID "network"
  23. #define WIFI_PASSWORD "123456789"
  24. // Telegram BOT Token (Get from Botfather)
  25. #define BOT_TOKEN "6743180949:AAFE4lN6QQsvEY9U33Pv2a-ta45f-1iOrb4"
  26.  
  27. // Use @myidbot (IDBot) to find out the chat ID of an individual or a group
  28. // Also note that you need to click "start" on a bot before it can
  29. // message you
  30. #define CHAT_ID "793949283"
  31. #define CHAT_ID1 "6458727643"
  32.  
  33.  
  34. WiFiClientSecure secured_client;
  35. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  36.  
  37. //////////////////////////////////////////////////////////
  38.  
  39. void handleNewMessages(int numNewMessages)
  40. {
  41. Serial.println("handleNewMessages");
  42. Serial.println(String(numNewMessages));
  43.  
  44. for (int i = 0; i < numNewMessages; i++)
  45. {
  46. String chat_id = bot.messages[i].chat_id;
  47. String text = bot.messages[i].text;
  48.  
  49. String from_name = bot.messages[i].from_name;
  50. if (from_name == "")
  51. from_name = "Guest";
  52.  
  53. if (text == "/send_test_action")
  54. {
  55. bot.sendChatAction(chat_id, "typing");
  56. delay(4000);
  57. bot.sendMessage(chat_id, "Did you see the action message?");
  58. }
  59.  
  60. if (text == "/Sensor_Data") {
  61. int value1 = checkGAS_1();
  62. bot.sendMessage(chat_id, "GAS[1] reading is : " + String(value1));
  63. }
  64.  
  65. if (text == "/start")
  66. {
  67. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  68. welcome += "This is Chat Action Bot example.\n\n";
  69. welcome += "/Sensor_Data : to return the current sensors data\n";
  70. bot.sendMessage(chat_id, welcome);
  71. }
  72. }
  73. }
  74. //////////////////////////////////////////////////////////
  75.  
  76.  
  77.  
  78. void setup() {
  79. Serial.begin(115200);
  80. Serial.println();
  81.  
  82. pinMode(GAS_1_PIN, INPUT);
  83. pinMode(redLED_PIN, OUTPUT);
  84. pinMode(buzzer1_PIN, OUTPUT);
  85.  
  86.  
  87. // attempt to connect to Wifi network:
  88. Serial.print("Connecting to Wifi SSID ");
  89. Serial.print(WIFI_SSID);
  90. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  91. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  92. while (WiFi.status() != WL_CONNECTED)
  93. {
  94. Serial.print(".");
  95. delay(500);
  96. }
  97. Serial.print("\nWiFi connected. IP address: ");
  98. Serial.println(WiFi.localIP());
  99.  
  100. Serial.print("Retrieving time: ");
  101. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  102. time_t now = time(nullptr);
  103. while (now < 24 * 3600)
  104. {
  105. Serial.print(".");
  106. delay(100);
  107. now = time(nullptr);
  108. }
  109. Serial.println(now);
  110.  
  111. bot.sendMessage(CHAT_ID, "Bot started up", "");
  112. delay(20000);
  113. Serial.println("delay finished");
  114. }
  115.  
  116. int checkGAS_1() {
  117. int value1 = analogRead(GAS_1_PIN);
  118. Serial.print("GAS[1] value: ");
  119. Serial.println(value1);
  120. return value1;
  121. }
  122.  
  123.  
  124. void loop() {
  125.  
  126. ///////////////////////////////////////////
  127. if (millis() - bot_lasttime > BOT_MTBS)
  128. {
  129. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  130.  
  131. while (numNewMessages)
  132. {
  133. Serial.println("got response");
  134. handleNewMessages(numNewMessages);
  135. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  136. }
  137.  
  138. bot_lasttime = millis();
  139. }
  140. ///////////////////////////////////////////
  141. GAS_1_Value = checkGAS_1();
  142. if (GAS_1_Value > 900) {
  143. digitalWrite(redLED_PIN, 1);
  144. digitalWrite(buzzer1_PIN, 1);
  145. bot.sendMessage(CHAT_ID, "GAS[1] detected " + String(GAS_1_Value));
  146. bot.sendMessage(CHAT_ID1, "GAS[1] detected " + String(GAS_1_Value));
  147. }
  148. else if (GAS_1_Value < 900)
  149. {
  150. digitalWrite(redLED_PIN, 0);
  151. digitalWrite(buzzer1_PIN, 0);
  152. }
  153.  
  154. delay(50);
  155.  
  156. }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement