Compare commits

..

No commits in common. "38f26f3e6502eb4cfbb0db248581d6f5000bd4d0" and "3117487346657e89c1c238676767325ca612d575" have entirely different histories.

3 changed files with 27 additions and 96 deletions

View File

@ -33,13 +33,10 @@ void fanOff() {
} }
const int PIN_RELAY4 = 4;
const int PIN_RELAY3 = 5;
void setup() void setup()
{ {
pinMode(PIN_RELAY4, OUTPUT); pinMode(4, OUTPUT);
pinMode(PIN_RELAY3, OUTPUT); pinMode(5, OUTPUT);
pinMode(7, OUTPUT); pinMode(7, OUTPUT);
digitalWrite(7, LOW); digitalWrite(7, LOW);
@ -56,6 +53,8 @@ void setup()
//sht20.checkSHT20(); // Check SHT20 Sensor //sht20.checkSHT20(); // Check SHT20 Sensor
} }
const int PIN_RELAY4 = 4;
const int PIN_RELAY3 = 5;
void loop() void loop()
{ {
@ -75,14 +74,13 @@ void loop()
Serial.print(temp); Serial.print(temp);
Serial.print(","); Serial.print(",");
Serial.print(digitalRead(PIN_RELAY4)); Serial.print(digitalRead(PIN_RELAY4));
Serial.print(","); //Serial.print(",");
Serial.print(digitalRead(PIN_RELAY3)); //Serial.println(volts2);
Serial.println(""); Serial.println("");
} } else if (c == 'z' || c == 'Z') {
if (c == 'z') digitalWrite(PIN_RELAY4, LOW); if (c == 'z') digitalWrite(PIN_RELAY4, LOW);
if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH); if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH);
if (c == 'y') digitalWrite(PIN_RELAY3, LOW); }
if (c == 'Y') digitalWrite(PIN_RELAY3, HIGH);
} }
delay(1); delay(1);
} }

View File

@ -113,66 +113,17 @@ def stdout_loop():
stdout_thread = threading.Thread(target=stdout_loop) stdout_thread = threading.Thread(target=stdout_loop)
stdout_thread.start() stdout_thread.start()
def restart_pipe():
global exiting, stdout_thread
exiting = True
utils.process.kill()
time.sleep(0.1)
stdout_thread.join()
exiting = False
start_process()
stdout_thread = threading.Thread(target=stdout_loop)
stdout_thread.start()
frame_num = 0 frame_num = 0
last_sample = 0 last_sample = 0
last_pipe_reboot = 0
last_running = False
pipe_timeout = 10
fan_on = False
try: try:
while True: while True:
now = time.time() now = time.time()
# check to see if the SSH pipe is working
if utils.running():
if not last_running and utils.running():
print("pipe is now running, resetting timeout", pipe_timeout)
pipe_timeout = 10
last_running = utils.running()
if not utils.running() and (now - last_pipe_reboot) > pipe_timeout:
try:
restart_pipe()
except Exception as e:
print("error restarting pipe: {}".format(repr(e)))
last_pipe_reboot = now
pipe_timeout *= 1.5
pipe_timeout = min(pipe_timeout, 5*60)
print("new pipe timeout ", pipe_timeout)
now = time.time()
if now - last_sample < SAMPLE_PERIOD: if now - last_sample < SAMPLE_PERIOD:
time.sleep(SAMPLE_PERIOD - (now - last_sample) + 0.001) time.sleep(SAMPLE_PERIOD - (now - last_sample) + 0.001)
continue continue
last_sample = now last_sample = now
# turn on the fan 1 minutes every 5 minutes
cur_min = int(now / 60)
fan_should_be_on = (cur_min % 5 == 0)
if fan_on != fan_should_be_on:
if fan_should_be_on:
s.write(b"Y")
else:
s.write(b"y")
#print("write s") #print("write s")
s.write(b"s") s.write(b"s")
s.flush() s.flush()
@ -186,8 +137,7 @@ try:
humidity = float(parts[0]) humidity = float(parts[0])
temp = float(parts[1]) temp = float(parts[1])
volts = float(parts[2]) volts = float(parts[2])
volts2 = float(parts[3]) #volts2 = float(parts[3])
fan_on = int(volts2)
#print(parts) #print(parts)
try: try:
@ -202,16 +152,25 @@ try:
"temp": temp, "temp": temp,
"hum": humidity, "hum": humidity,
"hv": volts, "hv": volts,
"hv2": volts2, "hv2": 1 if humidifier.on else 0,
} }
} }
send_update(update) send_update(update)
#print("sending update {}".format(update)) #print("sending update {}".format(update))
frame_num = (frame_num + 1) % DECIMATION_RATE frame_num = (frame_num + 1) % DECIMATION_RATE
except Exception as e: except Exception as e:
print("pipe errored out, restarting: ", repr(e)) print("pipe errored out, restarting: ", e)
# restart the process I guess # restart the process I guess
restart_pipe() exiting = True
utils.process.kill()
time.sleep(0.1)
stdout_thread.join()
exiting = False
start_process()
stdout_thread = threading.Thread(target=stdout_loop)
stdout_thread.start()
finally: finally:
# kill ssh connection # kill ssh connection

View File

@ -1,28 +1,10 @@
import subprocess import subprocess
import threading
import os import os
import json import json
import queue
process = None process = None
update_thread = None
update_queue = queue.LifoQueue(maxsize=1)
def update_loop(p, q):
print("update_loop start")
while p.poll() is None:
msg = q.get()
#print("got msg")
p.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
p.stdin.flush()
def start_process(): def start_process():
print("starting shroom pipe") global process
global process, update_thread
if process is not None and process.poll() is None:
print("shroom pipe is still running, exiting init")
return
try: try:
is_mock = os.environ['MOCK'] is_mock = os.environ['MOCK']
@ -34,17 +16,9 @@ def start_process():
#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@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) 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)
update_thread = threading.Thread(target=update_loop, args = (process, update_queue))
update_thread.start()
def running():
global process
return process is not None and process.poll() is None
def send_update(msg): def send_update(msg):
global update_queue global process
try: process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
update_queue.put_nowait(msg) process.stdin.flush()
except queue.Full:
print("queue full, skipping message")