Created
May 25, 2024 08:49
-
-
Save j4velin/192b81dadd924bad2b5c77f28a1cbfed to your computer and use it in GitHub Desktop.
React on door ring button press by taking a photo, sending UDP broadcast and playing jingle bells
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from gpiozero import Button, TonalBuzzer | |
from gpiozero.tones import Tone | |
from picamera import PiCamera | |
from time import sleep | |
import socket | |
import threading | |
buzzer = TonalBuzzer(27, octaves=1, mid_tone="A4") | |
button = Button(4) | |
camera = PiCamera() | |
camera.rotation = 180 | |
msg = bytes("door ring\n", "utf-8") | |
notes = "EEE EEE EGCDE FFF FFEE EEGGFDC" | |
beats = [2,2,3,1, 2,2,3,1, 2,2,3,1,4,4, 2,2,3,0, 1,2,2,2,0, 1,1,2,2,2,2,4,4] | |
delay = 0.07 | |
#print(buzzer.min_tone) | |
#print(buzzer.max_tone) | |
def playSong(): | |
for n, b in zip(notes, beats): | |
duration = b * delay | |
if n != ' ': | |
buzzer.play(Tone(n + "4")) | |
sleep(duration) | |
buzzer.stop() | |
sleep(duration) | |
while True: | |
button.wait_for_press() | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) as sock: | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
sock.sendto(msg, ("255.255.255.255", 5005)) | |
beep_thread = threading.Thread(target=playSong, name="Beep") | |
beep_thread.start() | |
camera.start_preview() | |
sleep(2) | |
camera.capture('/home/pi/cam.jpg') | |
camera.stop_preview() | |
#print("photo taken") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment