home sensor prototype on arduino & raspberry pi with node.js
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
Home Sensing Prototype
On Raspberry Pi & ArduinoWith Node.JS
June. 2014
Overview
Home Temperature & Humidity MonitorNode.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 LAN
Circuit Configuration
Uses DHT11 sensor
Resistance is re-quired
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
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
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));
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
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/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
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
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 & 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);
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.
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.