In the previous post, we made an RFID and Keypad based Access Control using Arduino in which the user had to first scan the right tag and then he had to enter the password for that tag to open the door lock. There was a master tag that was used to add/remove other tags and each tag had its own password.
In this post, we are going to add the Sim900 GSM Module in it to make it an RFID and Keypad based access control and alert system using Arduino. The system will send us messages when the access will be granted or denied. We will also be able to open the door lock or halt the system by sending the message to Arduino. The system will come back to normal mode by scanning the master tag or by sending the ‘open’ message to Arduino.
For Custom Projects, hire me at https://www.freelancer.com/u/Muhammadaqibdutt
This is seventh article of the RFID Arduino series. All articles of RFID Arduino series are as follows
- RFID Basics and RFID Module interfacing with Arduino
- RFID and Keypad based Door Lock using Arduino
- RFID and Keypad based Door Lock and Alert System using Arduino
- RFID based Access Control System using Arduino
- RFID based Access Control and Alert System using Arduino
- RFID and Keypad based Access Control using Arduino
- RFID and Keypad Based Access Control and Alert System using Arduino
On starting the project for the first time, it will ask you to define a master tag and password for it. The master tag will act as programmer and you can use it to add or remove other tags.
After defining the master tag, you will have to add other tags that you can use to open the door. To do this, scan the master tag and enter the password for it and it will take the system into program mode.
In the program mode, scanning the tags will add/Remove these from the system. Scan the tags that you want to use to open the door and enter the password for that tag. The system will store the UID and password of these tags in the EEPROM. Scan the tag again to remove it from the EEPROM. To exit the program mode, scan the master tag.
Now scan the tags that you have added in the system and enter their passwords to open the door and on opening the door lock, it will send us the confirmation message. On scanning the wrong tag or on entering the wrong password, the door will remain closed and it will send us an alert message.
We will also be able to halt the system by sending the ’close’ message to Arduino. During this time, the system will only look for master tag and messages.
To reset the system, press the reset button of Arduino and then long press the wipe button for 10 seconds. This will remove all the data from the EEPROM including the master tag.
Circuit Diagram and ExplanationThe RFID reader communicates with the Arduino through the SPI protocol and different Arduino boards have different SPI pins.
To test if the RFID reader is working properly or not, upload the “dumpinfo” from the examples in the Arduino and see if it is showing the information of the tags on the serial monitor or not. If you are new to RFID, then follow this tutorial | RFID basics and RFID module interfacing with Arduino
The I2C LCD communicates with the Arduino through the I2C protocol. Different Arduino boards have different I2C pins. The I2C pins on Arduino Mega are 20, 21.
Next we are going to connect the keypad with Arduino. The 4X4 keypad has 8 connections but we don’t require the last column of keypad. We only require numbers for the password. So we won’t use the last pin of keypad which is for fourth column. You can also use 4X3 keypad instead of 4X4 keypad.
For powering the SIM900 module, the recommended power to use is 5V, 2A but I have used the 5V, 1.5A power adapter and it worked fine.
Once you have powered the SIM900 module, the power light will light up and on pressing the power key, the status led should light up and the netlight should start blinking. At this point, make a call from your mobile to the sim you have placed in the SIM900 module. If you are successful in making the call, then your sim is working fine with the SIM900 module.
In the end, connect the power source to the Arduino. I have used three 18650 cells. We can give 6 to 12V to the Arduino through the barrel jack.
Add you number in the below line of code. This line is in the last function.
SIM900.println(“AT+CMGS=\”+XXXXXXXXXXXX\””); // Replace it with your mobile number
The complete code for RFID and Keypad based access control and alert system using Arduino is as follows
#include <EEPROM.h> // We are going to read and write Tag's UIDs from/to EEPROM
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
// Create instances
SoftwareSerial SIM900(10, 11); // SoftwareSerial SIM900(Rx, Tx)
MFRC522 mfrc522(53, 5); // MFRC522 mfrc522(SS_PIN, RST_PIN)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
Servo myServo; // create servo object to control a servo
// Set Pins for led's, servo, buzzer and wipe button
constexpr uint8_t greenLed = 8;
constexpr uint8_t blueLed = 7;
constexpr uint8_t redLed = 6;
constexpr uint8_t ServoPin = 9;
constexpr uint8_t BuzzerPin = 4;
constexpr uint8_t wipeB = 3; // Button pin for WipeMode
boolean match = false; // initialize card match to false
boolean programMode = false; // initialize programming mode to false
boolean replaceMaster = false;
uint8_t successRead; // Variable integer to keep if we have Successful Read from Reader
byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM
char storedPass[4]; // Variable to get password from EEPROM
char password[4]; // Variable to store users password
char masterPass[4]; // Variable to store master password
boolean RFIDMode = true; // boolean to change modes
boolean NormalMode = true; // boolean to change modes
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0; // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Initializing pins for keypad
byte row_pins[rows] = {43, 41, 39, 37};
byte column_pins[columns] = {35, 33, 31};
// Create instance for keypad
Keypad newKey = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
//Arduino Pin Configuration
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
// Make sure leds are off
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
//Protocol Configuration
lcd.backlight();
lcd.begin(); // initialize the LCD
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
myServo.attach(ServoPin); // attaches the servo on pin 8 to the servo object
myServo.write(10); // Initial Position
// Arduino communicates with SIM900 GSM shield at a baud rate of 19200
// Make sure that corresponds to the baud rate of your module
SIM900.begin(19200);
//If you set Antenna Gain to Max it will increase reading distance
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
digitalWrite(redLed, HIGH); // Red Led stays on to inform user we are going to wipe
lcd.setCursor(0, 0);
lcd.print("Button Pressed");
digitalWrite(BuzzerPin, HIGH);
delay(1000);
digitalWrite(BuzzerPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("This will remove");
lcd.setCursor(0, 1);
lcd.print("all records");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You have 10 ");
lcd.setCursor(0, 1);
lcd.print("secs to Cancel");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Unpres to cancel");
lcd.setCursor(0, 1);
lcd.print("Counting: ");
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
if (buttonState == true && digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
lcd.clear();
lcd.print("Wiping EEPROM...");
for (uint16_t x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
if (EEPROM.read(x) == 0) { //If EEPROM address 0
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
}
else {
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wiping Done");
// visualize a successful wipe
cycleLeds();
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wiping Cancelled"); // Show some feedback that the wipe button did not pressed for 10 seconds
digitalWrite(redLed, LOW);
}
}
// Check if master card defined, if not let user choose a master card
// This also useful to just redefine the Master Card
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
// EEPROM address 1 should hold magical number which is '143'
if (EEPROM.read(1) != 143) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Master Card ");
lcd.setCursor(0, 1);
lcd.print("Defined");
delay(2000);
lcd.setCursor(0, 0);
lcd.print("Scan A Tag to ");
lcd.setCursor(0, 1);
lcd.print("Define as Master");
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
// Visualize Master Card need to be defined
digitalWrite(blueLed, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(200);
digitalWrite(BuzzerPin, LOW);
digitalWrite(blueLed, LOW);
delay(200);
}
while (!successRead); // Program will not go further while you not get a successful read
for ( uint8_t j = 0; j < 4; j++ ) { // Loop 4 times
EEPROM.write( 2 + j, readCard[j] ); // Write scanned Tag's UID to EEPROM, start from address 3
}
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Master Defined");
delay(2000);
storePassword(6); // Store password for master tag. 6 is the position in the EEPROM
}
for ( uint8_t i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
masterPass[i] = EEPROM.read(6 + i);
}
ShowOnLCD(); // Print on the LCD
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
// System will first check the mode
if (NormalMode == false) { // If the RFID Mode is false then it will only look for master tag and messages
// Function to receive message
receive_message();
// Function to get tag's UID
getID();
if ( isMaster(readCard) ) { // If Scanned tag is master tag
NormalMode = true; // Go back to RFID mode
RFIDMode == true;
cycleLeds();
}
}
else if (NormalMode == true && RFIDMode == true) { // If RFID Mode is true then it will look for all tags and messages
do {
// Function to receive message
receive_message();
if (NormalMode == false) {
break;
}
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
if (programMode) {
cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
}
else {
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
}
}
while (!successRead ); //the program will not go further while you are not getting a successful read
if (programMode && RFIDMode == true) {
if ( isMaster(readCard) ) { //When in program mode check First If master card scanned again to exit program mode
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Exiting Program Mode");
digitalWrite(BuzzerPin, HIGH);
delay(1000);
digitalWrite(BuzzerPin, LOW);
ShowOnLCD();
programMode = false;
return;
}
else {
if ( findID(readCard) ) { // If scanned card is known delete it
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Already there");
deleteID(readCard);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tag to ADD/REM");
lcd.setCursor(0, 1);
lcd.print("Master to Exit");
}
else { // If scanned card is not known add it
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("New Tag,adding...");
writeID(readCard);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scan to ADD/REM");
lcd.setCursor(0, 1);
lcd.print("Master to Exit");
}
}
}
else {
if ( isMaster(readCard)) { // If scanned card's ID matches Master Card's ID - enter program mode
programMode = true;
matchpass();
if (programMode == true) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Program Mode");
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores the number of ID's in EEPROM
lcd.setCursor(0, 1);
lcd.print("I have ");
lcd.print(count);
lcd.print(" records");
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scan a Tag to ");
lcd.setCursor(0, 1);
lcd.print("ADD/REMOVE");
}
}
else {
if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
granted();
RFIDMode = false; // Make RFID mode false
ShowOnLCD();
}
else { // If not, show that the Access is denied
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
denied();
send_message("Someone Tried with the wrong tag");
ShowOnLCD();
}
}
}
}
// If RFID mode is false, get keys
else if (NormalMode == true && RFIDMode == false) {
key_pressed = newKey.getKey(); //Store new key
if (key_pressed)
{
password[i++] = key_pressed; // Storing in password variable
lcd.print("*");
}
if (i == 4) // If 4 keys are completed
{
delay(200);
if (!(strncmp(password, storedPass, 4))) // If password is matched
{
lcd.clear();
lcd.print("Pass Accepted");
lcd.setCursor(0, 1);
lcd.print("Door Opened");
granted();
send_message("Door Opened \n If it wasn't you, type 'close'");
RFIDMode = true; // Make RFID mode true
ShowOnLCD();
i = 0;
}
else // If password is not matched
{
lcd.clear();
lcd.print("Wrong Password");
denied();
send_message("Someone Tried with the wrong tag");
RFIDMode = true; // Make RFID mode true
ShowOnLCD();
i = 0;
}
}
}
}
///////////////////////////////////////// Access Granted ///////////////////////////////////
void granted () {
digitalWrite(blueLed, LOW); // Turn off blue LED
digitalWrite(redLed, LOW); // Turn off red LED
digitalWrite(greenLed, HIGH); // Turn on green LED
if (RFIDMode == false) {
myServo.write(90);
delay(3000);
myServo.write(10);
}
delay(1000);
digitalWrite(blueLed, HIGH);
digitalWrite(greenLed, LOW);
}
///////////////////////////////////////// Access Denied ///////////////////////////////////
void denied() {
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(blueLed, LOW); // Make sure blue LED is off
digitalWrite(redLed, HIGH); // Turn on red LED
digitalWrite(BuzzerPin, HIGH);
delay(1000);
digitalWrite(BuzzerPin, LOW);
digitalWrite(blueLed, HIGH);
digitalWrite(redLed, LOW);
}
///////////////////////////////////////// Get Tag's UID ///////////////////////////////////
uint8_t getID() {
// Getting ready for Reading Tags
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new Tag placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a Tag placed get Serial and continue
return 0;
}
// There are Mifare Tags which have 4 byte or 7 byte UID care if you use 7 byte Tag
// I think we should assume every Tag as they have 4 byte UID
// Until we support 7 byte Tags
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
}
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
/////////////////////// Check if RFID Reader is correctly initialized or not /////////////////////
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
lcd.setCursor(0, 0);
lcd.print("Communication Failure");
lcd.setCursor(0, 1);
lcd.print("Check Connections");
digitalWrite(BuzzerPin, HIGH);
delay(2000);
// Visualize system is halted
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(blueLed, LOW); // Make sure blue LED is off
digitalWrite(redLed, HIGH); // Turn on red LED
digitalWrite(BuzzerPin, LOW);
while (true); // do not go further
}
}
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
void cycleLeds() {
digitalWrite(redLed, LOW); // Make sure red LED is off
digitalWrite(greenLed, HIGH); // Make sure green LED is on
digitalWrite(blueLed, LOW); // Make sure blue LED is off
delay(200);
digitalWrite(redLed, LOW); // Make sure red LED is off
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(blueLed, HIGH); // Make sure blue LED is on
delay(200);
digitalWrite(redLed, HIGH); // Make sure red LED is on
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(blueLed, LOW); // Make sure blue LED is off
delay(200);
digitalWrite(redLed, LOW); // Make sure red LED is on
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(blueLed, HIGH); // Make sure blue LED is off
}
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
void normalModeOn () {
digitalWrite(blueLed, HIGH); // Blue LED ON and ready to read card
digitalWrite(redLed, LOW); // Make sure Red LED is off
digitalWrite(greenLed, LOW); // Make sure Green LED is off
}
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
void readID( uint8_t number ) {
uint8_t start = (number * 8) + 2; // Figure out starting position
for ( uint8_t i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
storedPass[i] = EEPROM.read(start + i + 4);
}
}
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
void writeID( byte a[] ) {
if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
uint8_t start = ( num * 8 ) + 10; // Figure out where the next slot starts
num++; // Increment the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( uint8_t j = 0; j < 4; j++ ) { // Loop 4 times
EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
}
storePassword(start + 4);
BlinkLEDS(greenLed);
lcd.setCursor(0, 1);
lcd.print("Added");
delay(1000);
}
else {
BlinkLEDS(redLed);
lcd.setCursor(0, 0);
lcd.print("Failed!");
lcd.setCursor(0, 1);
lcd.print("wrong ID or bad EEPROM");
delay(2000);
}
}
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
void deleteID( byte a[] ) {
if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
BlinkLEDS(redLed); // If not
lcd.setCursor(0, 0);
lcd.print("Failed!");
lcd.setCursor(0, 1);
lcd.print("wrong ID or bad EEPROM");
delay(2000);
}
else {
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
uint8_t slot; // Figure out the slot number of the card
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
uint8_t looping; // The number of times the loop repeats
uint8_t j;
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
start = (slot * 8) + 10;
looping = ((num - slot)) * 4;
num--; // Decrement the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( j = 0; j < looping; j++ ) { // Loop the card shift times
EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
}
for ( uint8_t k = 0; k < 4; k++ ) { // Shifting loop
EEPROM.write( start + j + k, 0);
}
BlinkLEDS(blueLed);
lcd.setCursor(0, 1);
lcd.print("Removed");
delay(1000);
}
}
///////////////////////////////////////// Check Bytes ///////////////////////////////////
boolean checkTwo ( byte a[], byte b[] ) {
if ( a[0] != 0 ) // Make sure there is something in the array first
match = true; // Assume they match at first
for ( uint8_t k = 0; k < 4; k++ ) { // Loop 4 times
if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
match = false;
}
if ( match ) { // Check to see if if match is still true
return true; // Return true
}
else {
return false; // Return false
}
}
///////////////////////////////////////// Find Slot ///////////////////////////////////
uint8_t findIDSLOT( byte find[] ) {
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
for ( uint8_t i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
// is the same as the find[] ID card passed
return i; // The slot number of the card
break; // Stop looking we found it
}
}
}
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
boolean findID( byte find[] ) {
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
for ( uint8_t i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
return true;
break; // Stop looking we found it
}
else { // If not, return false
}
}
return false;
}
///////////////////////////////////////// Blink LED's For Indication ///////////////////////////////////
void BlinkLEDS(int led) {
digitalWrite(blueLed, LOW); // Make sure blue LED is off
digitalWrite(redLed, LOW); // Make sure red LED is off
digitalWrite(greenLed, LOW); // Make sure green LED is off
digitalWrite(BuzzerPin, HIGH);
delay(200);
digitalWrite(led, HIGH); // Make sure blue LED is on
digitalWrite(BuzzerPin, LOW);
delay(200);
digitalWrite(led, LOW); // Make sure blue LED is off
digitalWrite(BuzzerPin, HIGH);
delay(200);
digitalWrite(led, HIGH); // Make sure blue LED is on
digitalWrite(BuzzerPin, LOW);
delay(200);
digitalWrite(led, LOW); // Make sure blue LED is off
digitalWrite(BuzzerPin, HIGH);
delay(200);
digitalWrite(led, HIGH); // Make sure blue LED is on
digitalWrite(BuzzerPin, LOW);
delay(200);
}
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
// Check to see if the ID passed is the master programing card
boolean isMaster( byte test[] ) {
if ( checkTwo( test, masterCard ) ) {
return true;
}
else
return false;
}
/////////////////// Counter to check in reset/wipe button is pressed or not /////////////////////
bool monitorWipeButton(uint32_t interval) {
unsigned long currentMillis = millis(); // grab current time
while (millis() - currentMillis < interval) {
int timeSpent = (millis() - currentMillis) / 1000;
Serial.println(timeSpent);
lcd.setCursor(10, 1);
lcd.print(timeSpent);
// check on every half a second
if (((uint32_t)millis() % 10) == 0) {
if (digitalRead(wipeB) != LOW) {
return false;
}
}
}
return true;
}
////////////////////// Print Info on LCD ///////////////////////////////////
void ShowOnLCD() {
if (RFIDMode == false) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
}
else if (RFIDMode == true) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Control");
lcd.setCursor(0, 1);
lcd.print(" Scan a Tag");
}
}
////////////////////// Store Passwords in EEPROOM ///////////////////////////////////
void storePassword(int j) {
int k = j + 4;
BlinkLEDS(blueLed);
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0, 1);
while (j < k)
{
char key = newKey.getKey();
if (key)
{
lcd.print("*");
EEPROM.write(j, key);
j++;
}
}
}
void matchpass() {
RFIDMode = false;
ShowOnLCD();
int n = 0;
while (n < 1) {
key_pressed = newKey.getKey(); //Store new key
if (key_pressed)
{
password[i++] = key_pressed; // Storing in password variable
lcd.print("*");
}
if (i == 4) // If 4 keys are completed
{
delay(200);
if (!(strncmp(password, masterPass, 4))) // If password is matched
{
RFIDMode = true;
programMode = true;
i = 0;
}
else // If password is not matched
{
lcd.clear();
lcd.print("Wrong Password");
digitalWrite(BuzzerPin, HIGH);
delay(1000);
digitalWrite(BuzzerPin, LOW);
programMode = false;
RFIDMode = true;
ShowOnLCD();
i = 0;
}
n = 4;
}
}
}
///////////////////////////// Receiving the message//////////////////////////////
void receive_message()
{
char incoming_char = 0; //Variable to save incoming SMS characters
String incomingData; // for storing incoming serial data
if (SIM900.available() > 0)
{
incomingData = SIM900.readString(); // Get the incoming data.
delay(10);
}
// if received command is to open the door
if (incomingData.indexOf("open") >= 0)
{
myServo.write(90);
NormalMode = true;
RFIDMode == true;
send_message("Opened");
delay(10000);
myServo.write(0);
}
// if received command is to halt the system
if (incomingData.indexOf("close") >= 0)
{
NormalMode = false;
RFIDMode == false;
send_message("Closed");
}
incomingData = "";
}
//////////// Send the message////////////////////////
void send_message(String message)
{
SIM900.println("AT+CMGF=1"); //Set the GSM Module in Text Mode
delay(100);
SIM900.println("AT+CMGS=\"+XXXXXXXXXXXX\""); // Replace it with your mobile number
delay(100);
SIM900.println(message); // The SMS text you want to send
delay(100);
SIM900.println((char)26); // ASCII code of CTRL+Z
delay(100);
SIM900.println();
}
Video
Comments