1 | #!/usr/bin/env python
|
2 | # -*- coding: utf-8 -*-
|
3 | import RPi.GPIO as GPIO
|
4 | import smtplib
|
5 | import time
|
6 |
|
7 | BtnPin = 13
|
8 | Gpin = 12
|
9 | Rpin = 11
|
10 | cnt = 0
|
11 | last_call = 0
|
12 |
|
13 |
|
14 | fromaddr = 'xx@googlemail.com'
|
15 | toaddrs = 'xx@googlemail.com'
|
16 | msg1 = """From: Raspberry Pi Alarmsystem
|
17 | To: xx@googlemail.com
|
18 | Subject: Lichtschranke unterbrochen
|
19 |
|
20 | Achtung: Die Lichtschranke wurde unterbrochen, bitte pruefen sie die Ursache!
|
21 | """
|
22 | msg2 = """From: Raspberry Pi Alarmsystem
|
23 | To: xx@googlemail.com
|
24 | Subject: Lichtschranke unterbrochen (dauerhaft)
|
25 |
|
26 | Achtung: Ihre Lichtschranke ist immer noch unterbrochen, bitte pruefen sie die Ursache!
|
27 | """
|
28 | username = 'xx@googlemail.com'
|
29 | password = 'xx'
|
30 |
|
31 | def setup():
|
32 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
|
33 | GPIO.setup(Gpin, GPIO.OUT) # Set Green Led Pin mode to output
|
34 | GPIO.setup(Rpin, GPIO.OUT) # Set Red Led Pin mode to output
|
35 | GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set BtnPin's mode is input, and pull down to low
|
36 | GPIO.add_event_detect(BtnPin, GPIO.BOTH, callback=detect, bouncetime=1000)
|
37 |
|
38 |
|
39 | def Led(x):
|
40 | if x == 1:
|
41 | GPIO.output(Rpin, 1)
|
42 | GPIO.output(Gpin, 0)
|
43 | if x == 0:
|
44 | GPIO.output(Rpin, 0)
|
45 | GPIO.output(Gpin, 1)
|
46 |
|
47 | def SendMail(x):
|
48 | global last_call
|
49 | now = time.time()
|
50 | if now - last_call < 60:
|
51 | return
|
52 | # Sind weniger als 3 Sekunden seit dem letzten Aufruf dieser Funktion vergangen?
|
53 | server = smtplib.SMTP('smtp.gmail.com:587')
|
54 | server.starttls()
|
55 | server.login(username,password)
|
56 | if x == 0:
|
57 | if cnt < 2:
|
58 | server.sendmail(fromaddr, toaddrs, msg1)
|
59 | print("Lichtschranke unterbrochen > Mail gesendet")
|
60 | if x == 1: #
|
61 | server.sendmail(fromaddr, toaddrs, msg2)
|
62 | print("Lichtschranke immer noch unterbrochen > erneut Mail gesendet")
|
63 | server.quit()
|
64 | # Zeitpunkt der letzten Ausführung speichern:
|
65 | last_call = now
|
66 |
|
67 | def WriteFile():
|
68 | date = time.strftime("%x")
|
69 | zeit = time.strftime("%X2")
|
70 | fileout = open("/home/pi/Desktop/Lichtschranke.txt","a")
|
71 | time.sleep(0.1)
|
72 | fileout.write(date+";"+zeit+"; Die Lichtschranke wurde unterbrochen \n")
|
73 | time.sleep(0.1)
|
74 | fileout.close()
|
75 | print("Log-File geschrieben")
|
76 |
|
77 | def detect(chn):
|
78 | time.sleep(0.1)
|
79 | print("in der detect ", GPIO.input(BtnPin))
|
80 | if GPIO.input(BtnPin) == GPIO.LOW: # Lichtschranke wurde unterbrochen
|
81 | WriteFile()
|
82 | Led(1)
|
83 | print("detect")
|
84 | SendMail(0)
|
85 |
|
86 | if GPIO.input(BtnPin) == GPIO.HIGH: # Lichtschranke wieder frei
|
87 | print("wieder frei")
|
88 | Led(0)
|
89 |
|
90 | def cntUnterbrochen(u):
|
91 | global cnt
|
92 | if u == 0:
|
93 | cnt = cnt + 1
|
94 | print("cnt aufaddieren = ", cnt)
|
95 | if cnt == 300: # 600 entspricht 10 min
|
96 | cnt = 0
|
97 | SendMail(1)
|
98 |
|
99 | if u == 1:
|
100 | cnt = 0
|
101 | print("cnt rucksetzen = ", cnt)
|
102 |
|
103 | def loop():
|
104 | while True:
|
105 | time.sleep(1)
|
106 | cntUnterbrochen(GPIO.input(BtnPin))
|
107 | print("main ",GPIO.input(BtnPin))
|
108 |
|
109 |
|
110 | def destroy():
|
111 | GPIO.output(Gpin, GPIO.HIGH) # Green led off
|
112 | GPIO.output(Rpin, GPIO.HIGH) # Red led off
|
113 | GPIO.cleanup() # Release resource
|
114 |
|
115 | if __name__ == '__main__': # Program start from here
|
116 | setup()
|
117 | try:
|
118 | loop()
|
119 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
|
120 | destroy()
|