我对编程非常陌生。我正在构建一个项目:当我按下门铃按钮时,图片会发送我的手机(使用twilio
和Imgur
),并且我还希望在按下相同按钮时,门铃声音会熄灭。编码我对最初的部分工作,并且图像被发送到我的手机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天,我在时间线上,所以我只是寻找任何帮助。
我不得不说,这是更漂亮的方法:然而,按键功能的循环,你可以通过将以前的按键值,并将其与当前的轮得到的多次迭代返回true做它 – 2016-07-06 13:20:55
谢谢!这帮了很多,我得到了:) – jsmith123