Skip to content

Instantly share code, notes, and snippets.

@karaage0703
Last active December 26, 2020 14:55
Show Gist options
  • Save karaage0703/8263451c7310f6fd203c to your computer and use it in GitHub Desktop.
Save karaage0703/8263451c7310f6fd203c to your computer and use it in GitHub Desktop.
raspberry pi digicam
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import RPi.GPIO as GPIO
import subprocess
import wiringpi2 as wiringpi
import time
from time import sleep
import picamera
import os
import syslog
# GPIO pin
SHUTTER_BTN = 17
# GPIO setting
wiringpi.wiringPiSetupGpio() # setting gpio number
wiringpi.pinMode(SHUTTER_BTN, 0) # setting input
wiringpi.pullUpDnControl(SHUTTER_BTN, 2) # pulled up by 3.3V
# Camera Setting
shutter_numb = 0
# global
SleepStepSec = 0.1
home_dir = '/home/pi/photo'
def cameraLoad():
global shutter_numb
filename = os.path.join(home_dir, 'camera.set')
fp = open(filename)
tmp_shutter_numb = fp.readlines()
tmp2_shutter_numb = tmp_shutter_numb[0].rstrip()
shutter_numb = int(tmp2_shutter_numb)
fp.close()
def cameraSave():
filename = os.path.join(home_dir, 'camera.set')
fp = open(filename, 'w')
fp.write(str(shutter_numb))
fp.close()
def shutter():
if KeepWatchForSeconds(5):
CallShutdown()
global shutter_numb
shutter_numb += 1
cameraSave()
filename = os.path.join(home_dir, str(shutter_numb) + '.jpg')
photofile = open(filename, 'wb')
with picamera.PiCamera() as camera:
camera.resolution = (2592,1944)
camera.start_preview()
time.sleep(1.000) # initialize camera
camera.capture(photofile)
photofile.close()
def KeepWatchForSeconds(seconds):
GoFlag = True
if seconds < 1:
seconds = 1
while seconds > 0:
time.sleep(SleepStepSec)
seconds -= SleepStepSec
if (wiringpi.digitalRead(SHUTTER_BTN) == True):
GoFlag = False
break
return GoFlag
def CallShutdown():
syslog.syslog(syslog.LOG_NOTICE, "Going shutdown by GPIO.")
cmd = "/sbin/shutdown -h now 'Poweroff by GPIO'"
subprocess.call(cmd, shell=True)
cameraLoad()
while True:
sleep(0.5)
if (wiringpi.digitalRead(SHUTTER_BTN) == False):
shutter()
@karaage0703
Copy link
Author

Raspberry Pi2 + Raspbian Jessie

$ cd
$ git clone https://gist.github.com/8263451c7310f6fd203c.git raspidigicam
$ chmod 755 raspidigicam/raspidigicam.py
$ sudo cp raspidigicam/raspidigicam.py /usr/local/bin/
$ mkdir photo
$ echo 0 > photo/camera.set

Edit /etc/rc.d/rc.local
Add next command to last line

sudo raspidigicam.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment