benjaminvr

Untitled

May 18th, 2024
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. //Uses Universal Telegram Bot library and Thread controller library
  2.  
  3.  
  4. #include <UniversalTelegramBot.h>
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiClientSecure.h>
  7. #include <Thread.h>
  8. #include <ThreadController.h>
  9.  
  10. ThreadController controll = ThreadController();
  11.  
  12. //My Thread (as a pointer)
  13. Thread* myThread = new Thread();
  14. //His Thread (not pointer)
  15. Thread hisThread = Thread();
  16.  
  17. char ssid[] = "";
  18. char password[] = "";
  19. #define BOT_TOKEN ""
  20. #define CHAT_ID ""
  21. WiFiClientSecure client;
  22. UniversalTelegramBot bot(BOT_TOKEN, client);
  23. String ipAddress = "";
  24. int detectionValue = 0;
  25. int alarmStatus = 0;
  26. int activeMonitor = 0;
  27. const int readingInterval = 10; //ms
  28.  
  29. void sendTelegramMessage() {
  30. String message = "SSID: ";
  31. message.concat(ssid);
  32. message.concat("\n");
  33. message.concat("Motion detected - START TIMER");
  34. message.concat("\n");
  35. message.concat("New commands will be executed after 20 seconds");
  36. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  37. Serial.println("TELEGRAM Successfully sent");
  38. }
  39. }
  40.  
  41. void sendTelegramMessage2() {
  42. String message = "SSID: ";
  43. message.concat(ssid);
  44. message.concat("\n");
  45. message.concat("END TIMER - Alarm activated");
  46. message.concat("\n");
  47. message.concat("Disable alarm with /alarmoff");
  48. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  49. Serial.println("TELEGRAM Successfully sent");
  50. }
  51. }
  52.  
  53. void sendTelegramMessage3() {
  54. String message = "SSID: ";
  55. message.concat(ssid);
  56. message.concat("\n");
  57. message.concat("Motion detected - ALARM ALREADY ON");
  58. message.concat("\n");
  59. message.concat("No action will be taken");
  60. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  61. Serial.println("TELEGRAM Successfully sent");
  62. }
  63. }
  64.  
  65. void sendTelegramMessage4() {
  66. String message = "Alarm disabled before execution";
  67. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  68. Serial.println("TELEGRAM Successfully sent");
  69. }
  70. }
  71.  
  72. void sendTelegramMessage5() {
  73. String message = "The system has rebooted. Please reactivate your alarm.";
  74. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  75. Serial.println("TELEGRAM Successfully sent");
  76. }
  77. }
  78.  
  79. void handleNewMessages(int numNewMessages) {
  80. Serial.println("handleNewMessages");
  81. Serial.println(String(numNewMessages));
  82.  
  83. for (int i=0; i<numNewMessages; i++) {
  84. String chat_id = String(bot.messages[i].chat_id);
  85. String text = bot.messages[i].text;
  86.  
  87. String from_name = bot.messages[i].from_name;
  88. if (from_name == "") from_name = "Guest";
  89.  
  90. if (text == "/alarmon") {
  91. alarmStatus = 1;
  92. bot.sendMessage(chat_id, "Alarm sound has been activated", "");
  93. digitalWrite(13, HIGH);
  94. }
  95.  
  96. if (text == "/activate") {
  97. activeMonitor = 1;
  98. bot.sendMessage(chat_id, "Security watchdog activated", "");
  99. }
  100.  
  101. if (text == "/deactivate") {
  102. activeMonitor = 0;
  103. bot.sendMessage(chat_id, "Security watchdog deactivated", "");
  104. }
  105.  
  106. if (text == "/alarmoff") {
  107. alarmStatus = 0;
  108. digitalWrite(13, LOW);
  109. bot.sendMessage(chat_id, "Alarm sound has been deactivated", "");
  110. }
  111.  
  112. if (text == "/status") {
  113. if(alarmStatus){
  114. bot.sendMessage(chat_id, "Alarm sound is ON", "");
  115. } else {
  116. bot.sendMessage(chat_id, "Alarm sound is OFF", "");
  117. }
  118. }
  119.  
  120. if (text == "/start") {
  121. String welcome = "Welcome to SecuriBot, \n \n";
  122. welcome += "/alarmon : to switch the alarm sound ON\n";
  123. welcome += "/alarmoff : to switch the alarm sound OFF\n";
  124. welcome += "/status : Returns current status of the alarm\n";
  125. welcome += "/activate : Activate the security watchdog\n";
  126. welcome += "/deactivate : Deactivate the security watchdog\n";
  127. bot.sendMessage(chat_id, welcome, "Markdown");
  128. }
  129. }
  130. }
  131.  
  132. void checkSensors(){
  133. if (activeMonitor == 1) {
  134. if (1 == digitalRead(12) && alarmStatus == 0) {
  135. sendTelegramMessage();
  136. alarmStatus = 1;
  137. delay(20000);
  138. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  139. while(numNewMessages) {
  140. Serial.println("got response");
  141. handleNewMessages(numNewMessages);
  142. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  143. }
  144. delay(500);
  145. if (alarmStatus == 1) {
  146. sendTelegramMessage2();
  147. digitalWrite(13, HIGH);
  148. } else {
  149. sendTelegramMessage4();
  150. }
  151. }
  152.  
  153. if (1 == digitalRead(12) && alarmStatus == 1) {
  154. sendTelegramMessage3();
  155. }
  156. } else {
  157. }
  158. }
  159.  
  160. void checkMessages (){
  161. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  162. while(numNewMessages) {
  163. Serial.println("got response");
  164. handleNewMessages(numNewMessages);
  165. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  166. }
  167. }
  168.  
  169.  
  170. void setup(){
  171. Serial.begin(38400);
  172. pinMode(12, INPUT_PULLUP); //d6
  173. pinMode(13, OUTPUT); //d7
  174. WiFi.mode(WIFI_STA);
  175. WiFi.disconnect();
  176. delay(100);
  177.  
  178. // attempt to connect to Wifi network:
  179. Serial.print("Connecting Wifi: ");
  180. Serial.println(ssid);
  181. WiFi.begin(ssid, password);
  182.  
  183. while (WiFi.status() != WL_CONNECTED) {
  184. Serial.print(".");
  185. delay(500);
  186. }
  187.  
  188. Serial.println("");
  189. Serial.println("WiFi connected");
  190. Serial.print("IP address: ");
  191. Serial.println(WiFi.localIP());
  192.  
  193. delay(10);
  194.  
  195. sendTelegramMessage5();
  196.  
  197. delay(50);
  198.  
  199. myThread->onRun(checkSensors);
  200. myThread->setInterval(5);
  201.  
  202. hisThread.onRun(checkMessages);
  203. hisThread.setInterval(250);
  204.  
  205. controll.add(myThread);
  206. controll.add(&hisThread); // & to pass the pointer to it
  207. }
  208.  
  209.  
  210.  
  211. void loop(){
  212. controll.run();
  213. }
Advertisement
Add Comment
Please, Sign In to add comment