Browse Source

initial sensor logging + AIO code

Brennen Bearnes 4 years ago
commit
e0933744d4
2 changed files with 124 additions and 0 deletions
  1. 64
    0
      mouse.py
  2. 60
    0
      sensor_test.py

+ 64
- 0
mouse.py View File

@@ -0,0 +1,64 @@
1
+#!/usr/bin/env python
2
+
3
+import ConfigParser
4
+import os
5
+import time
6
+
7
+import RPi.GPIO as io
8
+from Adafruit_IO import *
9
+
10
+config = ConfigParser.ConfigParser()
11
+config.read('mousetrap.cfg')
12
+io_key = config.get('io', 'key')
13
+
14
+aio = Client(io_key)
15
+def send_value(field, value):
16
+    print(field, value)
17
+    aio.send(field, value)
18
+
19
+io.setmode(io.BCM)
20
+
21
+pir_pin = 18
22
+door_pin = 23
23
+
24
+# Activate input:
25
+io.setup(pir_pin, io.IN)
26
+
27
+# Activate input with PullUp:
28
+io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)  
29
+
30
+trap_activity = 0
31
+previous_door_state = io.input(door_pin)
32
+motion_detected = False
33
+motion_instances = []
34
+recent_problem_count = 0
35
+
36
+while True:
37
+    previous_trap_value = trap_activity
38
+    previous_recent_problem_count = recent_problem_count
39
+
40
+    motion_detected = False
41
+
42
+    if io.input(pir_pin):
43
+       motion_detected = True
44
+
45
+    if motion_detected:
46
+        motion_instances.append(1)
47
+    else:
48
+        motion_instances.append(0)
49
+
50
+    if len(motion_instances) > 6:
51
+        motion_instances.pop(0)
52
+
53
+    if io.input(door_pin) != previous_door_state:
54
+        previous_door_state = io.input(door_pin)
55
+        trap_activity += 1
56
+
57
+    if (previous_trap_value != trap_activity):
58
+        send_value('Trap Activity', trap_activity)
59
+
60
+    recent_problem_count = sum(motion_instances)
61
+    if previous_recent_problem_count != recent_problem_count:
62
+        send_value('Mouse Problems', sum(motion_instances))
63
+
64
+    time.sleep(6)

+ 60
- 0
sensor_test.py View File

@@ -0,0 +1,60 @@
1
+#!/usr/bin/env python
2
+
3
+import time
4
+
5
+import RPi.GPIO as io
6
+
7
+def send_value(field, value):
8
+    print(field, value)
9
+
10
+io.setmode(io.BCM)
11
+
12
+pir_pin = 18
13
+door_pin = 23
14
+
15
+# Activate input:
16
+io.setup(pir_pin, io.IN)
17
+
18
+# Activate input with PullUp:
19
+io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)  
20
+
21
+trap_closed = False
22
+motion_detected = False
23
+motion_instances = []
24
+recent_problem_count = 0
25
+
26
+while True:
27
+    previous_trap_value = trap_closed
28
+    previous_recent_problem_count = recent_problem_count
29
+
30
+    motion_detected = False
31
+    trap_closed = False
32
+
33
+    if io.input(pir_pin):
34
+       motion_detected = True
35
+       print "MOTION DETECTED!"
36
+
37
+    if motion_detected:
38
+        motion_instances.append(1)
39
+    else:
40
+        motion_instances.append(0)
41
+
42
+    if len(motion_instances) > 6:
43
+        motion_instances.pop(0)
44
+
45
+    if io.input(door_pin):
46
+       trap_closed = False
47
+    else:
48
+       trap_closed = True
49
+
50
+    if (previous_trap_value != trap_closed):
51
+        if trap_closed:
52
+            send_value('Trap State', 'Trap Closed')
53
+        else:
54
+            send_value('Trap State', 'Trap Open')
55
+
56
+    recent_problem_count = sum(motion_instances)
57
+    if previous_recent_problem_count != recent_problem_count:
58
+        send_value('Mouse Problems', sum(motion_instances))
59
+
60
+    time.sleep(0.5)

Loading…
Cancel
Save