Web Application with Philips hue and motion sensor on Raspberry Pi
On this tutorial we are going to set up a WebApp that allows us to switch on Philips hue bulbs through a motion sensor. We’ll use node.js on the server side and a Little script of python to read data from the sensor.

Ingredients:
- A Raspberry Pi, with xway OS installed (download here!)
- WiFi dongle (I use TP-link TL-WN725N)
- Philips hue starter kit
- Motion sensor
- Protoboard
- Some wiring
I let you here a tutorial about how to install xway OS

Application Structure
The structure should be as follow:
/sensorHueLights
| __ bootstrap.min.css
| __ bulb-icon.png
| __ index.htm
| __ index.js
| __ node_modules/
| __ express
| __ minimist
| __ node-hue-api
| __ socket.io
| __ package.json
| __ README.md
| __ sensor.py
| __ server.js
| __ style.css
This tutorial about how to create and install and app on xway could be very useful :]
package.json
All the app information and node dependencies are in this file. In this case, we need to include express, minimist (for command line parser), socket.io and node-hue-api.
{
“name”: “SensorHueLights”,
“logo”: “bulb-icon.png”,
“version”: “0.0.1”,
“description”: “This is an app which allows you manage your philips lights with a presence sensor”,
“main”: “server.js”,
“dependencies”: {
“express”: “^4.12.3”,
“minimist”: “^1.1.1”,
“socket.io”: “^1.3.5”
},
“devDependencies”: {},
“scripts”: {
“test”: “node serverjs — port 31416”,
“start”: “node server.js”
},
“keywords”: [
“xway”,
“iot”,
“netbeast”,
“philips”
],
“author”: “pablo@netbeast.co & luis@netbeast.co”,
“license”: “GNU L2”
}server.py
Here is the python server which collect data from the sensor.
import RPi.GPIO as GPIO
import time
import sys
sensor = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
previous_state = False
current_state = False
while True:
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = “ON”
print(“%s” % (new_state))
sys.stdout.flush()
server.js
The Server runs at port 3000 by default. This file connect with the Philips bridge and receive the sensor info throught the file server.py.
With this block of code you will initiate the server:
var argv = require(‘minimist’)(process.argv.slice(2)); // must-have package
app.use(express.static(__dirname));
/* xwayos apps need to accept the port to be launched by parameters */
port = argv.port;
var auxport = 3000;
if(isNaN(port)) {
http.listen(auxport, function(){
console.log(“Listening on port “ + auxport);
});
} else {
http.listen(port, function(){
console.log(“Listening on port “ + port);
});
}On this part, we manage the information from the python server.
// — — — — — — — — — — — — —
// Datos Python
var on = false;
var time;
var spawn = require(‘child_process’).spawn,
ls = spawn(‘python’,[‘sensor.py’]);
var sensor = true;
io.on(“connection”, function(socket){
socket.on(“sensor”,function(data){
//console.log(“entra”);
if (data==”on”){
sensor = true;}
else{
sensor = false;}
});
});ls.stdout.on(‘data’, function (data) {
if (sensor) {
var data = String(data);
console.log(data);
if(ipbridge != null && userbridge != null && on != true){
lightsOn();
on = true;
}clearTimeout(time);
time = setTimeout(function(){
lightsOff();
on = false;
}, 5000)
}
});These are the functions which switch on/off the bulbs
var state = lightState.create();
function lightsOn() {
for(var i = 1; i< huelights.length; i++){
api.setLightState(i, state.on().brightness(100), function(err, result) {
if (err) throw err;
});
}
}// Now turn off the lamp
function lightsOff() {
for(var i = 1; i< huelights.length; i++){
api.setLightState(i, state.off(), function(err, result) {
if (err) throw err;
});
}
}
There are some steps missing, but you can find the whole code in our github. Good luck!!
If you had any doubt or you find a mistake anywhere, please reach us through twitter, facebook, github or at staff@netbeast.co.