-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpio-trigger.py
More file actions
executable file
·53 lines (38 loc) · 1.24 KB
/
gpio-trigger.py
File metadata and controls
executable file
·53 lines (38 loc) · 1.24 KB
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import sys
import RPi.GPIO as GPIO
import urllib.request
import subprocess
import threading
import queue
def execute_curl(url: str):
urllib.request.urlopen(url)
def execute_command(command: str):
subprocess.call(command, shell=True, stdout=subprocess.DEVNULL)
def execute_action(trigger_queue: queue.Queue, action_function: callable, action_param: str):
while True:
trigger_queue.get()
action_function(action_param)
ACTION_FUNCTIONS = {
"curl": execute_curl,
"command": execute_command
}
if __name__ == '__main__':
gpio = int(sys.argv[1])
action_type = sys.argv[2]
action = sys.argv[3]
if action_type not in ACTION_FUNCTIONS:
print("No such action type", action_type, file=sys.stderr)
exit(1)
event_queue = queue.Queue()
threading.Thread(target=execute_action, args=(event_queue, ACTION_FUNCTIONS[action_type], action), daemon=True).start()
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#
# bouncetime
# https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
#
while True:
GPIO.wait_for_edge(gpio, GPIO.FALLING, bouncetime=200)
event_queue.put(1, False)