---
title: Johnny-Five と MRAA
tags: 
author: [MATSUOKA Takashi](https://www.docswell.com/user/matsujirushi)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/P7R2V5Q9J9.jpg?width=480
description: Johnny-Five と MRAA - Node.jsからのハードウェアI/O -
published: November 04, 19
canonical: https://www.docswell.com/s/matsujirushi/K1J86G-2024-01-17-213003
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/P7R2V5Q9J9.jpg)

Johnny-Five と MRAA
- Node.jsからのハードウェアI/O -
2019/11/4
Takashi Matsuoka


# Page. 2

![Page Image](https://bcdn.docswell.com/page/PJXDZK53EX.jpg)

Takashi Matsuoka
(@matsujirushi12)
「e」3つ
2017～ MVP for Windows Development
Wio LTE
Wio 3G
Wio LTE M1/NB1(BG96)
MT3620
DevBoard
de:code 2019


# Page. 3

![Page Image](https://bcdn.docswell.com/page/3JKX856NED.jpg)

基礎からじっくり学ぶラズパイ電子工作
（2017年6月号～2018年6月号）
デジタル入力
デジタル出力
PWM出力
I2C通信(汎用編)
UART通信
SPI通信
I2C通信(SMBus編)


# Page. 4

![Page Image](https://bcdn.docswell.com/page/LE352K6ZJ5.jpg)



# Page. 5

![Page Image](https://bcdn.docswell.com/page/8EDDZ3V4EG.jpg)

J5 for Raspberry Pi
rwaldron/johnny-five
nebrius/raspi-io
nebrius/j5-io
nebrius/raspi-gpio
nebrius/raspi-i2c
nebrius/raspi-serial
fivdi/pigpio
fivdi/i2c-bus
serialport/node-serialport


# Page. 6

![Page Image](https://bcdn.docswell.com/page/V7PXD4VVE8.jpg)

J5 for BeagleBone Black
rwaldron/johnny-five
julianduque/beaglebone-io
fivdi/linux-io


# Page. 7

![Page Image](https://bcdn.docswell.com/page/2JVL1XZR7Q.jpg)

デジタル入力
var five = require(&quot;johnny-five&quot;);
var board = new five.Board();
board.on(&quot;ready&quot;, function() {
this.pinMode(9, five.Pin.INPUT);
this.digitalRead(9, function(value) {
console.log(value);
});
});


# Page. 8

![Page Image](https://bcdn.docswell.com/page/5EGX2V46JL.jpg)

デジタル出力
var five = require(&quot;johnny-five&quot;);
var board = new five.Board();
board.on(&quot;ready&quot;, function() {
this.pinMode(13, five.Pin.OUTPUT);
this.digitalWrite(13, 1);
});


# Page. 9

![Page Image](https://bcdn.docswell.com/page/4JQ8P6G27P.jpg)

I2C
var five = require(&quot;johnny-five&quot;);
var board = new five.Board();
board.on(&quot;ready&quot;, function() {
this.i2cConfig();
this.i2cWrite(0x01, [0x02, 0x03]);
});
var five = require(&quot;johnny-five&quot;);
var board = new five.Board();
board.on(&quot;ready&quot;, function() {
this.i2cConfig();
this.i2cRead(0x01, 6, function(bytes) {
console.log(&quot;Bytes read: &quot;, bytes);
});
});


# Page. 10

![Page Image](https://bcdn.docswell.com/page/K742Y45PJ1.jpg)

UART
• APIドキュメントに無い。
• 実際は、BoardにserialXxxx()とい
うAPIがあるみたい。
• https://npmdoc.github.io/nodenpmdoc-johnnyfive/build/apidoc.html


# Page. 11

![Page Image](https://bcdn.docswell.com/page/LJ1M64WXEG.jpg)



# Page. 12

![Page Image](https://bcdn.docswell.com/page/GJW3WX6KJ2.jpg)

mraa for Raspberry Pi
intel-iot-devkit/mraa


# Page. 13

![Page Image](https://bcdn.docswell.com/page/4EZV56YNJ3.jpg)

デジタル入力
const mraa = require(&#039;mraa&#039;); //require mraa
let myDigitalPin = new mraa.Gpio(6);
myDigitalPin.dir(mraa.DIR_IN);
function periodicActivity() {
let myDigitalValue = myDigitalPin.read();
console.log(&#039;Gpio value is &#039; + myDigitalValue);
}
setInterval(periodicActivity, 1000);


# Page. 14

![Page Image](https://bcdn.docswell.com/page/Y76592D9EV.jpg)

デジタル出力
const mraa = require(&#039;mraa&#039;); //require mraa
let myDigitalPin = new mraa.Gpio(5);
myDigitalPin.dir(mraa.DIR_OUT);
myDigitalPin.write(1);


# Page. 15

![Page Image](https://bcdn.docswell.com/page/G75YN23D74.jpg)

I2C
const mraa = require(&#039;mraa&#039;); //require mraa
let i2cDevice = new mraa.I2c(0);
i2cDevice.address(0x77);
buf[0] = 0xf4;
buf[1] = 0x2e;
i2cDevice.write(buf);
let result = i2cDevice.read(2);
console.log(result.toString(&#039;hex’));


# Page. 16

![Page Image](https://bcdn.docswell.com/page/9J2G54KMJR.jpg)

UART
&quot;use strict&quot;;
const mraa = require(&#039;mraa&#039;); //require mraa
uart.setBaudRate(115200);
uart.setMode(8, 0, 1);
uart.setFlowcontrol(false, false);
sleep(200);
uart.writeStr(&quot;test\n&quot;);
sleep(200);
console.log(uart.readStr(6));
sleep(200);
write(), read() API有り


# Page. 17

![Page Image](https://bcdn.docswell.com/page/DEYVKMNPEM.jpg)

まとめ
• Johnny-Five
• 各種デバイスのライブラリといった印象
• ボードの差異は、「IO Plugins」で吸収
• Raspberry PiやBeagleBone BlackのIO Pluginは、外部パッケージを利
用してハードウェアI/Oしている
• MRAA
• ハードウェアI/Oのライブラリ
• コア部分はCで書かれており、各種言語（Java, Python）から呼び出し
できるようにしている
• 外部パッケージの呼び出しは無し
• 各種デバイスは、「UPM」（別ライブラリ）で提供


