-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_temp.py
109 lines (99 loc) · 3.46 KB
/
app_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, jsonify, request
import glob
from pyHS100 import SmartPlug
import RPi.GPIO as GPIO
from datetime import datetime
from pytz import timezone
app = Flask(__name__)
gmt = timezone('GMT')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#set board numbering to BCM
GPIO.setmode(GPIO.BCM)
#setup output pins
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
@app.route("/get-temp")
def get_temp():
auth = request.args.get('auth')
if auth == "cabin":
plug_log = open("/d1/cabin_log.txt", "a")
temp = read_temp()
time_now = datetime.now(gmt)
string_time = time_now.strftime("%d/%m/%y %H:%M:%S")
plug_log.write(string_time+" /get-temp temperature is: "+str(temp)+"\n")
plug_log.close()
return jsonify({"temperature": temp, "status": "ok"})
else:
return jsonify({"message": "unauthorised", "status": "error"})
@app.route("/set-temp")
def set_temp():
auth = request.args.get('auth')
if auth == "cabin":
time_now = datetime.now(gmt)
string_time = time_now.strftime("%d/%m/%y %H:%M:%S")
plug_log = open("/d1/cabin_log.txt", "a")
plug = SmartPlug("192.168.1.144")
temp = read_temp()
if temp < 20:
GPIO.output(17,GPIO.HIGH)
GPIO.output(27,GPIO.HIGH)
plug_log.write(string_time+" /set-temp temperature set to reach optimum, current temperature is: "+str(temp)+"\n")
plug_log.close()
plug_status_file = open("/d1/webserver/reaching_optimum.txt", "w")
plug.turn_on()
plug_status_file.write("1")
plug_status_file.close()
return jsonify({"message": "Plug turned on, will turn off when 20C is reached.", "status": "ok"})
else:
plug_log.write(string_time+" /set-temp not activating as temperature is: "+str(temp)+"\n")
plug_log.close()
return jsonify({"message": "Temperature is already at optimum temperature.", "status": "error"})
else:
return jsonify({"message": "unauthorised", "status": "error"})
@app.route("/cancel-temp")
def cancel_temp():
auth = request.args.get('auth')
if auth == "cabin":
time_now = datetime.now(gmt)
string_time = time_now.strftime("%d/%m/%y %H:%M:%S")
plug_log = open("/d1/cabin_log.txt", "a")
plug = SmartPlug("192.168.1.144")
temp = read_temp()
plug_status_file = open("/d1/webserver/reaching_optimum.txt", "r")
cabin_plug_status = plug_status_file.read()
plug_status_file.close()
if cabin_plug_status == "1":
GPIO.output(17,GPIO.LOW)
GPIO.output(27,GPIO.LOW)
plug_log.write(string_time+" /cancel-temp heating cancelled, current temperature is: "+str(temp)+"\n")
plug_log.close()
plug_status_file = open("/d1/webserver/reaching_optimum.txt", "w")
plug.turn_off()
plug_status_file.write("0")
plug_status_file.close()
return jsonify({"message": "Heating cancelled.", "status": "ok"})
else:
plug_log.write(string_time+" /cancel-temp plug not on, temperature is: "+str(temp)+"\n")
plug_log.close()
return jsonify({"message": "Plug is not on so unable to cancel heating.", "status": "error"})
else:
return jsonify({"message": "unauthorised", "status": "error"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)