-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkwikset.py
87 lines (76 loc) · 2.88 KB
/
kwikset.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
# This is a library for providing access to Kwikset Smartcode Locks via UART
# The h/w interface is 3.3V 9600 baud 8N1 standard UART
# This is more of a protocol encoder/decoder though
import kwikset_protocol
import serial
import time
import os
import threading
from binascii import hexlify,unhexlify
ser = None
def setup_arduinobreakout_pins():
# First let's make sure all the pins are exported for config
os.system("echo 214 > /sys/class/gpio/export")
os.system("echo 248 > /sys/class/gpio/export")
os.system("echo 249 > /sys/class/gpio/export")
os.system("echo 216 > /sys/class/gpio/export")
os.system("echo 217 > /sys/class/gpio/export")
# Shouldn't need this as serial port opening should take care of it
#os.system("echo 130 > /sys/class/gpio/export")
#os.system("echo 131 > /sys/class/gpio/export")
# Next lets Tristate all the outputs
os.system("echo low > /sys/class/gpio/gpio214/direction")
# Next lets setup the buffer/level shifter I/O directions
os.system("echo low > /sys/class/gpio/gpio248/direction")
os.system("echo high > /sys/class/gpio/gpio249/direction")
# Disable the external pull ups
os.system("echo low > /sys/class/gpio/gpio216/direction")
os.system("echo low > /sys/class/gpio/gpio217/direction")
# Set Edison I/O directions (shouldn't be necessary, but here for posterity
#os.system("echo in > /sys/class/gpio/gpio130/direction")
#os.system("echo out > /sys/class/gpio/gpio131/direction")
# Remove tristate
os.system("echo high > /sys/class/gpio/gpio214/direction")
def setup_serial():
global ser
# Initialize serial port
ser = serial.Serial("/dev/ttyMFD1",9600,timeout=0)
def init_kwikset_lock():
global ser
if ser == None:
print "Serial Port not setup"
return False
for num in range(8):
ser.write(kwikset_protocol.generate_init_packet(num))
time.sleep(0.3)
def unlock():
global ser
if ser == None:
print "Serial Port not setup"
return False
ser.write(kwikset_protocol.generate_unlock_packet())
def lock():
global ser
if ser == None:
print "Serial Port not setup"
return False
ser.write(kwikset_protocol.generate_lock_packet())
def get_status():
global ser
if ser == None:
print "Serial Port not setup"
return False
limit = 0
MAX_TRIES = 20
header = None
while (limit < MAX_TRIES) and (header != '\xbd'):
header = ser.read()
limit += 1
if (limit == MAX_TRIES):
print "No start byte found in %d characters"%MAX_TRIES
return False
hex_length = hexlify(ser.read())
length = int(hex_length,16)
pkt = "bd%s%s"%(hex_length,hexlify(ser.read(length)))
return kwikset_protocol.parse_packet(pkt)