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

About: I am UX Consultant, an HCI Researcher and a former teaching assistant 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

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