0

I have an RFID keypad linked to my Raspberry Pi. It sends out a Wiegand 26 signal when someone presses a key or presents a card/fob. The code is based on http://abyz.me.uk/rpi/pigpio/examples.html

I can read the codes fine and write to GPIO (which operates a relay to open some electric gates) but the problem is that if someone were to press a single key, for example, then the code would work.

Let's say entry code is "2345". Should a user type "2345" into the keypad, then the programme will build the code string which I can subject to an authenticity test. The programme then resets the code and continues to wait for another one.

However, should someone type, say "1" into the keypad and then run away, when a genuine user enters "2345" the programme will recognise the code as "1234" which will fail the strcmp() test. It will also set the first digit of variable "code" to "5", which makes the problem continue.

I need to be able to reset the code if another key isn't pressed within, say, 5 seconds. I've fudged a solution for now by comparing time() variables but this needs to continuously call the time() function within the while() loop so is processor heavy.

This will need to run in the background continuously

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

char code[15]; // Code to be processed (could be 4 digits, could be 8)

void callback(int bits, uint32_t value)
{
printf("bits=%d value=%u\n", bits, value);

// Construct the code
sprintf(code,"%s%u",code, value);

if(strlen(code)>=4){ // 4 digit pin must have been entered or card presented
     printf("\nCode %s has been entered of length %d",code,strlen(code));
// Do authenticate stuff here
     if(strcmp(code,"2345")==0){
     // Open the gates
     ...
     }
// Reset code
     memset(code,0,15);

}
}

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

if (gpioInitialise() < 0) return 1;

w = Pi_Wieg(14, 15, callback, 5); 
while(1){ // run indefinitely
sleep(1); // does this need to be here at all?
}
// Not sure how these will be called as have to terminate programme with CTRL-C
Pi_Wieg_cancel(w);
gpioTerminate();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.