简体   繁体   English

当按下按钮时,我的python脚本无法完全正常发送电子邮件

[英]My python script doesn't work completely to send emails when a button is pressed

Howdy and good afternoon. 你好,下午好。 I am pretty much a NOOB on with programming. 我在编程方面几乎是NOOB。 I had a friend help me with a python script that when the button is pressed it will turn an led on and send an email message. 我有一个朋友帮我编写了python脚本,当按下按钮时,它将打开LED并发送电子邮件。 When the button is pressed and held for more than 2 seconds and released it is supposed to turn the led of and send a separate email. 按住该按钮2秒钟以上并松开时,应该将其转到原来的位置并发送单独的电子邮件。 The first "normal" email sends everytime even when the button is pressed and held, but the "Important" email doesn't send, however the led does turn off. 即使按下并按住按钮,第一个“正常”电子邮件也会每次发送,但“重要”电子邮件不会发送,但是led会关闭。 I am running Ubuntu Mate on a Raspberry PI 3 Here is part of the script. 我在Raspberry PI 3上运行Ubuntu Mate。这是脚本的一部分。

import RPi.GPIO as GPIO
import time
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import datetime



body = "Email message"
sender = "Room 202 Help"
subject = "Room 202 Help Button"
to = ""
stmpAddress = "smtp.gmail.com"
username = ""
password = ""
normalBody = "Help needed in room 202"
importantBody = "Request Canceled Room 202"
ledPin = 23
buttonPin = 18
importantPress = 2 #seconds


GPIO.setmode(GPIO.BCM)

GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) #makes the pin stay high another words it is set to about 3 volts
GPIO.setup(ledPin, GPIO.OUT) #makes the pin be an output

msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = to
msg['Subject'] = subject
GPIO.output(ledPin,GPIO.LOW)

while True:
    buttonPress = GPIO.input(buttonPin)
    if buttonPress == False: #is the pin low?
        GPIO.output(ledPin,GPIO.HIGH)#turn on the led
        print('Button Pressed')#Yes it is low meaning it was shorted to ground
        buttonPressStart = datetime.datetime.now() #Time the button got pressed
        while GPIO.input(buttonPin) == False: # while it is still low
            time.sleep(0.1)#stay here till they let go of the button
        buttonPressEnd = datetime.datetime.now() # time the button was let go

        diff = buttonPressEnd - buttonPressStart#the differnce of when the button got pressed and let go, duration on button press

        if diff > datetime.timedelta(seconds = importantPress):
            msg.attach(MIMEText(importantBody, 'plain'))
            GPIO.output(ledPin,GPIO.LOW)#turn off the led if it's on
            print("Canceling")
        else:
            msg.attach(MIMEText(normalBody, 'plain'))

        server = smtplib.SMTP(stmpAddress, 587)
        server.starttls()
        server.login(username, password)
        server.sendmail(username, to, msg.as_string())
        server.quit()#email sent



    time.sleep(0.01)#sleep a short time so to not eat all resources

Going off an answer posted on the Raspberry Pi forum , you can see how long a button was pressed as such: 通过在Raspberry Pi论坛上发布的答案,您可以看到按下按钮的时间如下:

while True:
    GPIO.wait_for_edge(PIN, GPIO.FALLING)
    print "Pressed"
    start = time.time()
    time.sleep(0.2)

    while GPIO.input(PIN) == GPIO.LOW:
        time.sleep(0.01)
    length = time.time() - start
    print length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当作为www-data运行时,为什么input.send_keys()在我的Selenium WebDriver Python脚本中不起作用? - Why doesn’t input.send_keys() work in my Selenium WebDriver Python script when run as www-data? 为什么我的Python脚本不起作用? - Why doesn't my Python script work? Python Mailgun 不发送电子邮件 - Python Mailgun doesn't send emails 按下Tkinter按钮时,Python运行脚本 - Python run script when Tkinter button pressed Python 脚本在通过其他脚本启动时不起作用 - Python Script doesn´t work when started via other script 当我的 Python 3.6 脚本将请求模块导入其中时,任务计划程序不起作用。 - Task Scheduler doesn't work when my Python 3.6 script has the requests module imported into it. Tmux,cron 不适用于我在谷歌云上的 python 脚本 - Tmux, cron doesn't work with my python script on google clouds 在 python shell 中运行时,我的 Python 乌龟不起作用 - My Python turtle doesn't work when ran in python shell 在python脚本文件中使用时,type()函数不起作用 - type() function doesn't work when used in a python script file 为什么我的使用 smtplib 发送电子邮件的机器人不起作用? - Why doesn't my bot of sending Emails using smtplib work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM