2016-07-06 32 views
1

我对编程非常陌生。我正在构建一个项目:当我按下门铃按钮时,图片会发送我的手机(使用twilioImgur),并且我还希望在按下相同按钮时,门铃声音会熄灭。编码我对最初的部分工作,并且图像被发送到我的手机Python/Pygame按钮推送声音

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 

此代码工作正常的图片发送给我的电话,我现在需要的是代码有按钮也通过我的RPI上的3.5毫米插孔发出声音。编码我有那个(不工作)是这样的:

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() 

当我尝试和运行此我得到:

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

,如果有人知道如何解决这个问题我想知道,和如果有将这两个脚本合并成一个的方法?

我一直在这最后一部分现在约4天,我在时间线上,所以我只是寻找任何帮助。

回答

2

mixer.music.load()返回None无论输入是什么(请参阅文档here)。这意味着sndA也得到None

pygame.mixer.music.play()方法需要两个数字(实际上是可选的,因此您不需要指定它们),如您所见here

您不必使用任何变量来保存声音。只要打电话play()和先前加载的文件将被播放:

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

# ... 

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

尝试使用Sound对象从搅拌机而不是音乐功能。

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

退房此链接: Pygame Sound object

至于合并的代码,我建议包装它发送图片到一个函数的代码,把它在你的,如果在第二个发言。

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

我不得不说,这是更漂亮的方法:然而,按键功能的循环,你可以通过将以前的按键值,并将其与当前的轮得到的多次迭代返回true做它 – 2016-07-06 13:20:55

+0

谢谢!这帮了很多,我得到了:) – jsmith123