*: initial toying with gpiozero python3

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2024-11-15 21:08:19 -05:00
parent ac063c96e3
commit b10dcbba57
Signed by: vbatts
GPG key ID: 71F35A9A3D56A0EA
5 changed files with 93 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*~

View file

@ -1,2 +1,17 @@
# bathroom-timer
Problem: the kids take showers or are "on the can" for longer than 30min.
Solution: make an alarm that goes off when the door has been closed for 30min.
## Notes
Started with this kit: [Huzzah! Adafruit.io Internet of Things Feather ESP8266 - WiFi Starter Kit](https://www.adafruit.com/product/2680)
But I needed a clock to measure 30min, so I am now using an old, low-voltage RPI Model A+ v1.1.
Still considering the solder kit: [Ladyada's Electronics Toolkit](https://www.adafruit.com/product/136)
- [python GPIO zero](https://gpiozero.readthedocs.io/en/latest/installing.html)
- [MagPI article on programming GPIO on the RPI](https://raw.githubusercontent.com/raspberrypipress/released-pdfs/main/simple-electronics-with-gpio-zero.pdf)

13
blink.py Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from gpiozero import LED
from time import sleep
led = LED(25)
while True:
led.on()
sleep(1)
led.off()
sleep(1)

25
button.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python3
from gpiozero import LED
from gpiozero import Button
from time import sleep
led = LED(25)
button = Button(21)
def pressed():
led.on()
print("button pressed. LED on")
def released():
led.off()
print("button released. LED off")
button.when_pressed = pressed
button.when_released = released
print("ready")
while True:
sleep(0.2)

39
door.py Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python3
from gpiozero import Button, Buzzer
from time import sleep
from datetime import datetime
# https://docs.python.org/3/library/datetime.html#datetime.datetime.now
# the door sensor
door = Button(23)
btn = Button(19)
bz = Buzzer(26)
#bz.on()
#bz.off()
def alarm():
bz.beep(0.5,0.25,8)
def doorClosed():
t = datetime.now()
print(f"{t} - door closed. LED on")
def doorOpen():
t = datetime.now()
print(f"{t} - door open. LED off")
def whenPressed():
bz.toggle()
btn.when_pressed = whenPressed
door.when_pressed = doorClosed
door.when_released = doorOpen
print("ready")
while True:
sleep(0.2)