Various tweaks
This commit is contained in:
parent
df4d3b130d
commit
ad2b15d49e
|
@ -1,3 +1,4 @@
|
||||||
shroom_server
|
shroom_server
|
||||||
shrooms.db
|
shrooms.db
|
||||||
auth_secret
|
auth_secret
|
||||||
|
__pycache__/
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
|
import numpy as np
|
||||||
|
import time
|
||||||
|
|
||||||
|
from utils import send_update
|
||||||
|
|
||||||
|
from humidifier import Humidifier, HumidifierV2
|
||||||
|
from humidifier_v3 import HumidifierV3
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
def __init__(self, humidifiers):
|
def __init__(self, humidifiers):
|
||||||
self.target_lower = 85
|
self.target_lower = 83
|
||||||
self.target_upper = 90
|
self.target_upper = 87
|
||||||
self.feedforward_coeff = 50
|
self.feedforward_coeff = 50
|
||||||
self.last_toggle = 0
|
self.last_toggle = 0
|
||||||
|
|
||||||
|
@ -25,13 +33,14 @@ class Controller:
|
||||||
|
|
||||||
def set_checked(self, s, humidifier, on):
|
def set_checked(self, s, humidifier, on):
|
||||||
if time.time() - self.last_toggle > 0.8:
|
if time.time() - self.last_toggle > 0.8:
|
||||||
if humidifier is HumidifierV3:
|
print("setting {} to {}".format(humidifier, on))
|
||||||
|
if isinstance(humidifier, HumidifierV3):
|
||||||
humidifier.set(s, on)
|
humidifier.set(s, on)
|
||||||
else:
|
elif isinstance(humidifier, Humidifier) or isinstance(humidifier, HumidifierV2):
|
||||||
humidifier.toggle(s)
|
humidifier.toggle(s)
|
||||||
self.last_toggle = time.time()
|
self.last_toggle = time.time()
|
||||||
|
|
||||||
def update(self, humidity):
|
def update(self, s, humidity):
|
||||||
if self.first_sample:
|
if self.first_sample:
|
||||||
self.humidifier_history[:] = humidity
|
self.humidifier_history[:] = humidity
|
||||||
self.first_sample = False
|
self.first_sample = False
|
||||||
|
@ -49,16 +58,16 @@ class Controller:
|
||||||
|
|
||||||
if self.manual_mode:
|
if self.manual_mode:
|
||||||
for humidifier in self.humidifiers:
|
for humidifier in self.humidifiers:
|
||||||
if humidifier.off and self.manual_on:
|
if not humidifier.on and self.manual_on:
|
||||||
self.set_checked(s, humidifier, True)
|
self.set_checked(s, humidifier, True)
|
||||||
elif humidifier.on and not self.manual_on:
|
elif not humidifier.off and not self.manual_on:
|
||||||
self.set_checked(s, humidifier, False)
|
self.set_checked(s, humidifier, False)
|
||||||
else:
|
else:
|
||||||
if comp_humidity < self.target_lower:
|
if comp_humidity < self.target_lower:
|
||||||
for humidifier in self.humidifiers:
|
for humidifier in self.humidifiers:
|
||||||
if humidifier.off:
|
if not humidifier.on:
|
||||||
self.set_checked(s, humidifier, True)
|
self.set_checked(s, humidifier, True)
|
||||||
elif comp_humidity > self.target_upper:
|
elif comp_humidity > self.target_upper:
|
||||||
for humidifier in self.humidifiers:
|
for humidifier in self.humidifiers:
|
||||||
if humidifier.on:
|
if not humidifier.off:
|
||||||
self.set_checked(s, humidifier, False)
|
self.set_checked(s, humidifier, False)
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
import numpy as np
|
||||||
|
import time
|
||||||
|
|
||||||
|
from utils import send_update
|
||||||
|
|
||||||
class Humidifier:
|
class Humidifier:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.off_threshold = 0.4
|
self.off_threshold = 0.4
|
||||||
|
|
|
@ -53,33 +53,14 @@ void setup()
|
||||||
//sht20.checkSHT20(); // Check SHT20 Sensor
|
//sht20.checkSHT20(); // Check SHT20 Sensor
|
||||||
}
|
}
|
||||||
|
|
||||||
// relay 4
|
const int PIN_RELAY4 = 4;
|
||||||
void toggle() {
|
const int PIN_RELAY3 = 5;
|
||||||
digitalWrite(4, HIGH);
|
|
||||||
delay(100);
|
|
||||||
digitalWrite(4, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
// relay 3
|
|
||||||
void toggle2() {
|
|
||||||
digitalWrite(5, HIGH);
|
|
||||||
delay(100);
|
|
||||||
digitalWrite(5, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void toggle_test(int p) {
|
|
||||||
digitalWrite(p, HIGH);
|
|
||||||
delay(100);
|
|
||||||
digitalWrite(p, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
while (Serial.available()) {
|
while (Serial.available()) {
|
||||||
const int c = Serial.read();
|
const int c = Serial.read();
|
||||||
if (c == 's') {
|
if (c == 's') {
|
||||||
// TODO dump the current status
|
|
||||||
#ifdef USE_SHT30
|
#ifdef USE_SHT30
|
||||||
sht30.read();
|
sht30.read();
|
||||||
float humd = sht30.getHumidity();
|
float humd = sht30.getHumidity();
|
||||||
|
@ -88,28 +69,17 @@ void loop()
|
||||||
float humd = sht20.readHumidity(); // Read Humidity
|
float humd = sht20.readHumidity(); // Read Humidity
|
||||||
float temp = sht20.readTemperature(); // Read Temperature
|
float temp = sht20.readTemperature(); // Read Temperature
|
||||||
#endif
|
#endif
|
||||||
const int NUM_AVG = 64;
|
|
||||||
float volts = 0.0f;
|
|
||||||
float volts2 = 0.0f;
|
|
||||||
for (int i = 0; i < NUM_AVG; i++) {
|
|
||||||
volts += (5.0f/1024.0f)*analogRead(A2);
|
|
||||||
volts2 += (5.0f/1024.0f)*analogRead(A0);
|
|
||||||
}
|
|
||||||
volts *= 1.0f/NUM_AVG;
|
|
||||||
volts2 *= 1.0f/NUM_AVG;
|
|
||||||
Serial.print(humd);
|
Serial.print(humd);
|
||||||
Serial.print(",");
|
Serial.print(",");
|
||||||
Serial.print(temp);
|
Serial.print(temp);
|
||||||
Serial.print(",");
|
//Serial.print(",");
|
||||||
Serial.print(volts);
|
//Serial.print(volts);
|
||||||
Serial.print(",");
|
//Serial.print(",");
|
||||||
Serial.println(volts2);
|
//Serial.println(volts2);
|
||||||
} else if (c == 'h') {
|
Serial.println("");
|
||||||
toggle();
|
} else if (c == 'z' || c == 'Z') {
|
||||||
} else if (c == 'i') {
|
if (c == 'z') digitalWrite(PIN_RELAY4, LOW);
|
||||||
toggle2();
|
if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH);
|
||||||
} else if (c >= 'A' && c <= 'Z') {
|
|
||||||
toggle_test(c - 'A');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delay(1);
|
delay(1);
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from utils import send_update
|
||||||
|
|
||||||
# this is driving a SSR that's powering a AC-powered humidifier
|
# this is driving a SSR that's powering a AC-powered humidifier
|
||||||
# we don't need voltage readings for this one
|
# we don't need voltage readings for this one
|
||||||
class HumidifierV3:
|
class HumidifierV3:
|
||||||
def __init__(self):
|
def __init__(self, humidifier_id="humidifier"):
|
||||||
self.last_toggle = 0
|
self.last_toggle = 0
|
||||||
self.last_state = False
|
self.last_state = None
|
||||||
self.cooldown = 5
|
self.cooldown = 5
|
||||||
|
self.humidifier_id = humidifier_id
|
||||||
|
|
||||||
def set(self, s, on):
|
def set(self, s, on):
|
||||||
if on != self.last_state:
|
if on != self.last_state:
|
||||||
|
@ -20,4 +23,22 @@ class HumidifierV3:
|
||||||
s.flush()
|
s.flush()
|
||||||
if self.last_state != on:
|
if self.last_state != on:
|
||||||
self.last_toggle = time.time()
|
self.last_toggle = time.time()
|
||||||
|
if on:
|
||||||
|
print("send {} on".format(self.humidifier_id))
|
||||||
|
else:
|
||||||
|
print("send {} off".format(self.humidifier_id))
|
||||||
|
send_update({"status": {self.humidifier_id: on}})
|
||||||
|
|
||||||
self.last_state = on
|
self.last_state = on
|
||||||
|
|
||||||
|
# TODO: read this from Arduino to verify the pin state
|
||||||
|
def update(self, val):
|
||||||
|
self.last_state = (val != 0)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def on(self):
|
||||||
|
return self.last_state is not None and self.last_state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def off(self):
|
||||||
|
return self.last_state is not None and not self.last_state
|
||||||
|
|
|
@ -11,6 +11,9 @@ from humidifier import Humidifier, HumidifierV2
|
||||||
from humidifier_v3 import HumidifierV3
|
from humidifier_v3 import HumidifierV3
|
||||||
from controller import Controller
|
from controller import Controller
|
||||||
|
|
||||||
|
import utils
|
||||||
|
from utils import start_process, send_update
|
||||||
|
|
||||||
SERIAL_PATH = "/dev/ttyACM0"
|
SERIAL_PATH = "/dev/ttyACM0"
|
||||||
SERIAL_BAUD = 115200
|
SERIAL_BAUD = 115200
|
||||||
|
|
||||||
|
@ -23,21 +26,8 @@ except KeyError:
|
||||||
is_mock = False
|
is_mock = False
|
||||||
|
|
||||||
print("controller start")
|
print("controller start")
|
||||||
process = None
|
|
||||||
def start_process():
|
|
||||||
global process
|
|
||||||
if is_mock:
|
|
||||||
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
||||||
else:
|
|
||||||
#process = subprocess.Popen(["ssh", "shrooms@threefortiethofonehamster.com", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
||||||
process = subprocess.Popen(["ssh", "shrooms@35.211.7.97", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
||||||
start_process()
|
start_process()
|
||||||
|
|
||||||
def send_update(msg):
|
|
||||||
global process
|
|
||||||
process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
|
|
||||||
process.stdin.flush()
|
|
||||||
|
|
||||||
if is_mock:
|
if is_mock:
|
||||||
import mock_serial
|
import mock_serial
|
||||||
s = mock_serial.MockSerial()
|
s = mock_serial.MockSerial()
|
||||||
|
@ -62,7 +52,7 @@ exiting = False
|
||||||
def stdout_loop():
|
def stdout_loop():
|
||||||
global process, controller, humidifier, humidifier2
|
global process, controller, humidifier, humidifier2
|
||||||
while not exiting:
|
while not exiting:
|
||||||
msg = process.stdout.readline()
|
msg = utils.process.stdout.readline()
|
||||||
if len(msg) <= 1:
|
if len(msg) <= 1:
|
||||||
continue
|
continue
|
||||||
print("got message ", msg)
|
print("got message ", msg)
|
||||||
|
@ -151,7 +141,7 @@ try:
|
||||||
#print(parts)
|
#print(parts)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
controller.update(humidity)
|
controller.update(s, humidity)
|
||||||
|
|
||||||
if frame_num == 0:
|
if frame_num == 0:
|
||||||
#print(humidity, temp, volts)
|
#print(humidity, temp, volts)
|
||||||
|
@ -161,7 +151,7 @@ try:
|
||||||
"temp": temp,
|
"temp": temp,
|
||||||
"hum": humidity,
|
"hum": humidity,
|
||||||
"hv": 1 if humidifier.on else 0,
|
"hv": 1 if humidifier.on else 0,
|
||||||
#"hv2": volts2,
|
"hv2": 1 if humidifier.on else 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
send_update(update)
|
send_update(update)
|
||||||
|
@ -170,7 +160,7 @@ try:
|
||||||
print("pipe errored out, restarting: ", e)
|
print("pipe errored out, restarting: ", e)
|
||||||
# restart the process I guess
|
# restart the process I guess
|
||||||
exiting = True
|
exiting = True
|
||||||
process.kill()
|
utils.process.kill()
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
stdout_thread.join()
|
stdout_thread.join()
|
||||||
exiting = False
|
exiting = False
|
||||||
|
@ -183,5 +173,5 @@ try:
|
||||||
finally:
|
finally:
|
||||||
# kill ssh connection
|
# kill ssh connection
|
||||||
exiting = True
|
exiting = True
|
||||||
process.kill()
|
utils.process.kill()
|
||||||
stdout_thread.join()
|
stdout_thread.join()
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
process = None
|
||||||
|
def start_process():
|
||||||
|
global process
|
||||||
|
|
||||||
|
try:
|
||||||
|
is_mock = os.environ['MOCK']
|
||||||
|
except KeyError:
|
||||||
|
is_mock = False
|
||||||
|
if is_mock:
|
||||||
|
process = subprocess.Popen(["/usr/bin/env", "python", "/home/kelvin/src/shroom-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
|
else:
|
||||||
|
#process = subprocess.Popen(["ssh", "shrooms@threefortiethofonehamster.com", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
|
process = subprocess.Popen(["ssh", "shrooms@35.211.7.97", "/usr/bin/env", "python3", "/home/shrooms/shrooms-server/shroom_pipe.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
|
|
||||||
|
def send_update(msg):
|
||||||
|
global process
|
||||||
|
process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
|
||||||
|
process.stdin.flush()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue