obnizで電気をつける

せっかくMFTでobnizを買ったので少し遊んでいる。

ざっくりいうと、学習リモコンをつくって、イベント動作させるということ。

受光器と送光器がついていて、受光モードにしておくと、適当にサンプリングしてくれる。

送光モードにしておくと、サンプリングしたとおりに赤外線を光らせてくれる。

このサンプリングの話はまた今度書きます。

f:id:derfrosch:20181003225556j:plain:w300obniz

いきなりですがソースコード

<!-- HTML Example -->
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@1.6.1/obniz.js"></script>
  
</head>
<body>

<script>
  
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  
  if (typeof req === "object") {
    obniz.display.clear();
    obniz.display.print("requested");
    
    if(req.body.light) {
      obniz.display.print("toggling light");
      var infraredLed = obniz.wired('InfraredLED', {anode: 1, cathode: 3});
      infraredLed.send([1,1,1/*長いので省略*/]);
    }
    
    else{
      obniz.display.print("error");
    }
    
  }

  if(typeof done === "function") {
    await obniz.wait(1000); // 無いと、ほかが実行される前にdone()される。
    obniz.display.print("done");
    done();
  }
  
}

</script>
</body>
</html>

「オーケーグーグル」する

Google Assistant -> IFTTT -> webhook -> obniz -> 電気。

derfrosch.hatenablog.jp

参考にしたもの

https://obniz.io/doc/about_eventobniz.io

qiita.com