modified: python_code/temp-humid-read-loop.py
This commit is contained in:
@@ -6,33 +6,39 @@ import dhtreader
|
|||||||
import updateMysql
|
import updateMysql
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import time
|
import time
|
||||||
|
from time import strftime
|
||||||
from threading import Timer,Thread,Event
|
from threading import Timer,Thread,Event
|
||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
oldtemp = "NULL"
|
oldtemp = "NULL"
|
||||||
oldhumid = "NULL"
|
oldhumid = "NULL"
|
||||||
|
|
||||||
def exec_every_n_seconds(n,f,*args):
|
def exec_every_n_seconds(n,f,*args):
|
||||||
first_called=datetime.now()
|
waitInterval = timedelta(seconds=n)
|
||||||
print args
|
print('We should read the sensor every {0} seconds'.format(waitInterval))
|
||||||
f(*args)
|
duration=f(*args)
|
||||||
num_calls=1
|
offset = waitInterval - duration
|
||||||
drift=timedelta()
|
print('Running the loop again in {0} seconds'.format(offset))
|
||||||
time_period=timedelta(seconds=n)
|
offset2 = offset.total_seconds()
|
||||||
|
print('Running the loop again in {0} seconds'.format(offset2))
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(n-drift.microseconds/1000000.0)
|
print('We are in the loop now ...')
|
||||||
current_time = datetime.now()
|
time.sleep(offset2)
|
||||||
f(*args)
|
print('We are about to run the sensor read again')
|
||||||
num_calls += 1
|
duration=f(*args)
|
||||||
difference = current_time - first_called
|
print('Duration of sequence was: {0}'.format(duration))
|
||||||
drift = difference - time_period* num_calls
|
offset = waitInterval - duration
|
||||||
print "drift=",drift
|
print('Running the loop again in {0} seconds'.format(offset))
|
||||||
|
offset2 = offset.total_seconds()
|
||||||
|
print('Running the loop again in {0} seconds'.format(offset2))
|
||||||
|
|
||||||
def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhumid):
|
def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhumid):
|
||||||
global oldtemp
|
global oldtemp
|
||||||
global oldhumid
|
global oldhumid
|
||||||
|
startTime=datetime.now()
|
||||||
|
print('This round of results started at {0}'.format(startTime))
|
||||||
for num in range(retries):
|
for num in range(retries):
|
||||||
try:
|
try:
|
||||||
t, h = dhtreader.read(dev_type, dhtpin)
|
t, h = dhtreader.read(dev_type, dhtpin)
|
||||||
@@ -67,7 +73,11 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum
|
|||||||
else:
|
else:
|
||||||
print("Failed to read from sensor, maybe try again?")
|
print("Failed to read from sensor, maybe try again?")
|
||||||
logging.warning('Failed to read from sensor, maybe try again?')
|
logging.warning('Failed to read from sensor, maybe try again?')
|
||||||
return 0
|
endTime=datetime.now()
|
||||||
|
print('This round of results ended at {0}'.format(endTime))
|
||||||
|
duration = endTime - startTime
|
||||||
|
print('Thiis round of results took {0} to complete'.format(duration))
|
||||||
|
return duration
|
||||||
|
|
||||||
DHT11 = 11
|
DHT11 = 11
|
||||||
DHT22 = 22
|
DHT22 = 22
|
||||||
|
|||||||
Reference in New Issue
Block a user