Arduino & node.js
Upcoming SlideShare
Loading in...5
×
 

Arduino & node.js

on

  • 312 views

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

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

Statistics

Views

Total Views
312
Views on SlideShare
294
Embed Views
18

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 18

http://www.slideee.com 18

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Arduino & node.js Arduino & node.js Presentation Transcript

  • How to build a depression predictor (a.k.a. a belgian weather station) using Arduino & Node.js @stevenbeeckman #iotbe #njugbe
  • 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
  • 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 web app ADC http httpusb
  • Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  • 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 web app ADC http httpusb
  • 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
  • Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  • 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
  • Packages you need "dependencies": { "hapi": "^6.5.1", "moment": "^2.8.2", "tempodb": "^1.0.0" } https://github.com/stevenbeeckman/iotbe-njugbe/
  • 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.
  • 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/
  • 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); }); } } });
  • 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.
  • 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...
  • 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!
  • Heroku + node.js + hapi.js + Tempo DB add-on photoresistor Arduino Uno Node.js + Johnny Five + request + forever TempoDB web app ADC http httpusb
  • 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-with- forever/ http://heroku.com http://hapijs.com http://momentjs.com https://tempo-db.com
  • Questions? @stevenbeeckman #iotbe #njugbe