waterlife.py (7210B)
1 import RPi.GPIO as GPIO 2 import time 3 import json 4 import requests 5 import socket 6 import os 7 import pickle 8 import datetime 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(15, GPIO.OUT) #PUMP 25 GPIO.setup(22, GPIO.OUT) #HOSE1 26 GPIO.setup(27, GPIO.OUT) #HOSE2 27 GPIO.setup(17, GPIO.OUT) #HOSE3 28 GPIO.setup(26, GPIO.OUT) #HOSE4 29 GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen 30 GPIO.output(15,GPIO.HIGH) 31 GPIO.output(22,GPIO.HIGH) 32 GPIO.output(27,GPIO.HIGH) 33 GPIO.output(17,GPIO.HIGH) 34 GPIO.output(26,GPIO.HIGH) 35 36 oldcode = '' 37 38 def save_passcodes(passcodes): 39 global folder 40 with open('water.json', 'w') as out_file: 41 json.dump(passcodes, out_file, sort_keys = True, indent = 4, ensure_ascii = False) 42 print('save passcodes for offline mode') 43 44 def load_passcodes(): 45 global folder 46 try: 47 with open('water.json') as data_file: 48 passcodes = json.load(data_file) 49 print('loaded passcodes') 50 print(passcodes) 51 except: 52 passcodes = '' 53 return passcodes 54 55 def check_if_open(): 56 if GPIO.input(14): 57 #print('Dörrn e ypi') 58 GPIO.output(26, GPIO.HIGH) 59 return True 60 else: 61 #print('Dörrn e stängd') 62 GPIO.output(26, GPIO.LOW) 63 return False 64 65 def is_webz_on(): 66 try: 67 socket.create_connection(("google.com",80)) 68 return True 69 except: 70 pass 71 print('no internet connection!') 72 return False 73 74 def getcodes(): 75 global apikey 76 url = 'https://water.tarina.org/waterapi?apikey='+apikey 77 try: 78 resp = requests.get(url=url) 79 data = resp.json() 80 save_passcodes(data) 81 print(data) 82 except: 83 print('cant access water tarina') 84 data = load_passcodes() 85 return data 86 87 def keylogger(code): 88 global apikey, oldcode 89 if oldcode != code: 90 oldcode = code 91 print('logging key ' + str(code)) 92 url = 'https://water.tarina.org/waterapi?apikey='+apikey+'&logger=' + str(code) 93 try: 94 resp = requests.get(url=url) 95 print(resp) 96 except: 97 print('cant access water tarina') 98 99 def doordebug(): 100 global apikey, open_door, keypass 101 print('sending debug info to water api') 102 doorstate=check_if_open() 103 print(doorstate) 104 debug=str(keypass) 105 url = 'https://water.tarina.org/waterapi?apikey='+apikey+'&doorstate='+str(doorstate)+'&debug='+str(debug) 106 try: 107 resp = requests.get(url=url) 108 print(resp) 109 except: 110 print('cant send debug info to water tarina') 111 return 112 113 def keybeep(): 114 GPIO.output(21,GPIO.HIGH) 115 time.sleep(0.05) 116 GPIO.output(21,GPIO.LOW) 117 118 def failbeep(): 119 successbeep() 120 successbeep() 121 122 def successbeep(): 123 GPIO.output(21,GPIO.HIGH) 124 time.sleep(0.25) 125 GPIO.output(21,GPIO.LOW) 126 127 def processKey(key): 128 global keypass 129 keybeep() 130 print(key) 131 keypass += key 132 if len(keypass) == 4: 133 check(keypass) 134 keypass = '' 135 print(keypass) 136 137 def check(keypass): 138 global passcodes, open_door 139 print(passcodes) 140 if keypass in passcodes: 141 successbeep() 142 open_door = True 143 keylogger(keypass) 144 else: 145 failbeep() 146 147 def opendoor(): 148 GPIO.output(26, GPIO.HIGH) 149 GPIO.output(16,GPIO.HIGH) 150 GPIO.output(20,GPIO.HIGH) 151 time.sleep(5) 152 153 def closedoor(): 154 GPIO.output(26, GPIO.LOW) 155 GPIO.output(16,GPIO.LOW) 156 157 158 start_time = time.time() 159 get_code_time = time.time() 160 waterschedule = load_passcodes() 161 watering=False 162 status='system running' 163 oldstatus='' 164 if is_webz_on(): 165 keylogger('system up running') 166 else: 167 print('offline') 168 169 while 1: 170 run_time = time.time() - start_time 171 update = time.time() - get_code_time 172 time.sleep(0.1) 173 if update > 10: 174 get_code_time = time.time() 175 if is_webz_on(): 176 waterschedule = getcodes() 177 if status != oldstatus: 178 keylogger(status) 179 oldstatus = status 180 else: 181 waterschedule = load_passcodes() 182 if watering==False and waterschedule['system']=='on': 183 if datetime.datetime.now().hour==waterschedule['bevattning1'] and datetime.datetime.now().minute==55: 184 watering=True 185 wateringstart=time.time() 186 keylogger('watering started') 187 elif datetime.datetime.now().hour==waterschedule['bevattning2'] and datetime.datetime.now().minute==55: 188 watering=True 189 wateringstart=time.time() 190 keylogger('watering started') 191 if watering == True and waterschedule['system']=='on': 192 try: 193 hose1=int(waterschedule['hose1']*60) 194 except: 195 hose1=0 196 try: 197 hose2=int(waterschedule['hose2']*60) 198 except: 199 hose2=0 200 try: 201 hose3=int(waterschedule['hose3']*60) 202 except: 203 hose3=0 204 try: 205 hose4=int(waterschedule['hose4']*60) 206 except: 207 hose4=0 208 wateringtime = time.time()-wateringstart 209 GPIO.output(15,GPIO.LOW) 210 if wateringtime < hose1: 211 status='watering with hose1 '+str(int(hose1-wateringtime))+' seconds left' 212 GPIO.output(15,GPIO.LOW) 213 GPIO.output(22,GPIO.LOW) 214 GPIO.output(27,GPIO.HIGH) 215 GPIO.output(17,GPIO.HIGH) 216 GPIO.output(26,GPIO.HIGH) 217 elif wateringtime > hose1 and wateringtime < hose1+hose2: 218 status='watering with hose2 '+str(int((hose1+hose2)-wateringtime))+' seconds left' 219 GPIO.output(15,GPIO.LOW) 220 GPIO.output(22,GPIO.HIGH) 221 GPIO.output(27,GPIO.LOW) 222 GPIO.output(17,GPIO.HIGH) 223 GPIO.output(26,GPIO.HIGH) 224 elif wateringtime > hose1+hose2 and wateringtime < hose1+hose2+hose3: 225 status='watering with hose3 '+str(int((hose1+hose2+hose3)-wateringtime))+' seconds left' 226 GPIO.output(15,GPIO.LOW) 227 GPIO.output(22,GPIO.HIGH) 228 GPIO.output(27,GPIO.HIGH) 229 GPIO.output(17,GPIO.LOW) 230 GPIO.output(26,GPIO.HIGH) 231 elif wateringtime > hose1+hose2+hose3 and wateringtime < hose1+hose2+hose3+hose4: 232 status='watering with hose3 '+str(int((hose1+hose2+hose3+hose4)-wateringtime))+' seconds left' 233 GPIO.output(15,GPIO.LOW) 234 GPIO.output(22,GPIO.HIGH) 235 GPIO.output(27,GPIO.HIGH) 236 GPIO.output(17,GPIO.HIGH) 237 GPIO.output(26,GPIO.LOW) 238 elif wateringtime > hose1+hose2+hose3+hose4: 239 GPIO.output(15,GPIO.HIGH) 240 GPIO.output(22,GPIO.HIGH) 241 GPIO.output(27,GPIO.HIGH) 242 GPIO.output(17,GPIO.HIGH) 243 GPIO.output(26,GPIO.HIGH) 244 watering=False 245 keylogger('watering done') 246 else: 247 keylogger('system off') 248 GPIO.output(15,GPIO.HIGH) 249 GPIO.output(22,GPIO.HIGH) 250 GPIO.output(27,GPIO.HIGH) 251 GPIO.output(17,GPIO.HIGH) 252 GPIO.output(26,GPIO.HIGH) 253 watering=False 254