234_RasPiで監視カメラの動体検知で電話通知・・slack投稿OK

カメラで検知して自動通知(slack)
http://qiita.com/hasudon7171/items/c7e408545b2c4b37af01
で続き!
# vi /etc/motion/motion.conf
threshold 8000   #写真を撮影するトリガとなるしきい値とりあえず8000そのまま
target_dir /var/lib/motion #撮影した画像の保存先を設定、libにそのまま
on_picture_save python /home/pi/motion/motion.py %f
motionを実行して、カメラが動体検知して/var/lib/motion/に画像ファイルが生成されていれば成功です
# motion -c /etc/motion/motion.conf
(うん、何か保存している)
Slackの設定
SlackのAPIを利用して投稿するため、トークンとチャンネルIDを確認します。
https://api.slack.com/methods/channels.list/test
URL
https://slack.com/api/channels.list?token=xoxp-169850188932-170503111255-169851330916-e8dfbff8d7561bf686f4fc656083ed2f
{
“ok”: true,
“channels”: [
{
“id”: “C50ET3AGP”,
“name”: “general”,
“is_channel”: true,
“created”: 1492320797,
“creator”: “U50ET397H”,
“is_archived”: false,
“is_general”: true,
“name_normalized”: “general”,
“is_shared”: false,
“is_org_shared”: false,
“is_member”: true,
“members”: [
“U50ET397H”
],
“topic”: {
“value”: “Company-wide announcements and work-based matters”,
“creator”: “”,
“last_set”: 0
},
“purpose”: {
“value”: “This channel is for team-wide communication and announcements. All team members are in this channel.”,
“creator”: “”,
“last_set”: 0
},
“previous_names”: [],
“num_members”: 1
},
{
“id”: “C50EEK970”,
“name”: “random”,
“is_channel”: true,
“created”: 1492320797,
“creator”: “U50ET397H”,
“is_archived”: false,
“is_general”: false,
“name_normalized”: “random”,
“is_shared”: false,
“is_org_shared”: false,
“is_member”: true,
“members”: [
“U50ET397H”
],
“topic”: {
“value”: “Non-work banter and water cooler conversation”,
“creator”: “”,
“last_set”: 0
},
“purpose”: {
“value”: “A place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you’d prefer to keep out of more focused work-related channels.”,
“creator”: “”,
“last_set”: 0
},
“previous_names”: [],
“num_members”: 1
}
]
}
画像のアップロード
SlackのAPIを利用して動体検知時にSlackへ自動的に画像投稿します。
まずはSlackへ投稿する箇所をつくります。
motion.py
TOKEN      = ‘xoxp-169850188932-170503111255-169851330916-e8dfbff8d7561bf686f4fc656083ed2f’
CHANNEL    = ‘C50ET3AGP’
# slack API : files.upload
# 参考:https://api.slack.com/methods/files.upload
####
def upload_file(file_path, channel):
    with open(file_path,’rb’) as f:
param = {‘token’:TOKEN, ‘channels’:CHANNEL,}
r = requests.post(“https://slack.com/api/files.upload”, params=param,files={‘file’:f})
if __name__ == “__main__”:
    arg     = sys.argv
file_path = arg[1]
    upload_file(file_path, CHANNEL)
Terminal
# chmod 755 motion.py
# python motion.py /var/lib/motion/01-20170422154935-00.jpg
(だめ、エラー・・・・)
Traceback (most recent call last):
File “motion.py”, line 30, in <module>
upload_file(file_path, CHANNEL)
File “motion.py”, line 22, in upload_file
r = requests.post(“https://slack.com/api/files.upload”, params=param,files={‘file’:f})
NameError: global name ‘requests’ is not defined
でGoogle先生に聞くと、、、
install requests
# pip install requests
で、コードも
import requests
を追加!!
# python motion.py /var/lib/motion/01-20170422154935-00.jpg
# python motion.py /var/lib/motion/01-20170422154933-01.jpg
(あれ!エラー消えた!!)
おおーー怪しいオレがアップされとる!!!よっしゃー!!
以上(次は、電話通知)