LoginSignup
9
6

More than 5 years have passed since last update.

Node.jsとArduinoで音量をモニタリング #nodebots #nodebots_jp

Last updated at Posted at 2017-07-29

7/29はInternational NodeBots Dayということで、世界中でNodeBotsイベントが実施されています。

ということでJohnny-Fiveを触っています。

Grove Sound Sensor

Groveの音量センサーです。

サンプルコードを書くとArduino IDEでデータが確認できます。

Johnny-Fiveで値取得

これだけでいけます。今っぽいJohnny-Fiveコードですね。

app.js
'use strict';

const { Board, Sensor } = require('johnny-five');
const board = new Board();

board.on('ready', () => {
    const sensor = new Sensor(`A0`);
    sensor.on('change', (value) => console.log(value));
});

freeboardにつなげて可視化まで

Dweet.ioっていうデータをなげこめるサービスを使ってfreeboardにつなぎます。

0.8秒ごとにデータを投げてます。

app.js
'use strict';

const dweetClient = require("node-dweetio");
const dweetio = new dweetClient();
const { Board, Sensor } = require('johnny-five');
const board = new Board();
const THINGS_NAME = '任意のキー';

let sound = 0;

board.on('ready', () => {
    const sensor = new Sensor(`A0`);
    sensor.on('change', (value) => sound = value);
});

setInterval(() => {
    dweetio.dweet_for(THINGS_NAME, {sound: sound}, (err, dweet) => {
        if(err) {
            console.log(`err: ${err}`);
        }else{
            console.log(dweet);
        }
    });
}, 800);

所感

通信が発生しているので仕方ないけどもうすこしリアルタイムに連動できると楽しそうですね。

とはいえWebからサクッと確認できるのはさすがです。

https://freeboard.io/board/2qjddS

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6