**Group Tembo**\\ Arturo Apú Chinchilla. B20386\\ Erick Eduarte Rojas. B22305\\ Luis Felipe Rincón Riveros. B25530\\ ======= Description ======= The main goal of the project is to create a device that controls an electric latchkey with a circuit (with a relay and a transistor), a RaspberryPi and a Bar Code reader.\\ The RaspberryPi will receive a serial number from the Bar Code reader from the user's card. This number will travel from the RaspberrryPi to a server with a database. The database will have the registers of the authorized users (authorized by an administrator) to enter the room. Only if the user is authorized, the server will return a positive answer to the RaspberryPi for it to activate the circuit that sends the signal to the latchkey and opens it. ======= Justification ======= The project's goal is to control the entrance to the ArcosLab or any other room at the Escuela de Ingeniería Eléctrica. The device to be created is a low cost one; the RaspberryPi and the electric components are cheap artecfacts. It will include a permisions' database which only allows athorized people into the room.The device will also be usefull to keep a register of the people that enters the room. ======= Objectives ======= ===== General ==== - Build electric latchkey controlling device with a Bar Code reader, controlled by a RaspberryPi that comunicates with a database with user permissions. ===== Specific ===== - Design a electric circuit (with a transitor, a magnetic relay and a optoacoupler) to controll a electromecatic lock KP321 Assa Abloy Phillips. - Create a database, using SQlite, with the authorized users in specific periods. - Configurate the RaspberryPi to controll the circuit and to conect it-self with the database. ====== Contents ====== ===== Installing Raspbian ===== {{:ie0117_proyectos_2013:raspbian_logo.png?200|}} Raspbian is a Debian variant used for the Raspberry Pi hardware ((Further information about Raspbian at [[http://en.wikipedia.org/wiki/Raspbian]] and [[http://www.raspbian.org/]])) * Donwload the Raspbian "wheezy" image at the official Raspbian webpage. http://www.raspberrypi.org/downloads Unzip the image and write it to a empty SD card (4GB or more) * Extrac the image unzip ~/Donwloads/2012-12-16-wheezy-raspbian.zip Assuming that you put the zip file in ~/Donwloads/ df -h See what devices are currently mounted. Then, insert the card. The device that was not there the last time is the SD card. The device will appear as "/dev/sdd1" or similar. * Umount it. umount /dev/sdd1 * Write the image to the SD card. sudo dd bs=4M if=~/Downloads/2012-12-16-wheezy-raspbian.img of=/dev/sdd This may take 5 minutes or more. "dd" does not show its progress. sudo sync This ensures the write cache is flushed. * Remove the SD card from the computer and insert it in the Raspberry Pi. ===== Configuring the RaspberryPi ===== The RaspberryPi configuration (Raspi-config) is quite simple and known if you already intalled Debian. {{:ie0117_proyectos_2013:raspi-config_main.png?200|}} * Set your timezone. * Select //expan_rootfs// and responde //yes// to reboot. * After rebooting the device will ask for an user and password. Log in with the user "**pi**" and the password "**raspberry**". * You'll see something like **pi@raspberry ~ $**. * Enter the graphic interface startx If any doubt remains, consult [[http://elinux.org/RPi_Beginners]]. ===== Installing WiringPi ===== WiringPi is a library that allows to control the out signal in the RaspberryPI. It also can be used to read from the pins ((Further information about WiringPi at [[https://projects.drogon.net/raspberry-pi/wiringpi/]])). WiringPI is maintained under GIT. * Install GIT * sudo apt-get install git-core * Obtain WiringPi git clone git://git.drogon.net/wiringPi The script in the WiringPi directory compiles and intalls WiringPi cd wiringPI ./build ===== Installing WiringPi-python ===== This installation is useful if you want to control the pins using python * Install python-dev sudo apt-get install python-dev * Then the WiringPi-python git clone https://github.com/WiringPi/WiringPi-Python.git cd WiringPi-Python git submodule update --init python setup.py install ===== Installing ZBar ===== {{:ie0117_proyectos_2013:zbar.200.png?100|}} ZBar Bar Code Reader is software suite for reading bar codes ((Further information about ZBar at [[http://zbar.sourceforge.net/]])). * Install ZBar tools sudo apt-get install zbar-tools * Start the code reading zbarcam ===== Installing Camorama & Making python codes ===== Camorama is a gnome utility to view and save images from a webcam that allows to take snapshots in different times. * Intalling Camorama sudo apt-get install camorama Now we implemented Zbar and Camorama in python codes to simplify and zip the function of the Raspberry pi. These are the two codes implemented: * Using Zbarcam #!/usr/bin/env python import os #open zbarcam in a console p=os.popen('/usr/bin/zbarcam','r') #print the code if the zbarcam read something while True: code = p.readline() print 'Got barcode:', code * Using Zbar Image #!/usr/bin/env python import os import time #open camorama to take pictures each minute c=os.popen('/usr/bin/camorama','r') #analyzes the last picture taken wich is called Webcam.png while True: time.sleep(70) p=os.popen('/usr/bin/zbarimg'+' Webcam.png','r') code = p.readline() print 'Got barcode:', code ===== Remote accessing ===== sshfs is a filesystem client based on the SSH File Transfer Protocol * Install sshfs in the computer you will use to control the Raspberry pi sudo apt-get install sshfs * Then you will need to find the IP address from the Raspberry pi using the command (in Raspbian): sudo ifconfig Another way is to install the package nmap into your computer and run into a console: nmap -O * Once you have the IP address from the Raspberry pi, run this command to log in as root and have permissions to display windows: ssh -X pi@ It will ask you for the user password: 'raspberry' ===== Designing and creating the circuit ===== Resistors, an optocoupler, a diode, a transistor and a relay where elements necesary to build the circuit. {{:ie0117_proyectos_2013:untitled.png?300|}} The 3V source is the RaspberryPi's GPIO pin and the ground is the RaspberryPi's GND pin. Connected to the positive of that source is a 1KΩ resistor to reduce the tension and effectivly control the Darlington (NTE3044) optocupler. This device lights its internal led when the RaspberryPi sends a specific signal. At this point, the current travels to another resistor that protects a 2N2222 transistor. The transistor amplifies the current from the optocoupler. The transistor is connected to the 5V source-controlled relay and to a diode for protection. The relay's common terminal is connected to one of the latchkey's wire; the relay's normaly-open terminal goes to the 12V source that feeds the latchkey.\\ The optocoupler isolates one part of the circuit from the other. This protects the RaspberryPi components and the rest of the circuit's components.\\ ===== Creating a database with SQLite ===== {{:ie0117_proyectos_2013:200px-sqlite370.svg.png?200|}} The database to create is a //relational database//. It establishes inter-relations between the data following the relational model ((Further information about the Relational model at [[http://en.wikipedia.org/wiki/Relational_model]])). SQlite will be the database management system. SQLite is in a relatively small library (275 kiB) but allows databases as big as 2TB ((Further information about SQLite at [[http://en.wikipedia.org/wiki/SQLite]] and [[http://www.sqlite.org/download.html]])).\\ In this database will be the authorized users to enter the room in a specific schedule. The database will have 4 tables.\\ A table 'Usuario' with the user's information. A specific number ('Num_Usuario' the primary key) and the person's characteristics ('Name', 'Email', and 'Doc_Idenficacion').\\ Another table 'Tarjeta' with the information related to de card used by the user. This is 'Num_Serie', the number in the bar code (primary key); 'Esta_Activa' a boolean that tells if the card is active or not; and 'Num_Usuario' that tells which user is related to the specific card.\\ A third table 'Horario' with the period in which the users are authorized to enter the room. 'ID' a number related to a user (primary key); a time 'desde' and a time 'hasta'. The user can enter the room from time 'desde' until time 'hasta'.\\ A final table 'Tarjeta-Horario' references 'Tarjeta' and 'Horario'.\\ {{:ie0117_proyectos_2013:diagrama_arturo.png?300|}} * Download the precompiled binaries for Linux http://www.sqlite.org/download.html Then, run in the terminal sqlite3 You are know in the command line shell for SQLite. * Creating table 'Usuario' sqlite> create table Usuario ( ...> Num_Usuario int NOT NULL primary key, ...> Nombre varchar(50) NOT NULL, ...> Email varchar(50) NOT NULL, ...> Doc_Idenficacion varchar(50) NULL ...> ); * Creating table 'Horario' sqlite> create table Horario ( ...> ID int NOT NULL primary key, ...> Desde time(7) NOT NULL, ...> Hasta time(7) NULL, ...> Doc_Idenficacion varchar(50) NULL ...> ); * Creating table 'Tarjeta' sqlite> create table Tarjeta ( ...> Num_Serie varchar(100) NOT NULL primary key, ...> Activa bit NOT NULL, ...> Num_Usuario int NULL, ...> ); * Creating table 'Tarjeta_Horario' sqlite> create table Tarjeta ( ...> Num_Serie varchar(100) NOT NULL primary key, ...> ID int NOT NULL, ...> Es_Valida bit NOT NULL, ...> FOREIGN KEY Num_Serie REFERENCES Tarjeta ...> FOREIGN KEY ID REFERENCES Horario ...> ); ======= Results & Conclusions ======= * A circuit capable of opening the latchkey was created. Wiring-pi was succesfully installed. The coupling of these parts will be done later because the compatibility, regarding the safety circuit of RaspberryPi, must be verified. So, by the time, the RaspberryPi's voltage was simulated with two AA batteries. * It was possible to implement a remote computer to control the RaspberryPi that also handles the webcam. * It was achieved reading barcodes implementing programs in python, either directly to video or taking a photo and if possible decode data in it. * A sample relational database was created. In the second part of the project will be modified as needed to give input permits users in times needed.