-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_pushbutton.py
More file actions
40 lines (30 loc) · 1.04 KB
/
collect_pushbutton.py
File metadata and controls
40 lines (30 loc) · 1.04 KB
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
#!/usr/bin/python
import logging
import RPi.GPIO as GPIO
import time
import datetime
from logging.handlers import RotatingFileHandler
#GPIO SETUP
channel = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
#set path and name for log files
path = "/home/pi/Desktop/Bathroom/log/bathroom.log"
#create rotating log and set level
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
#specify the log size and log file count. 200 bytes will allow 3 logs per file
handler = RotatingFileHandler(path, maxBytes=200, backupCount=5)
logger.addHandler(handler)
#run by default
def callback(channel):
if GPIO.input(channel):
#send output to log file
logger.info("Time " + str(datetime.datetime.now()))
else:
print "Bathroom Used"
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)