Recently, I learned about Raspberry PI, so it was my first time to use Raspberry PI. I decided to make a surveillance camera that detects motion using a webcam.
The outline of the function to be created this time is like this. ・ Take a picture when motion is detected. (This time, I will only use photos.) ・ When you take a picture, it will be automatically posted to Slack. Only this. Since it is the first time, I will make a simple thing without greed.
・ RaspberryPI: Simply put, a small computer ・ Motion: Motion detection software (You can easily detect motion using this software) ・ Slack: Chat tool (If you imagine Skype or Line, it's OK) -Slacker: Function for posting to Slack programmatically
First, prepare motion.
motion installation
$ sudo apt-get install -y motion
Next, change the settings in motion.conf.
-First of all, turn off the daemon setting to start it manually every time in the development stage.
・ I don't need a video, so I'll stop.
-Since images are exported frequently, the SD card may deteriorate, so export photos to the memory area. (Note: The area under tmp has been changed to the memory area in advance.)
This is the end of motion settings for the time being.
At this point, run motion with sudo motion -c /etc/motion/motion.conf
and
You can confirm that the photo is written under / tmp / motion
by motion detection.
It's easy.
However, it is the production from here. To complete, I'll have to automatically post my photos to Slack so I can be notified on my phone. So, here is the main part of this article. From now on, we will set up Slack.
Complete Slack account registration first. Then, issue a Token for the API from the Slack Token Issuing Page.
Once you've created a Token, it's time to put Python's Slack library on your Raspberry PI. Install Slacker </ b>.
$ sudo pip install slacker
Once installed, first try interactively TEST.
$ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from slacker import Slacker
>>> token = "xxxx-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxx"
>>> slacker = Slacker(token)
>>> channel_name = "#" + "general"
>>> message = 'It's an API'
>>> slacker.chat.post_message(channel_name, message)
<slacker.Response object at 0xb64aca50>
Then I came! I was able to confirm that I could post to Slack safely.
By the way, I don't like the name bot, so I changed the user name to.
>>> slacker.chat.post_message(channel_name, message, as_user=True)
<slacker.Response object at 0xb6bff910>
>>> slacker.chat.post_message(channel_name, message, username='test')
<slacker.Response object at 0xb64ac210>
I was able to confirm that it has changed properly.
This time, we will mainly post images, so we will upload images immediately. When I refer to the README of slacker, it seems that the upload function is prepared in the files class, so I will use that.
$ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from slacker import Slacker
>>> token = "xxxx-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxx"
>>> slacker = Slacker(token)
>>> channel_name = "#" + "general"
>>> slacker.files.upload('/var/lib/motion/01-20151206004121-00.jpg')
<slacker.Response object at 0xb64b4e50>
Oh! No error! I thought it went up, but Slack didn't respond. .. .. I'm quite addicted to this ^^; If you check the Slack page often, is it a menu? There is an item called All Files in a place like this. .. ..
When I opened it, it went up properly!
I see, it doesn't post if you just upload it. Apparently, it seems that the image will not be posted unless you pin (Share) the image uploaded to this Files.
In order to pin, the target file information is required. So, save the return value when uploading.
>>> result = slacker.files.upload('/var/lib/motion/01-20151206004057-02.jpg')
When I tried to pin using that information, an error occurred.
>>> slacker.pins.add(channel='C0G0XXXXX', file_=result.body['file']['id'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/slacker/__init__.py", line 495, in add
'timestamp': timestamp,
File "/usr/local/lib/python2.7/dist-packages/slacker/__init__.py", line 69, in post
return self._request(requests.post, api, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/slacker/__init__.py", line 61, in _request
raise Error(response.error)
slacker.Error: file_not_shared
This was because there was no channel specified in the File information. It seems that you need to specify the channel that can be pinned when uploading the file to pin. Check the channel again.
>>> slacker.channels.list().body
{u'channels': [{u'name': u'general', u'id': u'C0G0XXXXX'....}, ....
Next, specify the channel properly and upload and pin.
>>> result = slacker.files.upload('/var/lib/motion/01-20151206004057-02.jpg', channels=['C0G0XXXXX'])
>>> slacker.pins.add(channel='C0G0XXXXX', file_=result.body['file']['id'])
<slacker.Response object at 0xb64ee6b0>
It's done! (* By the way, the small square frame at the bottom right is the place where the motion was detected.)
Now that the API confirmation is complete, it's time to work with motion.
Next, when it is detected by motion, we will specify event processing on the motion side in order to post to slack. As for how to specify, motion has the following options to specify and execute the script intended at the time of the event.
By the way, when I investigated how to specify the file path, It was said that the file information was passed as an argument to this site.
- 1 With the motion.conf options on_picture_save, on_movie_start, on_movie_end, the file name is passed to the command ($ 1).
By the way, in the case of on_picture_save, the path of the captured file is passed as an argument.
Based on that, I created a Python script.
Let's specify it in conf.
# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)
# To give the filename as an argument to a command append it with %f
#; on_picture_save value
on_picture_save python /home/pi/slackbot/slack_bot.py %f
Next, don't forget to grant execute permission.
$ chmod a+x slackbot/slack_bot.py
Now start motion! The photos are saved.
sudo motion -c /etc/motion/motion.conf
~~snip~~
[1] [NTC] [ALL] motion_detected: Motion detected - starting event 1
[1] [NTC] [EVT] event_newfile: File of type 1 saved to: /tmp/motion/01-20151208222942-04.jpg
[1] [NTC] [EVT] event_newfile: File of type 1 saved to: /tmp/motion/01-20151208222942-05.jpg
[1] [NTC] [EVT] event_newfile: File of type 1 saved to: /tmp/motion/01-20151208222943-01.jpg
~~snip~~
For slack. .. ..
Kita━━━ (゚ ∀ ゚) .━━━ !!! It's a success! !! I'm coming steadily !!!
Then, I would like to actually install it and verify it as a surveillance camera. I bought a plastic case for 100 yen and used it as a case.
Let's install it outside. It's perfect! No one doubts that it is a surveillance camera with this appearance. It seems that there is no doubt about it at all, so there seems to be no problem as a surveillance camera first. Lol </ b>
There is a suspicious figure right there. .. .. Then Slack will be notified!
It's perfect! Moreover, I can take pictures much better than I expected! This completes the surveillance camera.
I touched the Raspberry PI for the first time, and was surprised that a surveillance camera could be realized so easily. By the way, I was planning to use the infrared camera module this time, but I couldn't start it well and I couldn't make it in time. orz
Also, since I post to Slack every time I shoot, notifications etc. will be quite amazing orz So, eventually, I would like to use the infrared camera module to save the photo itself in cloud storage etc. and notify it with Slack.
Then tomorrow, hamano_t will have a baton touch. Tomorrow is finally Christmas, and the Advent Calendar will be closed. I'm sorry for the beginner's post the day before the closing, but thank you for reading ^ ^
At a later date, I made it possible to monitor videos on my mobile phone from outside. Please refer to this as well ^^
Try using an infrared camera module with Raspberry Pi How to make Raspberry Pi accessible from external network and monitor pets from outside with mobile phone
Recommended Posts