From db714d1a0bb665341d5394019bd7cfbf00cde6c4 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sun, 21 Jan 2018 21:42:52 -0500 Subject: [PATCH] Move BME280 monitoring to its own program --- bin/lcdproc-monitor.expect | 14 ++++++++++-- bin/monitor280.py | 39 ++++++++++++++++++++++++++++++++++ bin/pidmonitor.py | 13 ------------ runit/pi-sc-monitor280/log/run | 4 ++++ runit/pi-sc-monitor280/run | 2 ++ 5 files changed, 57 insertions(+), 15 deletions(-) create mode 100755 bin/monitor280.py create mode 100755 runit/pi-sc-monitor280/log/run create mode 100755 runit/pi-sc-monitor280/run diff --git a/bin/lcdproc-monitor.expect b/bin/lcdproc-monitor.expect index c6bcfc8..d1da8d8 100755 --- a/bin/lcdproc-monitor.expect +++ b/bin/lcdproc-monitor.expect @@ -5,6 +5,9 @@ package require lcdproclib spawn tail --retry --follow=name /run/snakecontrol/monitor.log/current set logsid ${spawn_id} +spawn tail --retry --follow=name /run/snakecontrol/monitor280.log/current +set log2sid ${spawn_id} + spawn tail --retry --follow=name /run/snakecontrol/door-monitor.log/current set doorlogsid ${spawn_id} @@ -94,8 +97,6 @@ while { true } { "hide-far temp=${datare}" { drawtemp ${sid} 1 "v_rh" 3 4 [lindex ${m} 1] } \ "tank-near temp=${datare}" { drawtemp ${sid} 1 "v_lt" 9 2 [lindex ${m} 1] } \ "tank-far temp=${datare}" { drawtemp ${sid} 1 "v_rt" 9 4 [lindex ${m} 1] } \ - "tank-mid temp=${datare}" { drawtemp ${sid} 1 "v_mt" 3 3 [lindex ${m} 1] } \ - "tank-mid humid=${datare}" { drawtemp ${sid} 1 "v_mh" 9 3 [lindex ${m} 1] } \ "dmx-tanknear dmx=${datare}" { drawdmx ${sid} 1 "v_ldmx" 15 3 [lindex ${m} 1] } \ "dmx-hidenear dmx=${datare}" { drawdmx ${sid} 1 "v_hdmx" 16 3 [lindex ${m} 1] } \ "dmx-hidefar dmx=${datare}" { drawdmx ${sid} 1 "v_fdmx" 17 3 [lindex ${m} 1] } \ @@ -106,6 +107,15 @@ while { true } { } -i ${logsid} -re "${dskip}\n" {} + -i ${log2sid} -re "${datapfx}(${dskip})\n" { + # This uses the many-argument form of switch to get expansion inside the + # templates. That's a little gross, but so it goes. + switch -regexp -matchvar m -- "${expect_out(1,string)}" \ + "tank-mid temp=${datare}" { drawtemp ${sid} 1 "v_mt" 3 3 [lindex ${m} 1] } \ + "tank-mid humid=${datare}" { drawtemp ${sid} 1 "v_mh" 9 3 [lindex ${m} 1] } \ + } + -i ${log2sid} -re "${dskip}\n" {} + -i ${doorlogsid} -re "${datapfx}door${dskip} left=${datare} right=${datare}${dskip}\n" { switch -regexp "${expect_out(1,string)}${expect_out(2,string)}" { "00" { diff --git a/bin/monitor280.py b/bin/monitor280.py new file mode 100755 index 0000000..1fce552 --- /dev/null +++ b/bin/monitor280.py @@ -0,0 +1,39 @@ +#!/usr/bin/python + +import sched +import time +import rrdtool +import BME280 + +bmemid = BME280.BME280(port=1, address=0x77) + +def log(devfn, temp, logname, rrd, kw="temp"): + try: + rrdtool.update(rrd, "N:" + ("%f" % temp)) + except Exception, e : + print("WARN: Cannot log %s: %s ." % (logname, e)) + print ("DATA: %s %s=%s" % (logname, kw, temp)) + +def logfail(devfn, name, *arg): + print ("FAIL: %s ; %s ; %s ." % (name, devfn, arg)) + +def poller(sc): + global bmemid + sc.enter(10, 1, poller, (sc,)) + + # BME280 in middle of tank + try: + top = bmemid.get_data() + log("bme280-77", top['t'], "tank-mid", "/home/pi/sc/data/tank-mid-temp.rrd") + log("bme280-77", top['h'], "tank-mid", "/home/pi/sc/data/tank-mid-humid.rrd", kw="humid") + log("bme280-77", top['p'], "tank-mid", "/home/pi/sc/data/tank-mid-press.rrd", kw="press") + except Exception, e: + logfail("bme280-77", "tank-mid", e) + bmemid = BME280.BME280(port=1, address=0x77) + +itime = time.time() +s = sched.scheduler(time.time, time.sleep) +s.enterabs(itime, 1, poller, (s,)) + +print("Monitor starting...") +s.run() diff --git a/bin/pidmonitor.py b/bin/pidmonitor.py index 54535a4..05f1b0d 100755 --- a/bin/pidmonitor.py +++ b/bin/pidmonitor.py @@ -2,14 +2,12 @@ # Inspired by code found at http://wannabe.guru.org/scott/hobbies/temperature/ -import os import sched import time import serial import rrdtool import pidloop -import BME280 dmxdev = serial.Serial("/dev/serial/by-id/usb-DMXking.com_DMX_USB_PRO_6A0SVM7J-if00-port0", 57600); @@ -38,8 +36,6 @@ loop_hidefar.setKP(60.0) # XXX These are un-tuned loop_hidefar.setKI(0.004) loop_hidefar.setKD(1000.0,0.95) -bmemid = BME280.BME280(port=1, address=0x77) - def log(devfn, temp, logname, rrd, kw="temp"): try: rrdtool.update(rrd, "N:" + ("%f" % temp)) @@ -90,15 +86,6 @@ def check_temps(sc): cache = {} - # BME280 atop - try: - top = bmemid.get_data() - log("bme280-77", top['t'], "tank-mid", "/home/pi/sc/data/tank-mid-temp.rrd") - log("bme280-77", top['h'], "tank-mid", "/home/pi/sc/data/tank-mid-humid.rrd", kw="humid") - log("bme280-77", top['p'], "tank-mid", "/home/pi/sc/data/tank-mid-press.rrd", kw="press") - except Exception, e: - logfail("bme280-77", "hide-mid", e) - # Log (and populate cache, while we're at it) with_ow_temp(cache, "/sys/bus/w1/devices/28-011620f10dee/w1_slave", log, logfail, diff --git a/runit/pi-sc-monitor280/log/run b/runit/pi-sc-monitor280/log/run new file mode 100755 index 0000000..e689c7b --- /dev/null +++ b/runit/pi-sc-monitor280/log/run @@ -0,0 +1,4 @@ +#!/bin/sh +mkdir -p /run/snakecontrol/monitor280.log +chown -R pi:pi /run/snakecontrol/monitor280.log +exec chpst -u pi svlogd -tt /run/snakecontrol/monitor280.log diff --git a/runit/pi-sc-monitor280/run b/runit/pi-sc-monitor280/run new file mode 100755 index 0000000..a25d984 --- /dev/null +++ b/runit/pi-sc-monitor280/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec chpst -u pi:pi:i2c python -u /home/pi/sc/bin/monitor280.py -- 2.50.1