doorlock

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit e58f868f21172ae2a7ea6855804493ec6c5659c2
parent 554060ea155b5fa58be43672d551ed6f465748b4
Author: rbckman <rob@tarina.org>
Date:   Mon, 26 Apr 2021 11:42:24 +0300

yes

Diffstat:
Mdoorlock.py | 101++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Ainstall.sh | 45+++++++++++++++++++++++++++++++++++++++++++++
Aremoteconnect.sh | 3+++
3 files changed, 136 insertions(+), 13 deletions(-)

diff --git a/doorlock.py b/doorlock.py @@ -1,22 +1,77 @@ from pad4pi import rpi_gpio import RPi.GPIO as GPIO import time +import json +import requests +import socket +import os +import pickle #bygdis keypad dörrlås system keypass = '' passcodes = ['2222', '0550'] +rundir = os.path.dirname(__file__) +if rundir != '': + os.chdir(rundir) +folder = os.getcwd() +print(folder) +f = open(folder+"/apikey", "r") +apikey = f.readline().strip() +open_door = False GPIO.setmode(GPIO.BCM) GPIO.setup(16, GPIO.OUT) #Grönled GPIO.setup(20, GPIO.OUT) #Rödled GPIO.setup(21, GPIO.OUT) #Piezo GPIO.setup(26, GPIO.OUT) #Door +GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen GPIO.output(16,GPIO.LOW) GPIO.output(20,GPIO.LOW) GPIO.output(21,GPIO.LOW) GPIO.output(26,GPIO.LOW) +def save_passcodes(passcodes): + global folder + with open(folder+'/.doorlock.p', 'wb') as f: + pickle.dump(passcodes, f) + print('save passcodes for offline mode') + +def load_passcodes(): + global folder + passcodes = pickle.load(open(folder+'/.doorlock.p', 'rb')) + +def check_if_open(): + if GPIO.input(14): + #print('open door') + GPIO.output(26, GPIO.HIGH) + else: + GPIO.output(26, GPIO.LOW) + +def is_webz_on(): + try: + socket.create_connection(("google.com",80)) + return True + except: + pass + print('no internet connection!') + return False + +def getcodes(): + global apikey + url = 'https://bygdis.fi/dorrkoderapi?apikey='+apikey + resp = requests.get(url=url) + data = resp.json() + return data + +def keylogger(code): + global apikey + print('logging key ' + str(code)) + url = 'https://bygdis.fi/dorrkoderapi?apikey='+apikey+'&logger=' + str(code) + resp = requests.get(url=url) + print(resp) + return + def keybeep(): GPIO.output(21,GPIO.HIGH) time.sleep(0.05) @@ -48,18 +103,25 @@ def processKey(key): print(keypass) def check(keypass): - global passcodes + global passcodes, open_door + print(passcodes) if keypass in passcodes: successbeep() - GPIO.output(26, GPIO.HIGH) - GPIO.output(16,GPIO.HIGH) - GPIO.output(20,GPIO.HIGH) - time.sleep(5) - GPIO.output(26, GPIO.LOW) - GPIO.output(16,GPIO.LOW) + open_door = True else: failbeep() +def opendoor(): + GPIO.output(26, GPIO.HIGH) + GPIO.output(16,GPIO.HIGH) + GPIO.output(20,GPIO.HIGH) + keylogger(keypass) + time.sleep(5) + +def closedoor(): + GPIO.output(26, GPIO.LOW) + GPIO.output(16,GPIO.LOW) + # Setup Keypad KEYPAD = [ ["*","0","#"], @@ -72,14 +134,27 @@ ROW_PINS = [11,23,24,25] # BCM numbering COL_PINS = [17,27,22] # BCM numbering factory = rpi_gpio.KeypadFactory() - keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS) - keypad.registerKeyPressHandler(processKey) + +start_time = time.time() +get_code_time = time.time() + while 1: - time.sleep(0.5) - #GPIO.output(16,GPIO.LOW) - #GPIO.output(20,GPIO.LOW) - #GPIO.output(21,GPIO.LOW) + run_time = time.time() - start_time + update = time.time() - get_code_time + time.sleep(0.2) + if update > 5: + get_code_time = time.time() + if is_webz_on(): + passcodes = getcodes() + save_passcodes(passcodes) + else: + passcodes = load_passcodes() + if open_door == True: + opendoor() + closedoor() + open_door = False + check_if_open() keypad.cleanup() diff --git a/install.sh b/install.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +echo "run with sudo ./install.sh" + +installfolder=" $(pwd)" +echo "$installfolder" + +cat <<EOF > /etc/systemd/system/remoteconnect.service +[Unit] +Description=remoteconnect +After=multi-user.target + +[Service] +Type=simple +ExecStart=$installfolder/remoteconnect.sh +User=pi +Restart=always +RestartSec=3 +StandardInput=tty-force +TTYPath=/dev/tty3 + +[Install] +WantedBy=multi-user.target +EOF +cat <<EOF > /etc/systemd/system/doorlock.service +[Unit] +Description=doorlock +After=multi-user.target + +[Service] +Type=simple +ExecStart=/usr/bin/python3 $installfolder/doorlock.py +User=pi +Restart=always +RestartSec=3 +StandardInput=tty-force +TTYPath=/dev/tty1 + +[Install] +WantedBy=multi-user.target +EOF +systemctl enable doorlock +systemctl enable remoteconnect +systemctl daemon-reload + diff --git a/remoteconnect.sh b/remoteconnect.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ssh -N -R 18888:localhost:22 tarina@radiorymd.com -p 13337