【发布时间】:2016-07-06 20:22:30
【问题描述】:
我对编程很陌生。我正在构建一个项目:当我按下门铃按钮时,一张图片会发送到我的手机(带有twilio 和Imgur),我还希望在按下同一个按钮时发出门铃声音。我为初始部分编写的编码正在工作,图片正在发送到我的手机
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()
当我尝试运行它时,我得到:
文件“/home/pi/Desktop/sound code.py”,第 23 行,在 Mixer.music.play(sndA) TypeError: an integer is required
我想知道是否有人知道如何解决这个问题,以及是否有办法将这两个脚本合并为一个?
我已经在最后一部分进行了大约 4 天了,而且我在时间线上,所以我只是在寻求任何帮助。
【问题讨论】:
标签: python audio raspberry-pi pygame