Le code Python
http://remuemeninges.org/code/preuve.py
Le code :
#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # preuve.py import os import subprocess import time from datetime import datetime import RPi.GPIO as GPIO GPIO.setwarnings(False) def main(): # tell the GPIO module that we want to use the # chip's pin numbering scheme GPIO.setmode(GPIO.BCM) GPIO.setup(24,GPIO.OUT) # rouge GPIO.setup(25,GPIO.OUT) # verte GPIO.setup(18, GPIO.IN) # détection de mouvement #PIR #GPIO data dans 18 #GPIO 5v sur fil rouge #GPIO GND sur fil noir # Photo dimensions and rotation photo_width = 640 photo_height = 480 photo_rotate = 0 # Intervalles (secondes) photo_intervalle = 1 detection_intervalle = 5.0 photos_par_intervalle = 4 photo_compteur = 0 photo_nombre = 0 intervalle_compteur = 0 heure_intrusion = 0.0 duree_detection = 0 GPIO.output(25,True) # Allume vert pendant 5 sec. GPIO.output(24,True) # Allume rouge pendant 5 sec time.sleep(5) # Détection démarre GPIO.output(24,False) # Rouge éteinte, verte demeure allumée print ("Détection démarre à " + str(datetime.now()) ) while True: try: if GPIO.input(18) == True: print('Mouvement détecté à ' + str(datetime.now()) ) heure_intrusion = time.clock() photo_compteur = 0 while time.clock() < heure_intrusion + detection_intervalle : while photo_compteur < photos_par_intervalle : photo_compteur = photo_compteur + 1 fichier = 'photo_' + str(heure_intrusion) + str(intervalle_compteur) + '_' + str(photo_compteur) + '.jpg' cmd = 'raspistill -o ' + fichier + ' -t 1000 ' pid = subprocess.call(cmd, shell=True) print ('Écriture de ' + str(fichier) + ' sur disque') photo_nombre = photo_nombre +1 GPIO.output(24,True) # rouge allumée time.sleep(photo_intervalle) else: GPIO.output(24,False) # rouge éteinte GPIO.output(25,True) # verte éteinte time.sleep(detection_intervalle) except (KeyboardInterrupt, SystemExit): GPIO.output(25,False) GPIO.output(24,False) GPIO.cleanup() print ('\nArrêt manuel à ' + str(datetime.now()) + '!') print ('Photo conservées = ' + str(photo_nombre) + '.') print ('\n...Programme arrêté!') break if __name__=="__main__": main()
Génèse du projet
Références :
- Pour détecter un mouvement (1) : https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/overview
- Pour déclencher la caméra :
- Appel en shell (2) : http://www.raspberrypi-spy.co.uk/2013/06/testing-multiple-pi-camera-options-with-python/
- Module pyton (3) : http://www.raspberrypi.org/picamera-pure-python-interface-for-camera-module/
Le code de détection [provenant de (1)]:
import time import RPi.GPIO as io io.setmode(io.BCM) pir_pin = 18 door_pin = 23 io.setup(pir_pin, io.IN) # activate input io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp while True: if io.input(pir_pin): print("PIR ALARM!") if io.input(door_pin): print("DOOR ALARM!") time.sleep(0.5)
Le code pour capturer une image en shell [Provenant de (2)]
#!/usr/bin/env python import os import time import subprocess # Full list of Exposure and White Balance options #list_ex = ['off','auto','night','nightpreview','backlight', # 'spotlight','sports','snow','beach','verylong', # 'fixedfps','antishake','fireworks'] #list_awb = ['off','auto','sun','cloud','shade','tungsten', # 'fluorescent','incandescent','flash','horizon'] # Refined list of Exposure and White Balance options. 50 photos. #list_ex = ['off','auto','night','backlight','fireworks'] #list_awb = ['off','auto','sun','cloud','shade','tungsten', # 'fluorescent','incandescent','flash','horizon'] # Test list of Exposure and White Balance options. 6 photos. list_ex = ['off','auto'] list_awb = ['off','auto','sun'] # EV level photo_ev = 0 # Photo dimensions and rotation photo_width = 640 photo_height = 480 photo_rotate = 0 photo_interval = 0.25 # Interval between photos (seconds) photo_counter = 0 # Photo counter total_photos = len(list_ex) * len(list_awb) # Delete all previous image files try: os.remove("photo_*.jpg") except OSError: pass # Lets start taking photos! try: print "Starting photo sequence" for ex in list_ex: for awb in list_awb: photo_counter = photo_counter + 1 filename = 'photo_' + ex + '_' + awb + '.jpg' cmd = 'raspistill -o ' + filename + ' -t 1000 -ex ' + ex + ' -awb ' + awb + ' -ev ' + str(photo_ev) + ' -w ' + str(photo_width) + ' -h ' + str(photo_height) + ' -rot ' + str(photo_rotate) pid = subprocess.call(cmd, shell=True) print ' [' + str(photo_counter) + ' of ' + str(total_photos) + '] ' + filename time.sleep(photo_interval) print "Finished photo sequence" except KeyboardInterrupt: # User quit print "\nGoodbye!"axd;a
Le code pour capturer une image en Python [Provenant de (3)] :
import picamera
from time import sleep
camera = picamera.PiCamera()
camera.capture('image.jpg')
camera.start_preview()
camera.vflip = True
camera.hflip = True
camera.brightness = 60
camera.start_recording('video.h264')
sleep(5)
camera.stop_recording()
Il reste à modifier et adapter l’ensemble !
Algorithme
- Tant que le bouton n’est pas pressé
- Si il y a mouvement
- Prendre 5 photos à 0,25 secondes d’intervalle
- Faire une pause de 2 minutes
- Reprendre
- Si il y a mouvement
- Si le bouton est pressé
- Faire entendre un buz
- Éteindre la lumière
- Arrêter le programme
Matériel
- Plaque de montage
- Piezo Buzzer
- LED
- Détecteur de mouvement
- Bouton
- Résistances
- Adafruit Pi Gobbler
Montage :
Code [on est loin du préambule, mais ça marche !]