MeteorNunDuino

Realtime prototype for the Internet of Things (IoT)

 

Fullstack JavaScript

Introduction

Demo

Arduino plus Node.js

Johnny-Five framework

Johnny-Five framework is based on the Firmata protocol

Johnny-Five

Sensors and Actors

 

  • Standard I2C interface
  • 3-axis accelerometer with 10-bit accuracy
  • 2-axis analog joystick with 8-bit A/D converter
  • 2 buttons
  • $20

Wii Nunchuck

Characteristics

Johnny-Five Nunchuck

var five = require("../lib/johnny-five.js"),
  board, nunchuk;

board = new five.Board();

board.on("ready", function() {

  new five.Pin("A2").low();
  new five.Pin("A3").low();

  // Create a new `nunchuk` hardware instance.
  nunchuk = new five.Wii.Nunchuk({
    freq: 50
  });


  // Nunchuk Event API
  //

  // "read" (nunchuk)
  //
  // Fired when the joystick detects a change in
  // axis position.
  //
  // nunchuk.on( "read", function( err ) {

  // });

  // "change", "axischange" (joystick)
  //
  // Fired when the joystick detects a change in
  // axis position.
  //
  nunchuk.joystick.on("change", function(err, event) {
    console.log(
      "joystick " + event.axis,
      event.target[event.axis],
      event.axis, event.direction
    );
  });

part one

var five = Meteor.require("johnny-five"),
    board, nunchuk;

Meteor.startup(function(){
board = new five.Board();

board.on('error', function (error) {
    console.error('Johnny Five Error', error);
});

board.on("ready", Meteor.bindEnvironment(function() {

    new five.Pin("A2").low();
    new five.Pin("A3").low();

    // Create a new `nunchuk` hardware instance.
    nunchuk = new five.Wii.Nunchuk({
        freq: 50
    });


    // Nunchuk Event API
    //

    // "read" (nunchuk)
    //
    // Fired when the joystick detects a change in
    // axis position.
    //
    // nunchuk.on( "read", function( err ) {

    // });

    // "change", "axischange" (joystick)
    //
    // Fired when the joystick detects a change in
    // axis position.
    //
    nunchuk.joystick.on("change", Meteor.bindEnvironment(function(err, event) {
      
        console.log(
                "joystick " + event.axis,
            event.target[event.axis],
            event.axis, event.direction
        );
        joystickData.insert({
            created: new Date(),
            value: event.target[event.axis],
            axis: event.axis,
            direction: event.direction
        });
    }, "joystickErr"));


    // Create a standard `led` hardware instance
    led = new five.Led({
        pin: 13
    });

}, "ready"));
});

Writing Nunchuck data into MongoDB

$ meteor mongo
MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:3001/meteor

meteor:PRIMARY> show dbs
local	0.0625GB
meteor	0.0625GB

meteor:PRIMARY> use meteor
switched to db meteor

meteor:PRIMARY> show collections
accelerometerData
joystickData
nunchuckData
system.indexes

MongoDB shell

meteor:PRIMARY> db.accelerometerData.find().pretty()
{
	"created" : ISODate("2014-08-11T14:18:50.938Z"),
	"value" : 340,
	"axis" : "x",
	"direction" : 1,
	"_id" : "MYybQF3EHMC23ENRi"
}
{
	"created" : ISODate("2014-08-11T14:18:50.943Z"),
	"value" : 528,
	"axis" : "y",
	"direction" : 1,
	"_id" : "Y5fhT3GHbFJpBWix4"
}
{
	"created" : ISODate("2014-08-11T14:18:50.945Z"),
	"value" : 600,
	"axis" : "z",
	"direction" : 1,
	"_id" : "FaG45gw8Q2pLbGCAN"
}

db.accelerometerData.find()

meteor:PRIMARY> db.joystickData.find().pretty()
{
	"created" : ISODate("2014-08-11T14:25:40.405Z"),
	"value" : 480,
	"axis" : "x",
	"direction" : 1,
	"_id" : "9eF4taYhK7ywKYrZx"
}
{
	"created" : ISODate("2014-08-11T14:25:40.413Z"),
	"value" : 508,
	"axis" : "y",
	"direction" : 1,
	"_id" : "t8K789ve5rm2ASmnX"
}
{
	"created" : ISODate("2014-08-11T14:26:19.787Z"),
	"value" : 1100,
	"axis" : "x",
	"direction" : 1,
	"_id" : "8cNtBgK85SB2EYdHT"
}

db.joystickData.find()

meteor:PRIMARY> db.nunchuckData.find().pretty()
{
	"created" : ISODate("2014-08-11T14:21:22.451Z"),
	"targetButton" : "z",
	"buttonState" : "down",
	"states" : {
		"isUp" : false,
		"isDown" : true
	},
	"_id" : "aSwLmNa8ujxSDJDk3"
}
{
	"created" : ISODate("2014-08-11T14:21:22.650Z"),
	"targetButton" : "z",
	"buttonState" : "up",
	"states" : {
		"isUp" : true,
		"isDown" : false
	},
	"_id" : "s4QRcvaMWjgofhkpz"
}

db.nunchuckData.find()

meteor:PRIMARY> db.system.indexes.find().pretty()
{
	"v" : 1,
	"key" : {
		"_id" : 1
	},
	"ns" : "meteor.accelerometerData",
	"name" : "_id_"
}
{
	"v" : 1,
	"key" : {
		"_id" : 1
	},
	"ns" : "meteor.nunchuckData",
	"name" : "_id_"
}
{
	"v" : 1,
	"key" : {
		"_id" : 1
	},
	"ns" : "meteor.joystickData",
	"name" : "_id_"
}

db.system.indexes.find()

part two



if (Meteor.isClient) {

}

if (Meteor.isServer) {
    Meteor.startup(function () {
        // code to run on server at startup
    });
}

equivalent to 

-appFolder
    -client
        index.html
        clientSide.js
        my.css
        
    -server
        serverSide.js

 anywhere.js

By default, a new Meteor app includes the autopublish and insecure packages, which together mimic the effect of each client having full read/write access to the server's database.


if autopublish package is removed

//anywhere.js
joystickData = new Meteor.Collection("joystickData");

//serverSide.js
Meteor.publish('joystickData', function(){
    return joystickData.find({}, {limit: 1, sort: {created: -1}});
});

//clientSide.js
 Meteor.subscribe('joystickData');

Blaze, next-generation live templating engine.

// myApp.html

<body>
<div class="container">
    {{> joystick}}
</div>
</body>

<template name="joystick">
    <h1>Joystick</h1>
    {{#each joysticks}}
    <div>{{value}}</div>
    <div>{{axis}}</div>
    <div>{{direction}}</div>
    {{/each}}
</template>


// clientSide.js

Template.joystick.joysticks = function () {
        return joystickData.find({}, {limit: 1, sort: {created: -1}});
    };

Thank you for listening!

  • http://slides.com/michaelmacherey/meteornunduino#/
  • https://github.com/Goyapa/mongoduinometeor 

Time for

Q & A

Made with Slides.com