Door lock

From Neuromeka Wiki
Jump to: navigation, search
BB-Air


Introduction

This page shows you how to make door lock system with BBAir.
Before begin to implement door lock system, you have to buy some electronic components such as door striker, power supply, etc.
In this door lock system, you can open the door by using NFC(Near Field Communications) module and web server.
You can change the way you open the door. It is up to you.
Below is the components list used In this project

( This presume, of course, that you already have BBAir or beaglebone series. )

If all components are ready, Let`s make it.

Hardware schematic

Door lock hardware schematic.png

You should wire each component correctly before door lock striker works.
First step is connecting NFC module with BBAir. The communication way between BBAir and NFC module is UART communication. You can get ID number of NFC Tag with this module.
Next step is connecting door striker with BBAir. But in most case, it is impossible to connect door striker with BBAir directly. The GPIO output voltage of BBAir is 3.3V, whereas door striker's input volatage is 12v ~ 24v.
So you have to make 3.3v to 12v level transfer module. Next section shows you how to do it.
(In this article, exclude explanation of pin connection for the reason there are various devices.)

Circuit diagram of transistor

Doorlockcircuit.png

To understand this picture is not easy for someone who don`t know the meaning of symbol.

Software schematic

Door lock software schematic.png

First of all, you have to study what html, javascript, node.js and c/c++ are. And then you can make this software system.
Here is example code.

Reading NFC code (c/c++)

Reading NFC module code with c/c++ is kind of difficult to explain it detail. I write this code with PN532 library.
Here is PN532 library link page https://github.com/Seeed-Studio/PN532/tree/master/PN532_HSU

 1   extern uint32_t nfc_val;                                                // get extern NFC data from PN532_HSU library 
 2   while (1)
 3   {
 4    send_nfc_request(fd, PN532_COMMAND_INLISTPASSIVETARGET);	          //send request to NFC module ( send_nfc_request function is in PN532 library)
 5    if (pn532_receiving(fd, 100)==0) continue;	                          //receiving data from NFC module ( pn532_receiving function is in PN532 library)   
 6    if (nfc_measure()==1)	                                          //process validate data ( nfc_measure function is in PN532 library)
 7    {
 8     const char str[8];                                                    
 9     sprintf(str,"%06x",nfc_val);                                          //nfc_val(uint32_t) to char
10     char line_storage[100], buffer[100];                                    
11     int check, line_num = 1;
12     input = fopen("/root/id.txt", "r");                                   //check id number of NFC tag
13     while( fgets(line_storage, sizeof(line_storage), input) != NULL )  {  
14      check = 0;
15      sscanf(line_storage,"%s",buffer);
16      if(strcmp(buffer,str) == 0)  check = 1;  
17      if(check == 1){                                                       // check that the number exist in id list
18      printf("%s found on line %d \n",str, line_num);
19      onpin(1,pin28);                                                       // set value of gpio output to 1 (open) - this onpin function NRMK made
20      usleep(30e5);                                                         // delay 3sec - this usleep function NRMK made
21      offpin(0,pin28)                                                       // set value of gpio output to 0 (close) 
22     }
23     else
24      offpin(1,pin28);                                                      // if there is no id number in id list, close the door
25     line_num++;
26    }
27     nfc_data_ready=0;
28     usleep(5e5);                                                            
29    }
30   }

node.js code(server)

Initialize server

 1     var http = require('http'), fs = require('fs');         // npm module http, fs
 2     var server = http.createServer(function (req, res) {    // http server create
 3     var file = '.'+((req.url=='/')?'/login.html':req.url);  // save web page(login.html) location
 4     var fileExtension = path.extname(file);
 5     var contentType = 'text/html';                          // set content type
 6     fs.exists(file, function(exists){                       // check reading web page file
 7         if(exists){
 8             fs.readFile(file, function(error, content){
 9                 if(!error){
10                     res.writeHead(200,{'content-type':contentType});
11                     res.end(content);
12                 }
13             })
14         }
15         else{
16             res.writeHead(404);
17             res.end('Page not found');
18         }
19       })
20     }).listen(8888);                                         // port 8888

open the door

 1     var b = require('bonescript'), io = require('socket.io').listen(server), winston = require('winston'); // npm module bonescript, socket.io and winston.
 2     var door_signal = "P9_12";                                                                    // BBAir GPIO output pin number
 3     b.pinMode(door_signal, b.OUTPUT);                                                             // set P9_12 output direction
 4     io.on('connection', function (socket) {                                                       // get string data from web page
 5         socket.on('changeState', handleChangeState);                                              // if open button click on web page, get the value
 6         socket.on('send_name', getName);                                                          // get who click open button on web page
 7     });
 8     function getName(data){                                                                       // save temporally name data
 9         nameData = JSON.parse(data);
10     }
11     function handleChangeState(data) {                                                            // the gpio pin value change when get the value(open the door)
12       var newData = JSON.parse(data);
13       winston.log('debug',nameData.name);                                                         // log name data
14       b.digitalWrite(door_signal, newData.state);
15       setTimeout(function(){                                                                      // after 5sec gpio pin value change automatically(close the door)
16         newData.state=0;
17         b.digitalWrite(door_signal, newData.state);
18         console.log("Door Close");
19       }, 5000);
20     }