ugnen.py (4115B)
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 serial 9 import blessed 10 import max6675 11 12 # set the pin for communicate with MAX6675 13 cs = 20 14 sck = 26 15 so = 16 16 # max6675.set_pin(CS, SCK, SO, unit) [unit : 0 - raw, 1 - Celsius, 2 - Fahrenheit] 17 max6675.set_pin(cs, sck, so, 1) 18 19 term = blessed.Terminal() 20 21 #Bra Brööö 22 23 rundir = os.path.dirname(__file__) 24 if rundir != '': 25 os.chdir(rundir) 26 folder = os.getcwd() 27 print(folder) 28 try: 29 f = open(folder+"/apikey", "r") 30 apikey = f.readline().strip() 31 except: 32 apikey = 'none' 33 heating = False 34 35 baseurl = 'https://brabro.fi/owenapi?apikey='+apikey+'&' 36 relaypin = 21 37 start_time = time.time() 38 get_code_time = time.time() 39 new_temp = 'gettin temp...' 40 cooldown = False 41 float_temp = 0.0 42 set_temp = 200.0 43 cooldown_degrees = 8.0 44 cooldown_half = cooldown_degrees/2 45 GPIO.setmode(GPIO.BCM) 46 GPIO.setup(relaypin, GPIO.OUT) #RELAY 47 #GPIO.setup(12, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen 48 GPIO.output(relaypin, GPIO.LOW) 49 50 51 #try: 52 # arduino = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=.1) 53 #except: 54 # arduino = '' 55 56 def read_temp(): 57 try: 58 temp = max6675.read_temp(cs) 59 #temp = arduino.readline().decode('utf-8').rstrip() 60 except: 61 return 0 62 return temp 63 64 def save_temp(set_temp): 65 global folder 66 with open(folder+'/.set_temp.p', 'wb') as f: 67 pickle.dump(set_temp, f) 68 #print('save temperature for offline mode') 69 70 def load_temp(): 71 global folder 72 temp = pickle.load(open(folder+'/.set_temp.p', 'rb')) 73 return float(temp) 74 75 def is_webz_on(): 76 try: 77 socket.create_connection(("google.com",80)) 78 return True 79 except: 80 pass 81 print('no internet connection!') 82 return False 83 84 def get_set_temp(old_temp): 85 global apikey, baseurl 86 url = baseurl + 'status=get_temp' 87 #print(url) 88 try: 89 resp = requests.get(url=url) 90 set_temp = resp.json() 91 except: 92 print('cant access brabro.fi') 93 try: 94 set_temp = float(set_temp) 95 except: 96 pass 97 if old_temp != set_temp: 98 save_temp(set_temp) 99 print('got new temp ' + str(set_temp) + ' from interweb') 100 time.sleep(2) 101 return set_temp 102 else: 103 return 104 105 def send_temp_interweb(temp): 106 global apikey, baseurl 107 url = baseurl + 'temp=' + str(temp) 108 #print('sending debug info to brabro.fi api') 109 try: 110 resp = requests.get(url=url) 111 #print(resp) 112 except: 113 print('cant send debug info to brabro.fi') 114 return 115 116 def failbeep(): 117 successbeep() 118 successbeep() 119 120 def successbeep(): 121 GPIO.output(21,GPIO.HIGH) 122 time.sleep(0.25) 123 GPIO.output(21,GPIO.LOW) 124 125 #load from last session 126 set_temp = load_temp() 127 128 with term.fullscreen(), term.cbreak(), term.hidden_cursor(): 129 while 1: 130 run_time = time.time() - start_time 131 update = time.time() - get_code_time 132 #time.sleep(0.2) 133 if update > 5: 134 get_code_time = time.time() 135 if is_webz_on(): 136 new_set_temp = get_set_temp(set_temp) 137 if new_set_temp: 138 set_temp = new_set_temp 139 send_temp_interweb(new_temp) 140 #doordebug() 141 temp = read_temp() 142 if temp != '': 143 new_temp = str(temp) 144 time.sleep(1) 145 print(term.clear) 146 print(term.bold_yellow('Temperature is now ' + new_temp + ' ℃')) 147 print(term.bold_yellow('Temperature is set for ' + str(set_temp) + ' ℃')) 148 #print(term.red(str(run_time))) 149 if cooldown == True: 150 print(term.green('RELAY IS OFF')) 151 else: 152 print(term.green('RELAY IS ON')) 153 try: 154 float_temp = float(new_temp) 155 except: 156 print('Inititializing...') 157 if float_temp > set_temp + float(cooldown_half): 158 GPIO.output(relaypin, GPIO.HIGH) 159 cooldown = True 160 if float_temp < set_temp - float(cooldown_half) and cooldown == True: 161 GPIO.output(relaypin, GPIO.LOW) 162 cooldown = False