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
     // Open the gates
// Reset code


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

1 Answer 1


First I believe this is a design problem rather than a technical issue: you need set a time limit to user input so previous incomplete input won't mess up with successive ones.

The most straight forward way I can figure out to implement this logic with minimum CPU expense would be using a timer interrupt to set a global timeout flag, and detect this flag in your main while loop. If the flag is set we validate user input immediately and clear the input buffer and flag, and reset the timer so the program is ready to process next user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.