Skip to content

Instantly share code, notes, and snippets.

@aalonzolu
Last active July 8, 2022 05:07
Show Gist options
  • Save aalonzolu/b3dd58a0ed8b916db880c3f22e097b88 to your computer and use it in GitHub Desktop.
Save aalonzolu/b3dd58a0ed8b916db880c3f22e097b88 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
#define buzzerPin D4
// Replace with your SSID and Password
const char *ssid = "NiJo";
const char *password = "uatguate7";
String Event_Name = "fingerprint";
String Key = "kzQBpHs6AajQVOphp3oC4DL2eTr_iT1j-nQ40mB18k_";
const char *server = "maker.ifttt.com";
// Your Domain name with URL path or IP address with path
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;
SoftwareSerial myserial(D5, D6); // RX, TX
const byte handshake[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x17, 0x00, 0x00, 0x1c};
const byte getimg[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x01, 0x00, 0x05};
const byte img2tz1[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x02, 0x01, 0x00, 0x08};
const byte img2tz2[13] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x04, 0x02, 0x01, 0x00, 0x08};
const byte regmodel[12] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x05, 0x00, 0x09};
const byte store[14] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00};
const byte search[17] = {0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x08, 0x04, 0x01, 0x00, 0x00, 0x00, 0xff, 0x01, 0x0d};
int REGISTRYID, SCORE;
int Buffer[25];
//////////////////////////////////////////////////////////
void clearBuffer()
{
for (int i = 0; i < 25; i++)
Buffer[i] = 0xff;
// for(int i=0;i<10;i++)myserial.read();
}
//////////////////////////////////////////////////////////
int startFP()
{
int i = 0;
for (i = 0; i < 13; i++)
{
myserial.write(handshake[i]);
}
while (!myserial.available())
;
i = 0;
while (i < 12)
{
if (myserial.available())
{
Buffer[i] = myserial.read();
i++;
}
}
if (Buffer[9] == 0)
{
clearBuffer();
return 1;
}
else
{
clearBuffer();
return 0;
}
}
//////////////////////////////////////////////////////////
int readFinger()
{
int i = 0;
for (i = 0; i < 12; i++)
{
myserial.write(getimg[i]);
}
while (!myserial.available())
;
i = 0;
while (i < 12)
{
if (myserial.available())
{
Buffer[i] = myserial.read();
i++;
}
}
if (Buffer[9] == 0)
{
clearBuffer();
return 1;
}
else if (Buffer[9] == 1 || Buffer[9] == 3)
{
clearBuffer();
return 2;
}
else
{
clearBuffer();
return 0;
}
}
//////////////////////////////////////////////////////////
int convertToCharArray()
{
int i = 0;
for (i = 0; i < 13; i++)
{
myserial.write(img2tz1[i]);
}
while (!myserial.available())
;
i = 0;
while (i < 12)
{
if (myserial.available())
{
Buffer[i] = myserial.read();
i++;
}
}
if (Buffer[9] == 0)
{
clearBuffer();
return 1;
}
else
{
clearBuffer();
return 0;
}
}
//////////////////////////////////////////////////////////
int findFP()
{
int i = 0;
for (i = 0; i < 17; i++)
{
myserial.write(search[i]);
}
while (!myserial.available())
;
i = 0;
while (i < 16)
{
if (myserial.available())
{
Buffer[i] = myserial.read();
i++;
}
}
if (Buffer[9] == 0)
{
REGISTRYID = (Buffer[10] << 8) + (Buffer[11] & 0x00ff);
SCORE = (Buffer[12] << 8) + (Buffer[13] & 0x00ff);
clearBuffer();
return 1;
}
else
{
clearBuffer();
return 0;
}
}
//////////////////////////////////////
void buzzerOK()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(buzzerPin, HIGH);
delay(30);
digitalWrite(buzzerPin, LOW);
delay(30);
}
}
void buzzerError()
{
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
}
//////////////////////////////////////
void setup()
{
ESP.wdtDisable();
Serial.begin(115200);
myserial.begin(57600);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
if (startFP())
Serial.println("Handshake OK");
else
Serial.println("Handshake Failed!");
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
// show the network details
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("Wifi Connection failed");
}
}
void enviarRequest(int REGISTRY_ID, int SCORE)
{
// WiFiClientSecure client;
WiFiClient client;
HTTPClient http;
int retries = 5;
while (!!!client.connect(server, 80) && (retries-- > 0))
{
Serial.print(".");
}
Serial.println();
if (!!!client.connected())
{
Serial.println("Failed to connect...");
}
Serial.print("Request resource: ");
Serial.println(resource);
// Temperature in Celsius
String jsonObject = String("{\"value1\":\"") + REGISTRY_ID + "\",\"value2\":\"" + SCORE + "\"}";
client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server);
client.println("Connection: close\r\nContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);
int timeout = 5 * 10; // 5 seconds
while (!!!client.available() && (timeout-- > 0))
{
delay(100);
}
if (!!!client.available())
{
Serial.println("No response...");
}
while (client.available())
{
Serial.write(client.read());
}
Serial.println("\nclosing connection");
client.stop();
}
void loop()
{
delay(1000);
int p = 0;
Serial.println("Touch Fingerprint Sensor...");
while (p == 0)
{
p = readFinger();
ESP.wdtFeed();
}
if (p == 1)
Serial.println("Finger OK");
else if (p == 2)
Serial.println("Please try again!");
p = convertToCharArray();
if (p == 1)
Serial.println("Converted to char array..");
else if (p == 2)
Serial.println("Converting to char array failed!");
p = findFP();
if (p == 1)
{
Serial.print("Fingerprint Matched, Registry ID:");
Serial.println(REGISTRYID);
Serial.print("Matching Score:");
Serial.println(SCORE);
buzzerOK();
enviarRequest(REGISTRYID, SCORE);
}
else if (p == 0)
{
Serial.println("Fingerprint match failed!");
Serial.println("************************");
buzzerError();
}
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment