#!/usr/bin/python3 # erweitert von M. Sprau # 31.10.2020 # Nach einem Neustart wird zuerst eine mail gesendet. import smtplib from time import sleep # warten nach Neustart, sonst kann die mail noch nicht abgesendet werden, weil die Dienste noch nicht verfügbar sind. sleep(15) # Benutzerdaten z.B. bei gmail.com user = 'BENUTZER' # @gmail.com kann hier weggelassen werden pwd = 'PASSWORT' subject = 'Neustart des Rapis' mail_text =''' Hallo, der Raspi ist gerade neu gestartet. Wassersensor ist nun aktiv. ''' MAIL_FROM = 'ABSENDER@ADRESSE' RCPT_TO = 'ZIEL@ADRESSE' DATA = 'From:%s\nTo:%s\nSubject:%s\n\n%s' % (MAIL_FROM, RCPT_TO, subject, mail_text) server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() #print ("Logging in ...") server.login(user, pwd) #print ("Begin sending ...") server.sendmail(MAIL_FROM, RCPT_TO, DATA) #print ("Logging out ...") server.quit() ####################################### # Eigentliches Wasser-Sensor-Programm # ####################################### ######### # About # ######### # This script uses a Raspberry Pi to sense for the presense or absense of water. # If there is water, an email is sent and a buzzer goes off. # When it's dry again, another email is sent, and the buzzer turns off. # To run this script at boot, edit /etc/rc.local to include (no quotes) 'sudo python .py' # bzw.: crontab -e anpassen ########### # License # ########### # Released under the WTFPL. #Full text and more information here: http://en.wikipedia.org/wiki/WTFPL ######################################## # Gmail login credentials to send email# ######################################## user = 'BENUTZER' # @gmail.com kann hier weggelassen werden pwd = 'PASSWORT' ############################ # General Email Parameters # ############################ MAIL_FROM = "ABSENDER@gmail.com" RCPT_TO = ['ZIEL@ADRESSE'] # max. nur 1 Adresse erlaubt, sonst Fehler im gmail-Konto! ####################################### # Email Parameters when sensor is Wet # ####################################### subject_wet = "Raspi Wasser-Sensor ist nass!" body_wet = "Achtung,\n der Raspi hat einen Wasseraustritt im Keller gemeldet!\n Bitte kontrollieren!\n" ####################################### # Email Parameters when semsor is Dry # ####################################### subject_dry = "Raspi Wasser-Sensor ist trocken!" body_dry = "Entwarnung, der Sensor ist trocken." import RPi.GPIO as GPIO # Function Definitions #takes either "wet" or "dry" as the condition. def email(condition): if condition == 'wet': DATA = 'From:%s\nTo:%s\nSubject:%s\n\n%s' % (MAIL_FROM, RCPT_TO, subject_wet, body_wet) if condition == 'dry': DATA = 'From:%s\nTo:%s\nSubject:%s\n\n%s' % (MAIL_FROM, RCPT_TO, subject_dry, body_dry) # The actual mail send server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(user, pwd) server.sendmail(MAIL_FROM, RCPT_TO, DATA) server.quit() # Tests whether water is present. # returns 0 for dry # returns 1 for wet # tested to work on pin 18 def RCtime (RCpin): reading = 0 GPIO.setmode(GPIO.BCM) GPIO.setup(RCpin, GPIO.OUT) GPIO.output(RCpin, GPIO.LOW) sleep(1) GPIO.setup(RCpin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # This takes about 1 millisecond per loop cycle while True: if (GPIO.input(RCpin) == GPIO.LOW): reading += 1 if reading >= 1000: return 0 if (GPIO.input(RCpin) != GPIO.LOW): return 1 # Main Loop sleep(25) # damit mail aus melder_neustart.py fertig gesendet wurde. print('Waiting for wetness...') while True: sleep(10) if RCtime(18) == 1: print("Sensor is wet") email('wet') while True: sleep(30) if RCtime(18) == 1: print("Sensor is still wet...") email('wet') continue if RCtime(18) == 0: print("Sensor is dry again") email('dry') print("Waiting for wetness...") break