実現したいこと
raspberry pi(ip 192.168.0.100:5000)上でflask(flask_bell.py)を実行し、webhookでチャイムを鳴らすようにしました。
flaskを起動時に自動実行させたいです。ですが、serviceを設定して起動させると何故か音が鳴らなくなりました。
自動起動させても音がなるようにしたいです。
調査したこと・試したこと
現状ターミナルから
python flask_bell.pyという風にflask_bell.pyを実行すれば、http://192.168.0.100:5000にアクセスして表示されるボタンを押すかあるいは直接 http://192.168.0.100:5000/program01にアクセスしてベルを鳴らすことが出来ます。
しかし、flask_bell.pyをraspberry pi起動時serviceに登録して自動実行させると、flaskは動いていて実行ボタンは表示されるのですがボタンを押してもhttp://192.168.0.100:5000/program01にアクセスしても音が鳴りません。flaskは動いているようなのですが。
serviceは
/etc/systemd/system
のフォルダーにflask_app.serviceというファイルを作りました。
以下の二つの中身で試しました。
python
1[Unit]2Description=Flask Web App 3 4[Service]5ExecStart=/usr/bin/python3 /home/user/RasPi_test/flask_bell.py 6 7[Install]8WantedBy=multi-user.target
や、これが駄目だったので以下の
python
1[Unit]2Description=Flask Web App 3 4[Service]5Type=simple 6WorkingDirectory=/home/user 7ExecStart=/usr/bin/python3 /home/user/RasPi_test/flask_bell.py 8TimeoutStopSec=59StandardOutput=null 10 11[Install]12WantedBy=multi-user.target
を試しました>この2つのserviceで起動時に実行するを試したのですが、どちらの場合でもringのボタンを押しても音が鳴りません。flask自体は起動しているのですが。音だけ鳴りません。
flask_bell.pyは以下の通りです。
python
1from flask import Flask, render_template 2import playsound_bell 3 4app = Flask(__name__)5 6 7@app.route('/')8def hello():9 # return 'Hello!'10 return render_template('layout.html', title='Bell ringer')11 12@app.route('/program01')13def play():14 # 15 playsound_bell.main()16 return render_template('layout.html', title='Bell ringer')17 # return playsound_bell.main()18 19if __name__ == "__main__":20 # app.run(debug=True)21 app.run(host='0.0.0.0')
上のflaskからボタンで呼び出しているplaysound_bell.pyは
python
1import playsound 2def main():3 try:4 playsound.playsound('/home/user/RasPi_test/Doorbell.mp3')5 except:6 pass7 8if __name__ == '__main__':9 main()
です。
playsound.pyを実行すれば音は鳴ります。
templateにあるlayout.htmlは
html
1<!doctype html>2<html>3 4<head>5 <title>{{ title }}</title>6</head>7 8<body>9 <form method="GET" action="/program01">10 <button type="submit" style="width:300px;height:200px">Ring door bell</button>11 </form>12</body>13 14</html>15
です。
0 コメント