Skip to content

Instantly share code, notes, and snippets.

@atuyosi
Created September 10, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuyosi/19c51d62cebfea7d2ce626a161ca5602 to your computer and use it in GitHub Desktop.
Save atuyosi/19c51d62cebfea7d2ce626a161ca5602 to your computer and use it in GitHub Desktop.
マイクモジュールからの入力値が一定時間内にしきい値を超えたら通知するスクリプト
#!/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