safwan092

project_9876_Telegram_test_1

Sep 26th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 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. int totalCountCalculated = 0;
  26. const int numberOfStudents = 4;
  27. int totalCount = numberOfStudents;
  28. int StudentNumber[numberOfStudents] = {0}; // 1 2 3 4
  29. int ParentNumber[numberOfStudents] = {0}; // 1 2 3 4
  30. //**************************************************************
  31. String ParentTelegramID[numberOfStudents] = {"5461258962", "", "", ""}; //Must Set!!!!!!
  32. //**************************************************************
  33. String driver_chat_id = "473975732";
  34.  
  35. bool parent_1_flag = 0;
  36. bool parent_2_flag = 0;
  37. bool parent_3_flag = 0;
  38. bool parent_4_flag = 0;
  39.  
  40. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  41.  
  42. void setup()
  43. {
  44. Serial.begin(9600);
  45. pinMode(btn, INPUT);
  46. //pinMode(redLED, OUTPUT);
  47. //digitalWrite(redLED, 0);
  48. initWiFi_and_Time_For_Telegram();
  49. initFingerPrint();
  50. resetData();
  51. bot.sendMessage(driver_chat_id, "Bus ONLINE");
  52. }
  53.  
  54. void loop() {
  55. /////////////////////////////////////////////////////////////////
  56. //Handle Messages from Parents Telegram
  57. handel_Messages_Loop();
  58. //Parent of Child #1
  59. //ParentNumber[0]=1
  60. //totalCount = totalCount - ParentNumber[0]
  61.  
  62. //Parent of Child #2
  63. //ParentNumber[1]=1
  64. //totalCount = totalCount - ParentNumber[1]
  65.  
  66. //Parent of Child #3
  67. //ParentNumber[2]=1
  68. //totalCount = totalCount - ParentNumber[2]
  69.  
  70. //Parent of Child #4
  71. //ParentNumber[3]=1
  72. //totalCount = totalCount - ParentNumber[3]
  73. /////////////////////////////////////////////////////////////////
  74.  
  75. if (digitalRead(btn) == 1) {
  76. Serial.println("Button Pressed!!");
  77. flashLED(1);
  78. bot.sendMessage(driver_chat_id, "Bus MOVING -->");
  79. if (totalCountCalculated != totalCount) {
  80. flashLED(10);
  81. Serial.println("totalCountCalculated != totalCount");
  82. //Send Alert To Driver
  83. bot.sendMessage(driver_chat_id, "Alert Not all Students on Board !!!!");
  84. for (int i = 0; i < numberOfStudents; i++) {
  85. if (ParentNumber[i] == 0 && StudentNumber[i] == 0) {
  86. String message = "Child #" + String(i + 1) + " did not enter the bus Alert!! and Parents did not pick up" + "\n";
  87. bot.sendMessage(driver_chat_id, message);
  88. bot.sendMessage(ParentTelegramID[(i)], "Your Child did not enter the bus Alert!!! [ Problem ]");
  89.  
  90. //Send Message to Parent of Child #(i+1) Child did not enter the bus Alert!!
  91. //Send Message to Driver Child #(i+1) did not enter the bus Alert!! and Parents did not pick up
  92. }
  93. }
  94. }
  95. else {
  96. for (int i = 0; i < numberOfStudents; i++) {
  97. if (ParentNumber[i] == 0 && StudentNumber[i] == 1) {
  98. bot.sendMessage(ParentTelegramID[(i)], "Your Child is IN the Bus! [ Moving--> ]");
  99. //flashLED(1);
  100. }
  101. }
  102. }
  103. }
  104. getFingerprintID();
  105. /*
  106. if (getFingerprintIDez() != -1) {
  107.  
  108. /////////////////////////////////////////////////
  109. if (StudentNumber[getFingerprintIDez() - 1] == 1) {
  110. StudentNumber[getFingerprintIDez() - 1] = 0;
  111. totalCountCalculated = totalCountCalculated - 1;
  112. printStatus();
  113. //Send Message to Parent Child is OUT of the Bus
  114. //Send Message to Driver totalCountCalculated/TotalCount
  115. }
  116. else {
  117. StudentNumber[getFingerprintIDez() - 1] = 1;
  118. totalCountCalculated = totalCountCalculated + 1;
  119. printStatus();
  120. //Send Message to Parent Child is IN the Bus
  121. //Send Message to Driver Child is IN the Bus
  122. //Send Message to Driver totalCountCalculated/TotalCount
  123. }
  124. /////////////////////////////////////////////////
  125.  
  126. }
  127. */
  128. delay(100);
  129. }
  130.  
  131.  
  132.  
  133. uint8_t getFingerprintID() {
  134. uint8_t p = finger.getImage();
  135. switch (p) {
  136. case FINGERPRINT_OK:
  137. Serial.println("Image taken");
  138. break;
  139. case FINGERPRINT_NOFINGER:
  140. Serial.println("No finger detected");
  141. return p;
  142. case FINGERPRINT_PACKETRECIEVEERR:
  143. Serial.println("Communication error");
  144. return p;
  145. case FINGERPRINT_IMAGEFAIL:
  146. Serial.println("Imaging error");
  147. return p;
  148. default:
  149. Serial.println("Unknown error");
  150. return p;
  151. }
  152.  
  153. // OK success!
  154.  
  155. p = finger.image2Tz();
  156. switch (p) {
  157. case FINGERPRINT_OK:
  158. Serial.println("Image converted");
  159. break;
  160. case FINGERPRINT_IMAGEMESS:
  161. Serial.println("Image too messy");
  162. return p;
  163. case FINGERPRINT_PACKETRECIEVEERR:
  164. Serial.println("Communication error");
  165. return p;
  166. case FINGERPRINT_FEATUREFAIL:
  167. Serial.println("Could not find fingerprint features");
  168. return p;
  169. case FINGERPRINT_INVALIDIMAGE:
  170. Serial.println("Could not find fingerprint features");
  171. return p;
  172. default:
  173. Serial.println("Unknown error");
  174. return p;
  175. }
  176.  
  177. // OK converted!
  178. p = finger.fingerSearch();
  179. if (p == FINGERPRINT_OK) {
  180. Serial.println("Found a print match!");
  181. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  182. Serial.println("Communication error");
  183. return p;
  184. } else if (p == FINGERPRINT_NOTFOUND) {
  185. Serial.println("Did not find a match");
  186. return p;
  187. } else {
  188. Serial.println("Unknown error");
  189. return p;
  190. }
  191. /////////////////////////////////////////////////
  192. if (StudentNumber[(finger.fingerID - 1)] == 1) {
  193. StudentNumber[(finger.fingerID - 1)] = 0;
  194. totalCountCalculated = totalCountCalculated - 1;
  195. printStatus();
  196. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  197. delay(500);
  198. bot.sendMessage(driver_chat_id, message);
  199. delay(500);
  200. bot.sendMessage(ParentTelegramID[(finger.fingerID - 1)], "Your Child is OUT of the Bus! [ Delivered ]");
  201. // flashLED(1);
  202. //Send Message to Parent Child is OUT of the Bus
  203. //Send Message to Driver totalCountCalculated/totalCount
  204. }
  205. else {
  206. StudentNumber[(finger.fingerID - 1)] = 1;
  207. totalCountCalculated = totalCountCalculated + 1;
  208. printStatus();
  209. String message = "Child #" + String(finger.fingerID) + " is IN the Bus \n" + "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  210. delay(500);
  211. bot.sendMessage(driver_chat_id, message);
  212. delay(500);
  213. bot.sendMessage(ParentTelegramID[(finger.fingerID - 1)], "Your Child is IN the Bus! [ On the Bus ]");
  214. //flashLED(1);
  215. //Send Message to Parent Child is IN the Bus
  216. //Send Message to Driver Child is IN the Bus
  217. //Send Message to Driver totalCountCalculated/totalCount
  218. }
  219. /////////////////////////////////////////////////
  220. // found a match!
  221. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  222. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  223. //flashLED(1);
  224. return finger.fingerID;
  225. }
  226.  
  227. // returns -1 if failed, otherwise returns ID #
  228. int getFingerprintIDez() {
  229. uint8_t p = finger.getImage();
  230. if (p != FINGERPRINT_OK) return -1;
  231.  
  232. p = finger.image2Tz();
  233. if (p != FINGERPRINT_OK) return -1;
  234.  
  235. p = finger.fingerFastSearch();
  236. if (p != FINGERPRINT_OK) return -1;
  237.  
  238. // found a match!
  239. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  240. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  241. return finger.fingerID;
  242. }
  243.  
  244. void initFingerPrint() {
  245. Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);
  246. while (!Serial);
  247. delay(100);
  248. Serial.println("\n\nAdafruit finger detect test");
  249. delay(5);
  250. if (finger.verifyPassword()) {
  251. Serial.println("Found fingerprint sensor!");
  252. } else {
  253. Serial.println("Did not find fingerprint sensor :(");
  254. while (1) {
  255. delay(1);
  256. }
  257. }
  258.  
  259. Serial.println(F("Reading sensor parameters"));
  260. finger.getParameters();
  261. Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  262. Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  263. Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  264. Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  265. Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  266. Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  267. Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
  268.  
  269. finger.getTemplateCount();
  270.  
  271. if (finger.templateCount == 0) {
  272. Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  273. }
  274. else {
  275. Serial.println("Waiting for valid finger...");
  276. Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  277. }
  278. }
  279.  
  280. void resetData() {
  281. StudentNumber[numberOfStudents] = 0;
  282. ParentNumber[numberOfStudents] = 0;
  283. totalCount = numberOfStudents;
  284. totalCountCalculated = 0;
  285. parent_1_flag = 0;
  286. parent_2_flag = 0;
  287. parent_3_flag = 0;
  288. parent_4_flag = 0;
  289. }
  290.  
  291. void handel_Messages_Loop() {
  292. if (millis() - bot_lasttime > BOT_MTBS)
  293. {
  294. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  295.  
  296. while (numNewMessages)
  297. {
  298. Serial.println("got response");
  299. handleNewMessages(numNewMessages);
  300. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  301. }
  302.  
  303. bot_lasttime = millis();
  304. }
  305. }
  306.  
  307. void initWiFi_and_Time_For_Telegram() {
  308. Serial.println();
  309.  
  310. // attempt to connect to Wifi network:
  311. Serial.print("Connecting to Wifi SSID ");
  312. Serial.print(WIFI_SSID);
  313. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  314. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  315. while (WiFi.status() != WL_CONNECTED)
  316. {
  317. Serial.print(".");
  318. delay(500);
  319. }
  320. Serial.print("\nWiFi connected. IP address: ");
  321. Serial.println(WiFi.localIP());
  322.  
  323. Serial.print("Retrieving time: ");
  324. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  325. time_t now = time(nullptr);
  326. while (now < 24 * 3600)
  327. {
  328. Serial.print(".");
  329. delay(100);
  330. now = time(nullptr);
  331. }
  332. Serial.println(now);
  333.  
  334. }
  335.  
  336. void handleNewMessages(int numNewMessages)
  337. {
  338. Serial.println("handleNewMessages");
  339. Serial.println(String(numNewMessages));
  340.  
  341. for (int i = 0; i < numNewMessages; i++)
  342. {
  343. String chat_id = bot.messages[i].chat_id;
  344. String text = bot.messages[i].text;
  345.  
  346. String from_name = bot.messages[i].from_name;
  347. if (from_name == "")
  348. from_name = "Guest";
  349.  
  350. if (text == "/send_test_action")
  351. {
  352. bot.sendChatAction(chat_id, "typing");
  353. delay(4000);
  354. bot.sendMessage(chat_id, "Did you see the action message?");
  355.  
  356. // You can't use own message, just choose from one of bellow
  357.  
  358. //typing for text messages
  359. //upload_photo for photos
  360. //record_video or upload_video for videos
  361. //record_audio or upload_audio for audio files
  362. //upload_document for general files
  363. //find_location for location data
  364.  
  365. //more info here - https://core.telegram.org/bots/api#sendchataction
  366. }
  367. /////////////////////////////////////
  368. if (text == "/child1pickedup63518926")
  369. {
  370. if (parent_1_flag == 0) {
  371. parent_1_flag = 1;
  372. delay(500);
  373. bot.sendMessage(chat_id, "Child (1) picked up by Parent. Driver will be notified");
  374. delay(500);
  375. bot.sendMessage(driver_chat_id, "Child (1) picked up by Parent.");
  376. ParentNumber[0] = 1;
  377. totalCount = totalCount - ParentNumber[0];
  378. printStatus();
  379. String message = "Total: " + String(totalCountCalculated) + "/" + String(totalCount) + "\n";
  380. delay(500);
  381. bot.sendMessage(driver_chat_id, message);
  382. //flashLED(1);
  383. }
  384. else {
  385. delay(500);
  386. bot.sendMessage(chat_id, "Driver notified already, Thank you.");
  387. delay(500);
  388. bot.sendMessage(driver_chat_id, "Child (1) Parent sent message again");
  389. printStatus();
  390. //flashLED(1);
  391. }
  392. }
  393. /////////////////////////////////////
  394. if (text == "/start")
  395. {
  396. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  397. welcome += "This is Chat Action Bot example.\n\n";
  398. welcome += "/send_test_action : to send test chat action message\n";
  399. bot.sendMessage(chat_id, welcome);
  400. }
  401. }
  402. }
  403.  
  404. void printStatus() {
  405. Serial.print("Student Array: \t\t");
  406. Serial.print(StudentNumber[0]); Serial.print(" , ");
  407. Serial.print(StudentNumber[1]); Serial.print(" , ");
  408. Serial.print(StudentNumber[2]); Serial.print(" , ");
  409. Serial.println(StudentNumber[3]);
  410.  
  411. Serial.print("Parent Array: \t\t");
  412. Serial.print(ParentNumber[0]); Serial.print(" , ");
  413. Serial.print(ParentNumber[1]); Serial.print(" , ");
  414. Serial.print(ParentNumber[2]); Serial.print(" , ");
  415. Serial.println(ParentNumber[3]);
  416.  
  417. Serial.print("Total Calculated Count:"); Serial.println(totalCountCalculated);
  418. Serial.print("Total Count:"); Serial.println(totalCount);
  419. }
  420.  
  421. void flashLED(int n) {
  422. for (int i = 0; i < n; i++) {
  423. //digitalWrite(redLED, 1);
  424. //(100);
  425. //digitalWrite(redLED, 0);
  426. //delay(100);
  427. }
  428. }
  429.  
Advertisement
Add Comment
Please, Sign In to add comment