safwan092

Project_9876_Final_code_with_Names

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