2017-03-01 6 views
0

いいえ、私は以下のコードをラズベリーパイで実行しています。問題は、以下のコードがテスト時に複数回動作しますが、1日かそれ以降に実行される、つまりスクリーン印刷が表示され、クラッシュやエラーは発生しませんが、sendmailは実行されません。私はより多くの.netプログラマですので、Pythonは非常に新しく、明白なバグがあるかどうかを知りたいと思っていました。またはPythonをデバッグする最良の方法です。事前におかげでどんな助けも大歓迎です。固定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]' 

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) 
+0

を含むようにwhileループを移動させ、私はmsgRootを含むようにwhileループを移動 –

答えて

0

、IはmsgRoot

固定
# 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]' 

    strFrom = '[email protected]' 
    strTo = '[email protected]' 

while True: 
     input_value = gpio.input(17) 
     if input_value == False: 


    # 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' 


      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) 
関連する問題