Building a Web Enabled Door Lock Using Rest API and Raspberry PI


Introduction: Building a Web Enabled Door Lock Using Rest API and Raspberry PI

About: I am an HCI Researcher and Teaching Assitant in information science working in Germany. An Electronic and software hobbyist.

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


var gpio = require("pi-gpio"); var gpioPin = 7;  
var isOpened=false; 
var unlockRelay = function(){ 
/* 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);


var http = require('http'); var express = require('express');
var app = express();
var GPIOCtrl = require('./GPIOcontroller.js');
app.get('/unlock/', function(req, res){ 
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.




    • Oil Contest

      Oil Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Clocks Contest

      Clocks Contest

    27 Discussions

    Do you have a spec list for the Transistor, resistor, diode and relay that you chose to use?

    1 reply

    As mentioned above by @msnyman, use a "KEYES 5V Relay Module", you do not need a transistor and diode.

    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!

    2 replies

    The diode at the transistor is called a flyback diode. Inside the relay is a coil, and the coil builds up a magnetic field as the relay is switched on. So what happens if the relay switches off is that this magnetic field collapses and this cause a flow of current in the coil. But because the transistor is now in the off state there would be no path for the currnet to flow, so the diode provides this path for that current. The Diode protects the transistor from being damaged.

    Get a "KEYES 5V Relay Module", no need for transistor and diode, all build in.

    This would be available online. It just need 5V, Ground and connect GPIO pin 7 to the signal input.

    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 ;-)

    There are two errors:

    1: You need to add module.exports.unlockRelay = unlockRelay; to GPIOcontroller.js

    2: You need to add, "output"); to the beginning of GPIOcontroller.js

    2 replies

    Between which lines do you add "module.exports.unlockRelay = unlockRelay"?

    Ya true the export I forgot to write in the instructable ... thnk u will do

    Hi, could you provide me which electronic lock you use??

    Thanks a lot!


    1 year ago

    Hi Hesham, great initiative! I'm curious which electronic lock you use? could you give a link to the item?

    Thanks a lot!

    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.

    2 replies

    You can send me inbox your contact, I think you have interesting ideas. Regards

    Hey, this might be a stupid question. But were would you get the wifi from? Mobile network would seem costly.

    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?

    Hope you are still tracking this since its been up for a few years.. sure has been helpful to me.

    1 reply

    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


    2 years ago

    Does anyone have a parts list?


    2 years ago

    Which dorr unl;ocker u are using kindly tell me?

    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´m using the wrong linux installation

    Can someone tell me where can I find inexpensive Bluetooth locks compatible with opensource? I have an urgent need.