-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpir-light.py
117 lines (92 loc) · 3.13 KB
/
pir-light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -----------------------------------------------------------
# Device : Raspberry-Pi 3 B+ (2018 Release)
# File name : pir-led-light-bulb.py
# Description : Raspberry-Pi 3 B+ project. Motion detection LEDs & Light-Bulb.
# Author : Kudzai Sean Huni
# E-mail : [email protected]
# -----------------------------------------------------------
# Website : www.kudzai.xyz
# Date : 19-05-2018
# -----------------------------------------------------------
# -----------------------------------------------------------
# Hardware:
# 1. Green LED
# 2. Red LED
# 3. Pir Motion Sensor
# 4. GPIO Expansion Board
# 5. GPIO Bread Board
# 6. 8-Channel Relay Module
# 7. Reading-Lamp/Light-Bulb (240 Volts)
# -----------------------------------------------------------
import RPi.GPIO as GPIO
import time
import collections as coll
freqs = coll.Counter() # Declaring the Counter
lBulb = 22 # GPIO.BOARD=15 OR GPIO.BCM=22
pir = 16
redLed = 13
greenLed = 5
# Timers
lbTimerONN = 3.5
lbTimerOFF = 0.5
txtOnMotion = "Motion Detected !!!"
txtOnIdle = "System Idle..."
txtBulbONN = "Light-Bulb ONN..."
txtBulbOFF = "Light-Bulb OFF..."
GPIO.setmode(GPIO.BCM) # Pin-Numbers by Broadcom SOC Channel
# Setting up/initiasing program parameters
def init():
# Light & Motion Sensor Setup
GPIO.setup(lBulb, GPIO.OUT) # Relay Module Channel 1
GPIO.setup(redLed, GPIO.OUT) # Red LED
GPIO.setup(greenLed, GPIO.OUT) # Green LED
GPIO.setup(pir, GPIO.IN) # Motion Sesnor Input Expected
GPIO.output(lBulb, GPIO.LOW) # Turn off Chanel 1
GPIO.output(redLed, GPIO.LOW)
GPIO.output(greenLed, GPIO.HIGH) # Turn onn LED
# Function Definition for cleaning-up environment.
def clear_up():
GPIO.output(redLed, GPIO.LOW)
GPIO.output(greenLed, GPIO.LOW)
GPIO.output(lBulb, GPIO.LOW) # GREEN LED-OFF
GPIO.cleanup() # Release Hardware Resources
# Turn onn the Red-Led to indicate motion detection.
# Simultaneously turn off the Green-Led as the Light Bulb Turns ONN.
def on_motion():
print(txtBulbONN)
GPIO.output(redLed, GPIO.HIGH)
GPIO.output(greenLed, GPIO.LOW)
GPIO.output(lBulb, GPIO.LOW)
print(txtOnMotion)
# Waiting for an event to occur on the Motion-Sensor
def on_idle():
print(txtBulbOFF)
GPIO.output(redLed, GPIO.LOW)
GPIO.output(greenLed, GPIO.HIGH)
GPIO.output(lBulb, GPIO.HIGH)
print(txtOnIdle)
def prg_loop():
i = 1
while True:
# Counter Feedback
print("Cycle: " + str(i))
# Read input from the Pir-Motion Sensor
irp = GPIO.input(pir)
if irp == 0:
on_idle()
time.sleep(lbTimerOFF)
elif irp == 1:
on_motion()
time.sleep(lbTimerONN)
i += 1 # Increment counter by 1
# Program execution begins here...
if __name__ == '__main__':
print('Press Ctrl-C to terminate program...')
print('Initialising...')
init()
print('Initialisation complete!!!')
try:
prg_loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the flowing code will be executed.
print(" Oh Noo, looks like our fun has been cut-short!!!")
clear_up()