ugnen

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

commit b4eaec0cfaf00ecb5c50cb393698fa15dc9911b7
Author: rbckman <rob@tarina.org>
Date:   Sat, 27 Aug 2022 07:37:28 +0100

first

Diffstat:
Ainstall.sh | 48++++++++++++++++++++++++++++++++++++++++++++++++
Augnen.py | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 201 insertions(+), 0 deletions(-)

diff --git a/install.sh b/install.sh @@ -0,0 +1,48 @@ +#!/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/ugnen.service +[Unit] +Description=ugnen +After=multi-user.target + +[Service] +Type=simple +ExecStart=/usr/bin/python3 $installfolder/ugnen.py +User=pi +Restart=always +RestartSec=3 +StandardInput=tty-force +TTYPath=/dev/tty1 + +[Install] +WantedBy=multi-user.target +EOF +systemctl enable ugnen +systemctl enable remoteconnect +systemctl daemon-reload + +usermod -a -G dialout pi +usermod -a -G tty pi + diff --git a/ugnen.py b/ugnen.py @@ -0,0 +1,153 @@ +import RPi.GPIO as GPIO +import time +import json +import requests +import socket +import os +import pickle +import serial +import blessed + +term = blessed.Terminal() + +#Bra Brööö + +rundir = os.path.dirname(__file__) +if rundir != '': + os.chdir(rundir) +folder = os.getcwd() +print(folder) +try: + f = open(folder+"/apikey", "r") + apikey = f.readline().strip() +except: + apikey = 'none' +heating = False + +baseurl = 'https://reikaleipa.fi/owenapi?apikey='+apikey+'&' +relaypin = 21 +start_time = time.time() +get_code_time = time.time() +new_temp = 'gettin temp...' +cooldown = False +float_temp = 0.0 +set_temp = 200.0 +cooldown_degrees = 8.0 +cooldown_half = cooldown_degrees/2 +GPIO.setmode(GPIO.BCM) +GPIO.setup(relaypin, GPIO.OUT) #RELAY +#GPIO.setup(12, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) #Öppen knappen +GPIO.output(relaypin, GPIO.LOW) + + +try: + arduino = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=.1) +except: + arduino = '' + +def read_temp(): + try: + temp = arduino.readline().decode('utf-8').rstrip() + except: + return 0 + return temp + +def save_temp(set_temp): + global folder + with open(folder+'/.set_temp.p', 'wb') as f: + pickle.dump(set_temp, f) + #print('save temperature for offline mode') + +def load_temp(): + global folder + temp = pickle.load(open(folder+'/.set_temp.p', 'rb')) + return float(temp) + +def is_webz_on(): + try: + socket.create_connection(("google.com",80)) + return True + except: + pass + print('no internet connection!') + return False + +def get_set_temp(old_temp): + global apikey, baseurl + url = baseurl + 'status=get_temp' + #print(url) + try: + resp = requests.get(url=url) + set_temp = resp.json() + except: + print('cant access reikaleipa.fi') + try: + set_temp = float(set_temp) + except: + pass + if old_temp != set_temp: + save_temp(set_temp) + print('got new temp ' + str(set_temp) + ' from interweb') + time.sleep(2) + return set_temp + else: + return + +def send_temp_interweb(temp): + global apikey, baseurl + url = baseurl + 'temp=' + str(temp) + #print('sending debug info to reikaleipa.fi api') + try: + resp = requests.get(url=url) + #print(resp) + except: + print('cant send debug info to reikaleipa.fi') + return + +def failbeep(): + successbeep() + successbeep() + +def successbeep(): + GPIO.output(21,GPIO.HIGH) + time.sleep(0.25) + GPIO.output(21,GPIO.LOW) + +#load from last session +set_temp = load_temp() + +with term.fullscreen(), term.cbreak(), term.hidden_cursor(): + while 1: + 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(): + new_set_temp = get_set_temp(set_temp) + if new_set_temp: + set_temp = new_set_temp + send_temp_interweb(new_temp) + #doordebug() + temp = read_temp() + if temp != '': + new_temp = str(temp) + time.sleep(1) + print(term.clear) + print(term.bold_yellow('Temperature is now ' + new_temp + ' ℃')) + print(term.bold_yellow('Temperature is set for ' + str(set_temp) + ' ℃')) + #print(term.red(str(run_time))) + if cooldown == True: + print(term.green('RELAY IS OFF')) + else: + print(term.green('RELAY IS ON')) + try: + float_temp = float(new_temp) + except: + print('Inititializing...') + if float_temp > set_temp + float(cooldown_half): + GPIO.output(relaypin, GPIO.HIGH) + cooldown = True + if float_temp < set_temp - float(cooldown_half) and cooldown == True: + GPIO.output(relaypin, GPIO.LOW) + cooldown = False