obniz developer team
Published

Recording temperature with obniz Board 1Y

A new board, obniz board 1Y works 1 year with AA batteries! It has sleep mode function. Let's record data while saving power.

BeginnerWork in progress492
Recording temperature with obniz Board 1Y

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
SHT31 (temperature sensor)
×1

Software apps and online services

Maker service
IFTTT Maker service
google account

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>
      //-----------------------------
      // ifttt setting
      //-----------------------------
      let ifttt_event = "sleep_temp";
      let ifttt_secret_key = "Your Key";

      //Timeout 25s
      let tid = setTimeout(()=>{
        let dt = new Date();
        dt.setMinutes(Math.floor(dt.getMinutes()/10)*10+10,0,0);
        obniz.sleep(dt);
      },25000);

      //-----------------------------
      // obniz setting
      //-----------------------------      
      let obniz = new Obniz("OBNIZ_ID_HERE");
      obniz.onconnect = async () => {
        //センシング
        var sensor = obniz.wired("SHT31", {vcc : 0, sda:1, scl:2, adr:3, gnd:4, addressmode:5});
        var temp = await sensor.getTempWait();
        var humd = await sensor.getHumdWait();
        console.log('temperature:' + temp);
        console.log('humidity:' + humd);

        //送信データ作成
        var formData = new FormData();
        formData.append('value1',temp);
        formData.append('value2',humd);

        //IFTTTリクエスト
        await fetch(`https://maker.ifttt.com/trigger/${ifttt_event}/with/key/${ifttt_secret_key}`, {
          method: 'POST',
          mode: "no-cors",
          body: formData
        }).then(()=>console.log("success")).catch((error) => console.log(error));

        //20分ごとに起動するように
        let dt = new Date();
        dt.setMinutes(Math.floor(dt.getMinutes()/10)*10+20,0,0);
        obniz.sleep(dt);
        //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