|
| 1 | +""" |
| 2 | +VEML 6075 UV sensor |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from mqtt_io.types import ConfigType, SensorValueType |
| 7 | +from . import GenericSensor |
| 8 | + |
| 9 | +REQUIREMENTS = ("smbus2", "veml6075",) |
| 10 | + |
| 11 | +CONFIG_SCHEMA = { |
| 12 | + "i2c_bus_num": {"type": "integer", "required": True, "empty": False}, |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +# UV COEFFICIENTS AND RESPONSIVITY |
| 17 | +# More details here : |
| 18 | +# https://web.archive.org/web/20190416120825/http://www.vishay.com/docs/84339/designingveml6075.pdf |
| 19 | +# For more details |
| 20 | +################################################################################## |
| 21 | +# Configuration # a # b # c # d # UVAresp # UVBresp # |
| 22 | +################################################################################## |
| 23 | +# No teflon (open air) # 2.22 # 1.33 # 2.95 # 1.74 # 0.001461 # 0.002591 # |
| 24 | +# 0.1 mm teflon 4.5 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002303 # 0.004686 # |
| 25 | +# 0.1 mm teflon 5.5 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002216 # 0.005188 # |
| 26 | +# 0.1 mm teflon 10 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002681 # 0.004875 # |
| 27 | +# 0.25 mm teflon 10 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002919 # 0.009389 # |
| 28 | +# 0.4 mm teflon 10 mm window # 2.22 # 1.17 # 2.95 # 1.58 # 0.004770 # 0.006135 # |
| 29 | +# 0.7 mm teflon 10 mm window # 2.22 # 1.17 # 2.95 # 1.58 # 0.007923 # 0.008334 # |
| 30 | +# 1.0 mm teflon 5.5 mm window # 2.55 # 1.00 # 3.80 # 1.10 # 0.006000 # 0.003100 # |
| 31 | +################################################################################## |
| 32 | + |
| 33 | +_LOG = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class Sensor(GenericSensor): |
| 37 | + """ |
| 38 | + Implementation of Sensor class for the VEML 6075 UV sensor. |
| 39 | + """ |
| 40 | + |
| 41 | + SENSOR_SCHEMA = { |
| 42 | + "a": {"type": "float", "required": False, "empty": False, "default": 2.22}, |
| 43 | + "b": {"type": "float", "required": False, "empty": False, "default": 1.33}, |
| 44 | + "c": {"type": "float", "required": False, "empty": False, "default": 2.95}, |
| 45 | + "d": {"type": "float", "required": False, "empty": False, "default": 1.74}, |
| 46 | + "UVAresp": {"type": "float", "required": False, "empty": False, "default": 0.001461}, |
| 47 | + "UVBresp": {"type": "float", "required": False, "empty": False, "default": 0.002591}, |
| 48 | + } |
| 49 | + |
| 50 | + def setup_module(self) -> None: |
| 51 | + # pylint: disable=import-outside-toplevel,import-error |
| 52 | + from smbus2 import SMBus # type: ignore |
| 53 | + from veml6075 import VEML6075 # type: ignore |
| 54 | + |
| 55 | + self.bus = SMBus(self.config["i2c_bus_num"]) |
| 56 | + self.sensor = VEML6075(i2c_dev=self.bus) |
| 57 | + self.sensor.set_shutdown(True) |
| 58 | + self.sensor.set_high_dynamic_range(False) |
| 59 | + self.sensor.set_integration_time('100ms') |
| 60 | + self.sensor.set_shutdown(False) |
| 61 | + |
| 62 | + |
| 63 | + def calculate_uv_index(self, sens_conf: ConfigType, \ |
| 64 | + uva: float, uvb: float, uv_comp1: float, uv_comp2: float) -> float: |
| 65 | + |
| 66 | + """ |
| 67 | + Calculate the UV index from received values. |
| 68 | + """ |
| 69 | + |
| 70 | + _LOG.debug("UVA: %f UVB: %f UV_comp1: %f UV_comp2: %f)", uva, uvb, uv_comp1, uv_comp2) |
| 71 | + uva_calc = uva - (sens_conf["a"] * uv_comp1) - (sens_conf["b"] * uv_comp2) |
| 72 | + uvb_calc = uvb - (sens_conf["c"] * uv_comp1) - (sens_conf["d"] * uv_comp2) |
| 73 | + _LOG.debug("uva_calc: %f uvb_calc: %f", uva_calc, uvb_calc) |
| 74 | + uva_index = uva_calc * sens_conf["UVAresp"] |
| 75 | + uvb_index = uvb_calc * sens_conf["UVBresp"] |
| 76 | + _LOG.debug("uva_index: %f uvb_index: %f", uva_index, uvb_index) |
| 77 | + uv_index: float = (uva_index + uvb_index) / 2.0 |
| 78 | + return uv_index |
| 79 | + |
| 80 | + def get_value(self, sens_conf: ConfigType) -> SensorValueType: |
| 81 | + """ |
| 82 | + Get the UV index from the sensor |
| 83 | + """ |
| 84 | + |
| 85 | + # Fetch the values |
| 86 | + uva, uvb = self.sensor.get_measurements() |
| 87 | + uv_comp1, uv_comp2 = self.sensor.get_comparitor_readings() |
| 88 | + |
| 89 | + # Calculate and return the UV index |
| 90 | + return self.calculate_uv_index(sens_conf, uva, uvb, uv_comp1, uv_comp2) |
0 commit comments