loading

In this instructable, we will build a web enabled electronic door lock from scratch using RESTFUL API, Node JS and Raspberry PI to provide the web service consumed by the mobile app or any REST client.

The Architecture is divided in two 3 parts :

  1. The REST Server (NodeJS, express and pi-gpio) installation and configuration
  2. The Lock and Raspberry PI hardware connection (NPN-Transistor, 12V Relay ... etc)
  3. The Server application code to control the lock and publish the REST functions used by the REST client
  4. The REST Client (IPhone App)

Step 1: Install and Configure the Raspberry PI

First you need a install and configure a RESTFUL server to accept connections over the internet, and a framework to access the Raspberry PI-GPIO pins for reading and writing data over the pins.

  1. Configure you Raspberry PI to access your wireless network (you can buy a wireless dongle and connect through the Raspberry PI USB interface. (How-To Link)
  2. Install Node JS on Raspberry PI. (How-To Link)
  3. Install pi-gpio on Node.JS to access Raspberry PI pins through nodeJS (Install PI-GPIO)
  4. install express on Node.JS to create the RESTFUL Server (Install express)
  5. Create two files the GPIOcontroller.js file (functions responsible for the control of the hardware pins and lock) and myService.js (the service that will be called by node in console to run the server, it will call the GPIOcontroller.js functions) ... This will be demonstrated later in -- Step 3 --

Now The Hardware !

Step 2: Building the Hardware Between the Raspberry PI and the Lock

1 Raspberry PI

From the Raspberry PI, we will use GPIO pin 7, GRD and 5V output, as the GPIO pin producing 3.3V its not able to activate the solenoid of the relay, due to the high current drawn leading to a voltage drop below 3V. Thus we can not depend on the GPIO voltage output to open the relay switch.

2 Amplification

Using an NPN-Transistor, a Diode and a Resistor, we use the transistor to channel the 5V output from the Raspberry PI to the contact relay using the input from the GPIO output as a signal to open and close the transistor to activate the relay switch. (Check Breadboard)

3 The Relay

(Link to instructional video for the relay)

The 12V relay has 5 legs, two legs which create the current in the solenoid, thus opening the switch, and the three other legs usually

  • middle leg for the common.
  • one leg for the normal close (when relay activated, the switch opens the circuit).
  • one leg for the normal open (when relay activated, the switch closes the circuit).

4 Electronic Switch

The Electronic lock will have one pin connected to the GRD of the 12V power adaptor, and the other connected to the relay common (middle leg), while the relay normal open pin is connected to the 12V power adaptor VCC

Step 3: Server Application Code (RESTFUL API and PI-GPIO)

Returning again to the software code, remember the two files (GPIOController.js and myService.js) from step 2

GPIOcontroller.js

var gpio = require("pi-gpio"); var gpioPin = 7;  
var isOpened=false; 
var unlockRelay = function(){ 
if(!isOpened){
/* Open the door lock */
	gpio.write(gpioPin, 1, function() {
	isOpened = true;
	});
/*setTimeOut will be activated in 2 seconds, closing the lock as we set gpioPin value to 0*/
	setTimeout(function () {
	 gpio.write(gpioPin, 0, function() {
	isOpened = false;
	});
	}, 2000);
    }
}

myService.js

var http = require('http'); var express = require('express');
var app = express();
var GPIOCtrl = require('./GPIOcontroller.js');
app.get('/unlock/', function(req, res){ 
	GPIOCtrl.unlockRelay();
 });
app.listen(3000); 
console.log('App Server running at port 3000');

Then in the terminal, at your application code file level

node myService.js &

The console will print "App Server running at port 3000"

To make this script run on Raspberry PI StartUp (Check this Link)

Step 4: Create the REST Client

By now you have a service running on the IP assigned to the Raspberry PI Wireless interface, and the port 3000.

To connect the Raspberry PI to the cloud, you have to check your wireless router settings (out of scope)

for now we can assume that the client is in the same network as the Raspberry PI.

Therefore, it is just a matter of a call, you can added it within seconds in your application :

// Create the URL to make the rest call.
NSURL *restURL = [NSURL URLWithString:@"http://raspberrypi-address:port/unlock/"];
NSURLRequest *restRequest = [NSURLRequest requestWithURL:restURL];
currentConnection = [[NSURLConnection alloc] initWithRequest:restRequest delegate:self];

This is an iOS code call example.

Thank you.

Hesham

<p>Hi Hesham, great initiative! I'm curious which electronic lock you use? could you give a link to the item?</p><p>Thanks a lot!</p>
Do you have a link to the electronic switch (item 4 on the parts list)? Also, can you explain why a diode is needed in front of the transistor. Many thanks!
<p>I have a project this would be useful on. Looking for ideas on how to use internet connected (wifi) pi and electronic door locks to be normally open (no power) but locked otherwise. The ideas is to be able to lock our storm shelter doors but if sent a command to open, or a button is pressed on the door for 10 seconds, an alarm will sound and the door will unlock - also activating our pi camera and sending notifications via email or ftp. I am the owner of a small mobile home park with very low income residents and I am now providing them free wifi - we are upgrading a lot of things but are on a tight budget. I thought this would be a good start to my project goals and that I might find some willing helpers to help me figure out what hardware I might need or give me some better ideas. I am not new to tech - I am a former US Navy ET and program a lot - just new to the Pi and Arduino world. A neat update to this idea would be to have the pi monitor weather undergound API for forecasts and national weather center watches and warnings in the area and to automatically unlock the doors or activate some sort of warning system (lights, email list to message residents of alerts in the area.. things like that). If any of you might like to help with this please let me know. I will be playing with this instructable tonight.</p>
<p>You can send me inbox your contact, I think you have interesting ideas. Regards</p>
<p>Hey, this might be a stupid question. But were would you get the wifi from? Mobile network would seem costly.</p>
<p>Excellent project! I made it flash a LED while waiting for more hardware. Being new to javascript I struggled to get it to work, but this made me learn a lot ;-)</p><p>There are two errors:</p><p>1: You need to add module.exports.unlockRelay = unlockRelay; to GPIOcontroller.js</p><p>2: You need to add gpio.open(gpioPin, &quot;output&quot;); to the beginning of GPIOcontroller.js</p>
Ya true the export I forgot to write in the instructable ... thnk u will do
<p>Thanks for taking the time to post.. I have a question, it is my understanding that a index.html is still required? If not, how are you creating your webpage with the content, ie buttons and text?</p><p>Hope you are still tracking this since its been up for a few years.. sure has been helpful to me.</p>
The buttond were created in ios using objective c and the raspberry pi only provides a rest api link to open the lock which u call from the ios app .. hence no webpage or index.html
<p>Does anyone have a parts list?</p>
<p>Which dorr unl;ocker u are using kindly tell me?</p>
<p>is it possible to get the compleate image of the installation for burning it on the sd? cant get it work on several installations, maby i&acute;m using the wrong linux installation</p>
<p>Can someone tell me where can I find inexpensive Bluetooth locks compatible with opensource? I have an urgent need. </p>
<p>Hey bud, </p><p>Sorry I'm fairly new at electronics... I've got everything for this except the diod and the transistor. I've looked on maplin.co.uk and there's a lot of each. Is there any specific one/specs for each I should be going for?</p><p>Thanks, </p><p>Mark</p>
<p>Hello HeshamO,</p><p>Thanks for sharing this great idea. If I am not wrong, we could simply open the url <a href="http://raspberrypi-address:port/unlock/" rel="nofollow">http://raspberrypi-address:port/unlock/ </a> (raspberrypi-address would be replaced by the ip for raspberry pi and port would be replaced by 3000), it should unlock the door using this setup. Is it so?</p><p>Thanks and Regards,</p><p>TJ</p>
Ya exactly ... it's just a web procedure call ;) ...
<p>hello from France, is it possible to use it with Andoid ?? and how ?</p>
<p>Sorry for the late reply, I did not see your post ... just use the address and port, similar to the one under the cloud as in the pictures above using your Android REST Client.</p><p>You can find many Android REST Clients on the web</p><p>The main architecture here is that the Raspberry PI is expecting a web call using the address aforementioned to send the command to the contact relay.</p><p>I hope this responded to your question.</p><p>Regards,</p>
<p>Very nice! Thanks for sharing this great idea. </p>
<p>Thank you </p>

About This Instructable

88,472views

121favorites

License:

Bio: I am an HCI Researcher and Teaching Assitant in information science working in Germany. An Electronic and software hobbyist.
More by HeshamO:TextPlayBulb : REST Enabled PlayBulb using Raspberry Pi 3, BLE and Telegram Building a Web Enabled Door Lock using Rest API and Raspberry PI 
Add instructable to: