实验二:指纹单模块实验
一、实验目的
利用ESP32和FPM10A光学指纹模块,实现指纹录入、指纹匹配、指纹删除等功能
二、实验内容
1.录入指纹
2.匹配已录入的指纹
3.删除已录入的指纹
三、实验设备
1.ESP32-WROOM-32D开发板
2.FPM10A光学指纹模块(红色:5V TX:绿色 RX:黄色 黑色:GND)
3.杜邦线
四、实验步骤
1) 安装Adafruit指纹传感器库
1.Adafruit指纹传感器库下载:https://github.com/adafruit/Adafruit-Fingerprint-Sensor-Library/archive/master.zip
2.解压下载的.zip文件,你会看到一个Adafruit-Fingerprint-Sensor-Library-master文件夹
3.将Adafruit-Fingerprint-Sensor-Library-master文件夹重命名为Adafruit_Fingerprint_Sensor_Library文件夹
4.将文件夹移动到您的Arduino IDE安装库文件夹中的libraries文件夹
5.重新打开Arduino IDE
2) 录入指纹
1.连接设备
将FPM10A指纹模块与ESP32连接,如下图所示:
FPM10A的RX连接ESP32引脚17
FPM10A的TX连接ESP32引脚16
2.在Arduino IDE中编写如下代码
//ESP32 引脚17(TX)和16(RX)上的Serial2
/*指纹模块 连 接 ESP32*/
/* RX白 --- 17 */
/* TX绿 --- 16 */
#include <Adafruit_Fingerprint.h>
#define mySerial Serial2
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial); //在之前设置的串行引脚上创建一个名为finger的Adafruit_Fingerprint对象。
uint8_t id; //存储指纹编号
void setup()
{
Serial.begin(9600);
while (!Serial); // 如果没打开串口会一直循环,打开串口后继续进行
Serial.println("\n\nEnrolling Fingerprint");
delay(100);
finger.begin(57600); //设置传感器串行端口的数据速率
if (finger.verifyPassword()) { //查看指纹传感器是否存在
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :");
while (1) { delay(1000);Serial.println("waiting for fingerprint sensor..."); }
}
}
uint8_t readnumber(void) { //从串口输入存入指纹的id编号
uint8_t num = 0;
while (num == 0) {
while (! Serial.available());
num = Serial.parseInt();
}
return num;
}
void loop()
{
Serial.println("Ready to enroll a fingerprint!");
Serial.println("Please type in the ID # (from 1 to 127) you want to save this finger as...");
id = readnumber(); //获取串口输入的序号1-127
if (id == 0) { //输入0无效
return;
}
Serial.print("Enrolling ID =");
Serial.println(id);
while (!getFingerprintEnroll() );
}
uint8_t getFingerprintEnroll() {
int 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("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;
}
}
//第一次采集结束
//槽位置放置要素模板
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;
}
//OK Success
Serial.println("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("Image 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("Print 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!");
return 1;
} 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;
}
}
3.上传代码,并录入指纹
1.上传代码到ESP32开发板,并以9600的波特率打开串行监视器
2.这时你应该输入指纹的ID。由于这是你的第一个指纹,请在左上角输入1,然后单击Send按钮。
3.将手指放在扫描仪上,然后按照串行监视器上的说明进行操作系统会要求您将同一根手指放在扫描仪上两次。如果您收到“Prints matched!”消息,如下所示,您的指纹已成功存储。如果没有,重复这个过程,直到你成功
如果出现上图提示,表明上传成功。
3)匹配指纹
1.在Arduino IDE中编写如下代码
#include <Adafruit_Fingerprint.h>
#define mySerial Serial2
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
Serial.begin(9600);
while (!Serial);
delay(100);
Serial.println("\n\nAdafruit finger detect test");
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
finger.getTemplateCount();
Serial.print("Sensor contains ");
Serial.print(finger.templateCount);
Serial.println(" templates");
Serial.println("Waiting for valid finger...");
}
void loop()
{
getFingerprintIDez();
delay(50);
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK success!
p = finger.image2Tz();
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!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
2.上传代码,并匹配指纹
1.代码上传成功后,打开串口监视器显示如下,
2.在扫描仪中放置要识别的手指。
3.在串行监视器上,您可以看到与指纹匹配的ID。同时它也显示了信心值 – 信心值越高,表示该指纹与存储的指纹越相似。
4)删除指纹
1.在Arduino IDE中编写如下代码
#include <Adafruit_Fingerprint.h>
#define mySerial Serial2
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
Serial.begin(9600);
while (!Serial);
delay(100);
Serial.println("\n\nDelete Finger");
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (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("Please type in the ID # (from 1 to 127) you want to delete...");
uint8_t id = readnumber();
if (id == 0) {// ID #0 not allowed, try again!
return;
}
Serial.print("Deleting ID #");
Serial.println(id);
deleteFingerprint(id);
}
uint8_t deleteFingerprint(uint8_t id) {
uint8_t p = -1;
p = finger.deleteModel(id); //删除指纹成功返回0x00
if (p == FINGERPRINT_OK) {
Serial.println("Deleted!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not delete in that location");
return p;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
return p;
} else {
Serial.print("Unknown error: 0x"); Serial.println(p, HEX);
return p;
}
}
2.上传代码,并删除指纹
1.在串口中输入你要删除的指纹编号1-127,然后单击Send按钮
2.出现如下图所示内容,则指纹删除成功