waterlife

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

commit a7d6150a29d00018646f4298a9b6e628613424e4
Author: rob <rob@tarina.org>
Date:   Wed, 26 Jul 2023 01:55:46 +0300

first

Diffstat:
Ainstall.sh | 45+++++++++++++++++++++++++++++++++++++++++++++
Awater.json | 10++++++++++
Awaterlife.py | 234+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 289 insertions(+), 0 deletions(-)

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=root +Restart=always +RestartSec=3 +StandardInput=tty-force +TTYPath=/dev/tty3 + +[Install] +WantedBy=multi-user.target +EOF +cat <<EOF > /etc/systemd/system/waterlife.service +[Unit] +Description=doorlock +After=multi-user.target + +[Service] +Type=simple +ExecStart=/usr/bin/python3 $installfolder/waterlife.py +User=root +Restart=always +RestartSec=3 +StandardInput=tty-force +TTYPath=/dev/tty1 + +[Install] +WantedBy=multi-user.target +EOF +systemctl enable waterlife +systemctl enable remoteconnect +systemctl daemon-reload + diff --git a/water.json b/water.json @@ -0,0 +1,9 @@ +{ + "bevattning1": 8, + "bevattning2": 20, + "hose1": 10, + "hose2": 20, + "hose3": 5, + "hose4": 10, + "id": 1 +} +\ No newline at end of file diff --git a/waterlife.py b/waterlife.py @@ -0,0 +1,234 @@ +import RPi.GPIO as GPIO +import time +import json +import requests +import socket +import os +import pickle +import datetime + +#bygdis keypad dörrlås system + +keypass = '' +passcodes = [] +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(15, GPIO.OUT) #PUMP +GPIO.setup(22, GPIO.OUT) #HOSE1 +GPIO.setup(27, GPIO.OUT) #HOSE2 +GPIO.setup(17, GPIO.OUT) #HOSE3 +GPIO.setup(26, GPIO.OUT) #HOSE4 +GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen +GPIO.output(15,GPIO.HIGH) +GPIO.output(22,GPIO.HIGH) +GPIO.output(27,GPIO.HIGH) +GPIO.output(17,GPIO.HIGH) +GPIO.output(26,GPIO.HIGH) + +def save_passcodes(passcodes): + global folder + with open('water.json', 'w') as out_file: + json.dump(passcodes, out_file, sort_keys = True, indent = 4, ensure_ascii = False) + print('save passcodes for offline mode') + +def load_passcodes(): + global folder + try: + with open('water.json') as data_file: + passcodes = json.load(data_file) + print('loaded passcodes') + print(passcodes) + except: + passcodes = '' + return passcodes + +def check_if_open(): + if GPIO.input(14): + #print('Dörrn e ypi') + GPIO.output(26, GPIO.HIGH) + return True + else: + #print('Dörrn e stängd') + GPIO.output(26, GPIO.LOW) + return False + +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://water.tarina.org/waterapi?apikey='+apikey + try: + resp = requests.get(url=url) + data = resp.json() + save_passcodes(data) + print(data) + except: + print('cant access water tarina') + data = load_passcodes() + return data + +def keylogger(code): + global apikey + print('logging key ' + str(code)) + url = 'https://water.tarina.org/waterapi?apikey='+apikey+'&logger=' + str(code) + try: + resp = requests.get(url=url) + print(resp) + except: + print('cant access water tarina') + return + +def doordebug(): + global apikey, open_door, keypass + print('sending debug info to water api') + doorstate=check_if_open() + print(doorstate) + debug=str(keypass) + url = 'https://water.tarina.org/waterapi?apikey='+apikey+'&doorstate='+str(doorstate)+'&debug='+str(debug) + try: + resp = requests.get(url=url) + print(resp) + except: + print('cant send debug info to water tarina') + return + +def keybeep(): + GPIO.output(21,GPIO.HIGH) + time.sleep(0.05) + GPIO.output(21,GPIO.LOW) + +def failbeep(): + successbeep() + successbeep() + +def successbeep(): + GPIO.output(21,GPIO.HIGH) + time.sleep(0.25) + GPIO.output(21,GPIO.LOW) + +def processKey(key): + global keypass + keybeep() + print(key) + keypass += key + if len(keypass) == 4: + check(keypass) + keypass = '' + print(keypass) + +def check(keypass): + global passcodes, open_door + print(passcodes) + if keypass in passcodes: + successbeep() + open_door = True + keylogger(keypass) + else: + failbeep() + +def opendoor(): + GPIO.output(26, GPIO.HIGH) + GPIO.output(16,GPIO.HIGH) + GPIO.output(20,GPIO.HIGH) + time.sleep(5) + +def closedoor(): + GPIO.output(26, GPIO.LOW) + GPIO.output(16,GPIO.LOW) + + +start_time = time.time() +get_code_time = time.time() +waterschedule = load_passcodes() +watering=False + +while 1: + run_time = time.time() - start_time + update = time.time() - get_code_time + time.sleep(0.1) + if update > 15: + get_code_time = time.time() + if is_webz_on(): + waterschedule = getcodes() + else: + waterschedule = load_passcodes() + if watering==False: + if datetime.datetime.now().hour==waterschedule['bevattning1']: + watering=True + wateringstart=time.time() + keylogger('watering started') + elif datetime.datetime.now().hour==waterschedule['bevattning2']: + watering=True + wateringstart=time.time() + keylogger('watering started') + if watering == True: + try: + hose1=int(waterschedule['hose1']*60) + except: + hose1=0 + try: + hose2=int(waterschedule['hose2']*60) + except: + hose2=0 + try: + hose3=int(waterschedule['hose3']*60) + except: + hose3=0 + try: + hose4=int(waterschedule['hose4']*60) + except: + hose4=0 + wateringtime = time.time()-wateringstart + print('pump on') + GPIO.output(15,GPIO.LOW) + if wateringtime < hose1: + print('watering with hose1 ' + str(wateringtime)) + GPIO.output(15,GPIO.LOW) + GPIO.output(22,GPIO.LOW) + GPIO.output(27,GPIO.HIGH) + GPIO.output(17,GPIO.HIGH) + GPIO.output(26,GPIO.HIGH) + elif wateringtime > hose1 and wateringtime < hose1+hose2: + print('watering with hose2 ' + str(wateringtime)) + GPIO.output(15,GPIO.LOW) + GPIO.output(22,GPIO.HIGH) + GPIO.output(27,GPIO.LOW) + GPIO.output(17,GPIO.HIGH) + GPIO.output(26,GPIO.HIGH) + elif wateringtime > hose1+hose2 and wateringtime < hose1+hose2+hose3: + print('watering with hose3 ' + str(wateringtime)) + GPIO.output(15,GPIO.LOW) + GPIO.output(22,GPIO.HIGH) + GPIO.output(27,GPIO.HIGH) + GPIO.output(17,GPIO.LOW) + GPIO.output(26,GPIO.HIGH) + elif wateringtime > hose1+hose2+hose3 and wateringtime < hose1+hose2+hose3+hose4: + print('watering with hose4 ' + str(wateringtime)) + GPIO.output(15,GPIO.LOW) + GPIO.output(22,GPIO.HIGH) + GPIO.output(27,GPIO.HIGH) + GPIO.output(17,GPIO.HIGH) + GPIO.output(26,GPIO.LOW) + elif wateringtime > hose1+hose2+hose3+hose4: + GPIO.output(15,GPIO.HIGH) + GPIO.output(22,GPIO.HIGH) + GPIO.output(27,GPIO.HIGH) + GPIO.output(17,GPIO.HIGH) + GPIO.output(26,GPIO.HIGH) + watering=False + keylogger('watering done')