Hey guys, I am trying to implement a security module where the otp is sent to your mobile number and then you input that otp via a keypad.
I wanted to know how you can compare the generated otp with your entered passcode and then print the access granted or denied message. Can I use an array for this because the serial monitor shows access denied for each character i type
If you post your code, we might be able to help.
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
int getFingerprintIDez();
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = { 33, 32, 31, 30 };
byte colPins[COLS] = { 36, 35, 34 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int otp = random(1000,9999);
int password;
int key[4];
char key2;
int i;
void setup()
{
// For Yun/Leo/Micro/Zero/...
Serial.begin(9600); // set baud rate for serial monitor
Serial.println("Adafruit finger detect test");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
Serial.println("Waiting for valid finger...");
Serial1.begin(9600);
password = otp;
}
void loop()
{
getFingerprintIDez();
delay(50);
if (Serial.available()) {
Serial1.write(Serial.read());
}
if (Serial1.available()) {
Serial.write(Serial1.read());
}
char key = kpd.getKey();
for(i=0;i<4;i=i+1)
{
key2 = (key2*10)+key[i];
}
if (key2==password)
{
Serial.print("Access Granted");
}
else if(key!=password)
{
Serial.print("Access Denied");
}
}
}
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;
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
}
// 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);
Serial.println("ENTER OTP");
Serial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial1.println("AT+CMGS=\"+91xxxxxxxxx\"\r"); // Replace x with mobile number
delay(1000);
Serial1.println(password);// The SMS text you want to send
delay(100);
Serial1.println((char)26);// ASCII code of CTRL+Z
delay(1000);
return finger.fingerID;
}
i tried compiling but then it showed an error like this:
invalid types 'char[int]' for array subscript
the error pointed to this line:
key2 = (key210)+key;*
char key = kpd.getKey();
...
key2 = (key2*10)+key[i];
key is declared as a single char, as is key2, not an array. Leaving that aside, key will have the ASCII value of the key pressed, not the actual (numeric) value. Subtract '0' from it if it is a number to get the actual number of the key pressed but you cannot then put it in key2 as currently declared.
int key[4];
or:
char key = kpd.getKey();
which key are you referring to?
char key = kpd.getKey();
for(i=0;i<4;i=i+1)
I'd go back to the keypad demo and try to get that working filling an array of size four.
UKHeliBob:
char key = kpd.getKey();
...
key2 = (key2*10)+key[i];
key is declared as a single char, as is key2, not an array. Leaving that aside, key will have the ASCII value of the key pressed, not the actual (numeric) value. Subtract '0' from it if it is a number to get the actual number of the key pressed but you cannot then put it in key2 as currently declared.
So it should be "int key"? I want to send a 4-digit number, but after trying an if loop, it didnt work.
Can you give me a code as how I should declare and compare the keys i have entered with the password?
To get the user input into an integer you need to
read they key
if it is between '0' and '9'
subtract '0' to gtet the value of the number entered
multiply the target integer variable by 10 and add the result of the subtraction
end if
do this 4 times if the passcode is a 4 digit integer
To get the user input into an array of chars you need to
read the key
if it is between '0' and '9'
put it into an array of chars at the current position
increment the current position variable
(optional) put '\0' in the current postion if you need the array to be a C string
end if
do this 4 times if the passcode is a 4 digit integer
UKHeliBob:
To get the user input into an integer you need toread they key
if it is between '0' and '9'
subtract '0' to gtet the value of the number entered
multiply the target integer variable by 10 and add the result of the subtraction
end if
do this 4 times if the passcode is a 4 digit integer
To get the user input into an array of chars you need toread the key
if it is between '0' and '9'
put it into an array of chars at the current position
increment the current position variable
(optional) put '\0' in the current postion if you need the array to be a C string
end if
do this 4 times if the passcode is a 4 digit integer
Something like this?:
int key=kpd.getKey();
if(key)
{
key=key-0;
key2=(key2*10)+key;
}
How do I do this 4 times?
Aakarsh:
Something like this?:int key=kpd.getKey();
if(key)
{
key=key-0;
key2=(key2*10)+key;
}
How do I do this 4 times?
Perhaps something like that
Did you forget that kpd.getKey(); returns a char and that you have to subtract '0' to get the actual number, not 0, which would do nothing ? You should also check whether what is returned is between '0' and '9' in case the user presses another key.
kpd.getKey();
count how many times you do it and stop when you have done it 4 times.
UKHeliBob:
You should also check whether what is returned is between '0' and '9' in case the user presses another key.
I should use a switch statement for that?
Another problem. The Program doesn't wait for me to input the characters. It starts printing access denied as soon as I receive the otp. What do I do?
Can I use another key such as the '#' or '*' as an Enter key or is it not required?
Aakarsh:
I should use a switch statement for that?
Another problem. The Program doesn't wait for me to input the characters. It starts printing access denied as soon as I receive the otp. Should I use a delay?
try something like this:
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
char* storedPassword = "5432";
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (char* enteredPassword = lookForPassword(keypad))
{
Serial.println(enteredPassword);
if (strcmp(enteredPassword, storedPassword) == 0)
{
Serial.println(F("SUCCESS"));
}
else
{
Serial.println(F("EPIC FAILURE"));
}
}
}
char* lookForPassword(Keypad& kpd)
{
static char password[5];
static int idx = 0;
if(char key = kpd.getKey())
{
if (key = '#')
{
idx = 0;
return password;
}
if (isDigit(key))
{
password[idx++] = key;
password[idx] = '\0';
if (idx > 3)
{
idx = 0;
return password;
}
}
}
return nullptr;
}
I should use a switch statement for that?
No.
if (key >= '0' && key <='9')
Should I use a delay?
No
If no key is entered go round again, either the loop() function or a local while loop, until you get a valid input. Process it, add to the digit count and when 4 have been entered and added to the target set a boolean variable to flag data entry is complete. When the flag is true, and only then, compare the user input with the password.
BulldogLowell:
try something like this:/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
char* storedPassword = "5432";
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (char* enteredPassword = lookForPassword(keypad))
{
Serial.println(enteredPassword);
if (strcmp(enteredPassword, storedPassword) == 0)
{
Serial.println(F("SUCCESS"));
}
else
{
Serial.println(F("EPIC FAILURE"));
}
}
}
char* lookForPassword(Keypad& kpd)
{
static char password[5];
static int idx = 0;
if(char key = kpd.getKey())
{
if (key = '#')
{
idx = 0;
return password;
}
if (isDigit(key))
{
password[idx++] = key;
password[idx] = '\0';
if (idx > 3)
{
idx = 0;
return password;
}
}
}
return nullptr;
}
I tried this but am getting the same result. The program isn't accepting more than 1 character at a time
typo in the function....
try:
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
char* storedPassword = "5432";
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (char* enteredPassword = lookForPassword(keypad))
{
Serial.println(enteredPassword);
if (strcmp(enteredPassword, storedPassword) == 0)
{
Serial.println(F("SUCCESS"));
}
else
{
Serial.println(F("EPIC FAILURE"));
}
}
}
char* lookForPassword(Keypad& kpd)
{
static char password[5];
static int idx = 0;
if(char key = kpd.getKey())
{
if (key == '#')
{
idx = 0;
return password;
}
if (isDigit(key))
{
password[idx++] = key;
password[idx] = '\0';
if (idx > 3)
{
idx = 0;
return password;
}
}
}
return nullptr;
}