- 
      
 - 
        
Save corychainsman/5480533 to your computer and use it in GitHub Desktop.  
| #!/usr/bin/python | |
| ######### | |
| # 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 <pathtoyourscript>.py' | |
| # Schematic here: http://fritzing.org/projects/raspberry-pi-water-sensor/ | |
| ########### | |
| # License # | |
| ########### | |
| # Released under the WTFPL. | |
| #Full text and more information here: http://en.wikipedia.org/wiki/WTFPL | |
| ######################################## | |
| # Gmail login credentials to send email# | |
| ######################################## | |
| username = 'exampleusername' #you don't need the "@gmail.com" bit. | |
| password = 'examplepassword' | |
| ############################ | |
| # General Email Parameters # | |
| ############################ | |
| From = "[email protected]" | |
| To = "recipientaddress#domain.com" | |
| ####################################### | |
| # Email Parameters when sensor is Wet # | |
| ####################################### | |
| Subject_wet = "RPi Water Sensor is WET" | |
| Body_wet = "Your water sensor is wet." | |
| ####################################### | |
| # Email Parameters when semsor is Dry # | |
| ####################################### | |
| Subject_dry = "RPi Water Sensor is DRY" | |
| Body_dry = " Your water sensor is dry again!" | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| import RPi.GPIO as GPIO | |
| import string | |
| import time | |
| # Function Definitions | |
| #takes either "wet" or "dry" as the condition. | |
| def email(condition): | |
| print "Attempting to send email" | |
| if condition == 'wet': | |
| Body = string.join(( | |
| "From: %s" % From, | |
| "To: %s" % To, | |
| "Subject: %s" % Subject_wet, | |
| "", | |
| Body_wet, | |
| ), "\r\n") | |
| if condition == 'dry': | |
| Body = string.join(( | |
| "From: %s" % From, | |
| "To: %s" % To, | |
| "Subject: %s" % Subject_dry, | |
| "", | |
| Body_dry, | |
| ), "\r\n") | |
| # The actual mail send | |
| server = smtplib.SMTP('smtp.gmail.com:587') | |
| server.starttls() | |
| print "Logging in..." | |
| server.login(username,password) | |
| print "Logged in as "+username+"." | |
| server.sendmail(From, [To], Body) | |
| server.quit() | |
| print "Email sent." | |
| #Tests whether wter 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) | |
| time.sleep(0.1) | |
| GPIO.setup(RCpin, GPIO.IN) | |
| # 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 | |
| # Turns on the piezo buzzer | |
| # tested to work on pin 17 | |
| def buzz_on (pin): | |
| GPIO.setmode(GPIO.BCM) | |
| GPIO.setup(pin, GPIO.OUT) | |
| GPIO.output(pin, GPIO.HIGH) | |
| # Turns off the piezo buzzer | |
| # tested to work on pin 17 | |
| def buzz_off(pin): | |
| GPIO.setmode(GPIO.BCM) | |
| GPIO.setup(pin, GPIO.OUT) | |
| GPIO.output(pin, GPIO.LOW) | |
| # Main Loop | |
| print 'Waiting for wetness...' | |
| while True: | |
| time.sleep(1) # check for wetness every second | |
| if RCtime(18) == 1: | |
| buzz_on(17) | |
| print "Sensor is wet" | |
| email('wet') | |
| print "Waiting for dryness..." | |
| while True: | |
| time.sleep(1) # check for dryness every second | |
| if RCtime(18) == 0: | |
| buzz_off(17) | |
| print "Sensor is dry again" | |
| email('dry') | |
| print "Waiting for wetness..." | |
| break | 
@speedy-10
I haven't tested this, but I think you need to change smtp.gmail.com:587 on line 78 to your email provider's smtp server and port.
I found on the web that t-online.de uses securesmtp.t-online.de and port 465 for its smtp configuration, so that whole line should be server = smtplib.SMTP('securesmtp.t-online.de:465')
for web.de I think it should be server = smtplib.SMTP('smtp.web.de:587')
Depending on the email provider's smtp server configuration, you may have issues with 2-factor authentication or other security settings, but trying the above would be a good starting point.
Hi, I've tried both, without success. Now I found a nice video, in which the following code ist presented:
# Datum: 22.10.2020
import smtplib
# Benutzerdaten z.B. bei web.de
user = '[email protected]'
pwd = 'password'
mail_text = 'Hallo,\n\das ist Text!\n\n'
subject = 'Test-Alarm'
MAIL_FROM = '[email protected]'
RCPT_TO = '[email protected]'
DATA = 'From:%s\nTo:%s\nSubject:%s\n\n%s' % (MAIL_FROM, RCPT_TO, subject, mail_text)
server = smtplib.SMTP('smtp.web.de:587')
server.starttls()
server.login(user, pwd)
server.sendmail(MAIL_FROM, RCPT_TO, DATA)
server.quit()
But even here (with the correct USER and PASSWORD) I got an error:
File "/home/USER/Downloads/melder.py", line 23, in <module>
    server.starttls(context=context)
  File "/home/USER/apps/thonny/lib/python3.7/smtplib.py", line 771, in starttls
    server_hostname=self._host)
  File "/home/USER/apps/thonny/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/home/USER/apps/thonny/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/home/USER/apps/thonny/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_ILLEGAL_PARAMETER] sslv3 alert illegal parameter (_ssl.c:1076)
(With an e-mail adress from t-online.de, I got the same error message.)
I didn't find a solution in the internet so far. Does anyone know, how to use this properly?
Regards
It worked with gmail, but you had to change something: allow unsecure apps.
Hi, I tried this programm but I'd prefer to use an e-mail Adress from t-online.de or web.de.
But after the line:
I got an error message 533 Authentification failed.
Does someone have experience with such e-mails?
Regards