On October 8th 2020, I tested the above line and it didn’t work with the current version of Raspberry Pi OS on a Raspberry Pi 4. You can try the following:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import smtplib
from datetime import datetime
current_time = datetime.now()
fromMy = 'fromaddress@yahoo.com' # fun-fact: from is a keyword in python, you can't use it as variable, did abyone check if this code even works?
to = 'toaddress@example.com'
subj='Water Heater is Down'
date=current_time.strftime('%m/%d/%Y')
message_text='Water heater is down'
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
import smtplib
username = str('fromaddress@yahoo.com')
password = str('fromaddresspassword')
__author__ = 'Gus (Adapted from Adafruit)'
__license__ = "GPL"
__maintainer__ = "pimylifeup.com"
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
pin_to_circuit = 7
threshold = 400
#time between notifications (in seconds)
time_between_notifications = 7200
last_notification = 0
def rc_time (pin_to_circuit):
count = 0
#Output on the pin for
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_to_circuit, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1
return count
#Catch when script is interupted, cleanup correctly
try:
# Main loop
while True:
if rc_time(pin_to_circuit) < threshold and (time.time() > last_notification + time_between_notifications):
try :
server = smtplib.SMTP("smtp.mail.yahoo.com",587)
server.starttls()
server.login(username,password)
server.sendmail(fromMy, to,msg)
server.quit()
print ('ok the email has sent ')
except :
print ('can\'t send the Email')
print 'test'
last_notification = time.time()
print rc_time(pin_to_circuit)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()