From edf716166585a88469d31b85b2ef0c0c437fad2a Mon Sep 17 00:00:00 2001 From: Jonny Ervine Date: Tue, 23 Jun 2015 07:08:25 +0800 Subject: [PATCH] modified: python_code/temp-humid-read-loop.py modified: python_code/temp-humid-read-single.py --- python_code/temp-humid-read-loop.py | 15 ++++++++++----- python_code/temp-humid-read-single.py | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/python_code/temp-humid-read-loop.py b/python_code/temp-humid-read-loop.py index 470f44f..710dfff 100755 --- a/python_code/temp-humid-read-loop.py +++ b/python_code/temp-humid-read-loop.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#/usr/bin/env python # -*- coding:utf-8 -*- import sys @@ -25,16 +25,20 @@ def exec_every_n_seconds(n,f,*args): logging.debug('Running the loop again in {0} seconds (printed as float)'.format(offset2)) while 1: logging.debug('We are in the loop now ...') - time.sleep(offset2) + initTime=time.time() + logging.debug('Start time is {0}'.format(initTime)) + #time.sleep(offset2) logging.debug('We are about to run the sensor read again') duration=f(*args) + offset2 = (60.0 - (time.time() - initTime)) + time.sleep(60.0 - ((time.time() - initTime) % 60.0)) logging.debug('Duration of sequence was: {0}'.format(duration)) offset = waitInterval - duration logging.debug('Running the loop again in {0} seconds (printed as dateime)'.format(offset)) - offset2 = offset.total_seconds() + #offset2 = offset.total_seconds() logging.debug('Running the loop again in {0} seconds (printed as float)'.format(offset2)) -def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhumid): +def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, tempdiff, maxhumid, minhumid, humiddiff): global oldtemp global oldhumid startTime=datetime.now() @@ -51,6 +55,7 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum else: if t and h: logging.debug('Temperature and humidity read as {0} and {1}'.format(t, h)) + logging.debug('Temperature and Humidity differences allowed: {0} and {1}'.format(tempdiff, humiddiff)) if (oldtemp != "NULL") and ((t - oldtemp < tempdiff) or (oldtemp - t < tempdiff)) and ((h - oldhumid < humiddiff) or (oldhumid - h < humiddif)): logging.debug('Current temperature close enough to previous temperature and previous temperature is not NULL, it is: %s', oldtemp) logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid) @@ -133,4 +138,4 @@ logging.info("using pin #{0}".format(dhtpin)) logging.info('Multiple (infinte looped) run temperature and humidity reading. [version: 1.0, Jonathan Ervine, 2015-06-17]') logging.debug("About to enter infinite loop") -exec_every_n_seconds(60,sensorRead,hwtype,pin,retries,timeout,maxtemp,mintemp,maxhumid,minhumid) +exec_every_n_seconds(60,sensorRead,hwtype,pin,retries,timeout,maxtemp,mintemp,tempdiff,maxhumid,minhumid,humiddiff) diff --git a/python_code/temp-humid-read-single.py b/python_code/temp-humid-read-single.py index ff884db..d1be2eb 100644 --- a/python_code/temp-humid-read-single.py +++ b/python_code/temp-humid-read-single.py @@ -14,7 +14,7 @@ import logging oldtemp = "NULL" oldhumid = "NULL" -def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhumid): +def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, tempdiff, maxhumid, minhumid, humiddiff): global oldtemp global oldhumid for num in range(retries): @@ -105,4 +105,4 @@ if dhtpin <= 0: logging.info("using pin #{0}".format(dhtpin)) logging.info('Single run temperature and humidity reading. [version: 1.0, Jonathan Ervine, 2015-06-17]') -sensorRead(hwtype,pin,retries,timeout,maxtemp,mintemp,maxhumid,minhumid) +sensorRead(hwtype,pin,retries,timeout,maxtemp,mintemp,tempdiff,maxhumid,minhumid,humiddiff)