How to build a depression
predictor
(a.k.a. a belgian weather station)
using Arduino & Node.js
@stevenbeeckman #iotbe #nju...
In short
Arduino IDE: arduino.cc/dowload
Install StandardFirmata sketch on the Arduino
npm install johnny-five
write your ...
You need a host
computer connected to
the Arduino
Let’s build a depression
predictor
(or a belgian weather station)
Things we need
A sensor
Something to gather the sensor data
Something to store the generated timeseries
A visualisation
photoresistor Arduino MacBook Browsercloud
Heroku
+
node.js
+
hapi.js
+
Tempo
DB
add-on
photoresistor Arduino Uno
Node.js
+
Johnny Five
+
request
+
forever
TempoDB
w...
Heroku
+
node.js
+
hapi.js
+
Tempo
DB
add-on
photoresistor Arduino Uno
Node.js
+
Johnny Five
+
request
+
forever
TempoDB
w...
MacGyver-ism: no resistors in house and
no nearby shops to buy them from -> use
a piezo as a resistor
Heroku
+
node.js
+
hapi.js
+
Tempo
DB
add-on
photoresistor Arduino Uno
Node.js
+
Johnny Five
+
request
+
forever
TempoDB
w...
Packages you need
npm install --save johnny-five
npm install --save request
npm install -g forever
usage:
forever start sc...
Heroku
+
node.js
+
hapi.js
+
Tempo
DB
add-on
photoresistor Arduino Uno
Node.js
+
Johnny Five
+
request
+
forever
TempoDB
w...
Design
Need a REST API over HTTP
(Mandatory) POST new sensor values
(Optional) GET overview of past sensor values
Store da...
Packages you need
"dependencies": {
"hapi": "^6.5.1",
"moment": "^2.8.2",
"tempodb": "^1.0.0"
}
https://github.com/stevenb...
Hosting
This code can run on your own server or in the
cloud.
I ran it in the cloud, on Heroku and used the
free TempoDB a...
Some code
var Hapi = require('hapi');
var server = new Hapi.Server(process.env.PORT || 3000);
server.route({
method: 'GET'...
Writing to your TempoDB
var TempoDBClient = require('tempodb').TempoDBClient;
var tempodb = new TempoDBClient('heroku-5e2f...
Writing to your TempoDB
var TempoDBClient = require('tempodb').TempoDBClient;
var tempodb = new TempoDBClient('heroku-5e2f...
Writing to your TempoDB
var TempoDBClient = require('tempodb').TempoDBClient;
var tempodb = new TempoDBClient('heroku-5e2f...
Writing to your TempoDB
var TempoDBClient = require('tempodb').TempoDBClient;
var tempodb = new TempoDBClient('heroku-5e2f...
Heroku
+
node.js
+
hapi.js
+
Tempo
DB
add-on
photoresistor Arduino Uno
Node.js
+
Johnny Five
+
request
+
forever
TempoDB
w...
MacBooks crash
too
hard drive failure
References
http://arduino.cc
https://github.com/rwaldron/johnny-five
http://blog.nodejitsu.com/keep-a-nodejs-server-up-wit...
Questions?
@stevenbeeckman #iotbe #njugbe
Arduino & node.js
Arduino & node.js
Arduino & node.js
Arduino & node.js
Upcoming SlideShare
Loading in...5
×

Arduino & node.js

743

Published on

How to combine Arduino, a photoresistor, node.js, hapi.js and a time series database called TempoDB?

Published in: Devices & Hardware
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
743
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Arduino & node.js

  1. 1. How to build a depression predictor (a.k.a. a belgian weather station) using Arduino & Node.js @stevenbeeckman #iotbe #njugbe
  2. 2. In short Arduino IDE: arduino.cc/dowload Install StandardFirmata sketch on the Arduino npm install johnny-five write your script.js Run node script.js
  3. 3. You need a host computer connected to the Arduino
  4. 4. Let’s build a depression predictor (or a belgian weather station)
  5. 5. Things we need A sensor Something to gather the sensor data Something to store the generated timeseries A visualisation
  6. 6. photoresistor Arduino MacBook Browsercloud
  7. 7. Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  8. 8. Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  9. 9. MacGyver-ism: no resistors in house and no nearby shops to buy them from -> use a piezo as a resistor
  10. 10. Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  11. 11. Packages you need npm install --save johnny-five npm install --save request npm install -g forever usage: forever start script.js forever list (to see what’s running) https://github.com/stevenbeeckman/arduino-j5
  12. 12. Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  13. 13. Design Need a REST API over HTTP (Mandatory) POST new sensor values (Optional) GET overview of past sensor values Store data in a time series friendly database
  14. 14. Packages you need "dependencies": { "hapi": "^6.5.1", "moment": "^2.8.2", "tempodb": "^1.0.0" } https://github.com/stevenbeeckman/iotbe-njugbe/
  15. 15. Hosting This code can run on your own server or in the cloud. I ran it in the cloud, on Heroku and used the free TempoDB add-on.
  16. 16. Some code var Hapi = require('hapi'); var server = new Hapi.Server(process.env.PORT || 3000); server.route({ method: 'GET' , path: '/' , handler: function(request, reply){ reply('Hello, Internet of Things fans!'); } }); server.start(function(){ console.log('Server running at:' + server.info.uri); }); heroku create git add index.js git commit -m “First deploy.” git push heroku master http://iotbe-njugbe.herokuapp.com/
  17. 17. Writing to your TempoDB var TempoDBClient = require('tempodb').TempoDBClient; var tempodb = new TempoDBClient('heroku-5e2f03bd25cf424098426a8a21db26f3', process.env.TEMPODB_API_KEY, process.env. TEMPODB_API_SECRET, {hostname: process.env.TEMPODB_API_HOST, port: process.env.TEMPODB_API_PORT}); var moment = require('moment'); server.route({ method: 'POST' , path: '/sensor/{sensor_id}/measurement' , config: { handler: function(request, reply){ var newMeasurement = new Object(); newMeasurement.t = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ"); newMeasurement.v = request.payload.value; var tempodb_data = new Array(); tempodb_data.push(newMeasurement); var series_key = 'sensor-' + request.params.sensor_id; tempodb.write_key(series_key, tempodb_data, function(error, result){ error ? reply(error) : reply(result); }); } } });
  18. 18. Writing to your TempoDB var TempoDBClient = require('tempodb').TempoDBClient; var tempodb = new TempoDBClient('heroku-5e2f03bd25cf424098426a8a21db26f3', process.env.TEMPODB_API_KEY, process.env. TEMPODB_API_SECRET, {hostname: process.env.TEMPODB_API_HOST, port: process.env.TEMPODB_API_PORT}); var moment = require('moment'); server.route({ method: 'POST' , path: '/sensor/{sensor_id}/measurement' , config: { handler: function(request, reply){ var newMeasurement = new Object(); newMeasurement.t = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ"); newMeasurement.v = request.payload.value; var tempodb_data = new Array(); tempodb_data.push(newMeasurement); var series_key = 'sensor-' + request.params.sensor_id; tempodb.write_key(series_key, tempodb_data, function(error, result){ error ? reply(error) : reply(result); }); } } }); Not documented: date must be formatted. new Date() will not work as is. Use moment.js for easy formatting.
  19. 19. Writing to your TempoDB var TempoDBClient = require('tempodb').TempoDBClient; var tempodb = new TempoDBClient('heroku-5e2f03bd25cf424098426a8a21db26f3', process.env.TEMPODB_API_KEY, process.env. TEMPODB_API_SECRET, {hostname: process.env.TEMPODB_API_HOST, port: process.env.TEMPODB_API_PORT}); var moment = require('moment'); server.route({ method: 'POST' , path: '/sensor/{sensor_id}/measurement' , config: { handler: function(request, reply){ var newMeasurement = new Object(); newMeasurement.t = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ"); newMeasurement.v = request.payload.value; var tempodb_data = new Array(); tempodb_data.push(newMeasurement); var series_key = 'sensor-' + request.params.sensor_id; tempodb.write_key(series_key, tempodb_data, function(error, result){ error ? reply(error) : reply(result); }); } } }); Single key value pairs {t,v} must be wrapped in an Array too...
  20. 20. Writing to your TempoDB var TempoDBClient = require('tempodb').TempoDBClient; var tempodb = new TempoDBClient('heroku-5e2f03bd25cf424098426a8a21db26f3', process.env.TEMPODB_API_KEY, process.env. TEMPODB_API_SECRET, {hostname: process.env.TEMPODB_API_HOST, port: process.env.TEMPODB_API_PORT}); var moment = require('moment'); server.route({ method: 'POST' , path: '/sensor/{sensor_id}/measurement' , config: { handler: function(request, reply){ var newMeasurement = new Object(); newMeasurement.t = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ"); newMeasurement.v = request.payload.value; var tempodb_data = new Array(); tempodb_data.push(newMeasurement); var series_key = 'sensor-' + request.params.sensor_id; tempodb.write_key(series_key, tempodb_data, function(error, result){ error ? reply(error) : reply(result); }); } } }); Warning: You have to pass the TempoDB database name too, which is not documented!
  21. 21. Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  22. 22. MacBooks crash too hard drive failure
  23. 23. References http://arduino.cc https://github.com/rwaldron/johnny-five http://blog.nodejitsu.com/keep-a-nodejs-server-up-with- forever/ http://heroku.com http://hapijs.com http://momentjs.com https://tempo-db.com
  24. 24. Questions? @stevenbeeckman #iotbe #njugbe
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×