From ad2b15d49eadc1967ca5dfdca9f6d947ebd9f717 Mon Sep 17 00:00:00 2001 From: Kelvin on RPi Date: Fri, 9 Aug 2024 21:39:50 +0100 Subject: [PATCH] Various tweaks --- .gitignore | 1 + controller.py | 27 ++++++---- humidifier.py | 5 ++ .../humidifier_controller.ino | 50 ++++--------------- humidifier_v3.py | 25 +++++++++- shroom_controller.py | 26 +++------- utils.py | 24 +++++++++ 7 files changed, 89 insertions(+), 69 deletions(-) create mode 100644 utils.py diff --git a/.gitignore b/.gitignore index 99b0e96..94c1b10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ shroom_server shrooms.db auth_secret +__pycache__/ diff --git a/controller.py b/controller.py index 8b1fce2..53214c2 100644 --- a/controller.py +++ b/controller.py @@ -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: def __init__(self, humidifiers): - self.target_lower = 85 - self.target_upper = 90 + self.target_lower = 83 + self.target_upper = 87 self.feedforward_coeff = 50 self.last_toggle = 0 @@ -25,13 +33,14 @@ class Controller: def set_checked(self, s, humidifier, on): 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) - else: + elif isinstance(humidifier, Humidifier) or isinstance(humidifier, HumidifierV2): humidifier.toggle(s) self.last_toggle = time.time() - def update(self, humidity): + def update(self, s, humidity): if self.first_sample: self.humidifier_history[:] = humidity self.first_sample = False @@ -49,16 +58,16 @@ class Controller: if self.manual_mode: 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) - elif humidifier.on and not self.manual_on: + elif not humidifier.off and not self.manual_on: self.set_checked(s, humidifier, False) else: if comp_humidity < self.target_lower: for humidifier in self.humidifiers: - if humidifier.off: + if not humidifier.on: self.set_checked(s, humidifier, True) elif comp_humidity > self.target_upper: for humidifier in self.humidifiers: - if humidifier.on: + if not humidifier.off: self.set_checked(s, humidifier, False) diff --git a/humidifier.py b/humidifier.py index 64c005a..12f48b1 100644 --- a/humidifier.py +++ b/humidifier.py @@ -1,3 +1,8 @@ +import numpy as np +import time + +from utils import send_update + class Humidifier: def __init__(self): self.off_threshold = 0.4 diff --git a/humidifier_controller/humidifier_controller.ino b/humidifier_controller/humidifier_controller.ino index fb92677..0d21615 100644 --- a/humidifier_controller/humidifier_controller.ino +++ b/humidifier_controller/humidifier_controller.ino @@ -53,33 +53,14 @@ void setup() //sht20.checkSHT20(); // Check SHT20 Sensor } -// relay 4 -void toggle() { - 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); -} - +const int PIN_RELAY4 = 4; +const int PIN_RELAY3 = 5; void loop() { while (Serial.available()) { const int c = Serial.read(); if (c == 's') { - // TODO dump the current status #ifdef USE_SHT30 sht30.read(); float humd = sht30.getHumidity(); @@ -88,28 +69,17 @@ void loop() float humd = sht20.readHumidity(); // Read Humidity float temp = sht20.readTemperature(); // Read Temperature #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(","); Serial.print(temp); - Serial.print(","); - Serial.print(volts); - Serial.print(","); - Serial.println(volts2); - } else if (c == 'h') { - toggle(); - } else if (c == 'i') { - toggle2(); - } else if (c >= 'A' && c <= 'Z') { - toggle_test(c - 'A'); + //Serial.print(","); + //Serial.print(volts); + //Serial.print(","); + //Serial.println(volts2); + Serial.println(""); + } else if (c == 'z' || c == 'Z') { + if (c == 'z') digitalWrite(PIN_RELAY4, LOW); + if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH); } } delay(1); diff --git a/humidifier_v3.py b/humidifier_v3.py index 9bf7421..2dbfada 100644 --- a/humidifier_v3.py +++ b/humidifier_v3.py @@ -1,12 +1,15 @@ import time +from utils import send_update + # this is driving a SSR that's powering a AC-powered humidifier # we don't need voltage readings for this one class HumidifierV3: - def __init__(self): + def __init__(self, humidifier_id="humidifier"): self.last_toggle = 0 - self.last_state = False + self.last_state = None self.cooldown = 5 + self.humidifier_id = humidifier_id def set(self, s, on): if on != self.last_state: @@ -20,4 +23,22 @@ class HumidifierV3: s.flush() if self.last_state != on: 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 + + # 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 diff --git a/shroom_controller.py b/shroom_controller.py index 20884d4..415f3ee 100644 --- a/shroom_controller.py +++ b/shroom_controller.py @@ -11,6 +11,9 @@ from humidifier import Humidifier, HumidifierV2 from humidifier_v3 import HumidifierV3 from controller import Controller +import utils +from utils import start_process, send_update + SERIAL_PATH = "/dev/ttyACM0" SERIAL_BAUD = 115200 @@ -23,21 +26,8 @@ except KeyError: is_mock = False 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() -def send_update(msg): - global process - process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8")) - process.stdin.flush() - if is_mock: import mock_serial s = mock_serial.MockSerial() @@ -62,7 +52,7 @@ exiting = False def stdout_loop(): global process, controller, humidifier, humidifier2 while not exiting: - msg = process.stdout.readline() + msg = utils.process.stdout.readline() if len(msg) <= 1: continue print("got message ", msg) @@ -151,7 +141,7 @@ try: #print(parts) try: - controller.update(humidity) + controller.update(s, humidity) if frame_num == 0: #print(humidity, temp, volts) @@ -161,7 +151,7 @@ try: "temp": temp, "hum": humidity, "hv": 1 if humidifier.on else 0, - #"hv2": volts2, + "hv2": 1 if humidifier.on else 0, } } send_update(update) @@ -170,7 +160,7 @@ try: print("pipe errored out, restarting: ", e) # restart the process I guess exiting = True - process.kill() + utils.process.kill() time.sleep(0.1) stdout_thread.join() exiting = False @@ -183,5 +173,5 @@ try: finally: # kill ssh connection exiting = True - process.kill() + utils.process.kill() stdout_thread.join() diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..8ef289b --- /dev/null +++ b/utils.py @@ -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() + +