Created
August 12, 2020 11:50
-
-
Save twtjudy1128/05972d30e240979338476d3bc2eef82f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Obniz = require('obniz'); | |
const obniz = new Obniz('Obniz_ID'); // Obniz_ID に自分のIDを入れます | |
const express = require('express'); | |
const line = require('@line/bot-sdk'); | |
const axios = require('axios'); | |
const PORT = process.env.PORT || 3000; | |
const config = { | |
channelAccessToken: 'チャンネルアクセストークン', | |
channelSecret: 'チャンネルシークレット' | |
}; | |
const app = express(); | |
app.post('/webhook', line.middleware(config), (req, res) => { | |
console.log(req.body.events); | |
Promise | |
.all(req.body.events.map(handleEvent)) | |
.then((result) => res.json(result)); | |
}); | |
const client = new line.Client(config); | |
// obniz接続 | |
obniz.onconnect = async function () { | |
obniz.display.clear(); | |
obniz.display.print('Lets SQUAT TIME!!'); | |
} | |
function handleEvent(event) { | |
if (event.type !== 'message' || event.message.type !== 'text') { | |
return Promise.resolve(null); | |
} | |
let original ='' | |
let prev ='' | |
if(event.message.text.match('よろしく')){ | |
original = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812192444.png'; | |
prev = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812192444.png'; | |
getAskObnizCount(event.source.userId); | |
}else if(event.message.text.match('無理')){ | |
original = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812200429.png'; | |
prev = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812200429.png'; | |
}else if(event.message.text.match('20回')){ | |
original = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812201343.png'; | |
prev = 'https://cdn-ak.f.st-hatena.com/images/fotolife/u/unias_tawa/20200812/20200812201343.png'; | |
}else{ | |
original = ''; | |
prev = ''; | |
} | |
return client.replyMessage(event.replyToken, { | |
type: 'image', | |
originalContentUrl:original, | |
previewImageUrl:prev, | |
}); | |
} | |
const getAskObnizCount = async (userId) => { | |
// 超音波距離センサーを呼び出す | |
const hcsr04 = obniz.wired('HC-SR04', {gnd:0, echo:1, trigger:2, vcc:3}); | |
obniz.display.clear(); // 一旦クリアする | |
obniz.display.print('Lets SQUAT TIME!!'); // Hello obniz!という文字を出す | |
//スクワットした時の最大股下距離 | |
const minDistance = 500; | |
var count = 0; | |
let squat = true; | |
const nosquat = (distance) => { | |
//しゃがんでる時は常にtrue | |
//体を起こしきって初めて距離の判定をする(squat = falseになるので) | |
return squat || distance > minDistance | |
} | |
while(true) { | |
const distance = await hcsr04.measureWait(); | |
squat = nosquat(distance); | |
currentAngle = distance > minDistance ? 90 : 0; | |
//体を起こしてから倒しきるまでは距離がminDistance以下であってもカウントしない | |
if (distance && distance <= minDistance && squat){ | |
obniz.display.clear(); | |
squat = false; | |
console.log(count); | |
obniz.display.print(count); | |
count++ | |
await obniz.wait(1000); | |
} | |
} | |
} | |
app.listen(PORT); | |
console.log(`Server running at ${PORT}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment