Connecting a light sensor:
https://pimylifeup.com/raspberry-pi-light-sensor/
Sending email in python:
https://stackoverflow.com/questions/16381527/sending-e-mails-using-yahoo-account-in-python
Photoresistors: https://amzn.to/2tSmzfm
1uf electrolytic capactiors: https://amzn.to/2VGYVOG
Solderless prototyping breadboard: https://amzn.to/2Hhxyac
Breadboard jumper wires: https://amzn.to/2XE850k
Raspberry Pi case: https://amzn.to/2Up8hia
Raspberry Pi 3 (Amazon Affiliate)
US: https://amzn.to/2z01nKu
UK: https://amzn.to/3fO896P
CA: https://amzn.to/360IEL4
ES: https://amzn.to/2T3o4nY
FR: https://amzn.to/2WUiKVb
IT: https://amzn.to/3brTtqA
DE: https://amzn.to/361cqPR
IN: https://amzn.to/3eG2LBx
#!/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()
