doorlock

git clone https://git.tarina.org/doorlock
Log | Files | Refs

doorlock.py (4165B)


      1 from pad4pi import rpi_gpio
      2 import RPi.GPIO as GPIO
      3 import time
      4 import json
      5 import requests
      6 import socket
      7 import os
      8 import pickle
      9 
     10 #bygdis keypad dörrlås system
     11 
     12 keypass = ''
     13 passcodes = []
     14 rundir = os.path.dirname(__file__)
     15 if rundir != '':
     16     os.chdir(rundir)
     17 folder = os.getcwd()
     18 print(folder)
     19 f = open(folder+"/apikey", "r")
     20 apikey = f.readline().strip()
     21 open_door = False
     22 
     23 GPIO.setmode(GPIO.BCM)
     24 GPIO.setup(16, GPIO.OUT) #Grönled
     25 GPIO.setup(20, GPIO.OUT) #Rödled
     26 GPIO.setup(21, GPIO.OUT) #Piezo
     27 GPIO.setup(26, GPIO.OUT) #Door
     28 GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen
     29 GPIO.output(16,GPIO.LOW)
     30 GPIO.output(20,GPIO.LOW)
     31 GPIO.output(21,GPIO.LOW)
     32 GPIO.output(26,GPIO.LOW)
     33 
     34 def save_passcodes(passcodes):
     35     global folder
     36     with open(folder+'/.doorlock.p', 'wb') as f:
     37         pickle.dump(passcodes, f)
     38         print('save passcodes for offline mode')
     39 
     40 def load_passcodes():
     41     global folder
     42     try:
     43         passcodes = pickle.load(open(folder+'/.doorlock.p', 'rb'))
     44     except:
     45         passcodes = ''
     46     return passcodes
     47 
     48 def check_if_open():
     49     if GPIO.input(14):
     50         #print('Dörrn e ypi')
     51         GPIO.output(26, GPIO.HIGH)
     52         return True
     53     else:
     54         #print('Dörrn e stängd')
     55         GPIO.output(26, GPIO.LOW)
     56         return False
     57 
     58 def is_webz_on():
     59     try:
     60         socket.create_connection(("google.com",80))
     61         return True
     62     except:
     63         pass
     64     print('no internet connection!')
     65     return False
     66 
     67 def getcodes():
     68     global apikey
     69     url = 'https://bygdis.fi/dorrkoderapi?apikey='+apikey
     70     try:
     71         resp = requests.get(url=url)
     72         data = resp.json()
     73         save_passcodes(data)
     74     except:
     75         print('cant access bygdis.fi')
     76         data = load_passcodes()
     77     return data
     78 
     79 def keylogger(code):
     80     global apikey
     81     print('logging key ' + str(code))
     82     url = 'https://bygdis.fi/dorrkoderapi?apikey='+apikey+'&logger=' + str(code)
     83     try:
     84         resp = requests.get(url=url)
     85         print(resp)
     86     except:
     87         print('cant access bygdis.fi')
     88     return
     89 
     90 def doordebug():
     91     global apikey, open_door, keypass
     92     print('sending debug info to bygdis.fi api')
     93     doorstate=check_if_open()
     94     print(doorstate)
     95     debug=str(keypass)
     96     url = 'https://bygdis.fi/dorrkoderapi?apikey='+apikey+'&doorstate='+str(doorstate)+'&debug='+str(debug)
     97     try:
     98         resp = requests.get(url=url)
     99         print(resp)
    100     except:
    101         print('cant send debug info to bygdis.fi')
    102     return
    103 
    104 def keybeep():
    105     GPIO.output(21,GPIO.HIGH)
    106     time.sleep(0.05)
    107     GPIO.output(21,GPIO.LOW)
    108 
    109 def failbeep():
    110     successbeep()
    111     successbeep()
    112 
    113 def successbeep():
    114     GPIO.output(21,GPIO.HIGH)
    115     time.sleep(0.25)
    116     GPIO.output(21,GPIO.LOW)
    117 
    118 def processKey(key):
    119     global keypass
    120     keybeep()
    121     print(key)
    122     keypass += key
    123     if len(keypass) == 4:
    124         check(keypass)
    125         keypass = ''
    126     print(keypass)
    127 
    128 def check(keypass):
    129     global passcodes, open_door
    130     print(passcodes)
    131     if keypass in passcodes:
    132         successbeep()
    133         open_door = True
    134         keylogger(keypass)
    135     else:
    136         failbeep()
    137 
    138 def opendoor():
    139     GPIO.output(26, GPIO.HIGH)
    140     GPIO.output(16,GPIO.HIGH)
    141     GPIO.output(20,GPIO.HIGH)
    142     time.sleep(5)
    143 
    144 def closedoor():
    145     GPIO.output(26, GPIO.LOW)
    146     GPIO.output(16,GPIO.LOW)
    147 
    148 # Setup Keypad
    149 KEYPAD = [
    150         ["*","0","#"],
    151         ["7","8","9"],
    152         ["4","5","6"],
    153         ["1","2","3"]
    154 ]
    155 
    156 ROW_PINS = [11,23,24,25] # BCM numbering
    157 COL_PINS = [17,27,22] # BCM numbering
    158 
    159 factory = rpi_gpio.KeypadFactory()
    160 keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
    161 keypad.registerKeyPressHandler(processKey)
    162 
    163 start_time = time.time()
    164 get_code_time = time.time()
    165 
    166 while 1:
    167     run_time = time.time() - start_time
    168     update = time.time() - get_code_time
    169     time.sleep(0.1)
    170     if update > 15:
    171         get_code_time = time.time()
    172         if is_webz_on():
    173             passcodes = getcodes()
    174             doordebug()
    175         else:
    176             passcodes = load_passcodes()
    177     if open_door == True:
    178         opendoor()
    179         closedoor()
    180         open_door = False
    181     check_if_open()
    182 
    183 keypad.cleanup()