add uv sensor veml6070

master
ademant 2019-06-18 06:48:23 +02:00
parent 62e9a8d7fd
commit 8c4b3afcbe
5 changed files with 149 additions and 0 deletions

View File

@ -25,6 +25,8 @@
"mcp9808": {"enable": 1},
"tsl2591":
{"enable": 1, "port": 1,"address": "0x29"},
"veml6070":
{"enable": 1, "port": 1},
"ads1x15":
{"enable": 1,"gain": 1,
"adc":{

13
demo_6070.py Normal file
View File

@ -0,0 +1,13 @@
import veml6070
if __name__ == '__main__':
veml = veml6070.Veml6070()
for i in [veml6070.INTEGRATIONTIME_1_2T,
veml6070.INTEGRATIONTIME_1T,
veml6070.INTEGRATIONTIME_2T,
veml6070.INTEGRATIONTIME_4T]:
veml.set_integration_time(i)
uv_raw = veml.get_uva_light_intensity_raw()
uv = veml.get_uva_light_intensity()
print "Integration Time setting %d: %f W/(m*m) from raw value %d" % (i, uv, uv_raw)

View File

@ -151,6 +151,26 @@ if "tsl2591" in log_conf:
channel_names.append("lux")
channel_info["lux"]={"sensor":"tsl2591","timestamp":0,"i2c":tsl_add}
# config of lux sensor tls2591
bveml=False
if "veml6070" in log_conf:
bveml=True
if "enable" in log_conf['veml6070']:
if log_conf['veml6070']['enable'] == 0:
bveml=False
if bveml:
import veml6070
veml_port=1
if "port" in log_conf['veml6070']:
veml_port=int(log_conf['veml6070']['port'])
try:
veml = veml6070.Veml6070(i2c_bus=veml_port)
except:
bveml=False
else:
channel_names.append("uv")
channel_info["uv"]={"sensor":"veml6070","timestamp":0,"i2c":0x38}
# config of bme280 sensor
# use pip install RPi.bme280 for library
bbme=False
@ -364,6 +384,12 @@ while a > 1:
if btls:
tsl_full,tsl_ir=tsl.get_full_luminosity()
ch_val[channel_names.index("lux")] = int(1000*tsl.calculate_lux(tsl_full,tsl_ir))
# get uv index
if bveml:
tsl_full,tsl_ir=tsl.get_full_luminosity()
veml.set_integration_time(veml6070.INTEGRATIONTIME_1T)
# uv_raw = veml.get_uva_light_intensity_raw()
ch_val[channel_names.index("uv")] = int(1000*veml.get_uva_light_intensity())
# read adc's
if badc:

3
veml6070/__init__.py Normal file
View File

@ -0,0 +1,3 @@
from .veml6070 import Veml6070
from .veml6070 import INTEGRATIONTIME_1_2T, INTEGRATIONTIME_1T, INTEGRATIONTIME_2T, INTEGRATIONTIME_4T, RSET_240K, RSET_270K, RSET_300K, RSET_600K

105
veml6070/veml6070.py Normal file
View File

@ -0,0 +1,105 @@
import time
import smbus # pylint: disable=import-error
ADDR_L = 0x38 # 7bit address of the VEML6070 (write, read)
ADDR_H = 0x39 # 7bit address of the VEML6070 (read)
RSET_240K = 240000
RSET_270K = 270000
RSET_300K = 300000
RSET_600K = 600000
SHUTDOWN_DISABLE = 0x00
SHUTDOWN_ENABLE = 0x01
INTEGRATIONTIME_1_2T = 0x00
INTEGRATIONTIME_1T = 0x01
INTEGRATIONTIME_2T = 0x02
INTEGRATIONTIME_4T = 0x03
class Veml6070(object):
def __init__(self, i2c_bus=1, sensor_address=ADDR_L, rset=RSET_270K, integration_time=INTEGRATIONTIME_1T):
self.bus = smbus.SMBus(i2c_bus)
self.sendor_address = sensor_address
self.rset = rset
self.shutdown = SHUTDOWN_DISABLE # before set_integration_time()
self.set_integration_time(integration_time)
self.disable()
def set_integration_time(self, integration_time):
self.integration_time = integration_time
self.bus.write_byte(self.sendor_address, self.get_command_byte())
# constant offset determined experimentally to allow sensor to readjust
time.sleep(0.2)
def get_integration_time(self):
return self.integration_time
def enable(self):
self.shutdown = SHUTDOWN_DISABLE
self.bus.write_byte(self.sendor_address, self.get_command_byte())
def disable(self):
self.shutdown = SHUTDOWN_ENABLE
self.bus.write_byte(self.sendor_address, self.get_command_byte())
def get_uva_light_intensity_raw(self):
self.enable()
# wait two times the refresh time to allow completion of a previous cycle with old settings (worst case)
time.sleep(self.get_refresh_time()*2)
msb = self.bus.read_byte(self.sendor_address+(ADDR_H-ADDR_L))
lsb = self.bus.read_byte(self.sendor_address)
self.disable()
return (msb << 8) | lsb
def get_uva_light_intensity(self):
uv = self.get_uva_light_intensity_raw()
return uv * self.get_uva_light_sensitivity()
def get_command_byte(self):
"""
assembles the command byte for the current state
"""
cmd = (self.shutdown & 0x01) << 0 # SD
cmd = (self.integration_time & 0x03) << 2 # IT
cmd = ((cmd | 0x02) & 0x3F) # reserved bits
return cmd
def get_refresh_time(self):
"""
returns time needed to perform a complete measurement using current settings (in s)
"""
case_refresh_rset = {
RSET_240K: 0.1,
RSET_270K: 0.1125,
RSET_300K: 0.125,
RSET_600K: 0.25
}
case_refresh_it = {
INTEGRATIONTIME_1_2T: 0.5,
INTEGRATIONTIME_1T: 1,
INTEGRATIONTIME_2T: 2,
INTEGRATIONTIME_4T: 4
}
return case_refresh_rset[self.rset] * case_refresh_it[self.integration_time]
def get_uva_light_sensitivity(self):
"""
returns UVA light sensitivity in W/(m*m)/step
"""
case_sens_rset = {
RSET_240K: 0.05,
RSET_270K: 0.05625,
RSET_300K: 0.0625,
RSET_600K: 0.125
}
case_sens_it = {
INTEGRATIONTIME_1_2T: 0.5,
INTEGRATIONTIME_1T: 1,
INTEGRATIONTIME_2T: 2,
INTEGRATIONTIME_4T: 4
}
return case_sens_rset[self.rset] / case_sens_it[self.integration_time]