Cov txheej txheem:

Lub Hwj Huam-Tawm Khawm rau Raspberry Pi: 3 Kauj Ruam
Lub Hwj Huam-Tawm Khawm rau Raspberry Pi: 3 Kauj Ruam

Video: Lub Hwj Huam-Tawm Khawm rau Raspberry Pi: 3 Kauj Ruam

Video: Lub Hwj Huam-Tawm Khawm rau Raspberry Pi: 3 Kauj Ruam
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim
Lub Hwj Huam-Tawm khawm rau Raspberry Pi
Lub Hwj Huam-Tawm khawm rau Raspberry Pi

Raspberry Pi yog lub platform siv tau txiaj ntsig zoo uas tso cai tsim ntau yam IoT/neeg hlau/ntse-tsev/… project thov. Ib yam uas nws tsis muaj txawm tias, piv rau lub khoos phis tawj ib txwm, yog kaw lub zog-tua khawm. Yog li peb tuaj yeem tsim ib tus kheej li cas? Yog lawm, cia peb ua nws ua ke!

Rau qhov kev qhia no, koj yuav xav tau cov hauv qab no:

  • 1 Raspberry Pi twb tau teeb tsa thiab npaj siv
  • 1 lub khob cij los yog qee yam uas yuav tso cai rau koj los tsim hluav taws xob hluav taws xob
  • 1 laub-khawm
  • 2 jumper xov

Yog tias koj tsis tau siv Raspberry Pi, koj tuaj yeem tshawb xyuas kuv cov lus qhia yuav ua li cas thiab yuav ua dab tsi los teeb tsa nws:

www.instructables.com/How-to-Setup-a-Raspberry-Pi-and-Start-Using-It/

Kauj Ruam 1: Hluav Taws Xob Hluav Taws Xob

Hauv Circuit Court
Hauv Circuit Court
Hauv Circuit Court
Hauv Circuit Court
Hauv Circuit Court
Hauv Circuit Court

Tsis muaj dab tsi nyuab heev ntawm no, nws yog qhov yooj yim Circuit Court. 2 daim duab saum toj no piav qhia txog kev tsim hluav taws xob. Koj tuaj yeem siv qhov twg GPIO tus pin koj xav tau rau lub pob nkag, koj tsuas yog yuav tsum tau ua kom hloov kho cov cai kom muaj kev cuam tshuam qhov ntawd.

Cia peb piav qhia sai npaum li cas qhov no yuav ua haujlwm:

  • RED xaim tau noj 3.3V mus rau ib qho kawg ntawm lub laub khawm.
  • BLACK cable txuas rau lwm qhov kawg ntawm lub laub khawm mus rau Pi GPIO uas yuav siv los ua tswv yim.
  • los ntawm lub neej ntawd lub pob tau qhib, yog li tsis muaj qhov hluav taws xob hla nws. Yog li BLACK cable nyob ntawm 0V thaum lub khawm tsis nias. Qhov no txhais tau tias lub xeev cov ntsiab lus ntawm Pi GPIO cov tswv yim yog 0.
  • thaum lub pob yuav raug nias, qhov hluav taws xob yuav hla nws thiab BLACK cable yuav txuas rau 3.3V. Raspberry Pi yuav pom qhov hluav taws xob ntawm 3.3V ntawm nws cov tswv yim, sib thooj rau lub xeev muaj tswv yim ntawm 1.

Kauj ruam 2: Python Code

Python Code
Python Code

Tam sim no qhov kev siv hluav taws xob tau npaj siv, peb yuav tsum sau cov cai uas yuav ua nws, thiab kuv siv Python hauv PyCharm ib puag ncig ntawm no. Kuv teeb tsa lub pob kom lub zog-tawm ntawm Raspberry Pi tsuas yog thaum nws raug nias ntau dua 3 vib nas this. Qhov laj thawj yog vim li cas kuv thiaj ua li ntawd yog tias nws yooj yim heev kom nws nias yuam kev, thiab koj tsis xav kaw koj Pi los ntawm kev sib tsoo.

Cov ntawv luam tawm saum toj no yog los ntawm kuv ib puag ncig PyCharm, thiab cov cai yog cov hauv qab no (nrog qhov sib txawv me me ntawm kab 26 uas txuas rau lwm txoj haujlwm tab sis tsis xav tau ntawm no):

# Qhov no yog aa code rau poweroff Raspberry Pi thaum nias thiab tuav lub khawm txhais # Cov khoom siv sab nrauv importimport RPi. GPIO li GPIOimport timeimport os # Raspberry Pi tus pin & cov ntsiab lus hloov pauv txhais & hold_time = 3 # Tuav lub sijhawm hauv sec rau poweroffbutton_poweroff = 1 # Nias khawm txhawm rau tua Raspberry PiGPIO.setwarnings (Cuav) GPIO.setmode (GPIO. BCM) # Broadcom tus lej-tus lej txheej txheem GPIO.setup (button_poweroff, GPIO. IN, pull_up_down = GPIO. PUD_DOWN) # Khawm teeb raws li kev nkag siab tseeb: GPIO.wait_for_edge (button_poweroff, GPIO. RISING) pib = time.time () time.sleep (0.2) # Hloov debounce thaum GPIO.input (button_poweroff) == 1: time.sleep (0.01) ntev = time.time () - pib yog ntev > tuav_time: os.system ("sudo poweroff")

Thawj qhov, yog tias koj tau txuas lub laub khawm mus rau GPIO sib txawv, yog hloov kho kab 11 ntawm cov ntawv sau saum toj no nrog cov ntsiab lus cuam tshuam GPIO:

button_poweroff = GPIO_X # Hloov kho nrog qhov tseeb GPIO siv rau hauv koj lub Circuit Court

Tsis tas li, qhov tuav tuav lub sijhawm tso cai rau koj hloov kho lub sijhawm tos uas ua rau Pi kaw.

Kauj Ruam 3: Yuav Ua Li Cas Tso Tawm Tsab Ntawv Tom Qab Txheej Txheem Khau

Yuav Ua Li Cas Thiaj Li Tawm Tsab Ntawv Tom Qab Cov Txheej Txheem Boot
Yuav Ua Li Cas Thiaj Li Tawm Tsab Ntawv Tom Qab Cov Txheej Txheem Boot
Yuav Ua Li Cas Thiaj Li Tawm Tsab Ntawv Tom Qab Cov Txheej Txheem Boot
Yuav Ua Li Cas Thiaj Li Tawm Tsab Ntawv Tom Qab Cov Txheej Txheem Boot

Tam sim no tias peb muaj cov cai npaj, peb tsuas yog yuav tsum tau ua nws. Tab sis, nws yuav yooj yim yog tias peb tuaj yeem ua tsab ntawv no ua tiav txhua lub sijhawm Pi tau pib, nyob rau hauv txoj kev tsis siv neeg, yog li lub pob yuav ua haujlwm yam tsis muaj peb kom khiav tsab ntawv txhua lub sijhawm. Muaj ntau txoj hauv kev los ua. Kuv tau ntxiv ntawm no ib kab rau hauv rc.local cov ntaub ntawv uas nyob hauv / etc / folder ntawm koj Pi. Nws tau ua tiav raws li ib feem ntawm cov khau raj sib lawv liag.

Koj yuav tsum qhib kab hais kom ua thiab ntaus cov lus txib hauv qab no (1st printscreen saum toj no):

cd / cov

cd thiab lwm yam sudo nano rc.local

Thawj lo lus txib yuav coj koj los ntawm koj /tsev /pi phau ntawv teev npe mus rau lub hauv paus, uas yog /.

Qhov lus txib thib ob yuav coj koj mus rau / etc / directory.

Thaum kawg, tus thib peb yuav qhib rc.local cov ntaub ntawv ua tus siv superuser, nrog rau txoj cai hloov pauv tag nrho, uas koj yuav tsum hloov kho cov ntaub ntawv.

Ib zaug hauv cov ntawv, koj tsuas yog xav tau ntxiv kab ntawm qhov kawg ntawm nws, tab sis ua ntej tawm lus 0 nqe lus (2nd printscreen saum toj no):

# Ntxiv kab ntawv no rau rc.local cov ntaub ntawv kom qhib tsab ntawv

sudo python /home/pi/Documents/shutdown_with_hold.py &

Muaj qee yam uas koj yuav tsum tau ua tib zoo saib, ntawm no:

  1. lub npe ntawm koj cov ntaub ntawv: hauv kab saum toj no, Kuv xav tias cov ntaub ntawv raug kaw_with_hold.py. Tab sis nws tuaj yeem yog txhua yam koj xav tau, tsuas yog hloov lub npe nrog koj li.
  2. qhov twg koj tau khaws koj cov ntaub ntawv: hauv kab saum toj no, Kuv xav tias nws tau khaws tseg hauv koj/tsev/pi/Cov ntawv teev npe. Tab sis ntxiv, nws tuaj yeem nyob txhua qhov chaw. Koj tsuas yog yuav tsum paub tseeb tias yuav muab txoj hauv kev rau koj cov ntaub ntawv ntawm no.
  3. tus cim "&" qhov kawg ntawm nws: qhov no yog qhov tseem ceeb, thiab nws tso cai rau qhov kev hais kom ua no rau tom qab

Thiab qhov ntawd yog nws! Yog li tam sim no, tsab ntawv yuav ua tiav txhua lub sijhawm koj Pi yuav ON, thiab koj tsuas yog yuav tsum tau nyem ntau dua 3 vib nas this ntawm lub pob kom tua nws.

Pom zoo: