LoginSignup
18

More than 3 years have passed since last update.

バイブレーションモーターをクラウド経由で動かしてみる ~obniz BordとFirebaseの連携~

Last updated at Posted at 2019-11-03

概要

Firebase経由でobniz BordとGroveのバイブレータを動かす知見が少なかったので
ほぼ自分用に書いておきます。

firebaseの値が変わるとバイブが鳴り始める仕様です。

参考記事(ワンフットシーバスの田中さん)
https://www.1ft-seabass.jp/memo/2019/09/24/obniz-hand-connect-hololens/

概要図

image.png
※白色の線は実質使っていないのでIOポートがもったいない。

必要な材料

・obniz Bord
・GROVE Vibration Motor コード番号 SEEED-105020003

コード

index.html
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
    <script src="https://unpkg.com/obniz@2.4.0/obniz.js" crossorigin="anonymous"></script>
</script>
    <script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>

  </head>
  <body>

    <div id="obniz-debug"></div>
    <button id="on">ON</button>
    <button id="off">OFF</button>
    <div id="print"></div>

    <h1>Hello Grove Vibration Motor</h1>
    <script>
        // Initialize Firebase
        var config = {
            apiKey: "[apiKey]",
            authDomain: "[authDomain]",
            databaseURL: "[databaseURL]",
            projectId: "[projectId]",
            storageBucket: "[storageBucket]",
            messagingSenderId: "[messagingSenderId]",
            appId: "[appId]"
        };
        firebase.initializeApp(config);
    </script>

    <script>
    //読み込み処理

      firebase.database().ref('/tables1/').on('value',function(snapshot) {
        var istatus = snapshot.child("status").val();  
        var obniz = new Obniz("obnizID");

        obniz.onconnect = async function () {
          obniz.display.clear();

          // IO3をGNDとして使う
          obniz.io3.output(false);
          // IO2をVINとして使う
          obniz.io2.drive('5v');
          obniz.io2.output(true);

          // istatus が10になったらON
          if (istatus ===10){
              while(istatus ===10){                
                obniz.io0.pull('5v');
                obniz.display.clear();
                obniz.display.print("ステータス = ON");
                $('#print').text("ステータス = ON");

                await obniz.wait(500);

                obniz.io0.pull(null);          
                obniz.display.clear();
                obniz.display.print("ステータス = OFF");
                $('#print').text("ステータス = OFF");

                await obniz.wait(500);

                firebase.database().ref('/tables1/').on('value',function(snapshot) {
                istatus = snapshot.child("status").val();
                });
              }


          }else{

            obniz.io0.pull(null);          
            obniz.display.clear();
            obniz.display.print("ステータス = OFF");
            $('#print').text("ステータス = OFF");

            await obniz.wait(1000);

          }
      }
      });


    </script>

  </body>

デモ動画

twilioマグカップに取り付けて電話っぽくしてみた。
http://www.youtube.com/watch?v=6ETPtop1_uw
IMAGE ALT TEXT HERE

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
18