obniz developer team
Published

Power-Saving Door Open/Close Detection: Linking Slack

Let's detect door open/close while using sleep mode function of the board!

BeginnerFull instructions provided787
Power-Saving Door Open/Close Detection: Linking Slack

Things used in this project

Hardware components

Cambrian Robotics obniz Board 1Y
×1
magnetic switch
×1
obniz Grove connector
×1

Software apps and online services

Slack
Slack

Story

Read more

Code

Untitled file

HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Sleep機能を使ったスイッチ</title>
    <script src="https://unpkg.com/obniz@3.0.0/obniz.js" crossorigin="anonymous"></script>
  </head>
  <body>
    <script>
      //-----------------------------
      // slack setting
      //-----------------------------
      let slack_url = "YOUR Slack Webhooks";

      //Timeout 25s
      let tid = setTimeout(()=>{
        //HIGH -> LOW 立ちさがりを検知するまでスリープ
        obniz.sleepIoTrigger(false);
      },25000);

      //-----------------------------
      // obniz setting
      //-----------------------------
      let obniz = new Obniz("OBNIZ_ID_HERE");
      obniz.onconnect = async () => {
        //slack post
        await fetch(slack_url, {
          method: 'POST',
          mode: "no-cors",
          headers:{'Content-Type': 'application/json'},
          body: JSON.stringify( {"text":"obniz Push SW!"})
        }).then(()=>console.log("success")).catch((error) => console.log(error));

        //HIGH -> LOW 立ちさがりを検知するまでスリープ
        obniz.sleepIoTrigger(false);
        //program finish wait...
        await obniz.wait(1000);

        clearTimeout(tid);
        if(typeof done === "function"){
          done();
        }
      }
    </script>
  </body>
</html>

Credits

obniz developer team
80 projects • 36 followers
Development board "obniz" is controlled via the internet.

Comments