0
좋아, 나는 라스베리 파이에서 실행되는 아래의 코드가 있습니다. 문제는 아래 코드가 테스트 할 때 여러 번 작동하지만 하루가 지나면 화면 인쇄가 표시되고 충돌이나 오류는 발생하지 않지만 센드 메일은 실행되지 않는다는 것입니다. 저는 파이썬이 저에게 아주 새로운 것이고 명백한 버그가 있는지 알고 싶기 때문에 더 많은 닷넷 프로그래머입니다. 또는 파이썬을 디버깅하는 가장 좋은 방법입니다. 미리 도움을 주셔서 감사합니다 크게 감사드립니다.Python RPI GPIO : SMTP 코드 캡처 HTTP 이미지 저장 및 전송
# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.
import datetime
import time
import smtplib
import urllib2
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN, pull_up_down = gpio.PUD_DOWN)
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
print 'SMTP Doorbell Application loaded @ ' + dtnow
smtpServer = 'smtp.server.com'
smtpPort = '587'
smtpUser = '[email protected]'
smtpPass = '[email protected]ord1'
strFrom = '[email protected]'
strTo = '[email protected]'
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'RPI - Doorbell Alert'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
header = 'From: ' + strFrom + '\n' + 'To: ' + strTo + '\n' + 'Subject: RPI - Doorbell Alert'
while True:
input_value = gpio.input(17)
if input_value == False:
print("\033c")
dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
print 'Doorbell has been pressed @ ' + dtnow
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
msgText = MIMEText('Doorbell Rang @ ' + dtnow)
msgAlternative.attach(msgText)
# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('Doorbell Rang @ ' + dtnow + ' <br><img src="cid:image1">', 'html')
msgAlternative.attach(msgText)
# Get ReoLink Image
request = urllib2.Request(
r'http://192.168.0.11/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=12345',
headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
page = urllib2.urlopen(request)
with open('doorbell.png','wb') as f:
f.write(page.read())
# This example assumes the image is in the current directory
fp = open('doorbell.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
# Send the email (this example assumes SMTP authentication is required)
smtp = smtplib.SMTP(smtpServer, smtpPort)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(smtpUser,smtpPass)
smtp.sendmail(strFrom, strTo.split(','), msgRoot.as_string())
time.sleep(15)
smtp.quit()
print 'SMS sent successful!'
print header + '\n' + 'Message: Doorbell Rang @ ' + dtnow
while input_value == False:
input_value = gpio.input(17)
고정 msgRoot를 포함 while 루프 이동, I는 msgRoot를 포함 while 루프 이동 –