jsmith123
jsmith123

Reputation: 11

Python/Pygame button push for sound

I am very new to programming. I am building a project: when i push my doorbell button, a picture sends my phone (with twilio and Imgur) and I also want a doorbell sound to go off when the same button is pushed. the coding i have for the initial part is working, and the picture is sending to my phone

import os.path as pth
import os
import re
import pyimgur
import time
import picamera
import RPi.GPIO as GPIO
from twilio.rest import TwilioRestClient

# Defining GPIO port on RPI
BUTTON = 19 

# setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position for a pushbutton)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Twilio credentials 
TWILIO_SID = "####"
TWILIO_AUTH = "####"

# Phone Numbers
HOME_PHONE = "####"
TWILIO_PHONE = "####"

# text message to send with photo
TXT = "Someones at the Door!"

# directory to save the snapshot in
IMAGE_STORAGE = "/home/pi/Pictures/"

# imgur client setup
IMGUR_ID = "#####"

# name and dimensions of snapshot image
IMG = "snaps.jpg"
IMG_WIDTH = 800
IMG_HEIGHT = 600

# initalize the Twilio client
client = TwilioRestClient(TWILIO_SID, TWILIO_AUTH)

# initialize imgur client
im = pyimgur.Imgur(IMGUR_ID)


try:


    # indefinite loop for the doorbell
    while True:

        GPIO.wait_for_edge(BUTTON, GPIO.RISING)
        print("DoorBell\n")
        with picamera.PiCamera() as camera:
            camera.resolution = (IMG_WIDTH, IMG_HEIGHT)
            camera.capture(IMAGE_STORAGE + IMG)  

        uploaded_image = im.upload_image(IMAGE_STORAGE + IMG, title=TXT)
        client.messages.create(
            to=HOME_PHONE,
            from_=TWILIO_PHONE,
            body=TXT,
            media_url=uploaded_image.link,
        )
finally:
    GPIO.cleanup() # ensures a clean exit

This code works fine to send the pictures to my phone, what i need now is the code to have the pushbutton also make a sound through my 3.5mm jack on my RPI. The coding i have for that (that does not work) is this:

from pygame import mixer
import RPi.GPIO as GPIO
from time import sleep
from sys import exit

# Defining GPIO port on RPI
BUTTON = 19 

# setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position for a pushbutton)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

mixer.init(48000, -16, 1, 1024)

sndA = mixer.music.load('/home/pi/Desktop/doorbell-7.mp3')

while True:
    try:
        if (GPIO.input(19) == True ):
            mixer.music.play(sndA)
            sleep(.01)
    except KeyboardInterrupt:
        exit()

When i try and run this i get:

File "/home/pi/Desktop/sound code.py", line 23, in mixer.music.play(sndA) TypeError: an integer is required

I was wondering if anyone knew how to fix this, and if there was a way of combining these 2 scripts into one?

I have been going at this last part for about 4 days now and I am on a time line so I am just looking for any help.

Upvotes: 1

Views: 1161

Answers (2)

Almonso
Almonso

Reputation: 51

Try using the Sound object from the mixer instead of the music function.

doorbell = pygame.mixer.Sound(filename)
doorbell.play()

Check out this link: Pygame Sound object

As for combining the code, I suggest packaging the code which sends the picture into a function and calling it in your if statement in the second one. However, the keypress function will return true for multiple iterations of the loop, which you can get round by storing the previous keypress value and comparing it to the current one:

last_keypress = False
while True:
    if (not last_keypress) and (GPIO.Input(19)):
        <do stuff>
    last_keypress = GPIO.Input(19)
    time.sleep(.01)

Upvotes: 0

user5653854
user5653854

Reputation:

mixer.music.load() returns None no matter what the input is (see the Docs here). That means that sndA also gets None.

But the pygame.mixer.music.play() method requires two numbers (which are actually optional so you don't need to specify them) as you can see here.

You don't have to use any variables to hold the sound. Just call play() and the previously loaded file will be played:

mixer.music.load('/home/pi/Desktop/doorbell-7.mp3')

# ...

mixer.music.play(-1) # -1 = infinite loop

Upvotes: 2

Related Questions