1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import smtplib
import RPi.GPIO as GPIO
import time
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Serveur SMTP de Google
SMTP_PORT = 587 #Serveur SMTP => Port
GMAIL_USERNAME = 'monAdresseMail@gmail.com' #Username du compte Google
GMAIL_PASSWORD = 'monMotDePasse123' #Mot de passe du compte Google
# La numerotation choisie pour nommer les broches (BCM)
GPIO.setmode(GPIO.BCM)
# Evite l'affage des messages d'erreur inutile
GPIO.setwarnings(False)
# Definition des pins
Bouton = 4
Led1 = 14
Led2 = 15
# Definition des broches pour les entrees.
GPIO.setup(Bouton, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# Definition des broches pour les sorties
GPIO.setup(Led1, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(Led2, GPIO.OUT, initial = GPIO.LOW)
class Emailer:
def sendmail(self, recipient, subject, content):
#Create Headers
headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
"MIME-Version: 1.0", "Content-Type: text/html"]
headers = "\r\n".join(headers)
#Connect to Gmail Server
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
#Login to Gmail
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
#Send Email & Exit
session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
session.quit
sender = Emailer()
while True:
if (GPIO.input(Bouton) == True):
GPIO.output(Led1, GPIO.LOW)
GPIO.output(Led2, GPIO.HIGH)
#sendTo = 'adresseMailDeMonDestinataire@gmail.com'
#emailSubject = "Alarme - MonTexte"
#emailContent = "Alarme déclenché le " + time.ctime()
#sender.sendmail(sendTo, emailSubject, emailContent)
print("Email transmis")
time.sleep(1.0)
else:
GPIO.output(Led1, GPIO.HIGH)
GPIO.output(Led2, GPIO.LOW)
print("En attente")
time.sleep(1.0)
GPIO.cleanup() |
Partager