-
Notifications
You must be signed in to change notification settings - Fork 0
/
porky.py
66 lines (53 loc) · 1.59 KB
/
porky.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
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import httplib
import os
import logging
logging.basicConfig(
filename='/var/log/porky.log',
level=logging.DEBUG,
format='%(asctime)s.%(msecs)d %(levelname)s %(module)s - %(funcName)s: %(message)s',
datefmt="%Y-%m-%d %H:%M:%S"
)
logging.debug('PorkyPi started')
def porkput(path):
logging.info("Request: PUT " + path)
api_key = os.getenv("API_KEY")
conn = httplib.HTTPConnection("porkchop.club")
headers = {"Content-type": "application/x-www-form-urlencoded"}
conn.request('PUT', path, "table_token="+api_key, headers)
result = conn.getresponse()
logging.info("Response: " + str(result.status) + " " + str(result.reason))
conn.close()
def homeButton(channel):
porkput("/api/table/home_button")
def awayButton(channel):
porkput("/api/table/away_button")
HOME_BUTT = 23
AWAY_BUTT = 24
HOME_LED = 25
AWAY_LED = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(HOME_BUTT, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(AWAY_BUTT, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(HOME_LED, GPIO.OUT, initial = 0)
GPIO.setup(AWAY_LED, GPIO.OUT, initial = 0)
def waitForDown(pin):
while GPIO.input(pin):
time.sleep(0.05)
while True:
if GPIO.input(HOME_BUTT):
GPIO.output(HOME_LED, GPIO.HIGH)
homeButton(HOME_BUTT)
waitForDown(HOME_BUTT)
time.sleep(0.5)
GPIO.output(HOME_LED, GPIO.LOW)
if GPIO.input(AWAY_BUTT):
GPIO.output(AWAY_LED, GPIO.HIGH)
awayButton(AWAY_BUTT)
waitForDown(AWAY_BUTT)
time.sleep(0.5)
GPIO.output(AWAY_LED, GPIO.LOW)
time.sleep(0.05)
GPIO.cleanup()