Skip to content

Commit e4af383

Browse files
authored
Add VEML7700 Light Level Sensor (#398)
* Add VEML7700 Light Level Sensor * Set lux_corrected if < 1000
1 parent ad8514c commit e4af383

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Hardware support is provided by specific GPIO, Sensor and Stream modules. It's e
4848
- PMS5003 particulate sensor (`pms5003`)
4949
- SHT40/SHT41/SHT45 temperature and humidity sensors (`sht4x`)
5050
- TLSl2561 light level sensor (`tsl2561`)
51+
- VEML7700 light level sensor (`veml7700`)
5152
- YF-S201 flow rate sensor (`yfs201`)
5253

5354

mqtt_io/modules/sensor/veml7700.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
VEML7700 luminosity sensor
3+
"""
4+
from typing import cast
5+
6+
from ...types import CerberusSchemaType, ConfigType, SensorValueType
7+
from . import GenericSensor
8+
9+
REQUIREMENTS = ("adafruit-circuitpython-veml7700",)
10+
CONFIG_SCHEMA: CerberusSchemaType = {
11+
"chip_addr": {
12+
"type": 'integer',
13+
"required": False,
14+
"empty": False,
15+
"default": '0x10'},
16+
"integration_time": {
17+
"required": False,
18+
"empty": False,
19+
"allowed": [25, 50, 100, 200, 400, 800],
20+
"default": 25,
21+
},
22+
"gain": {
23+
"required": False,
24+
"empty": False,
25+
"allowed": [0.125, 0.25, 1, 2],
26+
"default": 0.125,
27+
},
28+
}
29+
30+
31+
class Sensor(GenericSensor):
32+
"""
33+
Implementation of Sensor class for the Adafruit_VEML7700
34+
"""
35+
36+
SENSOR_SCHEMA: CerberusSchemaType = {
37+
"type": {
38+
"type": 'string',
39+
"required": False,
40+
"empty": False,
41+
"allowed": ['light', 'lux', 'lux_corrected'],
42+
"default": 'lux_corrected',
43+
},
44+
}
45+
46+
def setup_module(self) -> None:
47+
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
48+
# pylint: disable=import-error,no-member
49+
import board # type: ignore
50+
import busio # type: ignore
51+
import adafruit_veml7700 # type: ignore
52+
# Create the I2C bus
53+
self.i2c = busio.I2C(board.SCL, board.SDA)
54+
55+
# Convert sensor address from hex to dec
56+
self.address = int(0x10)
57+
if self.config["chip_addr"]:
58+
self.address = int(self.config["chip_addr"])
59+
60+
self.veml7700 = adafruit_veml7700.VEML7700(self.i2c, self.address)
61+
62+
# Set gain
63+
gains = {
64+
"0.125": 'ALS_GAIN_1_8',
65+
"0.25": 'ALS_GAIN_1_4',
66+
"1": 'ALS_GAIN_1',
67+
"2": 'ALS_GAIN_2',
68+
}
69+
if 'gain' in self.config:
70+
self.veml7700.light_gain = getattr(self.veml7700, gains[str(self.config['gain'])])
71+
72+
# Set integration time
73+
ints = {
74+
"25": 'ALS_25MS',
75+
"50": 'ALS_50MS',
76+
"100": 'ALS_100MS',
77+
"200": 'ALS_200MS',
78+
"400": 'ALS_400MS',
79+
"800": 'ALS_800MS',
80+
}
81+
if 'integration_time' in self.config:
82+
self.veml7700.light_integration_time = getattr(self.veml7700,
83+
ints[str(self.config['integration_time'])])
84+
85+
#print("veml7700 Gain = {}".format(self.veml7700.light_gain))
86+
#print("veml7700 Integration time = {}".format(self.veml7700.light_integration_time))
87+
88+
def get_value(self, sens_conf: ConfigType) -> SensorValueType:
89+
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
90+
# pylint: disable=import-error,no-member
91+
sens_type = sens_conf["type"]
92+
data = {
93+
"light": self.veml7700.light,
94+
"lux": self.veml7700.lux,
95+
"lux_corrected": "-1",
96+
}
97+
98+
if data['lux'] > 1000:
99+
data['lux_corrected'] = (6.0135e-13 * data['lux'] ** 4) + \
100+
(-9.3924e-9 * data['lux'] ** 3) + \
101+
(8.1488e-5 * data['lux'] ** 2) + \
102+
(1.0023 * data['lux'])
103+
else:
104+
data['lux_corrected'] = data['lux']
105+
106+
return cast(
107+
float,
108+
data[sens_type],
109+
)

0 commit comments

Comments
 (0)