safwan092

Project_9876_Final_code

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