Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1264

Python • help wanted with code not working

$
0
0
Hi i tried to make an old project from hackershack (the rpi doorbell) and when trying to run the code i get this error

Code:

sudo python doorbell.pyStarting Doorbell...sh: 1: tvservice: not foundsh: 1: tvservice: not foundTraceback (most recent call last):  File "/home/pi/doorbell.py", line 179, in <module>    doorbell.run()  File "/home/pi/doorbell.py", line 153, in run    self._setup_gpio()  File "/home/pi/doorbell.py", line 170, in _setup_gpio    GPIO.add_event_detect(self._doorbell_button_pin, GPIO.RISING, callback=ring_doorbell, bouncetime=2000)RuntimeError: Failed to add edge detection
the whole code for the project is

Code:

############## User Parameters############## Doorbell pinDOORBELL_PIN = 26# Number of seconds to keep the call activeDOORBELL_SCREEN_ACTIVE_S = 60# ID of the JITSI meeting roomJITSI_ID = None  # If None, the program generates a random UUID# JITSI_ID = "hackershackdoorbellexample"# Path to the SFX fileRING_SFX_PATH = None  # If None, no sound effect plays# RING_SFX_PATH = "/home/pi/ring.wav"# Enables email notificationsENABLE_EMAIL = False# Email you want to send the notification from (only works with gmail)FROM_EMAIL = 'sender@gmail.com'# You can generate an app password here to avoid storing your password in plain text# this should also come from an environment variable# https://support.google.com/accounts/answer/185833?hl=enFROM_EMAIL_PASSWORD = 'password'# Email you want to send the update toTO_EMAIL = 'receiver@gmail.com'############## Program#############import timeimport osimport signalimport subprocessimport smtplibimport uuidfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEImage import MIMEImagetry:    import RPi.GPIO as GPIOexcept RuntimeError:    print("Error importing RPi.GPIO. This is probably because you need superuser. Try running again with 'sudo'.")def show_screen():    os.system("tvservice -p")    os.system("xset dpms force on")def hide_screen():    os.system("tvservice -o")def send_email_notification(chat_url):    if ENABLE_EMAIL:        sender = EmailSender(FROM_EMAIL, FROM_EMAIL_PASSWORD)        email = Email(            sender,            'Video Doorbell',            'Notification: A visitor is waiting',            'A video doorbell caller is waiting on the virtual meeting room. Meet them at %s' % chat_url        )        email.send(TO_EMAIL)def ring_doorbell(pin):    SoundEffect(RING_SFX_PATH).play()    chat_id = JITSI_ID if JITSI_ID else str(uuid.uuid4())    video_chat = VideoChat(chat_id)    send_email_notification(video_chat.get_chat_url())       show_screen()    video_chat.start()    time.sleep(DOORBELL_SCREEN_ACTIVE_S)    video_chat.end()    hide_screen()class SoundEffect:    def __init__(self, filepath):        self.filepath = filepath    def play(self):        if self.filepath:            subprocess.Popen(["aplay", self.filepath])class VideoChat:    def __init__(self, chat_id):        self.chat_id = chat_id        self._process = None    def get_chat_url(self):        return "http://meet.jit.si/%s" % self.chat_id    def start(self):        if not self._process and self.chat_id:            self._process = subprocess.Popen(["chromium-browser", "-kiosk", self.get_chat_url()])        else:            print("Can't start video chat -- already started or missing chat id")    def end(self):        if self._process:            os.kill(self._process.pid, signal.SIGTERM)class EmailSender:    def __init__(self, email, password):        self.email = email        self.password = passwordclass Email:    def __init__(self, sender, subject, preamble, body):        self.sender = sender        self.subject = subject        self.preamble = preamble        self.body = body    def send(self, to_email):        msgRoot = MIMEMultipart('related')        msgRoot['Subject'] = self.subject        msgRoot['From'] = self.sender.email        msgRoot['To'] = to_email        msgRoot.preamble = self.preamble        msgAlternative = MIMEMultipart('alternative')        msgRoot.attach(msgAlternative)        msgText = MIMEText(self.body)        msgAlternative.attach(msgText)        smtp = smtplib.SMTP('smtp.gmail.com', 587)        smtp.starttls()        smtp.login(self.sender.email, self.sender.password)        smtp.sendmail(self.sender.email, to_email, msgRoot.as_string())        smtp.quit()class Doorbell:    def __init__(self, doorbell_button_pin):        self._doorbell_button_pin = doorbell_button_pin    def run(self):        try:            print("Starting Doorbell...")            hide_screen()            self._setup_gpio()            print("Waiting for doorbell rings...")            self._wait_forever()        except KeyboardInterrupt:            print("Safely shutting down...")        finally:            self._cleanup()    def _wait_forever(self):        while True:            time.sleep(0.1)    def _setup_gpio(self):        GPIO.setmode(GPIO.BCM)        GPIO.setup(self._doorbell_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)        GPIO.add_event_detect(self._doorbell_button_pin, GPIO.RISING, callback=ring_doorbell, bouncetime=2000)    def _cleanup(self):        GPIO.cleanup(self._doorbell_button_pin)        show_screen()if __name__ == "__main__":    doorbell = Doorbell(DOORBELL_PIN)    doorbell.run()
also the github page https://github.com/HackerShackOfficial/Smart-Doorbell for the code i have changed the mime from email.MIMEMultipart to email.mime.multipart the same goes for the rest of the mime parts but i have no clue how to fix the whole project. Any help would be greatly appreciated!

Statistics: Posted by atmineroomba — Sun May 26, 2024 10:26 am



Viewing all articles
Browse latest Browse all 1264

Trending Articles