Cov txheej txheem:

Raspberry Pi Security Camera: 11 Kauj Ruam (nrog Duab)
Raspberry Pi Security Camera: 11 Kauj Ruam (nrog Duab)

Video: Raspberry Pi Security Camera: 11 Kauj Ruam (nrog Duab)

Video: Raspberry Pi Security Camera: 11 Kauj Ruam (nrog Duab)
Video: Настя и Арбуз с вымышленной историей для детей. 2024, Hlis ntuj nqeg
Anonim
Raspberry Pi Security Camera
Raspberry Pi Security Camera

Nov yog cov lus qhia ib qib zuj zus qhia yuav ua li cas thiaj tsim tau IoT, lub zog ua kom lub koob yees duab ruaj ntseg siv Raspberry Pi. Koj yuav kawm paub yuav tsim lub tshuab raj lub vev xaib li cas thiab daim ntawv uas tso cai rau tus neeg siv kho lub koob yees duab kom nkag siab thiab lub sijhawm kaw, ua haujlwm pib/nres kaw, thiab/lossis thaij duab uas yuav raug khaws cia rau hauv zos.

Khoom siv

  • Raspberry Pi 3
  • Pi lub koob yees duab
  • PIR tsab ntawv tsa suab sensor
  • SD daim npav
  • Lub hwj chim

Kauj Ruam 1: Sib dhos Cov Khoos Phis Tawj

Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse
Sib dhos kho vajtse

Thaum Pi raug tua, ntxig daim npav micro-SD rau hauv Pi. Ntxig lub koob yees duab module ribbon cable rau hauv lub koob yees duab module chaw nres nkoj ntawm Pi. Tom qab ntawd, txuas 3 tus pin (sau VCC, OUT, thiab GND) ntawm PRI lub suab ntsuas mus rau Pi's GPIO pins. Txuas VCC mus rau 5.5V ntawm lub zog, GND rau hauv av, thiab OUT txhawm rau pin 11 ntawm Pi.

Kauj ruam 2: Xyuas kom tseeb tias koj tus Pi txuas nrog Is Taws Nem

Xyuas kom tseeb tias koj tus Pi txuas nrog Is Taws Nem
Xyuas kom tseeb tias koj tus Pi txuas nrog Is Taws Nem

Tam sim no, qhib Pi los ntawm kev txuas nws mus rau lub hwj chim thiab txheeb xyuas tias koj tau txuas nrog internet siv cov lus txib ping. Yog tias koj tsis paub yuav txuas koj Pi li cas rau hauv internet, nyem qhov no.

sudo ping www.google.com

Yog tias koj ua tiav, koj yuav tsum pom tias cov ntaub ntawv tau txais los ntawm google.

Ib qho ntxiv, koj tuaj yeem siv ifconfig kom pom koj tus IP chaw nyob.

sudo ifconfig

Kauj Ruam 3: Teeb Lub Koob Yees Duab

Siv cov lus txib hauv qab no los qhib lub teeb tsa kev teeb tsa, thiab ua kom lub koob yees duab nyob hauv "kev xaiv cuam tshuam".

sudo raspi-teeb tsa

Tom qab rov pib dua, koj tuaj yeem tso saib cov xwm txheej ntawm koj lub koob yees duab kom ntseeg tau tias nws tau txuas nrog kom raug.

vcgencmd get_camera

Thaum kawg, teeb tsa lub picamera module.

pip nruab picamera

Kauj Ruam 4: Nruab Flask

Nruab ib lub raj mis thiab lub raj mis-so tus qauv rau Python:

sudo apt-tau nruab python-dev python-pip

nab hab sej -m pip teeb tsa lub raj mis -so kom txaus

Tom ntej no, peb yuav teeb tsa tus nab npawb nab raj mis siv rau tsim cov ntawv.

pip nruab flask-wtf

Kauj Ruam 5: Tsim Chav Kawm Ntawv

Ua cov npe hu ua iotProject khaws tag nrho koj cov ntaub ntawv hauv.

sudo mkdir iotProject

Tsim cov ntaub ntawv nab npawb npe "camControl.py".

sudo nano camControl.py

Hauv cov ntawv no peb yuav tsim peb chav kawm ntawv, uas tso cai rau peb los tsim lub vev xaib nrog cov ntawv ntawv thiab cov ntawv qhia zaub mov nco rau tus neeg siv kom tuaj yeem hloov pauv lub koob yees duab teeb tsa, pib ua/nres kaw, thiab ntes cov vis dis aus.

los ntawm flask_wtf ntshuam FlaskFormfrom wtforms.validators ntshuam Cov Ntaub Ntawv Xav tau los ntawm wtforms ntshuam SubmitField los ntawm wtforms ntshuam validators, IntegerField, BooleanField, SelectField

chav kawm camFrame (FlaskForm):

videoDuration = IntegerField ('Teev Sijhawm Teev Npe (vib nas this)')

rhiab heev = IntegerField ('Motion Sensitivity (thaj tsam 2500-10000) n Tus lej siab dua, lub koob yees duab tsis zoo dua yog', validators = [validators. NumberRange (min = 2500, max = 10000, message = 'Tus Nqi Tawm Ntawm Qhov Ntau')])

options = SelectField ('Options', options = [('none', 'No action'), ('rec', 'Start Recording'), ('stop', 'Stop Recording'), ('pic', 'Siv Duab')])

xa = SubmitField ('Xa')

Kauj Ruam 6: Tsim lub raj mis Template

Tsim lub Flask Template
Tsim lub Flask Template

Txhawm rau tsim cov neeg siv kev sib tham, koj yuav tsum tsim Flask template uas siv daim ntawv uas koj nyuam qhuav tsim. Cov ntaub ntawv no yuav raug sau ua html, thiab khaws cia rau hauv daim nplaub tshev hu ua qauv, uas yuav tsum muaj nyob hauv cov npe tib yam li koj daim ntawv.

Sab hauv ntawm koj cov ntawv tais ceev tseg, tsim cov ntawv hu ua index.html. Hauv cov ntawv no, rov ua dua cov cai uas tau hais los saum no.

Kauj Ruam 7: Muab Tus Qauv

Tam sim no nws yog lub sijhawm los tsim cov ntawv uas ua rau tus qauv. Tsim cov ntaub ntawv npe appCam.py (nco ntsoov tias koj tsis nyob hauv cov ntawv tais ceev tseg lawm). Txhua cov ntsiab lus hloov pauv siv hauv tus qauv yuav tsum raug siv ua lub npe sib cav hauv kev hu mus rau render_template ().

ntshuam camControlfrom flask ntshuam Flask, render_template, thov, Teb los ntawm flask_restful ntshuam peev txheej, Api, reqparse

app = Flask (_ npe_)

app.config ['SECRET_KEY'] = '13542' api = Api (app)

parser = reqparse. RequestParser ()

parser.add_argument ('dur', type = int, help = 'Duration ntawm cov vis dis aus thaum pom lub zog') parser.add_argument ('sens', type = int, help = 'Theem ntawm kev txav mus los kom ua kom kaw') parser.add_argument ('opt', type = str, help = 'Manually record a video or capture an image')

Hloov kho chav kawm (Cov peev txheej):

#Cov khoom rau wtforms def post (tus kheej): args = parser.parse_args () #rc.input (args ['dur'], args ['sens'], args ['opt']) #sau rau cov ntawv ntawv uas tham nrog lub koob yees duab khiav hauv lub koob yees duab parallelSettingsFile = qhib ("cameraSettings.txt", 'w') cameraSettingsFile.write (args ['dur'] + '\ n') #wur dur cameraSettingsFile.write (args ['hnov'] + '\ n') #write sens cameraSettingsFile.write (args ['opt'] + '\ n') #write opt cameraSettingsFile.close () rov qab {'dur': args ['dur'], 'sens': args ['kev nkag siab'], 'xaiv': args ['xaiv']}

@app.route ('/', cov hau kev = ['GET', 'POST'])

def index (): "" "Tus tswj lub vev xaib" "" form = camControl.camFrame () #this yog daim ntawv thov yog thov.method == 'POST': print (request.form) args = [i rau kuv hauv kev thov.form.items ()] #rc.input (int (args [0] [1]), int (args [1] [1]), args [2] [1]) cameraSettingsFile = qhib ("cameraSettings.txt ", 'w') cameraSettingsFile.write (args [0] [1] + '\ n') #write dur cameraSettingsFile.write (args [1] [1] + '\ n') #write sens cameraSettingsFile.write (args [2] [1] + '\ n') #write opt cameraSettingsFile.close () imageDictionary = {"filename": "image.jpg"} return render_template ('index.html', form = form, image = imageDictionary)

api.add_resource (Hloov tshiab, '/hloov tshiab/')

yog _name_ == '_main_':

app.run (host = '0.0.0.0', chaw nres nkoj = 80, debug = Tseeb, threaded = Tseeb)

Kauj Ruam 8: Tsim Lub Koob Yees Duab Chav Kawm

Tam sim no peb xav tsim cov ntaub ntawv hu ua camOperator.py. Hauv nws peb yuav ua chav kawm lub koob yees duab nrog cov txheej txheem los tswj lub koob yees duab, siv cov haujlwm PiCamera uas twb muaj lawm. Peb yuav siv qhov piv txwv ntawm cov khoom no hauv cov kauj ruam tom ntej uas peb yuav ua ke ua haujlwm ntawm lub koob yees duab thiab lub suab txav.

Cov txheej txheem tau hais tseg hauv chav kawm no hloov pauv "cov ntaub ntawv" teeb tsa ntawm lub koob yees duab kev nyab xeeb siv qhov nkag siab thiab lub sijhawm nkag mus uas tus neeg siv muab, thaum tsim cov txiaj ntsig tsis raug rau cov kev hloov pauv no yog tias cov neeg siv tswv yim tsis tuaj.

ntshuam RPi. GPIO ua GPIOimport lub sij hawm ntshuam picamera los ntawm lub sijhawm ntshuam datetime

GPIO.setmode (GPIO. BOARD)

GPIO.setup (11, GPIO. IN)

ntes = 0

chav kawm lub koob yees duab

def _init _ (tus kheej):

#constructor self.cam = picamera. PiCamera () self.data = self.dur = 10 self.sens = 2500 self.opt = "tsis muaj"

def cov ntaub ntawv (tus kheej, dur):

#Records rau lub sijhawm muab los ntawm tus tswj videoName = str (datetime.now ()) videoName = videoName.replace (':', ') videoName = videoName.replace ('. ',') Self.cam.start_recording ('/home/pi/iotProject/videos/' + videoName + '.h264') time.sleep (dur) self.cam.stop_recording ()

def ua haujlwm (tus kheej, dur, sens):

#Kev ua haujlwm tseem ceeb ntawm lub koob yees duab uas tshawb xyuas tas li yog tias tib neeg nyob ze, yog tias tib neeg nyob ib puag ncig ntev txaus, peb pib kaw! thoob ntiaj teb pom kuv = GPIO.input (11) yog tias kuv == 0: #Thaum cov zis los ntawm lub suab sensor yog LOW pom = 0 lub sijhawm pw tsaug zog (0.1) elif i == 1: #Thaum cov zis los ntawm lub suab sensor yog HIGH luam tawm (" tsab ntawv tsa suab pom " +str (ntes)) yog tias pom> = sens*10: self.record (dur) print (" RECORDED ") ntes = 0 time.sleep (0.1) detect += 1

Kauj Ruam 9: Tsim Cov Qauv Sau

Qhov kev zov me nyuam kawg uas xav tau rau txoj haujlwm no yuav raug sau rau hauv cov ntawv hu ua rec.py. Cov ntaub ntawv no qhia lub koob yees duab thaum kaw, ntev npaum li cas kaw, thiab yog/thaum yuav thaij duab. Nws ua qhov no los ntawm kev tshuaj xyuas tas li thiab nyeem cov neeg siv cov ntaub ntawv sau rau hauv kab ntawv los ntawm qib 5. tus pi, xws li hauv.h264 lossis-j.webp

'' 'Ua haujlwm ua ke nrog cov neeg rau zaub mov flask, nyeem cov kev hloov pauv teeb tsa los ntawm cov ntawv server. Tus neeg rau zaub mov tswj kev hloov pauv tau teeb tsa hauv cov ntawv cais thaum xa cov ntawv. Tus lej module nyeem cov kev hloov pauv no thiab hloov kho lub koob yees duab raws lawv. '' 'ntshuam camOperator los ntawm lub sijhawm ntshuam datetime ntshuam sijhawm

rc = camOperator.cameraOperator ()

cameraSettingsFile = qhib ("cameraSettings.txt", 'w') cameraSettingsFile.close () #qhov no, peb qhib thiab kaw hauv kev sau hom txhawm rau tshem tawm cov ntsiab lus hauv cov ntawv ua ntej lub voj tseem ceeb khiav

#Lub voj txuas mus tas li uas saib xyuas seb tib neeg nyob ze. Yog lawv yog, ces

#lub koob yees duab pib kaw. Txoj haujlwm no khiav hauv paralell nrog lub hwj #server uas tswj lub koob yees duab no. recordingInProcess = Cuav thaum Muaj Tseeb: #kos/kaw yog (kawInProcess == Cuav): rc.operation (rc.dur, rc.sens) #hloov pauv cov chaw ntawm lub koob yees duab raws li server serverSettingsFile = qhib ("cameraSettings.txt", 'r') settingNum = 0 rau teeb tsa hauv lub koob yees duabSettingsFile.readlines (): yog teeb tsaNum == 0: #Duration hloov rc.dur = int (teeb tsa) elif settingNum == 1: #Sensitivity hloov rc.sens = int (teeb tsa) elif settingNum == 2: #Action hloov rc.opt = teeb tsa settingNum += 1 lub koob yees duabSettingsFile.close ()

#execute ib qho kev ua

# yog rc.opt == "tsis muaj": # txuas ntxiv yog rc.opt == "rec / n" thiab kawInProcess == Tsis tseeb: luam tawm ("Khiav cov lus txib los ntawm tus tswj") # Tsim lub npe rau video raws li lub sijhawm tam sim no videoName = "snappedVid _"+str (datetime.now ()) videoName = videoName.replace (':', ') videoName = videoName.replace ('. ',') rc.cam.start_recording ('/home/pi/iotProject /videos/' + videoName +'.h264 ') recordingInProcess = True elif rc.opt == "nres / n" thiab kawInProcess == Muaj tseeb: luam tawm ("Nres kaw cov lus txib los ntawm tus tswj") rc.cam.stop_recording () kawInProcess = Cuav cameraSettingsFile = qhib ("cameraSettings.txt", 'w') cameraSettingsFile.write (str (rc.dur)+'\ n') cameraSettingsFile.write (str (rc.sens)+'\ n') cameraSettingsFile. sau ("tsis muaj / n") rc.opt = "tsis muaj / n" elif rc.opt == "pic / n" thiab kawInProcess == Tsis tseeb: luam tawm ("Snap ib pic txib los ntawm tus tswj") pictureName = "snappedPic_ "+str (datetime.now ()) pictureName = pictureName.replace (':', ') pictureName = pictureName.replace ('. ',') rc.cam.st art_preview () time.sleep (5) rc.cam.capture ('duab/' + pictureName + '.jpg') rc.cam.stop_preview () cameraSettingsFile = qhib ("cameraSettings.txt", 'w') cameraSettingsFile. sau (str (rc.dur)+'\ n') cameraSettingsFile.write (str (rc.sens)+'\ n') cameraSettingsFile.write ("none / n") rc.opt = "tsis muaj / n"

Kauj ruam 10: Pib Server

Pib Server
Pib Server

SSH rau hauv pi thiab pib server siv cov kab hais kom ua saum toj no.

Kauj ruam 11: Sim nws tawm

Sim nws tawm!
Sim nws tawm!
Sim nws tawm!
Sim nws tawm!

Nkag mus rau lub vev xaib siv tus IP chaw nyob thiab koj yuav tsum muaj peev xwm tswj lub koob yees duab nyob deb!

Pom zoo: