Created
September 10, 2018 12:01
-
-
Save atuyosi/19c51d62cebfea7d2ce626a161ca5602 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from DesignSpark.Pmod.HAT import createPmod | |
import slackweb | |
from dotenv import load_dotenv | |
import os | |
import time | |
import sys | |
sec = 1.0 / 10000.0 # 0.1ミリ秒 | |
base_value = 2000 | |
threshold_value = base_value * 1.25 | |
warn_count = 5 | |
def notify_slack(slack): | |
slack.notify(text="チャイムが鳴りました。誰か来たかも?") | |
if __name__ == '__main__': | |
# 環境変数の読み込み | |
load_dotenv('.env') | |
hock_url = os.environ.get("SLACK_HOCK_URL") | |
slack = slackweb.Slack(url=hock_url) | |
mic = createPmod('MIC3','JAA') | |
time.sleep(0.1) #マイクの初期化を待機 | |
try: | |
while True: | |
count = 0 | |
# 一定期間内にしきい値をこえた回数をカウントする | |
for i in range( int(1000 * 0.5 )) : | |
v_int = mic.readIntegerValue() # int value | |
if v_int > threshold_value : | |
count += 1 | |
time.sleep(sec) | |
if count > warn_count : | |
#detect | |
notify_slack(slack) | |
print("detect door bell", file=sys.stderr) | |
else: | |
print("Not detect door bell, count: {}".format(count), file=sys.stderr) | |
except Exception as e: | |
print(e) | |
finally: | |
mic.cleanup() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment