I am trying to code a program (as part of my learning to use GPIO and Python) which will send an email once a GPIO input goes high.
The code below should have waited for a button connected between 3.3v and GPIO 17 to be pushed and then sent the test email. however as soon as the program runs it Prints the text and then sends the email without any hardware being attached and as such no input to pin 17.
The email code works as I receive the mail,
Code: Select all
import smtplib from email.mime.text import MIMEText import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) print "press button to send email" input = GPIO.input(17) if input == True: #create email message = """Test email send via RPi and Python""" msg = MIMEText(message) msg['subject'] = 'Rpi test' msg['from'] = 'my email address>' msg['to'] = 'recipient email' # send mail s = smtplib.SMTP('smtp.o2.co.uk') s.login('username' , 'password') s.sendmail(msg['From'], msg['To'], msg.as_string()) s.quit