Restructuring
This commit is contained in:
parent
75d30731e4
commit
859b5dec67
28
__init__.py
28
__init__.py
@ -1,5 +1,33 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
DEBUG = False
|
||||||
|
|
||||||
logger_name = 'RPI_ENVSENS'
|
logger_name = 'RPI_ENVSENS'
|
||||||
logger = logging.getLogger(logger_name)
|
logger = logging.getLogger(logger_name)
|
||||||
|
|
||||||
|
|
||||||
|
class background_task(object):
|
||||||
|
RUN_SLEEP_TIME = 2.0
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.__active__ = True
|
||||||
|
self.__thread__ = threading.Thread(target=self.run, args=())
|
||||||
|
self.__thread__.daemon = True # Daemonize thread
|
||||||
|
self.__thread__.start() # Start the execution
|
||||||
|
|
||||||
|
def __run__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while self.__active__:
|
||||||
|
self.__run__()
|
||||||
|
time.sleep(self.RUN_SLEEP_TIME)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.__active__ = False
|
||||||
|
self.__thread__.join()
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.close()
|
||||||
|
BIN
_datasheets_/datasheet.pdf
Normal file
BIN
_datasheets_/datasheet.pdf
Normal file
Binary file not shown.
BIN
_datasheets_/schematic_ardu.pdf
Normal file
BIN
_datasheets_/schematic_ardu.pdf
Normal file
Binary file not shown.
BIN
_datasheets_/schematic_rpi.pdf
Normal file
BIN
_datasheets_/schematic_rpi.pdf
Normal file
Binary file not shown.
51
bmp.py
51
bmp.py
@ -1,46 +1,32 @@
|
|||||||
# Distributed with a free-will license.
|
|
||||||
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
|
|
||||||
# BMP180
|
|
||||||
# This code is designed to work with the BMP180_I2CS I2C Mini Module available from ControlEverything.com.
|
|
||||||
# https://www.controleverything.com/content/Pressure?sku=BMP180_I2CS#tabs-0-product_tabset-2
|
|
||||||
|
|
||||||
import smbus
|
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from . import logger
|
from . import logger, background_task, DEBUG
|
||||||
|
if not DEBUG:
|
||||||
|
import smbus
|
||||||
|
|
||||||
|
|
||||||
class bmp_180(object):
|
class bmp_180(background_task):
|
||||||
|
RUN_SLEEP_TIME = 0.5
|
||||||
|
SMBUS_DELAY = 0.5
|
||||||
|
MIN_REFRESH_RATE = RUN_SLEEP_TIME + 3 * SMBUS_DELAY
|
||||||
KEY_TEMPERATURE = 'temperature'
|
KEY_TEMPERATURE = 'temperature'
|
||||||
KEY_PRESSURE = 'pressure'
|
KEY_PRESSURE = 'pressure'
|
||||||
KEY_TIME = 'time'
|
KEY_TIME = 'time'
|
||||||
|
|
||||||
def __init__(self, data_callback=None):
|
def __init__(self, data_callback=None):
|
||||||
self.__data_callback__ = data_callback
|
self.__data_callback__ = data_callback
|
||||||
# Initial the dht device, with data pin connected to:
|
# Initialise background_task
|
||||||
self.__active__ = True
|
background_task.__init__(self)
|
||||||
self.__thread__ = threading.Thread(target=self.run, args=())
|
|
||||||
self.__thread__.daemon = True # Daemonize thread
|
|
||||||
self.__thread__.start() # Start the execution
|
|
||||||
|
|
||||||
def run(self):
|
def __run__(self):
|
||||||
while self.__active__:
|
|
||||||
data = self.__bmp_data_transmission__()
|
data = self.__bmp_data_transmission__()
|
||||||
if data is not None:
|
if data is not None:
|
||||||
logger.debug('BMP-Communication: Successfully: %s', repr(data))
|
logger.debug('BMP-Communication: Successfully: %s', repr(data))
|
||||||
if self.__data_callback__ is not None:
|
if self.__data_callback__ is not None:
|
||||||
self.__data_callback__(**data)
|
self.__data_callback__(**data)
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.__active__ = False
|
|
||||||
self.__thread__.join()
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
self.close()
|
|
||||||
|
|
||||||
def __bmp_data_transmission__(self):
|
def __bmp_data_transmission__(self):
|
||||||
|
if not DEBUG:
|
||||||
rv = {}
|
rv = {}
|
||||||
# Get I2C bus
|
# Get I2C bus
|
||||||
bus = smbus.SMBus(1)
|
bus = smbus.SMBus(1)
|
||||||
@ -78,14 +64,14 @@ class bmp_180(object):
|
|||||||
if MD > 32767 :
|
if MD > 32767 :
|
||||||
MD -= 65535
|
MD -= 65535
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(self.SMBUS_DELAY)
|
||||||
|
|
||||||
# BMP180 address, 0x77(119)
|
# BMP180 address, 0x77(119)
|
||||||
# Select measurement control register, 0xF4(244)
|
# Select measurement control register, 0xF4(244)
|
||||||
# 0x2E(46) Enable temperature measurement
|
# 0x2E(46) Enable temperature measurement
|
||||||
bus.write_byte_data(0x77, 0xF4, 0x2E)
|
bus.write_byte_data(0x77, 0xF4, 0x2E)
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(self.SMBUS_DELAY)
|
||||||
|
|
||||||
# BMP180 address, 0x77(119)
|
# BMP180 address, 0x77(119)
|
||||||
# Read data back from 0xF6(246), 2 bytes
|
# Read data back from 0xF6(246), 2 bytes
|
||||||
@ -100,14 +86,14 @@ class bmp_180(object):
|
|||||||
# 0x74(116) Enable pressure measurement, OSS = 1
|
# 0x74(116) Enable pressure measurement, OSS = 1
|
||||||
bus.write_byte_data(0x77, 0xF4, 0x74)
|
bus.write_byte_data(0x77, 0xF4, 0x74)
|
||||||
|
|
||||||
time.sleep(0.5)
|
time.sleep(self.SMBUS_DELAY)
|
||||||
|
|
||||||
# BMP180 address, 0x77(119)
|
# BMP180 address, 0x77(119)
|
||||||
# Read data back from 0xF6(246), 3 bytes
|
# Read data back from 0xF6(246), 3 bytes
|
||||||
# pres MSB1, pres MSB, pres LSB
|
# pres MSB1, pres MSB, pres LSB
|
||||||
data = bus.read_i2c_block_data(0x77, 0xF6, 3)
|
data = bus.read_i2c_block_data(0x77, 0xF6, 3)
|
||||||
|
|
||||||
rv[self.KEY_TIME] = time.time()
|
rv[self.KEY_TIME] = int(time.time())
|
||||||
|
|
||||||
# Convert the data
|
# Convert the data
|
||||||
pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
|
pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
|
||||||
@ -139,3 +125,10 @@ class bmp_180(object):
|
|||||||
rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
|
rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
|
||||||
|
|
||||||
return rv
|
return rv
|
||||||
|
else:
|
||||||
|
time.sleep(3 * self.SMBUS_DELAY)
|
||||||
|
return {
|
||||||
|
self.KEY_PRESSURE: 1717.,
|
||||||
|
self.KEY_TEMPERATURE: 17.17,
|
||||||
|
self.KEY_TIME: int(time.time())
|
||||||
|
}
|
||||||
|
40
dht.py
40
dht.py
@ -1,11 +1,14 @@
|
|||||||
import adafruit_dht
|
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from . import logger
|
from . import logger, background_task, DEBUG
|
||||||
|
|
||||||
|
if not DEBUG:
|
||||||
|
import adafruit_dht
|
||||||
|
|
||||||
|
|
||||||
class dht_22(object):
|
class dht_22(background_task):
|
||||||
|
RUN_SLEEP_TIME = 2.0
|
||||||
|
MIN_REFRESH_RATE = RUN_SLEEP_TIME
|
||||||
KEY_TEMPERATURE = 'temperature'
|
KEY_TEMPERATURE = 'temperature'
|
||||||
KEY_HUMIDITY = 'humidity'
|
KEY_HUMIDITY = 'humidity'
|
||||||
KEY_TIME = 'time'
|
KEY_TIME = 'time'
|
||||||
@ -13,43 +16,40 @@ class dht_22(object):
|
|||||||
def __init__(self, gpio, data_callback=None):
|
def __init__(self, gpio, data_callback=None):
|
||||||
self.__data_callback__ = data_callback
|
self.__data_callback__ = data_callback
|
||||||
# Initial the dht device, with data pin connected to:
|
# Initial the dht device, with data pin connected to:
|
||||||
|
if not DEBUG:
|
||||||
self.__dht_device__ = adafruit_dht.DHT22(gpio)
|
self.__dht_device__ = adafruit_dht.DHT22(gpio)
|
||||||
self.__active__ = True
|
# Initialise background_task
|
||||||
self.__thread__ = threading.Thread(target=self.run, args=())
|
background_task.__init__(self)
|
||||||
self.__thread__.daemon = True # Daemonize thread
|
|
||||||
self.__thread__.start() # Start the execution
|
|
||||||
|
|
||||||
def run(self):
|
def __run__(self):
|
||||||
while self.__active__:
|
|
||||||
data = self.__dht_data_transmission__()
|
data = self.__dht_data_transmission__()
|
||||||
if data is not None:
|
if data is not None:
|
||||||
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
||||||
if self.__data_callback__ is not None:
|
if self.__data_callback__ is not None:
|
||||||
self.__data_callback__(**data)
|
self.__data_callback__(**data)
|
||||||
time.sleep(2.0)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.__active__ = False
|
|
||||||
self.__thread__.join()
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
self.close()
|
|
||||||
|
|
||||||
def __dht_data_transmission__(self):
|
def __dht_data_transmission__(self):
|
||||||
|
if not DEBUG:
|
||||||
while self.__active__:
|
while self.__active__:
|
||||||
try:
|
try:
|
||||||
# Store the values
|
# Store the values
|
||||||
data = {}
|
data = {}
|
||||||
data[self.KEY_TEMPERATURE] = self.__dht_device__.temperature
|
data[self.KEY_TEMPERATURE] = self.__dht_device__.temperature
|
||||||
data[self.KEY_HUMIDITY] = self.__dht_device__.humidity
|
data[self.KEY_HUMIDITY] = self.__dht_device__.humidity
|
||||||
data[self.KEY_TIME] = time.time()
|
data[self.KEY_TIME] = int(time.time())
|
||||||
if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
|
if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
|
||||||
return data
|
return data
|
||||||
time.sleep(2.0)
|
time.sleep(2.0)
|
||||||
except RuntimeError as error:
|
except RuntimeError as error:
|
||||||
# Errors happen fairly often, DHT's are hard to read, just keep going
|
# Errors happen fairly often, DHT's are hard to read, just keep going
|
||||||
logger.debug('DHT-Communication: ' + error.args[0])
|
logger.info('DHT-Communication: ' + error.args[0])
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
self.__dht_device__.exit()
|
self.__dht_device__.exit()
|
||||||
raise error
|
raise error
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
self.KEY_TEMPERATURE: -17.17,
|
||||||
|
self.KEY_HUMIDITY: 1.7,
|
||||||
|
self.KEY_TIME: int(time.time()),
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user