Vous êtes sur la page 1sur 18

Home Sensing Prototype

On Raspberry Pi & Arduino


With Node.JS
June. 2014

Overview
Home Temperature & Humidity Monitor
Node.JS on Arduino + Raspberry Pi

Home Temp. Monitor


Features (based on v. 0.1)
mobile web app on
jQuery Mobile
show current temperature
show the trend of temp.
open API for using google chart

TO-DOs
add humidity
add hypermedia API

System Configuration
Arduino Nano + Raspberry Pi + Wi-Fi LA
N

Circuit Configuration
Uses DHT11 sensor

Resistance is
required

Install Node.JS
Node.JS on Raspberry Pi in 5 min.
http://joshondesign.com/2013/10/23/noderpi

Update

raspbian

sudo apt-get upgrade;


sudo apt-get update

Node & NPM

wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-armpi.tar.gz tar -xvzf node-v0.10.2-linux-arm-pi.tar.gz nodev0.10.2-linux-arm-pi/bin/node --version

Set environment

NODE_JS_HOME=/home/pi/node-v0.10.2-linux-arm-pi
PATH=$PATH:$NODE_JS_HOME/bin

Install MySQL (1/2)


Install MySQL server
sudo apt-get install mysql-server

Install node-mysql
npm install mysql

Create table
mysql -u root -p
Enter password:
mysql> CREATE DB temps
mysql> USE temps

Install MySQL (2/2)


Create schema
CREATE TABLE tempData (
tempId int(11) AUTO_INCREMENT NOT NULL,
tempDate datetime NOT NULL,
tempCelsius decimal(4,2) NOT NULL,
humidity decimal(4,2) NOT NULL,
constraint tempData_PK primary key (tempId)
);

Retrieving DB on Node.JS
http://iamapark89.wordpress.com/201
3/12/14/node-js-2-db-%EC%97%B0%EA%B
2%B0

Arduino + Node
Flushing binary with Arduino IDE
http://playground.arduino.cc/main/DHT11Lib

Install node-serialport

wget http://nodejs.org/dist/v0.10.12/node-v0.10.12-linuxarm-pi.tar.gz
tar xvfz node-v0.10.12-linux-arm-pi.tar.gz
sudo mv node-v0.10.12-linux-arm-pi /opt/node/
echo 'export PATH="$PATH:/opt/node/bin"' >> ~/.bashrc
source ~/.bashrc
npm install serialport

https://github.com/voodootikigod/node-serialport

Why Node.JS?
Programming server with JavaScript
JavaScript is also famous for front-end (client)

Simple & Short Code


No type declaration & implicit type conversion
No multi-threading control

Various open source libraries on NPM


Just type npm install {library name} to use it

Awesome playgrounds
Cloud IDE such as Cloud9 and web itself.

Node.JS key concepts


RequireJS
De facto JavaScript modularization
http://webofthink.tistory.com/12

Evented I/O asynchronous operation


var http=require(http);
http.createServer(function
(request, response) {
// invoked
asynchronously
});

WAS
Web Application Server
sudo npm install express routes
var express = require('express') , routes =
require('./routes') , user = require('./routes/user') ,
temperatures = require('./routes/temperature') , http =
require('http') , path = require('path');
var app = express();
// omitted
Handles GET /users
Handles GET /users

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/temperatures', temperatures.list);

Handles GET
/temperatures

Web Server
Jade Template Engine for Express
sudo node install jade

Simplified HTML tag hierarchy

doctype 5
html
head
title
meta(name='viewport', content='width=device-width,
initial-scale=1')
body
div(data-role='page', data-theme='a')
h1
hr
a

User Interface
Responsive web design
Uses jQuery Mobile for free
See the demo:
http://demos.jquerymobile.com/
1.3.0-beta.1/docs/demos/

Visualization
Using Google Charts
https://developers.google.com/chart/

RESTful Web API


HTTP command, URI & representation
HTTP command: GET, PUT, POST, DELETE,

URI: non-verb resources & its hierarchy


Representation : XML, JSON,

AJAX operation

var xhr = new XMLHttpRequest();


xhr.open('get', '/temperatures');
xhr.onreadystatechange = function() {
// code the event handler here
}
xhr.send(null);

Conclusions
JavaScript Everywhere
Consider 1st class citizen of web for all Intern
et of Things.

Develop more APIs then Apps


App is opened to use but closed to create.
API gives the chance to mash things up.

Data is just nothing without visualization


Imagine how the context can be found in da
ta.

Future works
Design hypermedia API
Achieve the level 3 at Richardson Maturity Level
http://webofthink.tistory.com/2

Design REST to JavaScript API for better dev


elopment

Make the mash-up and maintenance better


Provide users guide with Web IDL

Multi-screen web application


Design the process how to develop N-Screen A
pps.