-
Notifications
You must be signed in to change notification settings - Fork 0
/
fermentpi.py
132 lines (118 loc) · 3.49 KB
/
fermentpi.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sys
import json
import requests
from w1thermsensor import W1ThermSensor
import os
import socket
import time
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Try sudo <your command>")
test = False
configFileName = "fermentpi.config"
tickIntervalSec = 10
coolerPinNo = 10 #Assuming only one controller cooler TODO - change to array!
hysteresis = 1.0
coolerOn = False
def gpioSetup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(coolerPinNo, GPIO.OUT)
return
def controlCooler(isOn):
GPIO.output(coolerPinNo, isOn)
return
def isCoolerOn():
return GPIO.input(coolerPinNo)
def getControllerName():
return socket.gethostname()
def getSensorsTemp():
print("Getting temp")
if test:
result = [{'Address':"123456", 'CurrentValue':23.5}, {'Address':"123457", 'CurrentValue':24.5}]
else:
result = []
w1 = W1ThermSensor()
for sensor in W1ThermSensor.get_available_sensors():
result.append({"Address": sensor.id, "CurrentValue":sensor.get_temperature()})
print(result)
return result
def readConfig():
print("Reading config")
print(configFileName)
config = {
'ControllerName':getControllerName(),
'ServerURL':'http://162.243.82.40:3000/api/Controllers/report',
'Sensors' : []
}
configFile = False
try:
configFile = open(configFileName, "r")
config = json.load(configFile)
except:
print("cannot open config file")
finally:
if configFile:
configFile.close()
return config
def saveConfig(config):
print("Saving config:")
print(configFileName)
try:
configFile = open(configFileName, "w")
json.dump(config, configFile,sort_keys=True,
indent=4, separators=(',', ': '))
except Exception as e:
print(e)
finally:
configFile.close()
def doReport(config, temp):
print("Reporting status:")
print(config)
print(temp)
try:
config['Sensors'] = temp
data={"controllerStatus":json.dumps(config)}
url = config['ServerURL']
headers = {'accept':'application/json'}
response = requests.post(url, data, headers=headers, timeout=10)
result = response.json()
if result:
config = result['controllerStatus']
print("New config: ")
print(config)
except:
print("Request error")
finally:
return config
def doControl(config, temp):
if 'SetValue' in config['Sensors'][0]:
setValue = config['Sensors'][0]['SetValue']
currValue = temp[0]['CurrentValue']
if isCoolerOn():
if currValue < setValue :
print("curent temp is too low, stop cooling")
controlCooler(False)
else:
if currValue > setValue+hysteresis:
print("current temp is too high, start cooler")
controlCooler(True)
return
def main():
try:
gpioSetup()
config = readConfig()
while True:
temp = getSensorsTemp()
config = doReport(config, temp)
saveConfig(config)
doControl(config, temp)
print("===================== Sleeping ===========================")
time.sleep(tickIntervalSec)
except:
print "error",sys.exc_info()[0]
raise
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()