Program for R306 fingerprint module interfaced with Wiz750SR S2E device

Dependencies:   FPC_R306 MQTT WIZnetInterface mbed

Fork of GT511C3_TimeStamp_WIZwiki-W7500 by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Albinarackal
Date:
Mon Jul 16 09:49:46 2018 +0000
Parent:
12:0dbf1579a20f
Commit message:
Program for R306 fingerprint library interfaced with Wiz750SR S2E device

Changed in this revision

FPC_R306.lib Show annotated file Show diff for this revision Revisions of this file
GT511C3.lib Show diff for this revision Revisions of this file
MQTT.lib Show annotated file Show diff for this revision Revisions of this file
NTPClient.lib Show diff for this revision Revisions of this file
SDFileSystem.lib Show diff for this revision Revisions of this file
WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FPC_R306.lib	Mon Jul 16 09:49:46 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Albinarackal/code/FPC_R306/#00bf6d50bc74
--- a/GT511C3.lib	Thu Jul 30 10:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/WIZnet/code/GT511C3/#e53c3965131e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Mon Jul 16 09:49:46 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Albinarackal/code/MQTT/#f409e1191fa1
--- a/NTPClient.lib	Thu Jul 30 10:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/WIZnet/code/NTPClient/#36be990f21be
--- a/SDFileSystem.lib	Thu Jul 30 10:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/azsymaivan/code/SDFileSystem/#f242d7bdef28
--- a/WIZnetInterface.lib	Thu Jul 30 10:59:42 2015 +0000
+++ b/WIZnetInterface.lib	Mon Jul 16 09:49:46 2018 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#3b64df29662f
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
--- a/main.cpp	Thu Jul 30 10:59:42 2015 +0000
+++ b/main.cpp	Mon Jul 16 09:49:46 2018 +0000
@@ -1,169 +1,445 @@
 #include "mbed.h"
-#include "GT511C3.hpp"
-#include "SDFileSystem.h"
-#include "EthernetInterface.h"
-#include "NTPClient.h"
-
-#define MAX_ID_NUM  20
+#include "FPC_R306.hpp"
+#include "MQTTEthernet.h"
+#include "MQTTClient.h"
+#define ECHO_SERVER_PORT   7
 
-#ifdef TARGET_WIZWIKI_W7500
-GT511C3 finger(PA_13,PA_14);
-DigitalIn del_ID(D7);
-DigitalIn enroll_ID(D8);
-InterruptIn in(D9);
-SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // the pinout on the mbed
-uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0xab};
-#endif
+
+FPC_R306 finger(D1,D0);
 
 Serial pc(USBTX,USBRX);
-EthernetInterface eth;
-NTPClient ntpClient;
+int myid=456;
 
 FILE *fp;
+char userId[100];
+int p;
 
+int id =1;
+int choice;
+uint16_t deleteID;
+uint8_t sendData[16];
+uint8_t getImage(void);
+uint8_t image2Tz(uint8_t slot);
+int SendCommand();
+int RecvResponse();
 
-int progress(int status,char *msg)
+/*..........MQTT CALLBACK FUNCTION.........*/
+MQTTEthernet ipstack = MQTTEthernet();
+
+MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
+MQTT::Message message;
+
+void messageArrived(MQTT::MessageData& md)
 {
-    pc.printf("%s",msg);
-    return 0;
+    MQTT::Message &message = md.message;
+    printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
+    printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
 }
 
-void close_file(void)
-{
-    fclose(fp);
-}
-
+//....................................................MAIN Function.....................................................//
 int main()
 {
-    int i;
-    int sts = 0;
-    int ID = 0;
-    int cnt = 0;
-    int EnrollID;
-    
-    char domainName[3][80] = {"kr.pool.ntp.org", "time.bora.net", "time.nuri.net"};//SET TO DOMAIN NAME OF SERVER GETTING TIME FROM
-    char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA
-    time_t sysTime;
-        
-    in.rise(&close_file);
+    wait_ms(500);
+    pc.baud(9600);
+    finger.baud(57600);
+
+    /*****************************MQTT CONFIGURATION START FROM HERE *****************************************/
+
+    printf("Wait a second...\r\n");
+    char* topic = "openhab/parents/command";
+
+
+    char* hostname = "172.16.73.4";
+    int port = 1883;
+
+    int rc = ipstack.connect(hostname, port);
+    if (rc != 0)
+        printf("rc from TCP connect is %d\n", rc);
+
+    printf("Topic: %s\r\n",topic);
+
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "parents";
+
+    if ((rc = client.connect(data)) == 0)
+        printf("rc from MQTT connect is %d\n", rc);
+
+
+    /********************MQTT CONFIGURATION END FROM HERE********************/
+
+    pc.printf("..................................R306.............................................\n");
+
+    while(true) {
+        pc.printf("\n");
+        pc.printf("To Enroll ID-----------------------------------   1\n");
+
+        pc.printf("To get Template Count--------------------------   2\n");
+
+        pc.printf("To Search for an ID----------------------------   3\n");
+
+        pc.printf("To Delete an ID--------------------------------   4\n");
+
+        pc.printf("To Empty the library---------------------------   5\n");
+
+
+        choice = pc.getc();
+        choice -= '0';
+
+//.........................................ENROLLING FINGERPRINT........................................................//
+
+        switch(choice) {
+            case 1:
+
+                wait(1);
+                p=-1;
+                pc.printf("PLACE THE FINGER TO ENROLL\n");
+
+
+//.............Enrolling 1st time
+
+                while(p != FINGERPRINT_OK) {
+                    pc.printf(".");
+                    p = finger.CMD_GETIMG();;
+                    switch (p) {
+                        case FINGERPRINT_OK:
+                            pc.printf("Image Taken\n");
+                            break;
+
+                        case FINGERPRINT_NOFINGER:
+                            pc.printf(".");
+                            wait(1);
+
+                        case FINGERPRINT_PACKETRECIEVEERR:
+                            //pc.printf("Communication error\n");
+                            break;
+
+                        case FINGERPRINT_IMAGEFAIL:
+                            pc.printf("Imaging error\n");
+                            break;
+
+                        default:
+                            pc.printf("Unknown Error\n");
+                            break;
+                    }
+                }
+
+                p= -1;
+                while(p!= FINGERPRINT_OK) {
+                    p = finger.CMD_IMG2Tz1();
+
+                    switch (p) {
+                        case FINGERPRINT_OK:
+                            pc.printf("Image Converted\n");
+                            break;
+
+                        case FINGERPRINT_IMAGEMESS:
+                            pc.printf("Image too messy\n");
+                            return p;
+
+                        case FINGERPRINT_FEATUREFAIL:
+                            pc.printf("Could not find fingerprint features\n");
+                            return p;
+
+                        case FINGERPRINT_PACKETRECIEVEERR:
+                            pc.printf("Communication error\n");
+                            return p;
+
+                        default:
+                            pc.printf("Unknown error\n");
+                            return p;
+                    }
+                }
+
+                //...........Enrolling 2nd time
+
+
+                pc.printf("Remove the finger\n");
+                wait(1);
+                p=0;
+
+                while(p!= FINGERPRINT_NOFINGER) { //Waiting for finger to remove
+                    p= finger.CMD_GETIMG();
+                }
+                p =-1;
+                pc.printf("PLACE THE SAME FINGER AGAIN\n");
+
+                while(p!= FINGERPRINT_OK) {
+                    pc.printf(".");
+                    p = finger.CMD_GETIMG();;
+                    switch (p) {
+                        case FINGERPRINT_OK:
+                            pc.printf("Image Taken\n");
+                            break;
+
+                        case FINGERPRINT_NOFINGER:
+                            pc.printf(".");
+                            wait(1);
+
+                        case FINGERPRINT_PACKETRECIEVEERR:
+                            //pc.printf("Communication error\n");
+                            break;
+
+                        case FINGERPRINT_IMAGEFAIL:
+                            pc.printf("Imaging error\n");
+                            break;
+
+                        default:
+                            pc.printf("Unknown Error\n");
+                            break;
+                    }
+                }
+
+                p=-1;
+                while(p!= FINGERPRINT_OK) {
+                    p = finger.CMD_IMG2Tz2();
+
+                    switch (p) {
+                        case FINGERPRINT_OK:
+                            pc.printf("Image Converted\n");
+                            break;
+
+                        case FINGERPRINT_IMAGEMESS:
+                            pc.printf("Image too messy\n");
+                            return p;
+
+                        case FINGERPRINT_FEATUREFAIL:
+                            pc.printf("Could not find fingerprint features\n");
+                            return p;
+
+                        case FINGERPRINT_PACKETRECIEVEERR:
+                            pc.printf("Communication error\n");
+                            return p;
+
+                        default:
+                            pc.printf("Unknown error\n");
+                            return p;
+                    }
+                }
+
+                //........Creating Model
+
+                pc.printf("Creating Model\n");
+
+                p = finger.createModel();
+
+                if(p == FINGERPRINT_OK) {
+                    pc.printf("Prints Matched\n");
+                }
+
+                else if(p == FINGERPRINT_ENROLLMISMATCH) {
+                    pc.printf("Fingerprints did not match\n");
+                    break;
+                }
+
+                else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+                    pc.printf("Communication error\n");
+                    break;
+                } else {
+                    pc.printf("UNKNOWN ERROR\n");
+                    break;
+                }
 
-    pc.baud(115200);
-    
-    printf("File open...\r\n");
-    
-    if((fp = fopen("/sd/time.txt", "w")) == NULL)
-    {
-        printf("Cannot open file!!\r\n");
-        return 0;
-    }
-    
-    printf("GT511C3 open....\r\n");
-    
-    while(1)
-    {
-        sts = finger.Open();
-        if(sts == -1)
-        {
-            printf("GT511C3 Open failed!!\r\n");
-            cnt++;
-            while(cnt > 5);
-        }
-        else
-        {
-            break;
-        }
-        
-        wait(0.2);
-    }
-    
-    if(del_ID == 1)
-    {
-        finger.DeleteID_All();
-        printf("All ID are deleted!!\r\n");
-    }
-    
-    cnt = 0;
-    if(enroll_ID == 1)
-    {
-        while(1)
-        {
-            if(finger.CheckEnrolled(cnt) == -1)
-            {
-                EnrollID = cnt;
-                printf("ID(%d) is empty\r\n", EnrollID);
+
+//.....Storing Model
+
+                //uint8_t test = (uint8_t)(id & 0xFF);
+                pc.printf("%d\n", id);
+                p = finger.STORE_MODEL(id);
+
+                if(p == FINGERPRINT_OK) {
+                    pc.printf("Stored\n");
+                    id++;
+                }
+
+                else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+                    pc.printf("Communication error\n");
+                    break;
+                }
+
+                else if (p == FINGERPRINT_BADLOCATION) {
+                    pc.printf("Could not store in that location\n");
+                    break;
+                }
+
+                else if (p == FINGERPRINT_FLASHERR) {
+                    pc.printf("Error writing to flash\n");
+                    break;
+                } else {
+                    pc.printf("Unknown error\n");
+                    break;
+                }
+
+                break;
+
+
+//.............................................Get TEMPLATE Count.......................................................//
+            case 2:
+                pc.printf("Getting template count\n\n");
+                wait(1);
+                p = -1;
+                p = finger.TMPL_COUNT();
+
+                if(p==FINGERPRINT_OK) {
+                    pc.printf("SENSOR CONTAINS %d TEMPLATES",finger.templatecount);
+                }
+
+                else
+                    pc.printf("ERROR\n");
                 break;
-            }
-            cnt++;
-            if(cnt > MAX_ID_NUM)
-            {
-                printf("\r\nERROR : ID number is fulled!!Delete ID first!!\r\n\r\n");
-                while(1);
-            }
-        }
-        finger.Enroll(EnrollID,progress);
-    }
-    
-    eth.init(mac_addr); //Use DHCP
-    printf("Check Ethernet Link\r\n");
-    while(1) //Wait link up
-    {
-        if(eth.link() == true) 
-            break;
-    }
-    printf("Link up\r\n");
-    printf("Getting IP address by DHCP...\r\n");
-    eth.connect();
-    printf("Server IP Address is %s\r\n", eth.getIPAddress());
-    
-    printf("Getting time information by using NTP...\r\n");
-    
-    cnt = 0;
-    while(1)
-    {
-        if(ntpClient.setTime(domainName[cnt],123,0x00005000) != NTP_OK)
-        {
-            printf("Cannot get time information by NTP\r\n");
-            cnt++;
-        }
-        else
-            break;
-            
-        if(cnt > 3)
-        {
-            printf("All NTP servers are not resposed!!\r\n");
-            return 1;
+
+//.................................................SEARCHING............................................................//
+
+            case 3:
+                p= -1;
+                while(p!= FINGERPRINT_OK) {
+                    p= finger.CMD_GETIMG();
+
+                    switch (p) {
+                        case FINGERPRINT_OK:
+                            pc.printf("Image Taken\n");
+                            break;
+
+                        case FINGERPRINT_NOFINGER:
+                            pc.printf(".");
+                            wait(1);
+
+                        case FINGERPRINT_PACKETRECIEVEERR:
+                            //pc.printf("Communication error\n");
+                            break;
+
+                        case FINGERPRINT_IMAGEFAIL:
+                            pc.printf("Imaging error\n");
+                            break;
+
+                        default:
+                            pc.printf("Unknown Error\n");
+                            break;
+                    }
+                }
+
+                p = finger.CMD_IMG2Tz1();
+
+                switch (p) {
+                    case FINGERPRINT_OK:
+                        pc.printf("Image Converted\n");
+                        break;
+
+                    case FINGERPRINT_IMAGEMESS:
+                        pc.printf("Image too messy\n");
+                        return p;
+
+                    case FINGERPRINT_FEATUREFAIL:
+                        pc.printf("Could not find fingerprint features\n");
+                        return p;
+
+                    case FINGERPRINT_PACKETRECIEVEERR:
+                        pc.printf("Communication error\n");
+                        return p;
+
+                    default:
+                        pc.printf("Unknown error\n");
+                        return p;
+                }
+
+                p = finger.FAST_SEARCH();
+                if (p == FINGERPRINT_OK) {
+                    pc.printf("Found a print match!\n");
+                    pc.printf("FOUND ID #%d  with confidence of %d\n", finger.fingerID, finger.MatchScore);
+                    message.qos = MQTT::QOS0;
+                    message.retained = false;
+                    message.dup = false;
+                    
+                    sprintf(userId, "%d", finger.fingerID);
+
+                    pc.printf("User id  over MQTT is : %s\n", userId);
+                    message.payload =(void *)userId;
+                    message.payloadlen = strlen(userId);
+                    rc = client.publish("cdi/laxmi", message);
+                    pc.printf("send via MQTT\n");
+
+                }
+
+                else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+                    pc.printf("Communication error\n");
+                    break;
+                }
+
+                else if (p == FINGERPRINT_NOTFOUND) {
+                    pc.printf("Did not find a match\n");
+                    break;
+                }
+
+                else {
+                    pc.printf("Unknown error");
+                    break;
+                }
+
+                break;
+
+
+
+//............................................DELETE ID................................................................//
+
+            case 4:
+
+                pc.printf("Please, Enter the ID to be Deleted\n");
+                deleteID = pc.getc();
+                deleteID -='0';
+                pc.printf("Deleting the ID %d\n", deleteID);
+
+                p= -1;
+                p= finger.DELETE_ID(deleteID);
+
+                if (p == FINGERPRINT_OK) {
+                    pc.printf("Deleted!\n");
+                }
+
+                else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+                    pc.printf("Communication error\n");
+                    return p;
+                }
+
+                else if (p == FINGERPRINT_BADLOCATION) {
+                    pc.printf("Could not delete in that location\n");
+                    return p;
+                }
+
+                else if (p == FINGERPRINT_FLASHERR) {
+                    pc.printf("Error writing to flash\n");
+                    return p;
+                } else {
+                    pc.printf("Unknown error\n");
+                }
+                break;
+
+//................................................EMPTY LIBRARY......................................................//
+
+            case 5:
+
+                p= -1;
+                p = finger.EMPTY_LIB();
+
+                if (p == FINGERPRINT_OK) {
+                    pc.printf("Library Cleared!\n");
+                }
+
+                else if (p == FINGERPRINT_PACKETRECIEVEERR) {
+                    pc.printf("Communication error\n");
+                    return p;
+                }
+
+                else if (p == FINGERPRINT_DBCLEARFAIL) {
+                    pc.printf("Could not clear the library\n");
+                    return p;
+                }
+
+                else {
+                    pc.printf("Unknown error\n");
+                }
+                break;
+
+
         }
     }
-        
-    printf("Completed Get and Set Time\r\n\r\n");
-    eth.disconnect();
-    
-    //Start to capture fingerprint
-    finger.CmosLed(1);
-    
-    while(1)
-    {
-        printf("Press finger for Identify\r\n");
-        finger.WaitPress(1);
-        if(finger.Capture(1) != 0)
-            continue;
-            
-        ID = finger.Identify();
-        
-        if(ID == -1)
-            printf("\r\nERROR : There is no ID!!Enroll first!!\r\n\r\n");
-        else
-        {
-            printf("ID = %d\r\n",ID); 
-            sysTime = time(NULL)+(3600*9); //TIME with offset for eastern time KR
-            //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER
-            strftime(buffer,80,"%Y/%m/%d  %p %I:%M:%S \r\n",localtime(&sysTime));
-            fprintf(fp, "ID : %d\r\nTime : %s\r\n", ID, buffer);
-            printf("Date and Time\r\n%s\r\n", buffer);
-        }
-            
-        printf("Remove finger\r\n");
-        finger.WaitPress(0);
-    }
-}
-
+}
\ No newline at end of file
--- a/mbed-src.lib	Thu Jul 30 10:59:42 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed-src/#2d5fc5624619
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jul 16 09:49:46 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/a7c7b631e539
\ No newline at end of file