Sebuahhobi98

fingerprint arduino

Jul 16th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. #include <Adafruit_Fingerprint.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. SoftwareSerial fpsensor(2, 3);
  5.  
  6. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&fpsensor);
  7.  
  8.  
  9. uint8_t id = 1;
  10. int p;
  11.  
  12. void setup()
  13. {
  14. Serial.begin(9600);
  15. while (!Serial);
  16. delay(100);
  17.  
  18. Serial.println("\n[ Enroll + Show Templete ]\n");
  19.  
  20. // set the data rate for the sensor serial port
  21. // HIGH baudrate must use Hardware Serial (fpsensor / Serial2 / Serial1)
  22. finger.begin(57600);
  23.  
  24. if (finger.verifyPassword()) {
  25. Serial.println("Found fingerprint sensor!");
  26. } else {
  27. Serial.println("Did not find fingerprint sensor :(");
  28. while (1) {
  29. delay(1);
  30. }
  31. }
  32. }
  33.  
  34. uint8_t readnumber(void) {
  35. uint8_t num = 0;
  36.  
  37. while (num == 0) {
  38. while (! Serial.available());
  39. num = Serial.parseInt();
  40. }
  41. return num;
  42. }
  43.  
  44. void loop() // run over and over again
  45. {
  46. Serial.println("Ready to Show Fingerprint!");
  47. Serial.println("Please type Free Number :");
  48. id = 1;//readnumber();
  49. if (id == 0) {// ID #0 not allowed, try again!
  50. return;
  51. }
  52. Serial.print("Show ID #");
  53. Serial.println(id);
  54.  
  55. getFingerprintEnroll();
  56. }
  57.  
  58. uint8_t getFingerprintEnroll() {
  59.  
  60. p = -1;
  61. Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
  62. while (p != FINGERPRINT_OK) {
  63. p = finger.getImage();
  64. switch (p) {
  65. case FINGERPRINT_OK:
  66. Serial.println("\nImage taken");
  67. break;
  68. case FINGERPRINT_NOFINGER:
  69. Serial.print(",");
  70. break;
  71. case FINGERPRINT_PACKETRECIEVEERR:
  72. Serial.println("Communication error");
  73. break;
  74. case FINGERPRINT_IMAGEFAIL:
  75. Serial.println("Imaging error");
  76. break;
  77. default:
  78. Serial.println("Unknown error");
  79. break;
  80. }
  81. }
  82.  
  83. // OK success!
  84.  
  85. p = finger.image2Tz(1);
  86. switch (p) {
  87. case FINGERPRINT_OK:
  88. Serial.println("Image converted");
  89. break;
  90. case FINGERPRINT_IMAGEMESS:
  91. Serial.println("Image too messy");
  92. return p;
  93. case FINGERPRINT_PACKETRECIEVEERR:
  94. Serial.println("Communication error");
  95. return p;
  96. case FINGERPRINT_FEATUREFAIL:
  97. Serial.println("Could not find fingerprint features");
  98. return p;
  99. case FINGERPRINT_INVALIDIMAGE:
  100. Serial.println("Could not find fingerprint features");
  101. return p;
  102. default:
  103. Serial.println("Unknown error");
  104. return p;
  105. }
  106.  
  107. Serial.print("Remove finger ");
  108. delay(2000);
  109. p = 0;
  110. while (p != FINGERPRINT_NOFINGER) {
  111. p = finger.getImage();
  112. }
  113. Serial.print("ID "); Serial.println(id);
  114. p = -1;
  115. Serial.println("Place same finger again");
  116. while (p != FINGERPRINT_OK) {
  117. p = finger.getImage();
  118. switch (p) {
  119. case FINGERPRINT_OK:
  120. Serial.println("\nImage taken");
  121. break;
  122. case FINGERPRINT_NOFINGER:
  123. Serial.print(".");
  124. break;
  125. case FINGERPRINT_PACKETRECIEVEERR:
  126. Serial.println("Communication error");
  127. break;
  128. case FINGERPRINT_IMAGEFAIL:
  129. Serial.println("Imaging error");
  130. break;
  131. default:
  132. Serial.println("Unknown error");
  133. break;
  134. }
  135. }
  136.  
  137. // OK success!
  138.  
  139. p = finger.image2Tz(2);
  140. switch (p) {
  141. case FINGERPRINT_OK:
  142. Serial.println("Image converted");
  143. break;
  144. case FINGERPRINT_IMAGEMESS:
  145. Serial.println("Image too messy");
  146. return p;
  147. case FINGERPRINT_PACKETRECIEVEERR:
  148. Serial.println("Communication error");
  149. return p;
  150. case FINGERPRINT_FEATUREFAIL:
  151. Serial.println("Could not find fingerprint features");
  152. return p;
  153. case FINGERPRINT_INVALIDIMAGE:
  154. Serial.println("Could not find fingerprint features");
  155. return p;
  156. default:
  157. Serial.println("Unknown error");
  158. return p;
  159. }
  160.  
  161. // OK converted!
  162. Serial.print("Creating model for #"); Serial.println(id);
  163.  
  164. p = finger.createModel();
  165. if (p == FINGERPRINT_OK) {
  166. Serial.println("Prints matched!");
  167. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  168. Serial.println("Communication error");
  169. return p;
  170. } else if (p == FINGERPRINT_ENROLLMISMATCH) {
  171. Serial.println("Fingerprints did not match");
  172. return p;
  173. } else {
  174. Serial.println("Unknown error");
  175. return p;
  176. }
  177.  
  178. Serial.print("ID "); Serial.println(id);
  179. /*p = finger.storeModel(id);
  180. if (p == FINGERPRINT_OK) {
  181. Serial.println("Stored!");
  182. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  183. Serial.println("Communication error");
  184. return p;
  185. } else if (p == FINGERPRINT_BADLOCATION) {
  186. Serial.println("Could not store in that location");
  187. return p;
  188. } else if (p == FINGERPRINT_FLASHERR) {
  189. Serial.println("Error writing to flash");
  190. return p;
  191. } else {
  192. Serial.println("Unknown error");
  193. return p;
  194. }
  195. */
  196. downloadFingerprintTemplate(1);
  197. }
  198.  
  199. uint8_t downloadFingerprintTemplate(uint16_t id)
  200. {
  201. Serial.println("------------------------------------");
  202. Serial.print("Attempting to load #"); Serial.println(id);
  203. uint8_t p = finger.loadModel(id);
  204. switch (p) {
  205. case FINGERPRINT_OK:
  206. Serial.print("Template "); Serial.print(id); Serial.println(" loaded");
  207. break;
  208. case FINGERPRINT_PACKETRECIEVEERR:
  209. Serial.println("Communication error");
  210. return p;
  211. default:
  212. Serial.print("Unknown error "); Serial.println(p);
  213. return p;
  214. }
  215.  
  216. // OK success!
  217.  
  218. Serial.print("Attempting to get #"); Serial.println(id);
  219. p = finger.getModel();
  220. switch (p) {
  221. case FINGERPRINT_OK:
  222. Serial.print("Template "); Serial.print(id); Serial.println(" transferring:");
  223. break;
  224. default:
  225. Serial.print("Unknown error "); Serial.println(p);
  226. return p;
  227. }
  228.  
  229. // one data packet is 267 bytes. in one data packet, 11 bytes are 'usesless' :D
  230. uint8_t bytesReceived[534]; // 2 data packets
  231. memset(bytesReceived, 0xff, 534);
  232.  
  233. uint32_t starttime = millis();
  234. int i = 0;
  235. while (i < 534 && (millis() - starttime) < 20000) {
  236. if (fpsensor.available()) {
  237. bytesReceived[i++] = fpsensor.read();
  238. }
  239. }
  240. Serial.print(i); Serial.println(" bytes read.");
  241. Serial.println("Decoding packet...");
  242.  
  243. uint8_t fingerTemplate[512]; // the real template
  244. memset(fingerTemplate, 0xff, 512);
  245.  
  246. // filtering only the data packets
  247. int uindx = 9, index = 0;
  248. while (index < 534) {
  249. while (index < uindx) ++index;
  250. uindx += 256;
  251. while (index < uindx) {
  252. fingerTemplate[index++] = bytesReceived[index];
  253. }
  254. uindx += 2;
  255. while (index < uindx) ++index;
  256. uindx = index + 9;
  257. }
  258. for (int i = 0; i < 512; ++i) {
  259. //Serial.print("0x");
  260. printHex(fingerTemplate[i], 2);
  261. //Serial.print(", ");
  262. }
  263. Serial.println("\ndone.");
  264.  
  265. /*
  266. uint8_t templateBuffer[256];
  267. memset(templateBuffer, 0xff, 256); //zero out template buffer
  268. int index=0;
  269. uint32_t starttime = millis();
  270. while ((index < 256) && ((millis() - starttime) < 1000))
  271. {
  272. if (mySerial.available())
  273. {
  274. templateBuffer[index] = mySerial.read();
  275. index++;
  276. }
  277. }
  278.  
  279. Serial.print(index); Serial.println(" bytes read");
  280.  
  281. //dump entire templateBuffer. This prints out 16 lines of 16 bytes
  282. for (int count= 0; count < 16; count++)
  283. {
  284. for (int i = 0; i < 16; i++)
  285. {
  286. Serial.print("0x");
  287. Serial.print(templateBuffer[count*16+i], HEX);
  288. Serial.print(", ");
  289. }
  290. Serial.println();
  291. }*/
  292. }
  293.  
  294.  
  295.  
  296. void printHex(int num, int precision) {
  297. char tmp[16];
  298. char format[128];
  299.  
  300. sprintf(format, "%%.%dX", precision);
  301.  
  302. sprintf(tmp, format, num);
  303. Serial.print(tmp);
  304. }
Add Comment
Please, Sign In to add comment