RFID Door Access
 Page :   [ 1 ]    [ 2 ]    [ 3 ]  

After the hardware was made it was time to get some software going.  As for a GPIO library, first i tried WiringPi but was not getting good results.  I then tried pigpio which also happened to have a nice little Wiegand example on their site to use as a starting point!  This library and the example immediately resulted in accurate and reliable results when reading cards.

 

The following C program is run from /etc/rc.local when the system boots.  Whenever a card is scanned it hands it off to some PHP script I put together to do the work of any business logic associated with if the card is valid and if the door should be opened.  Depending on the exit code from the PHP script determines what action to take.  I also have a "master mode" which is activated whenever a "master" card is scanned.  If a master card is scanned then any card scanned for the next 10 seconds will be added to the system.  Also if there is no cards at all in the system then the very first card scanned is a master card.  I also made a web interface to manage the cards, locations and times of day a card is granted access to the building.

 

/*

REQUIRES

Wiegand contacts D0 and D1 connected to separate gpios (in this case defined as 14 and 15).

TO BUILD

gcc -o wiegand test_wiegand.c wiegand.c -lpigpio -lrt 

TO RUN

./wiegand

*/

#include <stdio.h>
#include <pigpio.h>
#include "wiegand.h"

typedef int bool;
#define true 1
#define false 0

#define DOOR_01 "FDOOR"
#define DOOR_01_D0 14
#define DOOR_01_D1 15
#define DOOR_01_SIG 12

char outBuffer[512];

static uint32_t waitSeconds = 5;
static uint32_t doorOpenStart = 0;

bool doorOpen = false;
double startDoorOpen;

bool masterMode = false;
double startMasterMode;




void callback1(int bits, uint32_t value){

	sprintf(outBuffer,"/usr/bin/php /var/scripts/wiegand.php \"%u\" \"%d\" \"%s\" \"%d\" ", value,bits, DOOR_01,masterMode);

	int i = system(outBuffer);

	if (i == 1024){

		if (!masterMode){
			doorOpen = true;
			startDoorOpen = time_time();
		} else {
			doorOpen = false;
		}

	} else if (i == 1280) {
		
		doorOpen = false;
		masterMode = true;
		startMasterMode = time_time();
	
	}
   
}




int main(int argc, char *argv[]){

	Pi_Wieg_t * w1;

	if (gpioInitialise() < 0){
		return 1;
	}
	
	gpioSetMode(DOOR_01_SIG, PI_OUTPUT);
	gpioWrite(DOOR_01_SIG, 1); 

	w1 = Pi_Wieg(DOOR_01_D0, DOOR_01_D1, callback1, 5);

	while(1){
 
 		if (masterMode){
 		
 			if ((time_time() - startMasterMode) < 10.0){
		 		masterMode = true;
	 		} else {
				masterMode = false;
 			}	
 		
 		} else {
 		
	 		if (doorOpen){
		 		if ((time_time() - startDoorOpen) < 5.0){
			 		doorOpen = true;
			 		gpioWrite(DOOR_01_SIG, 0);
	 			} else {
					doorOpen = false;
					gpioWrite(DOOR_01_SIG, 1);
	 			}	
	 		}

 		}

 		sleep(1);
		
	}

	Pi_Wieg_cancel(w1);
	
	gpioTerminate();

}

When running the program doesn't hog too much resources.

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  527 root      20   0    9856   1788   1584 S   7.5  0.2 803:06.12 wiegand

 

Here are some screen grabs of various parts of the web interface.

The main landing page:

Assigning names to cards.  The "label" here is simply for arbitrary labeling of the physical card:

For editing the times of day a card can access any given location (in this case the Front Door):

Day and time history of successful card usage:

 

 

(Page 3 of 3)