Raspberry Pi Door Lock 03

 

 

This project will show how to control door lock system using basic free code via keyboard.

 

 

All you need is:
1x Raspberry Pi
1x USB Keyboard
1x Electromechanical Strike
1x USB One Channel Relay controller

 

 

 

Electromechanical Strike
http://www.amig.es/en/strike-body-mod-18-19/g/372

 

 

 

 

 

By entering simple PIN code (example 1234 & ENTER) on the keyboard you can open the door.

 

 File : on.sh

#!/bin/bash
# sends on signal to the USB relay
echo -e "\xFF\x01\x01" > /dev/ttyUSB0 ;sleep 1
echo -e "\xFF\x01\x00" > /dev/ttyUSB0 

 

 

 

File : keyboard.c

 

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>
 
static const char *const evval[3] = {
    "RELEASED",
    "PRESSED ",
    "REPEATED"
};
 
char PIN[4] = {79,80,81,75}; //1,2,3,4
char inData[4] = {0,0,0,0}; //Input Buffer
char *ptr;
int cntr = 0; //Counter
 
int main(void)
{
    const char *dev = "/dev/input/event0";
    struct input_event ev;
    ssize_t n;
    int fd;
    ptr = &inData[0];
 
    fd = open(dev, O_RDONLY);
    if (fd == -1) {
        fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
        return EXIT_FAILURE;
    }
 
while (1) {
        n = read(fd, &ev, sizeof ev);
        if (n == (ssize_t)-1) {
            if (errno == EINTR)
                continue;
            else
                break;
        } else
        if (n != sizeof ev) {
            errno = EIO;
            break;
        }
  if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2)
           // printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);
            if(ev.value == 1){//Pressed
if(ev.code == 96){//Enter
if(memcmp(PIN,inData,4) == 0)
{
printf("Code accepted!\r\n");
popen("exec /home/pi/keyboard/./on.sh","r");
}
ptr = &inData[0];
memset(ptr,0,4);
cntr = 0;
}
else{
if(cntr == 4){//$ digits limit
cntr = 0;
ptr = &inData[0];
}
*ptr = ev.code;
ptr++;
                cntr++;
}
 
}
 
    }
    fflush(stdout);
    fprintf(stderr, "%s.\n", strerror(errno));
    return EXIT_FAILURE;
}

 

 

View how to compile C Files 

http://info.kmtronic.com/how-to-compiling-c-and-c-programs.html

 

 

All Files DOWNLOAD

 

 

 

 

 

 

 

Read 4850 times