]> hydra-www.ietfng.org Git - acmetensortoys-snakecontrol/commitdiff
Move BME280 monitoring to its own program
authorNathaniel Wesley Filardo <nwfilardo@gmail.com>
Mon, 22 Jan 2018 02:42:52 +0000 (21:42 -0500)
committerNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sun, 11 Feb 2018 00:07:50 +0000 (19:07 -0500)
bin/lcdproc-monitor.expect
bin/monitor280.py [new file with mode: 0755]
bin/pidmonitor.py
runit/pi-sc-monitor280/log/run [new file with mode: 0755]
runit/pi-sc-monitor280/run [new file with mode: 0755]

index c6bcfc870e08faf0ef0eb4e856878c8754d34bed..d1da8d831df45ba3e6f72994a84f104e19918778 100755 (executable)
@@ -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 (executable)
index 0000000..1fce552
--- /dev/null
@@ -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()
index 54535a4c27adb36d7220c9c2e1d936b862760d6c..05f1b0d4d2bdf1ada9e2f07d8ea8e533a4841a9c 100755 (executable)
@@ -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 (executable)
index 0000000..e689c7b
--- /dev/null
@@ -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 (executable)
index 0000000..a25d984
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec chpst -u pi:pi:i2c python -u /home/pi/sc/bin/monitor280.py