Compare commits
2 Commits
3117487346
...
38f26f3e65
Author | SHA1 | Date |
---|---|---|
|
38f26f3e65 | |
|
a65dcb1ee4 |
|
@ -33,10 +33,13 @@ void fanOff() {
|
|||
}
|
||||
|
||||
|
||||
const int PIN_RELAY4 = 4;
|
||||
const int PIN_RELAY3 = 5;
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(4, OUTPUT);
|
||||
pinMode(5, OUTPUT);
|
||||
pinMode(PIN_RELAY4, OUTPUT);
|
||||
pinMode(PIN_RELAY3, OUTPUT);
|
||||
pinMode(7, OUTPUT);
|
||||
digitalWrite(7, LOW);
|
||||
|
||||
|
@ -53,8 +56,6 @@ void setup()
|
|||
//sht20.checkSHT20(); // Check SHT20 Sensor
|
||||
}
|
||||
|
||||
const int PIN_RELAY4 = 4;
|
||||
const int PIN_RELAY3 = 5;
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
@ -74,13 +75,14 @@ void loop()
|
|||
Serial.print(temp);
|
||||
Serial.print(",");
|
||||
Serial.print(digitalRead(PIN_RELAY4));
|
||||
//Serial.print(",");
|
||||
//Serial.println(volts2);
|
||||
Serial.print(",");
|
||||
Serial.print(digitalRead(PIN_RELAY3));
|
||||
Serial.println("");
|
||||
} else if (c == 'z' || c == 'Z') {
|
||||
if (c == 'z') digitalWrite(PIN_RELAY4, LOW);
|
||||
if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH);
|
||||
}
|
||||
if (c == 'z') digitalWrite(PIN_RELAY4, LOW);
|
||||
if (c == 'Z') digitalWrite(PIN_RELAY4, HIGH);
|
||||
if (c == 'y') digitalWrite(PIN_RELAY3, LOW);
|
||||
if (c == 'Y') digitalWrite(PIN_RELAY3, HIGH);
|
||||
}
|
||||
delay(1);
|
||||
}
|
||||
|
|
|
@ -113,17 +113,66 @@ def stdout_loop():
|
|||
stdout_thread = threading.Thread(target=stdout_loop)
|
||||
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
|
||||
last_sample = 0
|
||||
|
||||
last_pipe_reboot = 0
|
||||
last_running = False
|
||||
pipe_timeout = 10
|
||||
|
||||
fan_on = False
|
||||
|
||||
try:
|
||||
while True:
|
||||
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:
|
||||
time.sleep(SAMPLE_PERIOD - (now - last_sample) + 0.001)
|
||||
continue
|
||||
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")
|
||||
s.write(b"s")
|
||||
s.flush()
|
||||
|
@ -137,7 +186,8 @@ try:
|
|||
humidity = float(parts[0])
|
||||
temp = float(parts[1])
|
||||
volts = float(parts[2])
|
||||
#volts2 = float(parts[3])
|
||||
volts2 = float(parts[3])
|
||||
fan_on = int(volts2)
|
||||
#print(parts)
|
||||
|
||||
try:
|
||||
|
@ -152,25 +202,16 @@ try:
|
|||
"temp": temp,
|
||||
"hum": humidity,
|
||||
"hv": volts,
|
||||
"hv2": 1 if humidifier.on else 0,
|
||||
"hv2": volts2,
|
||||
}
|
||||
}
|
||||
send_update(update)
|
||||
#print("sending update {}".format(update))
|
||||
frame_num = (frame_num + 1) % DECIMATION_RATE
|
||||
except Exception as e:
|
||||
print("pipe errored out, restarting: ", e)
|
||||
print("pipe errored out, restarting: ", repr(e))
|
||||
# restart the process I guess
|
||||
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()
|
||||
restart_pipe()
|
||||
|
||||
finally:
|
||||
# kill ssh connection
|
||||
|
|
34
utils.py
34
utils.py
|
@ -1,10 +1,28 @@
|
|||
import subprocess
|
||||
import threading
|
||||
|
||||
import os
|
||||
import json
|
||||
import queue
|
||||
|
||||
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():
|
||||
global process
|
||||
print("starting shroom pipe")
|
||||
global process, update_thread
|
||||
if process is not None and process.poll() is None:
|
||||
print("shroom pipe is still running, exiting init")
|
||||
return
|
||||
|
||||
try:
|
||||
is_mock = os.environ['MOCK']
|
||||
|
@ -16,9 +34,17 @@ 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@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):
|
||||
update_thread = threading.Thread(target=update_loop, args = (process, update_queue))
|
||||
update_thread.start()
|
||||
|
||||
def running():
|
||||
global process
|
||||
process.stdin.write(bytes(json.dumps(msg) + "\n", "utf8"))
|
||||
process.stdin.flush()
|
||||
return process is not None and process.poll() is None
|
||||
|
||||
def send_update(msg):
|
||||
global update_queue
|
||||
try:
|
||||
update_queue.put_nowait(msg)
|
||||
except queue.Full:
|
||||
print("queue full, skipping message")
|
||||
|
||||
|
|
Loading…
Reference in New Issue