Skip to content

Instantly share code, notes, and snippets.

@gishi-yama
Created August 7, 2018 03:03
Show Gist options
  • Save gishi-yama/e63403210ab524211d25a9e59b7b8a8a to your computer and use it in GitHub Desktop.
Save gishi-yama/e63403210ab524211d25a9e59b7b8a8a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
</head>
<body>
<p id="wait"><strong>じゅんびちゅう...</strong></p>
<p>心拍数:<span id="rate">?</span></p>
<script type="text/javascript">
"use strict";
// ミリ秒を取得する
var getMilis = function() {
return new Date().getTime();
}
// 心拍数を計算する(心拍合計/経過秒*60を小数点切り捨て)
var getHeartRate = function() {
let milis = getMilis() - initTime;
let rate = (beat / (milis / 1000.0)) * 60.0;
console.log(rate);
document.getElementById('rate').innerHTML = Math.floor(rate);
};
// 起動時のタイムスタンプ
var initTime = getMilis();
// 心拍合計
var beat = 0;
// 繰り返し処理
var interval = null;
// キー値
var key = '****';
// データをもらうためのWeb接続先
var ws = new WebSocket('wss://us.wio.seeed.io/v1/node/event');
// Webに接続するための処理
ws.onopen = function() {
ws.send(key);
document.getElementById('wait').style.display = 'none';
};
// Webからデータを取得できたときの処理
ws.onmessage = function(evt) {
var json = JSON.parse(evt.data);
if (json.msg.input_rise) {
beat = beat + 1;
if (!interval) {
interval = setInterval(getHeartRate, 5000);
}
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment