-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.py
58 lines (44 loc) · 1.3 KB
/
events.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Import the python libraries
import RPi.GPIO as GPIO
import logging
import MySQLdb as mdb
import time
logging.basicConfig(filename='/home/event_error.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# Function called when GPIO.RISING
def storeFunction(channel):
print("Signal detected")
con = mdb.connect('localhost', \
'pi_insert', \
'pi_insert', \
'measurements');
try:
cur = con.cursor()
cur.execute("""INSERT INTO events(event) VALUES(%s)""", ('catflap'))
con.commit()
except mdb.Error, e:
logger.error(e)
finally:
if con:
con.close()
print "Sensor event monitoring (CTRL-C to exit)"
# use the BCM GPIO numbering
GPIO.setmode(GPIO.BCM)
# Definie the BCM-PIN number
GPIO_PIR = 2
# Define pin as input with standard high signaal
GPIO.setup(GPIO_PIR, GPIO.IN, pull_up_down = GPIO.PUD_UP)
try:
# Loop while true = true
while True :
# Wait for the trigger then call the function
GPIO.wait_for_edge(GPIO_PIR, GPIO.RISING)
storeFunction(2)
time.sleep(1)
except KeyboardInterrupt:
# Reset the GPIO settings
GPIO.cleanup()