4
7

More than 5 years have passed since last update.

Raspberry PiとSlackで簡単な監視カメラを設計

Last updated at Posted at 2018-12-19

研究室をカメラで監視したいと思い、Rasberry PiにWebカメラを接続、研究室の状況を見たい時に確認できるシステムを設計した。
監視カメラと言っているが、確認できるのは静止画で動画での監視システムではない。

1.Webカメラで撮影(fswebcam)

まず初めにRaspberry PiにWebカメラを接続、写真を撮影してjpg形式の画像を作成する。

今回使用したカメラ:DIGITAL COWBOY 赤外線WEBカメラ(130万画素) DC-NCR13U

cheeseなどのGUIツールでの撮影は簡単にすることができたが、コマンドライン上でのコマンドでの写真撮影で躓いてしまった。

今回はfswebcamを使用して撮影を行った。

下記参考ページに従い、インストールを進める。

http://mononichi.com/2016/02/raspberrypi-intervalcamera/

fswebcamをインストール後、 早速コマンドを実行。

$fswebcam sample.jpg //カメラで撮影してsample.jpg画像ファイルを作成

エラー発生。

--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Error selecting input 0
VIDIOC_S_INPUT: Device or resource busy

どうやらWebカメラのプロセスが占有されているらしくエラーが出た。

https://qiita.com/him0net/items/b8dfda504263ed673a2b

上記サイトを参考にしてプロセスを停止。

再度コマンド実行。

$fswebcam sample.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 400x-1 to 352x288.
Delaying 2 seconds.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Setting output format to JPEG, quality 95
Writing JPEG image to 'capture.jpg'.

無事にjpg形式の画像ファイルが作成された。

2.pythonプログラムでjpgファイル作成

次にfswebcamで写真を撮影、保存するコマンドを実行するプログラムをpythonで作成。

cam.py
import os
os.system('fswebcam -r 320x240 -S 3 --jpeg 50 sample.jpg')

実行してテスト。

$python cam.py

問題がなけらば"sample.jpg"の画像ファイルが同じディレクトリに作成される。

3.Raspberry PiにHubotをインストール

次にRaspberry PiにHubotをインストールしていく。

Hubotとは文字列のコマンドを受け付け、指定されたコマンドに応じた処理を実行し、結果を表示する機能を提供するプログラムのことである。

まずは以下のサイトを参照してHubotインストールに必要なソフトをRaspberry Piにインストール、Hubotの動作確認まで行う。

https://www.kabegiwablog.com/entry/2017/09/26/113000

このページ通りに進める時に別に設定が必要であった。

$yo hubot

上記コマンドを実行時にメールアドレスやbotの名前を入力するところがでてくる。
そこで4つ目に聞かれるBot adapterのところでslackと入力してあげる。

4.HubotをSlackと接続

Raspberry PiにインストールしたHubotとSlackを繋げていく。

まずは先ほどと同じページを参考に今度はSlackにHubotをインストールする。

https://www.kabegiwablog.com/entry/2017/09/26/113000

SlackにHubotをインストール後、APITokenが表示されるのでコピー。

上記サイト通りに進めるとSlack上のHubotと接続ができなかったためこれより先の操作は別のページの下記を参考に進めた。

http://d.hatena.ne.jp/sunoho/20160621/p1

Bot用に作ったディレクトリにslack-token.shというファイルを作ってあげる

slack-token.sh
export HUBOT_SLACK_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
./bin/hubot --adapter slack

この中に先程コピーしたAPITokenを書き込む

後は参考にしたページ通りに実行権限などを付与してあげるとSlackと接続完了。

自分のSlackページでhubotがオンラインであるのを確認。

9659659.PNG

5.Webカメラの画像をSlackで確認

  
bot用に作ったディレクトにあるscriptsに移動。

$ cd bot/scripts

scriptsディレクトにsample.coffeeファイルを作成。

$nano sample.coffee
sample.coffee
module.exports = (robot) ->
  robot.respond /cam/, (msg) ->
    @exec = require('child_process').exec
    command = "fswebcam sample.jpg; #Raspberry pi上で$fswebcam sample.jpg実行 sample.jpgファイルが作成される
      curl -F file=@/home/pi/mybot/scripts/a.jpg -F channels=xxxxxxxx -F token=xoxb-9999999999999999-XXXXXXXXXXXXXXXXXXXXXXXX https://slack.com/api/files.upload"
    #msg.send "Command: #{command}"
    msg.send "take a picture"
    @exec command, (error, stdout, stderr) ->
      @msg.send error if error?
      #msg.send stdout if stdout?
      #@msg.send stderr if stderr?

channels=xxxxxxxには下記ページで確認できるものを入力。詳しい設定方法は参照URLに書いてある。

https://api.slack.com/methods/channels.list/test

参照URL

https://www.kabegiwablog.com/entry/2017/09/29/190000

今回は最初からあるgeneralチャンネルを設定した。

sample.coffeeプログラムの準備が完了したら、以下のコマンドでhubotを起動させる。

$ sh ./slack-token.sh

sample.coffeeプログラムに誤りがなければ、Slackとの接続が完了し、Slack画面上でhubotがオンラインになっているはずだ。

sample.coffeeに誤りがあれば、接続ができず、ここでエラーがでる。

最後に自分のSlackページでhubotにダイレクトメッセージでcamと送信。
すぐにtake a pictureと返信がくる。
その後チャンネルgeneralに移動すると画像が送られてくる。

sample.gif

6.Hubotのデーモン化

Hubotをデーモン化させる

デーモン化とはつまりバックグラウンドで起動させておくことである。

$./slack-token.sh

で起動するとHubot稼働中状態に移行してCtrl+Cを押さない限りHubotは起動しているが、停止しないと入力できる画面に戻れない。
そのためHubotをデーモン化させてあげる。

参考ページは下記になる

https://qiita.com/KeitaMoromizato/items/d9130b3f6c04292c129d

下記サイトと異なる部分は

/etc/init.d/hubot
#!/bin/sh
DIR="/home/pi/mybot/"
cd $DIR
sudo -u pi ./bin/hubot
 sh ./slack-token.sh
4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7