Home sensor prototype on Arduino & Raspberry Pi with Node.JS

18
Home Sensing Prototype On Raspberry Pi & Arduino With Node.JS June. 2014

description

This slide shows the steps to make home temperature & humidity sensor using two famous open source H/Ws (Arduino & Raspberry Pi) with Node.JS the server side JavaScript framework.

Transcript of Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Page 1: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Home Sensing Prototype

On Raspberry Pi & ArduinoWith Node.JS

June. 2014

Page 2: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Overview

Home Temperature & Humidity MonitorNode.JS on Arduino + Raspberry Pi

Page 3: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

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

Page 4: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

System Configuration

Arduino Nano + Raspberry Pi + Wi-Fi LAN

Page 5: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Circuit Configuration

Uses DHT11 sensor

Resistance is re-quired

Page 6: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Install Node.JS

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

Update raspbian

Node & NPM

Set environment

sudo apt-get upgrade; sudo apt-get update

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

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

Page 7: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Install MySQL (1/2)

Install MySQL server

Install node-mysql

Create table

sudo apt-get install mysql-server

npm install mysql

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

Page 8: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Install MySQL (2/2)

Create schema

Retrieving DB on Node.JS– http://iamapark89.wordpress.com/2013/12/1

4/node-js-2-db-%EC%97%B0%EA%B2%B0/

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

Page 9: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Arduino + Node

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

Install node-serialport

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

wget http://nodejs.org/dist/v0.10.12/node-v0.10.12-linux-arm-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 ~/.bashrcnpm install serialport

Page 10: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

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.

Page 11: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

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

});

Page 12: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

WAS

Web Application Server– sudo npm install express routes

var express = require('express') , routes = require('./routes') , user = require('./routes/user') , temperatures = require('./routes/tempera-ture') , http = require('http') , path = require('path');var app = express();// … omitted …

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

Handles GET /users

Handles GET /temper-atures

Page 13: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Web Server

Jade Template Engine for Express– sudo node install jade

Simplified HTML tag hierarchy doctype 5html

headtitlemeta(name='viewport', content='width=device-width,

initial-scale=1') body

div(data-role='page', data-theme='a') h1hra

Page 14: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

User Interface

Responsive web design– Uses jQuery Mobile for free

• See the demo:– http://demos.jquerymobile.com/

1.3.0-beta.1/docs/demos/

Page 15: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Visualization

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

Page 16: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

RESTful Web API

HTTP command, URI & representation– HTTP command: GET, PUT, POST, DELETE, …– URI: non-verb resources & it’s 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);

Page 17: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

Conclusions

JavaScript Everywhere– Consider 1st class citizen of web for all Internet

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 data.

Page 18: Home sensor prototype on Arduino & Raspberry Pi with Node.JS

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 development– Make the mash-up and maintenance better– Provide user’s guide with Web IDL

Multi-screen web application– Design the process how to develop N-Screen

Apps.