mozilla / connected-devices-experiments

INACTIVE - http://mzl.la/ghe-archive - A place to publish experiments and investigations from the Connected Devices team
10 stars 6 forks source link

Johnny-Five #20

Closed mikehenrty closed 2 months ago

mikehenrty commented 8 years ago

Let's make an experiment around Johnny-Five, the JavaScript IoT framework http://johnny-five.io/.

jedireza commented 8 years ago

Experiment: Johnny Five

Details
Summary Reviewing johnny-five; a Node.js framework for robotics.
GitHub https://github.com/rwaldron/johnny-five
Competitors cylon.js
Target market Developers who want to use JS to control robotics.
Marketshare / Contributors 5,700+ stars, 786 forks, 100+ contributors
Hardware Raspberry Pi 2 Model B, Canakit breadboard/LEDs/buttons
Target architecture ARMv7
Demo link https://github.com/jedireza/j5-experiment

Set up

Setup time

Setup details

You may need to execute code as a privileged user (root); to access to GPIO or privileged ports (80, 443).

If you want to use nvm to manage Node installs, you may have to $ sudo su and install nvm for the root user too. This is because nvm installs Node inside the user's home directory.

When running the example code I $ sudo su, [$ nvm use 4] and then $ npm start.

Make an LED blink

I started with my RPi, breadboard and LEDs:

kit

Then I found and followed the [raspi-io]() example:

raspi-j5-diagram

Source: http://johnny-five.io/examples/raspi-io/

The code I used for this:

'use strict'

const Five = require('johnny-five')
const Raspi = require('raspi-io')

const board = Five.Board({
  repl: false,
  io: new Raspi()
})
let led

board.on('ready', function () {
  led = new Five.Led('P1-37')

  led.blink()

  this.on('exit', () => {
    led.off()
  })
})

The result:

led-clip-01

Alternately blinking LEDs

Working from there I added another LED and started it with a delay. The code is now:

'use strict'

const Five = require('johnny-five')
const Raspi = require('raspi-io')

const board = Five.Board({
  repl: false,
  io: new Raspi()
})
let led
let led2

board.on('ready', function () {
  led = new Five.Led('P1-37')
  led2 = new Five.Led('P1-40')

  led.blink()

  setTimeout(() => {
    led2.blink()
  }, 250)

  this.on('exit', () => {
    led.off()
    led2.off()
  })
})

The result:

led-clip-02

Control LED via button

Next I wanted to control the LED with a button. I used the Arduino diagram from the button component docs:

uno-j5-diagram

Source: http://johnny-five.io/api/button/

The code for this demo:

'use strict'

const Five = require('johnny-five')
const Raspi = require('raspi-io')

const board = Five.Board({
  repl: false,
  io: new Raspi()
})
let led
let button

board.on('ready', function () {
  led = new Five.Led('P1-37')
  button = new Five.Button('P1-40')

  button.on('press', () => {
    led.toggle()
  })

  this.on('exit', () => {
    led.off()
  })
})

The result:

led-button-01