Your SlideShare is downloading. ×
  • Like
Arduino & node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Arduino & node.js

  • 484 views
Published

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?

Published in Devices & Hardware
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
484
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. How to build a depression predictor (a.k.a. a belgian weather station) using Arduino & Node.js @stevenbeeckman #iotbe #njugbe
  • 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. You need a host computer connected to the Arduino
  • 4. Let’s build a depression predictor (or a belgian weather station)
  • 5. Things we need A sensor Something to gather the sensor data Something to store the generated timeseries A visualisation
  • 6. photoresistor Arduino MacBook Browsercloud
  • 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. 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. MacGyver-ism: no resistors in house and no nearby shops to buy them from -> use a piezo as a resistor
  • 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. 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. 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. 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. Packages you need "dependencies": { "hapi": "^6.5.1", "moment": "^2.8.2", "tempodb": "^1.0.0" } https://github.com/stevenbeeckman/iotbe-njugbe/
  • 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. 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. 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. 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. 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. 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. 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. MacBooks crash too hard drive failure
  • 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. Questions? @stevenbeeckman #iotbe #njugbe