Closed mikehenrty closed 2 months ago
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 |
nvm
, then
installing Node 4 LTSYou 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
.
I started with my RPi, breadboard and LEDs:
Then I found and followed the [raspi-io
]() example:
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:
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:
Next I wanted to control the LED with a button. I used the Arduino diagram from the button component docs:
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:
Let's make an experiment around Johnny-Five, the JavaScript IoT framework http://johnny-five.io/.