modified: temp-humid-read-loop.py
modified: temp-humid-read-single.py modified: temphumid.conf modified: updateMysql.py
This commit is contained in:
Regular → Executable
+4
-2
@@ -56,7 +56,7 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum
|
|||||||
logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid)
|
logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid)
|
||||||
if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid):
|
if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid):
|
||||||
logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid))
|
logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid))
|
||||||
updateMysql.main(t, h, host, db, username, password, logging)
|
updateMysql.main(t, h, host, db, username, password, logging, sql_retries, sql_timeout)
|
||||||
oldtemp=t
|
oldtemp=t
|
||||||
oldhumid=h
|
oldhumid=h
|
||||||
break
|
break
|
||||||
@@ -75,7 +75,7 @@ DHT22 = 22
|
|||||||
AM2302 = 22
|
AM2302 = 22
|
||||||
|
|
||||||
config = ConfigParser.ConfigParser()
|
config = ConfigParser.ConfigParser()
|
||||||
config.read('temphumid.conf')
|
config.read('/etc/thMonitor.conf')
|
||||||
hwtype=config.get('hardware', 'DHT')
|
hwtype=config.get('hardware', 'DHT')
|
||||||
pin=config.get('hardware', 'PIN')
|
pin=config.get('hardware', 'PIN')
|
||||||
retries=int(config.get('software', 'retries'))
|
retries=int(config.get('software', 'retries'))
|
||||||
@@ -93,6 +93,8 @@ host=(config.get('database', 'host'))
|
|||||||
db=(config.get('database', 'db'))
|
db=(config.get('database', 'db'))
|
||||||
username=(config.get('database', 'username'))
|
username=(config.get('database', 'username'))
|
||||||
password=(config.get('database', 'password'))
|
password=(config.get('database', 'password'))
|
||||||
|
sql_retries=int((config.get('database', 'sql_retries')))
|
||||||
|
sql_timeout=(config.get('database', 'sql_timeout'))
|
||||||
|
|
||||||
|
|
||||||
if loglevel == "debug":
|
if loglevel == "debug":
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhum
|
|||||||
logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid)
|
logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid)
|
||||||
if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid):
|
if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid):
|
||||||
logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid))
|
logging.debug('Temperature is less than {0} and greater than {1}, humidity is less than {2} and greater than {3}'.format(maxtemp,mintemp,maxhumid,minhumid))
|
||||||
updateMysql.main(t, h, host, db, username, password, logging)
|
updateMysql.main(t, h, host, db, username, password, logging, sql_retries, sql_timeout)
|
||||||
oldtemp=t
|
oldtemp=t
|
||||||
oldhumid=h
|
oldhumid=h
|
||||||
break
|
break
|
||||||
@@ -67,6 +67,8 @@ host=(config.get('database', 'host'))
|
|||||||
db=(config.get('database', 'db'))
|
db=(config.get('database', 'db'))
|
||||||
username=(config.get('database', 'username'))
|
username=(config.get('database', 'username'))
|
||||||
password=(config.get('database', 'password'))
|
password=(config.get('database', 'password'))
|
||||||
|
sql_retries=int((config.get('database', 'sql_retries')))
|
||||||
|
sql_timeout=(config.get('database', 'sql_timeout'))
|
||||||
|
|
||||||
|
|
||||||
if loglevel == "debug":
|
if loglevel == "debug":
|
||||||
|
|||||||
@@ -53,3 +53,9 @@ username=USRENAME
|
|||||||
|
|
||||||
# The password of the username above
|
# The password of the username above
|
||||||
password=PASSWORD
|
password=PASSWORD
|
||||||
|
|
||||||
|
# This is the maximum number of retries made to update the MySQL database with the data. If these all fail, the failure is recorded, but the daemon should continue to run
|
||||||
|
sql_retries=3
|
||||||
|
|
||||||
|
# This is the timeout between retries when updating the MySQL database. It is useful to set this parameter and sql_retries parameter, as the thMonitor daemon frequently starts up before the MySQL daemon has finished starting.
|
||||||
|
sql_timeout=5
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
|
|
||||||
def main(temp, humid, host, db, user, passwd, logging):
|
def main(temp, humid, host, db, user, passwd, logging, sql_retries, sql_timeout):
|
||||||
logging.debug('Temp = {0} *C, Hum = {1} %'.format(temp, humid))
|
logging.debug('Temp = {0} *C, Hum = {1} %'.format(temp, humid))
|
||||||
logging.debug('Updating MySQL database next ...')
|
logging.debug('Updating MySQL database next ...')
|
||||||
connection = MySQLdb.connect(host,user,passwd,db)
|
connection = MySQLdb.connect(host,user,passwd,db)
|
||||||
@@ -15,7 +15,20 @@ def main(temp, humid, host, db, user, passwd, logging):
|
|||||||
logging.info('Temperature: {0}*C Humidity: {1}% ... updating to MySQL database'.format(sqltemp, sqlhumid))
|
logging.info('Temperature: {0}*C Humidity: {1}% ... updating to MySQL database'.format(sqltemp, sqlhumid))
|
||||||
sql = """INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) VALUES (unix_timestamp(now()), %s, %s, NULL)"""
|
sql = """INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) VALUES (unix_timestamp(now()), %s, %s, NULL)"""
|
||||||
args = (sqltemp, sqlhumid)
|
args = (sqltemp, sqlhumid)
|
||||||
cursor.execute(sql, args)
|
|
||||||
connection.commit()
|
success = False
|
||||||
cursor.close()
|
attempts = 0
|
||||||
|
while attempts < sql_retries and not success:
|
||||||
|
try:
|
||||||
|
cursor.execute(sql, args)
|
||||||
|
connection.commit()
|
||||||
|
cursor.close()
|
||||||
|
success = True
|
||||||
|
logging.debug('The MySQL database was successfully updated.')
|
||||||
|
except MySQLdb.Error, e:
|
||||||
|
logging.warn('The MySQL database could not be updated and returned the following error %d: %s', (e.args[0], e.args[1]))
|
||||||
|
attempts += 1
|
||||||
|
if attempts == sql_retries:
|
||||||
|
logging.error('All configured attempts to update the MySQL database have failed. We are going to skip this attempt.')
|
||||||
|
time.sleep(sql_interval)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user