diff --git a/.gitignore b/.gitignore index 93101ef487aa70257b40e0166ea8d88d5e06f1ba..0590c1fb9bf72c3d3e248b5ceb5a676236656171 100644 --- a/.gitignore +++ b/.gitignore @@ -96,3 +96,7 @@ ENV/ #Kate *.kate-swp + +# User's Config File +config.yml + diff --git a/README.md b/README.md index a0000c7707fb712d76535dee180fb1efbb5dbc9c..7dcb47211696d147ace3a674f9c630188e59097f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Sensors - LM75 i2c temperature sensor (`lm75`) - DHT11 DHT22 AM2302 temperature/humidity sensor (`dht22`) +- BH1750 light level sensor (`bh1750`) Installation ------------ @@ -135,6 +136,18 @@ sensor_inputs: interval: 10 #interval in seconds, that a value is read from the sensor and a update is published digits: 4 # number of digits to be round type: humidity # Can be temperature or humidity + +sensor_modules: + - name: bh1750 + module: bh1750 + i2c_bus_num: 1 + chip_addr: 0x23 + +sensor_inputs: + - name: bh1750_lux + module: bh1750 + interval: 10 + digits: 2 ``` diff --git a/pi_mqtt_gpio/modules/bh1750.py b/pi_mqtt_gpio/modules/bh1750.py new file mode 100644 index 0000000000000000000000000000000000000000..abec28dda4ead9be974572f6053e42113b2a285d --- /dev/null +++ b/pi_mqtt_gpio/modules/bh1750.py @@ -0,0 +1,51 @@ +"""BH1750 Light Intensity Sensor.""" +from pi_mqtt_gpio.modules import GenericSensor + + +CONFIG_SCHEMA = { + "i2c_bus_num": {"type": "integer", "required": True, "empty": False}, + "chip_addr": {"type": "integer", "required": True, "empty": False}, +} + +# Define some constants from the datasheet +DEVICE = 0x23 # Default device I2C address +POWER_DOWN = 0x00 # No active state +POWER_ON = 0x01 # Power on +RESET = 0x07 # Reset data register value +# Start measurement at 4lx resolution. Time typically 16ms. +CONTINUOUS_LOW_RES_MODE = 0x13 +# Start measurement at 1lx resolution. Time typically 120ms +CONTINUOUS_HIGH_RES_MODE_1 = 0x10 +# Start measurement at 0.5lx resolution. Time typically 120ms +CONTINUOUS_HIGH_RES_MODE_2 = 0x11 +# Start measurement at 1lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_HIGH_RES_MODE_1 = 0x20 +# Start measurement at 0.5lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_HIGH_RES_MODE_2 = 0x21 +# Start measurement at 1lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_LOW_RES_MODE = 0x23 + + +class Sensor(GenericSensor): + """Implementation of Sensor class for the BH1750 light sensor.""" + + def __init__(self, config): + """Initialize sensor class.""" + import smbus + + self.bus = smbus.SMBus(config["i2c_bus_num"]) + self.address = config["chip_addr"] + + def setup_sensor(self, config): + """Perform setup for the sensor.""" + return True # nothing to do here + + def get_value(self, config): + """Get the light value from the sensor.""" + value = self.bus.read_i2c_block_data(self.address, + ONE_TIME_HIGH_RES_MODE_1) + value = (value[1] + (256 * value[0])) / 1.2 + return value