LoginSignup
3
3

More than 5 years have passed since last update.

Tessel2で環境センサモジュールで騒音レベルと照度をサクッと取得 #tessel

Posted at

Tessel2では初代Tesselで使えたモジュールが再利用できます(割と朗報)。

今回は手元にあったambient-attx4モジュールをTessel2から試してみます。

Ambient-attx4モジュール

こんな感じのやつです。

getSoundLevelgetLightLevelのメソッド名からわかるように、この環境センサモジュールで騒音と照度の値が取得できます。

Web制作者にもチャレンジできるIoT入門~Tesselの基礎~の記事も参考にしてみましょう。

環境

Tessel2から制御してみる

公式サイトにもあるように。

環境センサー(ambient)モジュールをTesselのポートAに挿しこんでください。このとき、六角形のTesselロゴの面が裏側、電子部品が有る側を表側になるようにしてください。最後に、TesselとパソコンをUSBケーブルで接続してください。

こんな感じで差し込みます。

$ npm i ambient-attx4

これだけで準備完了です。

index.js
'use strict'

const tessel = require('tessel');
const ambientlib = require('ambient-attx4');
const ambient = ambientlib.use(tessel.port['A']); //ポートBならBと記述

ambient.on('ready',()=> {
 // Get points of light and sound data.
  setInterval(()=> {
    ambient.getLightLevel((err, lightdata) => {
      if (err) throw err;
      ambient.getSoundLevel((err, sounddata) => {
        if (err) throw err;
        console.log("Light level:", lightdata.toFixed(8), " ", "Sound Level:", sounddata.toFixed(8));
      });
    });
  }, 500); // The readings will happen every .5 seconds
});

ambient.on('error',(err) => {
  console.log(err);
});

  • 実行
$ t2 run index.js

こんな感じでサクッと騒音レベルと照度を取得できました。

まとめ

tessel1と2でモジュール共有ができるのがよかったですね。
去年買ってたやつをそのまま使えそうです笑

3
3
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
3
3