new file: c_code/th
new file: c_code/th.c new file: c_code/th.c-NOTES new file: c_code/thd new file: python_code/dhtreader.so new file: python_code/temp-humid-read-loop.py new file: python_code/temphumid.conf new file: python_code/updateMysql.py deleted: dhtreader.so deleted: temp-humid-read-loop.py deleted: temphumid.conf deleted: th.c deleted: th.c-NOTES deleted: thd deleted: updateMysql.py
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -0,0 +1,139 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
import sys
|
||||
import dhtreader
|
||||
import updateMysql
|
||||
import ConfigParser
|
||||
import time
|
||||
from threading import Timer,Thread,Event
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
from datetime import datetime
|
||||
|
||||
oldtemp = "NULL"
|
||||
oldhumid = "NULL"
|
||||
|
||||
def exec_every_n_seconds(n,f,*args):
|
||||
first_called=datetime.now()
|
||||
print args
|
||||
f(*args)
|
||||
num_calls=1
|
||||
drift=timedelta()
|
||||
time_period=timedelta(seconds=n)
|
||||
while 1:
|
||||
time.sleep(n-drift.microseconds/1000000.0)
|
||||
current_time = datetime.now()
|
||||
f(*args)
|
||||
num_calls += 1
|
||||
difference = current_time - first_called
|
||||
drift = difference - time_period* num_calls
|
||||
print "drift=",drift
|
||||
|
||||
def sensorRead(hwtype, pin, retries, timeout, maxtemp, mintemp, maxhumid, minhumid):
|
||||
global oldtemp
|
||||
global oldhumid
|
||||
for num in range(retries):
|
||||
try:
|
||||
t, h = dhtreader.read(dev_type, dhtpin)
|
||||
except:
|
||||
if ((num + 1) < retries):
|
||||
print('Exception detected! We will retry. Loop number: %d', num)
|
||||
logging.warning('Exception detected! We will retry. Loop number: %d', num)
|
||||
time.sleep(timeout)
|
||||
else:
|
||||
print('Exception detected - we are out of retries. Skipping the measurement in this cycle.')
|
||||
logging.error('Exception detected - we are out of retries. Skipping the measurement in this cycle.')
|
||||
else:
|
||||
if t and h:
|
||||
print(t, h)
|
||||
if (oldtemp != "NULL") and ((t - oldtemp < tempdiff) or (oldtemp - t < tempdiff)) and ((h - oldhumid < humiddiff) or (oldhumid - h < humiddif)):
|
||||
print('Current temperature close enough to previous temperature and previous temperature is not NULL, it is:')
|
||||
logging.debug('Current temperature close enough to previous temperature and previous temperature is not NULL, it is: %s', oldtemp)
|
||||
print oldtemp
|
||||
print('Current humidity close enough to previous humidity and previous humidity is not NULL, it is:')
|
||||
logging.debug('Current humidity close enough to previous humidity and previous humidity is not NULL, it is: %s', oldhumid)
|
||||
print oldhumid
|
||||
if (t < maxtemp) and (t > mintemp) and (h < maxhumid) and (h > minhumid):
|
||||
print('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)
|
||||
oldtemp=t
|
||||
oldhumid=h
|
||||
break
|
||||
else:
|
||||
print('Temperature {0} or humidity {1} is outside of allowable values - error! Check your configuration.'.format(t, h))
|
||||
logging.error('Temperature {0} or humidity {1} is outside of allowable values - error! Check your configuration.'.format(t, h))
|
||||
else:
|
||||
print("Failed to read from sensor, maybe try again?")
|
||||
logging.warning('Failed to read from sensor, maybe try again?')
|
||||
return 0
|
||||
|
||||
DHT11 = 11
|
||||
DHT22 = 22
|
||||
AM2302 = 22
|
||||
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read('temphumid.conf')
|
||||
hwtype=config.get('hardware', 'DHT')
|
||||
pin=config.get('hardware', 'PIN')
|
||||
retries=int(config.get('software', 'retries'))
|
||||
timeout=int(config.get('software', 'timeout'))
|
||||
interval=int(config.get('software', 'interval'))
|
||||
maxtemp=int(config.get('software', 'maxtemp'))
|
||||
mintemp=int(config.get('software', 'mintemp'))
|
||||
tempdiff=int(config.get('software', 'tempdiff'))
|
||||
maxhumid=int(config.get('software', 'maxhumid'))
|
||||
minhumid=int(config.get('software', 'minhumid'))
|
||||
humiddiff=int(config.get('software', 'humiddiff'))
|
||||
logfile=(config.get('software', 'logfile'))
|
||||
loglevel=(config.get('software', 'loglevel'))
|
||||
host=(config.get('database', 'host'))
|
||||
db=(config.get('database', 'db'))
|
||||
username=(config.get('database', 'username'))
|
||||
password=(config.get('database', 'password'))
|
||||
|
||||
|
||||
if loglevel == "debug":
|
||||
logging.basicConfig(filename = logfile, format='%(asctime)s %(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
|
||||
elif loglevel == "info":
|
||||
logging.basicConfig(filename = logfile, format='%(asctime)s %(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
|
||||
elif loglevel == "warn":
|
||||
logging.basicConfig(filename = logfile, format='%(asctime)s %(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.WARNING)
|
||||
elif loglevel == "error":
|
||||
logging.basicConfig(filename = logfile, format='%(asctime)s %(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.ERROR)
|
||||
else:
|
||||
logging.basicConfig(filename = logfile, format='%(asctime)s %(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.CRITICAL)
|
||||
|
||||
dhtreader.init()
|
||||
|
||||
dev_type = None
|
||||
if hwtype == "11":
|
||||
dev_type = DHT11
|
||||
print('Configured to use DHT11 device')
|
||||
logging.info('Configured to use DHT11 device')
|
||||
elif hwtype == "22":
|
||||
dev_type = DHT22
|
||||
print('Configured to use DHT22 device')
|
||||
logging.info('Configured to use DHT22 device')
|
||||
elif hwtype == "2302":
|
||||
dev_type = AM2302
|
||||
print('Configured to use AM 2302 device')
|
||||
logging.info('Configured to use AM2303 device')
|
||||
else:
|
||||
print('Invalid hardware type, only DHT11, DHT22 and AM 2302 are supported for now.!')
|
||||
logging.warn('Invalid hardware type, only DHT11, DHT22 and AM 2302 are supported for now.!')
|
||||
sys.exit(3)
|
||||
|
||||
dhtpin = int(pin)
|
||||
if dhtpin <= 0:
|
||||
print("Invalid GPIO pin#, correct your configuration file")
|
||||
logging.warn("Invalid GPIO pin#, correct your configuration file")
|
||||
sys.exit(3)
|
||||
|
||||
print("using pin #{0}".format(dhtpin))
|
||||
logging.info("using pin #{0}".format(dhtpin))
|
||||
|
||||
print('About to enter infinte loop ...')
|
||||
logging.info("About to enter infinite loop")
|
||||
exec_every_n_seconds(60,sensorRead,hwtype,pin,retries,timeout,maxtemp,mintemp,maxhumid,minhumid)
|
||||
@@ -0,0 +1,55 @@
|
||||
[hardware]
|
||||
## The hardare settings of the temperature humidity monitor
|
||||
# The hardware type - valid values are: 11 (DHT11 sensor), 22 (DHT22 sensor), and 2302 (AM2302 sensor)
|
||||
DHT=22
|
||||
|
||||
# The pin number used on the Raspberry Pi board
|
||||
PIN=4
|
||||
|
||||
[software]
|
||||
# The monitoring software will retry reading the temperature and humidity readings if the sensor returns an error. The number of retries is configured here. If no valid reading is made with all attempts, no reading will be recorded in this particular monitoring cycle.
|
||||
retries=3
|
||||
|
||||
# If the monitoring software returns an error from the temperature humidity sensor, it will wait before attempting to re-read the values. The timeout to wait between reading attempts is configured here. This value shoul dnot be set too low.
|
||||
timeout=5
|
||||
|
||||
# The interval between reading cycles is configured here. This is sually set to 1 minute (60 seconds).
|
||||
interval=60
|
||||
|
||||
# The maximum temperature that will be recorded by the monitoring software. Sometimes, the sensor can return an erroneous (too high) temperature reading. If the temperature returned is greater than this value, then it will be discarded, and the software will retry the sensor reading. If all readings are too high, no reading is recorded in this monitoring cycle.
|
||||
maxtemp=50
|
||||
|
||||
# The minimum temperature that will be recorded by the monitoring software. Sometimes, the sensor can return an erroneous (too low) temperature reading. If the temperature returned is less than this value, then it will be discarded, and the software will retry the sensor reading. If all readings are too low, no reading is recorded in this monitoring cycle.
|
||||
mintemp=15
|
||||
|
||||
# This is the maximum difference between the currently read temperature and the previously recorded temperature. If the difference is greater than this value, the reading is discarded and the software will retry the sensor reading. This should ensure that any erroneous readings are not used.
|
||||
tempdiff=10
|
||||
|
||||
# The maximum humidity that will be recorded by the monitoring software. Sometimes, the sensor can return an erroneous (too high) humidity reading. If the humidity returned is greater than this value, then it will be discarded, and the software will retry the sensor reading. If all readings are too high, no reading is recorded in this monitoring cycle.
|
||||
maxhumid=80
|
||||
|
||||
# The minimum humidity that will be recorded by the monitoring software. Sometimes, the sensor can return an erroneous (too low) humidity reading. If the humidity returned is less than this value, then it will be discarded, and the software will retry the sensor reading. If all readings are too low, no reading is recorded in this monitoring cycle.
|
||||
minhumid=20
|
||||
|
||||
# This is the maximum difference between the currently read humidity and the previously recorded humidity. If the difference is greater than this value, the reading is discarded and the software will retry the sensor reading. This should ensure that any erroneous readings are not used.
|
||||
humiddiff=10
|
||||
|
||||
# The name and location of the logfile
|
||||
logfile=/var/log/th-python.log
|
||||
|
||||
# The logging level (available: debug, info, warn, error, or critical). The default is critical.
|
||||
loglevel=debug
|
||||
|
||||
[database]
|
||||
## MySQL Database connectivity settings
|
||||
# The hostname of the MySQL database server
|
||||
host=localhost
|
||||
|
||||
# The name of the database
|
||||
db=Monitoring
|
||||
|
||||
# The username to us to connect to MySQL (should have INSERT rights on the database)
|
||||
username=USRENAME
|
||||
|
||||
# The password of the username above
|
||||
password=PASSWORD
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/python
|
||||
##
|
||||
## Script to import the temperature and humidity into a MySQL database
|
||||
##
|
||||
import sys
|
||||
import MySQLdb
|
||||
#import datetime
|
||||
#import time
|
||||
|
||||
def main(temp, humid, host, db, user, passwd):
|
||||
print('Temp = {0} *C, Hum = {1} %'.format(temp, humid))
|
||||
print('Updating MySQL database next ...')
|
||||
connection = MySQLdb.connect(host,user,passwd,db)
|
||||
cursor=connection.cursor()
|
||||
sqltemp=format(temp, '5.1f')
|
||||
sqlhumid=format(humid, '5.1f')
|
||||
print(sqltemp, sqlhumid)
|
||||
sql = """INSERT INTO TempHumid (ComputerTime, Temperature, Humidity, id) VALUES (unix_timestamp(now()), %s, %s, NULL)"""
|
||||
args = (sqltemp, sqlhumid)
|
||||
cursor.execute(sql, args)
|
||||
connection.commit()
|
||||
cursor.close()
|
||||
return 0
|
||||
Reference in New Issue
Block a user