Cov txheej txheem:
- Kauj Ruam 1: PIR Motion Sensor
- Kauj Ruam 2: Teeb PIR Tsab Ntawv Pom Zoo Sensor
- Kauj Ruam 3: Raspberry Pi Lub Koob Yees Duab Module thiab Teeb
- Kauj Ruam 4: Muab PIR Motion Sensor thiab Lub Koob Yees Duab Module
- Kauj Ruam 5: Teeb rau Flask
- Kauj Ruam 6: Qhov tshwm sim
Video: Raspberry Pi Human Detector + Camera + Flask: 6 Kauj Ruam
2024 Tus sau: John Day | [email protected]. Kawg hloov kho: 2024-01-30 09:29
Hauv qhov kev qhia no, Kuv yuav taug kev los ntawm cov kauj ruam rau kuv li Raspberry Pi IoT Project - Siv PIR Motion Sensor, Raspberry Lub Koob Yees Duab los tsim kom muaj kev ruaj ntseg yooj yim IoT ntaus ntawv, thiab nkag mus rau qhov ntsuas pom nrog Flask.
Kauj Ruam 1: PIR Motion Sensor
PIR sawv rau "Passive Infrared" thiab qhov ntsuas qhov ntsuas no tuaj yeem nqa cov lus tsa suab los ntawm kev saib lub teeb pom kev pom thiab khaws qhov hloov pauv infrared. Yog li, nrog nplooj thiab tib neeg hla lub ntsuas, nws tsuas yog kuaj pom tib neeg txij li thaum peb tib neeg tsim hluav taws xob thiab yog li tso tawm hluav taws xob hluav taws xob. Yog li ntawv, lub suab txav txav yog qhov kev xaiv zoo los txheeb xyuas tib neeg kev txav chaw.
Kauj Ruam 2: Teeb PIR Tsab Ntawv Pom Zoo Sensor
Muaj peb tus pin rau PIR tsab ntawv tsa suab sensor, Fais fab, tso zis thiab av. Hauv qab tus pin koj tuaj yeem pom cov ntawv lo, VCC rau Lub Hwj Chim, Tawm Tawm Tawm thiab GND rau hauv av. Thaum lub ntsuas pom pom kev txav mus, Tus pin tso tawm yuav tso tawm lub teeb liab HIGH rau Raspberry Pi tus pin uas koj txuas lub sensor nrog. Txog Lub Hwj Chim tus pin, koj xav kom ntseeg tau tias nws txuas rau 5V tus pin ntawm Raspberry Pi rau lub zog. Txog kuv qhov haujlwm, kuv xaiv txuas tus Pin Output nrog Pin11 ntawm Pi.
Tom qab txuas txhua yam, koj tuaj yeem sau koj lub sensor los ntawm kev sau cov ntawv zoo li cov hauv qab no:
ntshuam RPi. GPIO li GPIOimport lub sij hawm GPIO.cleanup () GPIO.setwarnings (Cuav) GPIO.setmode (GPIO. BOARD) GPIO.setup (11, GPIO. IN) #Nyeem cov zis los ntawm PIR tsab ntawv tsa suab sensor ntawm tus Pin 11 thaum muaj tseeb: = GPIO.input (11) yog kuv == 0: #Thaum cov zis tawm los ntawm lub suab sensor LOW luam tawm "Tsis pom", kuv lub sijhawm pw tsaug zog (0.1) elif i == 1: #Thaum cov zis tawm los ntawm lub suab sensor yog HIGH luam tawm " Kev txav txav pom ", kuv lub sijhawm pw tsaug zog (0.1)
Khiav tsab ntawv ntawm koj tus Pi, thiab tso koj txhais tes lossis koj tus phooj ywg nyob rau hauv pem hauv ntej ntawm lub sensor txhawm rau txheeb xyuas yog tias lub ntsuas pa tuaj yeem txav mus los.
Kauj Ruam 3: Raspberry Pi Lub Koob Yees Duab Module thiab Teeb
Tib neeg tso hluav taws xob hluav taws xob tawm vim cua sov, thiab yog li cov khoom muaj qhov kub. Yog li ntawd, tsiaj lossis khoom kub tuaj yeem ua rau lub suab txav txav ib yam. Peb xav tau ib txoj hauv kev los xyuas seb qhov kev kuaj pom puas siv tau. Muaj ntau txoj hauv kev los ua, tab sis hauv kuv qhov haujlwm, kuv xaiv siv Raspberry Pi lub koob yees duab lub koob yees duab los thaij duab thaum lub suab txav txav mus los.
Txhawm rau siv lub koob yees duab module, koj xub xav kom paub tseeb tias cov koob tau txuas rau hauv lub koob yees duab qhov ntawm Pi. Hom
sudo raspi-teeb tsa
ntawm koj tus Pi kom qhib kev teeb tsa lub ntsej muag, thiab ua kom lub koob yees duab nyob hauv 'kev xaiv cuam tshuam'. Tom qab rov pib dua, koj tuaj yeem sim yog tias Pi tau txuas nrog lub koob yees duab tiag los ntawm kev ntaus ntawv
vcgencmd get_camera
thiab nws yuav qhia koj li xwm txheej. Kauj ruam kawg yog teeb tsa lub picamera module los ntawm kev ntaus ntawv
pip nruab picamera
Tom qab txhua qhov teeb tsa, koj tuaj yeem sim koj lub koob yees duab los ntawm kev sau cov ntawv zoo li cov hauv qab no:
los ntawm picamera ntshuam PiCamera
los ntawm lub sijhawm ntshuam lub koob yees duab pw = PiCamera () camera.start_preview () tsaug zog (2) camera.capture ('image.jpg') camera.stop_preview ()
Daim duab yuav muab khaws cia ua 'image.jpg' ntawm phau ntawv teev npe ib yam li ib qho ntawm koj lub koob yees duab tsab ntawv. Daim ntawv ceeb toom, koj xav kom ntseeg tau tias 'pw tsaug zog (2)' nyob ntawd thiab tus lej ntau dua 2 yog li lub koob yees duab muaj sijhawm txaus los kho lub teeb pom kev zoo.
Kauj Ruam 4: Muab PIR Motion Sensor thiab Lub Koob Yees Duab Module
Lub tswv yim ntawm kuv qhov project yog tias lub suab sensor thiab lub koob yees duab yuav ntsib nyob rau tib txoj kev. Thaum twg los xij uas lub suab txav txav txav mus los, lub koob yees duab yuav thaij duab yog li peb tuaj yeem tshawb xyuas qhov ua rau muaj kev txav tom qab.
Tsab ntawv:
ntshuam RPi. GPIO li GPIO los ntawm lub sijhawm ntshuam lub sijhawm ntshuam lub sijhawm los ntawm picamera ntshuam PiCamera
GPIO.cleanup ()
GPIO.setwarnings (Cuav) GPIO.setmode (GPIO. BOARD) GPIO.setup (11, GPIO. IN) #Nyeem cov zis los ntawm PIR tsab ntawv tsa suab sensor lus = 'pib' counter = 0 log_f = qhib ('zoo li qub/log.txt', 'w') log_f.close ()
lub koob yees duab = PiCamera ()
pic_Name = 0
camera.start_preview ()
sijhawm.sleep (2)
thaum Muaj Tseeb:
i = GPIO.input (11) yog tias kuv == 0: #Thaum cov zis los ntawm cov lus tsa suab qis yog qhov txee> 0: kawg = str (datetime.now ()) log_f = qhib ('zoo li qub/log.txt', ' a ') lus = lus +'; kawg ntawm ' + kawg +' / n 'print (message) log_f.write (message) log_f.close () kawg =' zoo li qub/' + str (pic_name) + ".jpg" pic_name = pic_name + 1 camera.capture (zaum kawg) txee = 0 luam tawm "Tsis muaj cov neeg nkag mus", kuv lub sijhawm pw tsaug zog (0.1) elif i == 1: #Thaum cov zis los ntawm cov lus tsa suab sensor yog siab dua yog hais tias counter == 0: tam sim no = str (datetime.now ()) lus = 'Tib neeg kuaj pom:' + 'pib ntawm' + lub txee tam sim no = txee + 1 luam "Tus neeg nkag mus tshawb pom", kuv lub sijhawm pw tsaug zog (0.1) camera.stop_preview ()
Cov npe rau 'log.txt' thiab cov duab yog 'zoo li qub', uas tsim nyog rau Flask ua haujlwm.
Kauj Ruam 5: Teeb rau Flask
Flask yog lub vev xaib me me sau hauv Python thiab raws li Werkzeug cov cuab yeej thiab Jinja2 tus qauv cav. Nws yog ib qho yooj yim los siv thiab tswj. Txog kev qhia zoo rau Flask, Kuv pom zoo qhov txuas no: Flask Mega Tutorial
Tsab ntawv tseem ceeb, 'routes.py', ntawm kuv qhov project:
los ntawm appfolder ntshuam appFlask los ntawm cov raj ntshuam render_template, xa rov tuaj ntshuam os
APP_ROOT = os.path.dirname (os.path.abspath (_ file_)) # hais txog application_top
APP_STATIC = os.path.join (APP_ROOT, 'zoo li qub')
@appFlask.route ('/', txoj kev = ['Tau', 'POST'])
def view (): log_f = qhib (os.path.join (APP_STATIC, 'log.txt'), 'r') cav = log_f.readlines () final_logs = rau cav hauv cav: final_logs.append (log. strip ()) name = str (len (final_logs) -1)+'. jpg' return render_template ('view.html', cav = final_logs, filename = npe)
Cov ntaub ntawv HTML 'view.html' yog nyob rau sab saum toj bar (vim tias thaum kuv luam cov lej HTML ntawm no, nws yeej tig mus rau HTML FORMAT …)
Thiab cov qauv ntawm txoj haujlwm yuav tsum zoo li ib yam hauv qab no (tab sis tau kawg muaj ntau cov ntaub ntawv ntau dua li cov no):
iotproject / appfolder / routes.py templates / view.html zoo li qub / log.txt 0-j.webp
Kauj Ruam 6: Qhov tshwm sim
Txog qhov kev siv no, tom qab txhua yam teeb tsa kom raug, koj yuav tsum muaj peev xwm nkag mus rau koj li Raspberry Pi los ntawm kev ntaus nws tus IP chaw nyob ntawm qhov browser, thiab qhov tshwm sim yuav tsum zoo li daim duab ntawm kab saum toj ntawm theem no.