#! /usr/bin/python import ow from time import time import MySQLdb from mx.DateTime import now import os import string db = MySQLdb.connect("localhost","python","python","python") c=db.cursor() def convert(n): try: return int(n) except ValueError: return float(n) def heatOn(): s = ow.Sensor(sw_workshop) s.PIO_A = '1' c.execute("update settings set status = 1") def heatOff(): s = ow.Sensor(sw_workshop) s.PIO_A = '0' c.execute("update settings set status = 0") def checkTemp(t): if t >= (setpoint + deadband): heatOff() elif t < (setpoint - deadband): heatOn() c.execute("select deadband from settings") sqldata = c.fetchone() deadband = float(sqldata[0]) / 2 c.execute("select setpoint from settings") sqldata = c.fetchone() setpoint = sqldata[0] c.execute("select address from devices where alias = 'temp_workshop'") sqldata = c.fetchone() zt_workshop = '/' + sqldata[0] c.execute("select address from devices where alias = 'switch_workshop'") sqldata = c.fetchone() sw_workshop = '/' + sqldata[0] c.execute("select address from devices where alias = 'temp_outside'") sqldata = c.fetchone() zt_oat = sqldata[0] zt_oat = '/' + zt_oat ow.init('u') ow.error_level(ow.error_level.fatal) ow.error_print(ow.error_print.stderr) s = ow.Sensor(zt_workshop) temp = s.temperature temp = temp.strip() temp = convert(temp) s = ow.Sensor(zt_oat) oat = s.temperature oat = oat.strip() oat = convert(oat) readtime = now() checkTemp(temp) c.executemany( """INSERT INTO templog (time, temp, setpoint, oa_templog) VALUES (%s, %s, %s, %s)""", [ (readtime, temp, setpoint, oat ) ] ) ow.finish() commandArg = "rrdtool update /home/steve/owfs/ws_templog.rrd N:%s" % temp os.system(commandArg) commandArg = "rrdtool update /home/steve/owfs/ws_setpoint.rrd N:%s" % setpoint os.system(commandArg) commandArg = "rrdtool update /home/steve/owfs/oa_templog.rrd N:%s" % oat os.system(commandArg) commandArg = 'rrdtool graph /home/steve/owfs/web/temperature.png DEF\:temperature=/home/steve/owfs/ws_templog.rrd\:temperature\:AVERAGE LINE1\:temperature#FF0000\:"Shop Temp" DEF\:setpoint=/home/steve/owfs/ws_setpoint.rrd\:setpoint\:LAST LINE1\:setpoint#009900\:"Setpoint" DEF\:outside=/home/steve/owfs/oa_templog.rrd\:outside\:LAST LINE1\:outside#0000CC\:"Outside Temp" --width="800" --height="300" --vertical-label="Degrees Celsius" VDEF\:current-temp=temperature,LAST COMMENT\:"\\n" GPRINT\:current-temp\:"Shop\: %8.2lf C" COMMENT\:"\\n" VDEF\:current-setpoint=setpoint,LAST GPRINT\:current-setpoint\:"Setpoint\: %8.2lf C" COMMENT\:"\\n" VDEF\:current-outside=outside,LAST GPRINT\:current-outside\:"Outside\: %8.2lf C"' os.system(commandArg)