Python beginner opens and closes interlocking camera with Raspberry Pi

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. ..

Reason to make


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.

Why Raspberry Pi


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.

First of all, purchase knowledge


Actually, I didn't even understand Python at all, so I studied on the page python-izm. The rest was googled.

Things necessary


--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)

principle


  1. The door opens
  2. The open / close detection switch turns on
  3. Take a picture with the webcam 2 seconds after turning on
  4. Post the captured image to Slack

I wish I could do it in this way.

Installation of prerequisite applications, etc.


--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

wiring


This time I will use GPIO2. 回路図

Slacker settings


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.

Creating a storage directory


Create a suitable directory for saving

sudo mkdir /home/pi/camera

Write a script


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.

Features you want to add in the future


--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.

Finally


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

Python beginner opens and closes interlocking camera with Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Display USB camera video with Python OpenCV with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Create a web surveillance camera with Raspberry Pi and OpenCV
Create an LCD (16x2) game with Raspberry Pi and Python
Pet monitoring with Rekognition and Raspberry pi
Raspberry Pi Security Infrared Camera (Python Edition)
Working with GPS on Raspberry Pi 3 Python
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Discord bot with python raspberry pi zero with [Notes]
How to use Raspberry Pi pie camera Python
I tried L-Chika with Raspberry Pi 4 (Python edition)
MQTT RC car with Arduino and Raspberry Pi
Get CPU information of Raspberry Pi with Python
Get temperature and humidity with DHT11 and Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
GPGPU with Raspberry Pi
Raspberry Pi video camera
DigitalSignage with Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Create a color sensor using a Raspberry Pi and a camera
Easy IoT to start with Raspberry Pi and MESH
Display images taken with the Raspberry Pi camera module
Try debugging Python on Raspberry Pi with Visual Studio.
Detect mask wearing status with OpenCV and Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Face detection from images taken with Raspberry Pi camera
Install pyenv on Raspberry Pi and version control Python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
A memorandum when making a surveillance camera with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Raspberry Pi with Elixir, which is cooler than Python
Easy introduction to home hack with Raspberry Pi and discord.py
Programming with Python and Tkinter
Encryption and decryption with Python
Mutter plants with Raspberry Pi
Update Python for Raspberry Pi to 3.7 or later with pyenv
Python and hardware-Using RS232C with Python-
Play with the Raspberry Pi Zero WH camera module Part 1
Camera capture with Python + OpenCV
I tried running Movidius NCS with python of Raspberry Pi3
I tried connecting Raspberry Pi and conect + with Web API
Production of temperature control system with Raspberry Pi and ESP32 (1)
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
Raspberry Pi + Python + OpenGL memo
Measure and compare temperature with Raspberry Pi and automatically generate graph
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
raspberry pi 1 model b, python
I made a surveillance camera with my first Raspberry PI.
python with pyenv and venv
Web scraping beginner with python
Works with Python and R
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]