2016-07-06 48 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