Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_Fingerprint.h>
- #include <SoftwareSerial.h>
- SoftwareSerial fpsensor(2, 3);
- Adafruit_Fingerprint finger = Adafruit_Fingerprint(&fpsensor);
- uint8_t id = 1;
- int p;
- void setup()
- {
- Serial.begin(9600);
- while (!Serial);
- delay(100);
- Serial.println("\n[ Enroll + Show Templete ]\n");
- // set the data rate for the sensor serial port
- // HIGH baudrate must use Hardware Serial (fpsensor / Serial2 / Serial1)
- finger.begin(57600);
- if (finger.verifyPassword()) {
- Serial.println("Found fingerprint sensor!");
- } else {
- Serial.println("Did not find fingerprint sensor :(");
- while (1) {
- delay(1);
- }
- }
- }
- uint8_t readnumber(void) {
- uint8_t num = 0;
- while (num == 0) {
- while (! Serial.available());
- num = Serial.parseInt();
- }
- return num;
- }
- void loop() // run over and over again
- {
- Serial.println("Ready to Show Fingerprint!");
- Serial.println("Please type Free Number :");
- id = 1;//readnumber();
- if (id == 0) {// ID #0 not allowed, try again!
- return;
- }
- Serial.print("Show ID #");
- Serial.println(id);
- getFingerprintEnroll();
- }
- uint8_t getFingerprintEnroll() {
- p = -1;
- Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
- while (p != FINGERPRINT_OK) {
- p = finger.getImage();
- switch (p) {
- case FINGERPRINT_OK:
- Serial.println("\nImage taken");
- break;
- case FINGERPRINT_NOFINGER:
- Serial.print(",");
- break;
- case FINGERPRINT_PACKETRECIEVEERR:
- Serial.println("Communication error");
- break;
- case FINGERPRINT_IMAGEFAIL:
- Serial.println("Imaging error");
- break;
- default:
- Serial.println("Unknown error");
- break;
- }
- }
- // OK success!
- p = finger.image2Tz(1);
- switch (p) {
- case FINGERPRINT_OK:
- Serial.println("Image converted");
- break;
- case FINGERPRINT_IMAGEMESS:
- Serial.println("Image too messy");
- return p;
- case FINGERPRINT_PACKETRECIEVEERR:
- Serial.println("Communication error");
- return p;
- case FINGERPRINT_FEATUREFAIL:
- Serial.println("Could not find fingerprint features");
- return p;
- case FINGERPRINT_INVALIDIMAGE:
- Serial.println("Could not find fingerprint features");
- return p;
- default:
- Serial.println("Unknown error");
- return p;
- }
- Serial.print("Remove finger ");
- delay(2000);
- p = 0;
- while (p != FINGERPRINT_NOFINGER) {
- p = finger.getImage();
- }
- Serial.print("ID "); Serial.println(id);
- p = -1;
- Serial.println("Place same finger again");
- while (p != FINGERPRINT_OK) {
- p = finger.getImage();
- switch (p) {
- case FINGERPRINT_OK:
- Serial.println("\nImage taken");
- break;
- case FINGERPRINT_NOFINGER:
- Serial.print(".");
- break;
- case FINGERPRINT_PACKETRECIEVEERR:
- Serial.println("Communication error");
- break;
- case FINGERPRINT_IMAGEFAIL:
- Serial.println("Imaging error");
- break;
- default:
- Serial.println("Unknown error");
- break;
- }
- }
- // OK success!
- p = finger.image2Tz(2);
- switch (p) {
- case FINGERPRINT_OK:
- Serial.println("Image converted");
- break;
- case FINGERPRINT_IMAGEMESS:
- Serial.println("Image too messy");
- return p;
- case FINGERPRINT_PACKETRECIEVEERR:
- Serial.println("Communication error");
- return p;
- case FINGERPRINT_FEATUREFAIL:
- Serial.println("Could not find fingerprint features");
- return p;
- case FINGERPRINT_INVALIDIMAGE:
- Serial.println("Could not find fingerprint features");
- return p;
- default:
- Serial.println("Unknown error");
- return p;
- }
- // OK converted!
- Serial.print("Creating model for #"); Serial.println(id);
- p = finger.createModel();
- if (p == FINGERPRINT_OK) {
- Serial.println("Prints matched!");
- } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
- Serial.println("Communication error");
- return p;
- } else if (p == FINGERPRINT_ENROLLMISMATCH) {
- Serial.println("Fingerprints did not match");
- return p;
- } else {
- Serial.println("Unknown error");
- return p;
- }
- Serial.print("ID "); Serial.println(id);
- /*p = finger.storeModel(id);
- if (p == FINGERPRINT_OK) {
- Serial.println("Stored!");
- } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
- Serial.println("Communication error");
- return p;
- } else if (p == FINGERPRINT_BADLOCATION) {
- Serial.println("Could not store in that location");
- return p;
- } else if (p == FINGERPRINT_FLASHERR) {
- Serial.println("Error writing to flash");
- return p;
- } else {
- Serial.println("Unknown error");
- return p;
- }
- */
- downloadFingerprintTemplate(1);
- }
- uint8_t downloadFingerprintTemplate(uint16_t id)
- {
- Serial.println("------------------------------------");
- Serial.print("Attempting to load #"); Serial.println(id);
- uint8_t p = finger.loadModel(id);
- switch (p) {
- case FINGERPRINT_OK:
- Serial.print("Template "); Serial.print(id); Serial.println(" loaded");
- break;
- case FINGERPRINT_PACKETRECIEVEERR:
- Serial.println("Communication error");
- return p;
- default:
- Serial.print("Unknown error "); Serial.println(p);
- return p;
- }
- // OK success!
- Serial.print("Attempting to get #"); Serial.println(id);
- p = finger.getModel();
- switch (p) {
- case FINGERPRINT_OK:
- Serial.print("Template "); Serial.print(id); Serial.println(" transferring:");
- break;
- default:
- Serial.print("Unknown error "); Serial.println(p);
- return p;
- }
- // one data packet is 267 bytes. in one data packet, 11 bytes are 'usesless' :D
- uint8_t bytesReceived[534]; // 2 data packets
- memset(bytesReceived, 0xff, 534);
- uint32_t starttime = millis();
- int i = 0;
- while (i < 534 && (millis() - starttime) < 20000) {
- if (fpsensor.available()) {
- bytesReceived[i++] = fpsensor.read();
- }
- }
- Serial.print(i); Serial.println(" bytes read.");
- Serial.println("Decoding packet...");
- uint8_t fingerTemplate[512]; // the real template
- memset(fingerTemplate, 0xff, 512);
- // filtering only the data packets
- int uindx = 9, index = 0;
- while (index < 534) {
- while (index < uindx) ++index;
- uindx += 256;
- while (index < uindx) {
- fingerTemplate[index++] = bytesReceived[index];
- }
- uindx += 2;
- while (index < uindx) ++index;
- uindx = index + 9;
- }
- for (int i = 0; i < 512; ++i) {
- //Serial.print("0x");
- printHex(fingerTemplate[i], 2);
- //Serial.print(", ");
- }
- Serial.println("\ndone.");
- /*
- uint8_t templateBuffer[256];
- memset(templateBuffer, 0xff, 256); //zero out template buffer
- int index=0;
- uint32_t starttime = millis();
- while ((index < 256) && ((millis() - starttime) < 1000))
- {
- if (mySerial.available())
- {
- templateBuffer[index] = mySerial.read();
- index++;
- }
- }
- Serial.print(index); Serial.println(" bytes read");
- //dump entire templateBuffer. This prints out 16 lines of 16 bytes
- for (int count= 0; count < 16; count++)
- {
- for (int i = 0; i < 16; i++)
- {
- Serial.print("0x");
- Serial.print(templateBuffer[count*16+i], HEX);
- Serial.print(", ");
- }
- Serial.println();
- }*/
- }
- void printHex(int num, int precision) {
- char tmp[16];
- char format[128];
- sprintf(format, "%%.%dX", precision);
- sprintf(tmp, format, num);
- Serial.print(tmp);
- }
Add Comment
Please, Sign In to add comment