我一直在研究一种自动浇水的植物,我希望它能在水箱空了和水满的时候给我发电子邮件。一切正常,但是,我需要能够设置一个条件,触发电子邮件功能只有一次,当条件满足。相反,只要条件满足,它就会无限地发送电子邮件。在
例如:根据传感器,水箱已满,请发送电子邮件“水箱已满”
根据传感器,水箱是空的,发送电子邮件“水箱空了”
除非水位变化,否则这种情况只会发生一次。在
在不符合其他条件之前不要做任何事。所以只要有水,这个循环就永远不会触发任何条件。在
一旦没有更多的水,这种情况就会触发。当没有水的时候也是一样,然后我再把它装满
代码如下:import RPi.GPIO as GPIO, feedparser
from time import sleep
import smtplib, os, sys
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#Set up GPIO Inputs
# Yellow Input
GPIO.setup(2, GPIO.IN)
def send_email(msg):
USERNAME = "myemail@gmail.com"
PASSWORD = "my_email_password"
MAILTO "recipient email"
msg['From'] = USERNAME
msg['To'] = MAILTO
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME, PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()
print "Email sent to: "+ MAILTO
return
def Send_nowater_email():
print"No water"
msg = MIMEMultipart()
msg.attach(MIMEText('Water tank empty'))
msg['Subject'] = 'Plant notification'
send_email(msg)
return
def Send_watered_email():
msg = MIMEMultipart()
msg.attach(MIMEText('Water tank full'))
msg['Subject'] = 'Plant notification'
send_email(msg)
return
while True:
Input_yellow = GPIO.input(2)
print Input_yellow
if Input_yellow == False:
Send_watered_email()
if Input_yellow == True:
Send_nowater_email()