Skip to content

Commit

Permalink
Add support for veml6075 sensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmsoft-tuxicoman committed Jul 11, 2024
1 parent 8c5ecec commit 8fe0fa6
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions mqtt_io/modules/sensor/veml6075.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
VEML 6075 UV sensor
"""

from mqtt_io.types import ConfigType, SensorValueType
from . import GenericSensor
import logging

REQUIREMENTS = ("smbus2", "veml6075",)

CONFIG_SCHEMA = {
"i2c_bus_num": {"type": "integer", "required": True, "empty": False},
}


# UV COEFFICIENTS AND RESPONSIVITY
# See https://web.archive.org/web/20190416120825/http://www.vishay.com/docs/84339/designingveml6075.pdf
# For more details
##################################################################################
# Configuration # a # b # c # d # UVAresp # UVBresp #
##################################################################################
# No teflon (open air) # 2.22 # 1.33 # 2.95 # 1.74 # 0.001461 # 0.002591 #
# 0.1 mm teflon 4.5 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002303 # 0.004686 #
# 0.1 mm teflon 5.5 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002216 # 0.005188 #
# 0.1 mm teflon 10 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002681 # 0.004875 #
# 0.25 mm teflon 10 mm window # 2.22 # 1.33 # 2.95 # 1.74 # 0.002919 # 0.009389 #
# 0.4 mm teflon 10 mm window # 2.22 # 1.17 # 2.95 # 1.58 # 0.004770 # 0.006135 #
# 0.7 mm teflon 10 mm window # 2.22 # 1.17 # 2.95 # 1.58 # 0.007923 # 0.008334 #
# 1.0 mm teflon 5.5 mm window # 2.55 # 1.00 # 3.80 # 1.10 # 0.006000 # 0.003100 #
##################################################################################

_LOG = logging.getLogger(__name__)


class Sensor(GenericSensor):
"""
Implementation of Sensor class for the VEML 6075 UV sensor.
"""

SENSOR_SCHEMA = {
"alpha": {"type": "float", "required": False, "empty": False, "default": 1.0},
"beta": {"type": "float", "required": False, "empty": False, "default": 1.0},
"gamma": {"type": "float", "required": False, "empty": False, "default": 1.0},
"delta": {"type": "float", "required": False, "empty": False, "default": 1.0},
"a": {"type": "float", "required": False, "empty": False, "default": 2.22},
"b": {"type": "float", "required": False, "empty": False, "default": 1.33},
"c": {"type": "float", "required": False, "empty": False, "default": 2.95},
"d": {"type": "float", "required": False, "empty": False, "default": 1.74},
"UVAresp": {"type": "float", "required": False, "empty": False, "default": 0.001461},
"UVBresp": {"type": "float", "required": False, "empty": False, "default": 0.002591},
}

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,import-error
from smbus2 import SMBus # type: ignore
from veml6075 import VEML6075 # type: ignore

self.bus = SMBus(self.config["i2c_bus_num"])
self.sensor = VEML6075(i2c_dev=self.bus)


def calculate_uv_index(self, sens_conf, uva, uvb, uv_comp1, uv_comp2) -> float:

uva_calc = uva - ((sens_conf["a"] * sens_conf["alpha"] * uv_comp1) / sens_conf["gamma"]) - ((sens_conf["b"] * sens_conf["alpha"] * uv_comp2) / sens_conf["delta"])
_LOG.info("uva_calc " + str(uva_calc))
uvb_calc = uvb - ((sens_conf["c"] * sens_conf["beta"] * uv_comp1) / sens_conf["gamma"]) - ((sens_conf["d"] * sens_conf["beta"] * uv_comp2) / sens_conf["delta"])
_LOG.info("uvb_calc " + str(uvb_calc))
uva_index = uva_calc * (1 / sens_conf["alpha"]) * sens_conf["UVAresp"]
_LOG.info("uva_index " + str(uva_index))
uvb_index = uvb_calc * (1 / sens_conf["beta"]) * sens_conf["UVBresp"]
_LOG.info("uvb_index " + str(uvb_index))
uv_index = (uva_index + uvb_index) / 2
return uv_index

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
"""
Get the UV index from the sensor
"""
# Setup and turn on the sensor
self.sensor.set_shutdown(False)
self.sensor.set_high_dynamic_range(False)
self.sensor.set_integration_time('100ms')

# Fetch the values
uva, uvb = self.sensor.get_measurements()
uv_comp1, uv_comp2 = self.sensor.get_comparitor_readings()

# Turn off the sensor
self.sensor.set_shutdown(True)

# Calculate and return the UV index
return self.calculate_uv_index(sens_conf, uva, uvb, uv_comp1, uv_comp2)

0 comments on commit 8fe0fa6

Please sign in to comment.