safwan092

project_9876_Telegram

Sep 29th, 2022 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.89 KB | None | 0 0
  1. #include <Adafruit_Fingerprint.h>
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6. #define mySerial Serial2
  7. #define RXD2 16
  8. #define TXD2 17
  9. #define btn 4
  10. #define redLED 2
  11.  
  12.  
  13. #define WIFI_SSID "network"
  14. #define WIFI_PASSWORD "123456789"
  15. // Telegram BOT Token (Get from Botfather)
  16. #define BOT_TOKEN "5781435319:AAGaO5tSUXT2BrNy-RoYMTPtWjQ7vXac-lw"
  17. // @Bus_project_bot
  18.  
  19. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  20. WiFiClientSecure secured_client;
  21. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  22. unsigned long bot_lasttime; // last time messages' scan has been done
  23. bool Start = false;
  24.  
  25. String text;
  26. String chat_id;
  27. int totalCountCalculated = 0;
  28. const int numberOfStudents = 4;
  29. int totalCount = numberOfStudents;
  30. int StudentNumber[numberOfStudents] = {0}; // 1 2 3 4
  31. int ParentNumber[numberOfStudents] = {0}; // 1 2 3 4
  32.  
  33. //bot ID: @bus_project_1_bot
  34. //myIDbot: @myidbot
  35. /*
  36. Title Telegram ID Phone Number Telegram Pickup Code
  37. Driver 1009962795 N/A N/A
  38. Parent 1 1053936060 N/A /child1pickedup63518926
  39. Parent 2 1462597510 N/A /child2pickedup73246642
  40. Parent 3 744026825 N/A /child3pickedup95646081
  41. Parent 4 735084516 N/A /child4pickedup11562278
  42. */
  43. /* OLD Data xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  44. Title Telegram ID Phone Number Telegram Pickup Code
  45. Driver 473975732 05413XXXXX N/A
  46. Parent 1 5461258962 05823XXXXX /child1pickedup63518926
  47. Parent 2 178427444 05555XXXXX /child2pickedup73246642
  48. Parent 3 985890015 05544XXXXX /child3pickedup95646081
  49. Parent 4 5704514498 05438XXXXX /child4pickedup11562278
  50. OLD Data xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
  51. String driver_chat_id = "1009962795";
  52. String Parent_1_ID = "1053936060";
  53. String Parent_2_ID = "1462597510";
  54. String Parent_3_ID = "744026825";
  55. String Parent_4_ID = "735084516";
  56. String Child_1_Pick_Up_Key= "/child1pickedup63518926";
  57. String Child_2_Pick_Up_Key= "/child2pickedup73246642";
  58. String Child_3_Pick_Up_Key= "/child3pickedup95646081";
  59. String Child_4_Pick_Up_Key= "/child4pickedup11562278";
  60. //**************************************************************
  61. String ParentTelegramID[numberOfStudents] = {Parent_1_ID,
  62. Parent_2_ID,
  63. Parent_3_ID,
  64. Parent_4_ID
  65. }; //Must Set!!!!!!
  66. //**************************************************************
  67.  
  68.  
  69. bool parent_1_flag = 0;
  70. bool parent_2_flag = 0;
  71. bool parent_3_flag = 0;
  72. bool parent_4_flag = 0;
  73.  
  74. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  75.  
  76. void setup()
  77. {
  78. Serial.begin(9600);
  79. pinMode(btn, INPUT);
  80. //pinMode(redLED, OUTPUT);
  81. //digitalWrite(redLED, 0);
  82. initWiFi_and_Time_For_Telegram();
  83. initFingerPrint();
  84. resetData();
  85. bot.sendMessage(driver_chat_id, "Bus ONLINE");
  86. }
  87.  
  88. void loop() {
  89. /////////////////////////////////////////////////////////////////
  90. //Handle Messages from Parents Telegram
  91. handel_Messages_Loop();
  92. //Parent of Child #1
  93. //ParentNumber[0]=1
  94. //totalCount = totalCount - ParentNumber[0]
  95.  
  96. //Parent of Child #2
  97. //ParentNumber[1]=1
  98. //totalCount = totalCount - ParentNumber[1]
  99.  
  100. //Parent of Child #3
  101. //ParentNumber[2]=1
  102. //totalCount = totalCount - ParentNumber[2]
  103.  
  104. //Parent of Child #4
  105. //ParentNumber[3]=1
  106. //totalCount = totalCount - ParentNumber[3]
  107. /////////////////////////////////////////////////////////////////
  108.  
  109. if (digitalRead(btn) == 1) {
  110. Serial.println("Button Pressed!!");
  111. flashLED(1);
  112. bot.sendMessage(driver_chat_id, "Bus MOVING -->");
  113. if (totalCountCalculated != totalCount) {
  114. flashLED(10);
  115. Serial.println("totalCountCalculated != totalCount");
  116. //Send Alert To Driver
  117. bot.sendMessage(driver_chat_id, "Alert Not all Students on Board !!!!");
  118. for (int i = 0; i < numberOfStudents; i++) {
  119. if (ParentNumber[i] == 0 && StudentNumber[i] == 0) {
  120. String message = "Child #" + String(i + 1) + " did not enter the bus Alert!! and Parents did not pick up" + "\n";
  121. bot.sendMessage(driver_chat_id, message);
  122. bot.sendMessage(ParentTelegramID[(i)], "Your Child did not enter the bus Alert!!! [ Problem ]");
  123.  
  124. //Send Message to Parent of Child #(i+1) Child did not enter the bus Alert!!
  125. //Send Message to Driver Child #(i+1) did not enter the bus Alert!! and Parents did not pick up
  126. }
  127. }
  128. }
  129. else {
  130. for (int i = 0; i < numberOfStudents; i++) {
  131. if (ParentNumber[i] == 0 && StudentNumber[i] == 1) {
  132. bot.sendMessage(ParentTelegramID[(i)], "Your Child is IN the Bus! [ Moving--> ]");
  133. //flashLED(1);
  134. }
  135. }
  136. }
  137. }
  138. getFingerprintID();
  139. /*
  140. if (getFingerprintIDez() != -1) {
  141.  
  142. /////////////////////////////////////////////////
  143. if (StudentNumber[getFingerprintIDez() - 1] == 1) {
  144. StudentNumber[getFingerprintIDez() - 1] = 0;
  145. totalCountCalculated = totalCountCalculated - 1;
  146. printStatus();
  147. //Send Message to Parent Child is OUT of the Bus
  148. //Send Message to Driver totalCountCalculated/TotalCount
  149. }
  150. else {
  151. StudentNumber[getFingerprintIDez() - 1] = 1;
  152. totalCountCalculated = totalCountCalculated + 1;
  153. printStatus();
  154. //Send Message to Parent Child is IN the Bus
  155. //Send Message to Driver Child is IN the Bus
  156. //Send Message to Driver totalCountCalculated/TotalCount
  157. }
  158. /////////////////////////////////////////////////
  159.  
  160. }
  161. */
  162. delay(100);
  163. }
  164.  
  165.  
  166.  
  167. uint8_t getFingerprintID() {
  168. uint8_t p = finger.getImage();
  169. switch (p) {
  170. case FINGERPRINT_OK:
  171. Serial.println("Image taken");
  172. break;
  173. case FINGERPRINT_NOFINGER:
  174. Serial.println("No finger detected");
  175. return p;
  176. case FINGERPRINT_PACKETRECIEVEERR:
  177. Serial.println("Communication error");
  178. return p;
  179. case FINGERPRINT_IMAGEFAIL:
  180. Serial.println("Imaging error");
  181. return p;
  182. default:
  183. Serial.println("Unknown error");
  184. return p;
  185. }
  186.  
  187. // OK success!
  188.  
  189. p = finger.image2Tz();
  190. switch (p) {
  191. case FINGERPRINT_OK:
  192. Serial.println("Image converted");
  193. break;
  194. case FINGERPRINT_IMAGEMESS:
  195. Serial.println("Image too messy");
  196. return p;
  197. case FINGERPRINT_PACKETRECIEVEERR:
  198. Serial.println("Communication error");
  199. return p;
  200. case FINGERPRINT_FEATUREFAIL:
  201. Serial.println("Could not find fingerprint features");
  202. return p;
  203. case FINGERPRINT_INVALIDIMAGE:
  204. Serial.println("Could not find fingerprint features");
  205. return p;
  206. default:
  207. Serial.println("Unknown error");
  208. return p;
  209. }
  210.  
  211. // OK converted!
  212. p = finger.fingerSearch();
  213. if (p == FINGERPRINT_OK) {
  214. Serial.println("Found a print match!");
  215. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  216. Serial.println("Communication error");
  217. return p;
  218. } else if (p == FINGERPRINT_NOTFOUND) {
  219. Serial.println("Did not find a match");
  220. return p;
  221. } else {
  222. Serial.println("Unknown error");
  223. return p;
  224. }
  225. /////////////////////////////////////////////////
  226. if (StudentNumber[(finger.fingerID - 1)] == 1) {
  227. StudentNumber[(finger.fingerID - 1)] = 0;
  228. totalCountCalculated = totalCountCalculated - 1;
  229. printStatus();
  230. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  231. delay(500);
  232. bot.sendMessage(driver_chat_id, message);
  233. delay(500);
  234. bot.sendMessage(ParentTelegramID[(finger.fingerID - 1)], "Your Child is OUT of the Bus! [ Delivered ]");
  235. // flashLED(1);
  236. //Send Message to Parent Child is OUT of the Bus
  237. //Send Message to Driver totalCountCalculated/totalCount
  238. }
  239. else {
  240. StudentNumber[(finger.fingerID - 1)] = 1;
  241. totalCountCalculated = totalCountCalculated + 1;
  242. printStatus();
  243. String message = "Child #" + String(finger.fingerID) + " is IN the Bus \n" + "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  244. delay(500);
  245. bot.sendMessage(driver_chat_id, message);
  246. delay(500);
  247. bot.sendMessage(ParentTelegramID[(finger.fingerID - 1)], "Your Child is IN the Bus! [ On the Bus ]");
  248. //flashLED(1);
  249. //Send Message to Parent Child is IN the Bus
  250. //Send Message to Driver Child is IN the Bus
  251. //Send Message to Driver totalCountCalculated/totalCount
  252. }
  253. /////////////////////////////////////////////////
  254. // found a match!
  255. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  256. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  257. //flashLED(1);
  258. return finger.fingerID;
  259. }
  260.  
  261. // returns -1 if failed, otherwise returns ID #
  262. int getFingerprintIDez() {
  263. uint8_t p = finger.getImage();
  264. if (p != FINGERPRINT_OK) return -1;
  265.  
  266. p = finger.image2Tz();
  267. if (p != FINGERPRINT_OK) return -1;
  268.  
  269. p = finger.fingerFastSearch();
  270. if (p != FINGERPRINT_OK) return -1;
  271.  
  272. // found a match!
  273. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  274. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  275. return finger.fingerID;
  276. }
  277.  
  278. void initFingerPrint() {
  279. Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);
  280. while (!Serial);
  281. delay(100);
  282. Serial.println("\n\nAdafruit finger detect test");
  283. delay(5);
  284. if (finger.verifyPassword()) {
  285. Serial.println("Found fingerprint sensor!");
  286. } else {
  287. Serial.println("Did not find fingerprint sensor :(");
  288. while (1) {
  289. delay(1);
  290. }
  291. }
  292.  
  293. Serial.println(F("Reading sensor parameters"));
  294. finger.getParameters();
  295. Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  296. Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  297. Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  298. Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  299. Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  300. Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  301. Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
  302.  
  303. finger.getTemplateCount();
  304.  
  305. if (finger.templateCount == 0) {
  306. Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  307. }
  308. else {
  309. Serial.println("Waiting for valid finger...");
  310. Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  311. }
  312. }
  313.  
  314. void resetData() {
  315. StudentNumber[numberOfStudents] = 0;
  316. ParentNumber[numberOfStudents] = 0;
  317. totalCount = numberOfStudents;
  318. totalCountCalculated = 0;
  319. parent_1_flag = 0;
  320. parent_2_flag = 0;
  321. parent_3_flag = 0;
  322. parent_4_flag = 0;
  323. }
  324.  
  325. void handel_Messages_Loop() {
  326. if (millis() - bot_lasttime > BOT_MTBS)
  327. {
  328. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  329.  
  330. while (numNewMessages)
  331. {
  332. Serial.println("got response");
  333. handleNewMessages(numNewMessages);
  334. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  335. }
  336.  
  337. bot_lasttime = millis();
  338. }
  339. }
  340.  
  341. void initWiFi_and_Time_For_Telegram() {
  342. Serial.println();
  343.  
  344. // attempt to connect to Wifi network:
  345. Serial.print("Connecting to Wifi SSID ");
  346. Serial.print(WIFI_SSID);
  347. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  348. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  349. while (WiFi.status() != WL_CONNECTED)
  350. {
  351. Serial.print(".");
  352. delay(500);
  353. }
  354. Serial.print("\nWiFi connected. IP address: ");
  355. Serial.println(WiFi.localIP());
  356.  
  357. Serial.print("Retrieving time: ");
  358. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  359. time_t now = time(nullptr);
  360. while (now < 24 * 3600)
  361. {
  362. Serial.print(".");
  363. delay(100);
  364. now = time(nullptr);
  365. }
  366. Serial.println(now);
  367.  
  368. }
  369.  
  370. void handleNewMessages(int numNewMessages)
  371. {
  372. Serial.println("handleNewMessages");
  373. Serial.println(String(numNewMessages));
  374.  
  375. for (int i = 0; i < numNewMessages; i++)
  376. {
  377. chat_id = bot.messages[i].chat_id;
  378. text = bot.messages[i].text;
  379.  
  380. String from_name = bot.messages[i].from_name;
  381. if (from_name == "")
  382. from_name = "Guest";
  383.  
  384. if (text == "/send_test_action")
  385. {
  386. bot.sendChatAction(chat_id, "typing");
  387. delay(4000);
  388. bot.sendMessage(chat_id, "Did you see the action message?");
  389.  
  390. // You can't use own message, just choose from one of bellow
  391.  
  392. //typing for text messages
  393. //upload_photo for photos
  394. //record_video or upload_video for videos
  395. //record_audio or upload_audio for audio files
  396. //upload_document for general files
  397. //find_location for location data
  398.  
  399. //more info here - https://core.telegram.org/bots/api#sendchataction
  400. }
  401. /////////////////////////////////////
  402. c1p1();
  403. c2p2();
  404. c3p3();
  405. c4p4();
  406. /////////////////////////////////////
  407. if (text == "/start")
  408. {
  409. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  410. welcome += "This is Chat Action Bot example.\n\n";
  411. welcome += "/send_test_action : to send test chat action message\n";
  412. bot.sendMessage(chat_id, welcome);
  413. }
  414. }
  415. }
  416.  
  417. void printStatus() {
  418. Serial.print("Student Array: \t\t");
  419. Serial.print(StudentNumber[0]); Serial.print(" , ");
  420. Serial.print(StudentNumber[1]); Serial.print(" , ");
  421. Serial.print(StudentNumber[2]); Serial.print(" , ");
  422. Serial.println(StudentNumber[3]);
  423.  
  424. Serial.print("Parent Array: \t\t");
  425. Serial.print(ParentNumber[0]); Serial.print(" , ");
  426. Serial.print(ParentNumber[1]); Serial.print(" , ");
  427. Serial.print(ParentNumber[2]); Serial.print(" , ");
  428. Serial.println(ParentNumber[3]);
  429.  
  430. Serial.print("Total Calculated Count:"); Serial.println(totalCountCalculated);
  431. Serial.print("Total Count:"); Serial.println(totalCount);
  432. }
  433.  
  434. void flashLED(int n) {
  435. for (int i = 0; i < n; i++) {
  436. //digitalWrite(redLED, 1);
  437. //(100);
  438. //digitalWrite(redLED, 0);
  439. //delay(100);
  440. }
  441. }
  442.  
  443. void c1p1(){
  444. if (text == Child_1_Pick_Up_Key)
  445. {
  446. if (parent_1_flag == 0) {
  447. parent_1_flag = 1;
  448. delay(500);
  449. bot.sendMessage(chat_id, "Child (1) picked up by Parent. Driver will be notified");
  450. delay(500);
  451. bot.sendMessage(driver_chat_id, "Child (1) picked up by Parent.");
  452. ParentNumber[0] = 1;
  453. totalCount = totalCount - ParentNumber[0];
  454. printStatus();
  455. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  456. delay(500);
  457. bot.sendMessage(driver_chat_id, message);
  458. //flashLED(1);
  459. }
  460. else {
  461. delay(500);
  462. bot.sendMessage(chat_id, "Driver notified already, Thank you.");
  463. delay(500);
  464. bot.sendMessage(driver_chat_id, "Child (1) Parent sent message again");
  465. printStatus();
  466. //flashLED(1);
  467. }
  468. }
  469. }
  470. void c2p2(){
  471. if (text == Child_2_Pick_Up_Key)
  472. {
  473. if (parent_2_flag == 0) {
  474. parent_2_flag = 1;
  475. delay(500);
  476. bot.sendMessage(chat_id, "Child (2) picked up by Parent. Driver will be notified");
  477. delay(500);
  478. bot.sendMessage(driver_chat_id, "Child (2) picked up by Parent.");
  479. ParentNumber[1] = 1;
  480. totalCount = totalCount - ParentNumber[1];
  481. printStatus();
  482. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  483. delay(500);
  484. bot.sendMessage(driver_chat_id, message);
  485. //flashLED(1);
  486. }
  487. else {
  488. delay(500);
  489. bot.sendMessage(chat_id, "Driver notified already, Thank you.");
  490. delay(500);
  491. bot.sendMessage(driver_chat_id, "Child (2) Parent sent message again");
  492. printStatus();
  493. //flashLED(1);
  494. }
  495. }
  496. }
  497. void c3p3(){
  498. if (text == Child_3_Pick_Up_Key)
  499. {
  500. if (parent_3_flag == 0) {
  501. parent_3_flag = 1;
  502. delay(500);
  503. bot.sendMessage(chat_id, "Child (3) picked up by Parent. Driver will be notified");
  504. delay(500);
  505. bot.sendMessage(driver_chat_id, "Child (3) picked up by Parent.");
  506. ParentNumber[2] = 1;
  507. totalCount = totalCount - ParentNumber[2];
  508. printStatus();
  509. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  510. delay(500);
  511. bot.sendMessage(driver_chat_id, message);
  512. //flashLED(1);
  513. }
  514. else {
  515. delay(500);
  516. bot.sendMessage(chat_id, "Driver notified already, Thank you.");
  517. delay(500);
  518. bot.sendMessage(driver_chat_id, "Child (3) Parent sent message again");
  519. printStatus();
  520. //flashLED(1);
  521. }
  522. }
  523. }
  524. void c4p4(){
  525. if (text == Child_4_Pick_Up_Key)
  526. {
  527. if (parent_4_flag == 0) {
  528. parent_4_flag = 1;
  529. delay(500);
  530. bot.sendMessage(chat_id, "Child (4) picked up by Parent. Driver will be notified");
  531. delay(500);
  532. bot.sendMessage(driver_chat_id, "Child (4) picked up by Parent.");
  533. ParentNumber[3] = 1;
  534. totalCount = totalCount - ParentNumber[3];
  535. printStatus();
  536. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  537. delay(500);
  538. bot.sendMessage(driver_chat_id, message);
  539. //flashLED(1);
  540. }
  541. else {
  542. delay(500);
  543. bot.sendMessage(chat_id, "Driver notified already, Thank you.");
  544. delay(500);
  545. bot.sendMessage(driver_chat_id, "Child (4) Parent sent message again");
  546. printStatus();
  547. //flashLED(1);
  548. }
  549. }
  550. }
Advertisement
Add Comment
Please, Sign In to add comment