Skip to content
Snippets Groups Projects
Commit 88dd16c4 authored by shbatm's avatar shbatm Committed by Ellis Percival
Browse files

Add module for BH1750 light level sensor (#72)

parent 1445844f
No related branches found
No related tags found
No related merge requests found
......@@ -95,3 +95,7 @@ ENV/
#Kate
*.kate-swp
# User's Config File
config.yml
......@@ -18,6 +18,7 @@ Sensors
- LM75 i2c temperature sensor (`lm75`)
- DHT11 DHT22 AM2302 temperature/humidity sensor (`dht22`)
- BH1750 light level sensor (`bh1750`)
Installation
------------
......@@ -136,6 +137,18 @@ sensor_inputs:
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
```
#### SSL/TLS
......
"""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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment