title: Security Pi date:2016-08-12 category:raspberry pi tags:raspberry pi,python
Nice to meet you. This is Nioh. I tried Python for the first time and electronic work for the first time. ..
Since we are a micro-enterprise, people who open and close the cash register are currently self-reported. Fortunately, there is no excess or deficiency of cash at present, but I'm still worried when people aren't watching ...
So I wanted to make a camera that opens and closes the cash register.
The functions required to make an open / close interlocking camera this time
――I want to post the photos I took on Slack --I want to detect opening and closing (using GPIO) ――I don't know how to link it with the cash register of the mother ship ...
So I used a Raspberry Pi with many examples.
Actually, I didn't even understand Python at all, so I studied on the page python-izm. The rest was googled.
--Raspberry Pi body --SD card (16GB) -LOGICOOL Webcam HD image quality 1.2 million pixels C270 -Amon Open / Close Interlocking Switch 1588 --Breadboard, jumper wire, resistor (1kΩ? I don't know)
I wish I could do it in this way.
--Pi update
sudo apt-get update
sudo apt-get upgrade
--Installing RPi.GPIO
sudo pip install rpi.gpio
--Installing fswebcam
sudo apt-get install fswebcam
--Installing slacker
sudo pip install slacker
This time I will use GPIO2.
I made a surveillance camera with my first Raspberry PI. There is an item of Slacker setting in, so read it and set it. OK if you know the Token and Channel ID It's a good idea to start Python interactively and copy and paste it.
Create a suitable directory for saving
sudo mkdir /home/pi/camera
Open it with a suitable name in a suitable editor, copy and paste it, and save it as utf-8. I tried using Atom Editor.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Loading the required libraries
import RPi.GPIO as GPIO
from slacker import Slacker
import subprocess
import datetime
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.IN)
def camcapslack():
#1 Take a photo with your webcam and save it
today = datetime.datetime.today()
now = today.strftime("%Y%m%d%H%M")
cmd = "fswebcam [Directory you want to save(/home/pi/camera/etc)]%s.jpg " % now
jpgdir = "[Directory you want to save(/home/pi/camera/etc)]%s.jpg " % now
bolean = subprocess.call(cmd, shell=True)
if bolean == 0:
pass
else:
subprocess.call(cmd,shell=True)
#Post the photos you took to Slack
token = "[Token ID]"
slacker = Slacker(token)
channel = '[channel ID]'
result = slacker.files.upload(jpgdir,channels=['[channel ID]'])
slacker.pins.add(channel='[channel ID]',file_=result.body['file']['id'])
while True:
flag_GPIO = GPIO.input(2)
if flag_GPIO == 0:
time.sleep(2)
camcapslack()
GPIO.cleaup()
All you have to do is register at startup and restart. This may be described later.
--SD card protection (temporarily use Pi's RAM and delete the image after shooting) --Number of trials when saving failed (Because I didn't know how to do it more than once now) ――Think about whether or not the video is better.
To be honest, since I first touched Python or the programming language itself, I don't know if I understand all the meanings of this script **
I am new to Atom Editor and I am writing while learning Markdown notation.
I made the current script while fixing it in my own way.
I posted it on Qiita with the hope that it would be easier to understand and that it would be faster, so I would appreciate it if you could point out various things.
Thank you very much.
Recommended Posts