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

100,521

126

32

Posted

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

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

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    27 Comments

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

    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!

    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 gpio.open(gpioPin, "output"); to the beginning of GPIOcontroller.js

    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!