Skip to content

Commit

Permalink
Add tsl2561 light sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
mschlenstedt committed Aug 2, 2024
1 parent 91cea1c commit a2fc96d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ Hardware support is provided by specific GPIO, Sensor and Stream modules. It's e
- DHT11/DHT22/AM2302 temperature and humidity sensors (`dht22`)
- DS18S20/DS1822/DS18B20/DS1825/DS28EA00/MAX31850K temperature sensors (`ds18b`)
- ENS160 digital multi-gas sensor with multiple IAQ data (TVOC, eCO2, AQI) (`ens160`)
- FREQUENCYCOUNTER Counts pulses from GPIOs and return the frequency in Hz (`frequencycounterr`)
- FLOWSENSOR generic flow rate sensor like YF-S201, YF-DN50 or others (`flowsensor`)
- HCSR04 ultrasonic range sensor (connected to the Raspberry Pi on-board GPIO) (`hcsr04`)
- INA219 DC current sensor (`ina219`)
- LM75 temperature sensor (`lm75`)
- MCP3008 analog to digital converter (`mcp3008`)
- ADXl345 3-axis accelerometer up to ±16g (`adxl345`)
- PMS5003 particulate sensor (`pms5003`)
- SHT40/SHT41/SHT45 temperature and humidity sensors (`sht4x`)
- TLSl2561 light level sensor (`tsl2561`)
- YF-S201 flow rate sensor (`yfs201`)
- FREQUENCYCOUNTER Counts pulses from GPIOs and return the frequency in Hz (frequencycounter)
- FLOWSENSOR generic flow rate sensor like YF-S201 or YF-DN50 (`flowsensor`)


### Streams
Expand Down
107 changes: 107 additions & 0 deletions mqtt_io/modules/sensor/tsl2561.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
TSL2561 luminosity sensor
"""
import threading

from typing import cast

from ...types import CerberusSchemaType, ConfigType, SensorValueType
from . import GenericSensor

REQUIREMENTS = ("adafruit-circuitpython-tsl2561",)
CONFIG_SCHEMA: CerberusSchemaType = {
"chip_addr": dict(type="integer", required=False, empty=False, default=0x48),
"integration_time": dict(
required=False,
empty=False,
allowed=[13.7, 101, 402],
default=101,
),
"gain": dict(
required=False,
empty=False,
allowed=[1, 16],
default=1,
),
}


class Sensor(GenericSensor):
"""
Implementation of Sensor class for the Adafruit_ADS1x15.
"""

SENSOR_SCHEMA: CerberusSchemaType = {
"type": dict(
type="string",
required=False,
empty=False,
allowed=["broadband", "infrared", "lux"],
default="lux",
),
}

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
# pylint: disable=import-error,no-member
import board # type: ignore
import busio # type: ignore
import adafruit_tsl2561
# Create the I2C bus
self.i2c = busio.I2C(board.SCL, board.SDA)

# Convert sensor address from hex to dec
self.address = 57
if self.config["chip_addr"] == 0x39:
self.address = 57
elif self.config["chip_addr"] == 0x49:
self.address = 73
elif self.config["chip_addr"] == 0x29:
self.address = 41

self.tsl = adafruit_tsl2561.TSL2561(self.i2c, self.address)


# Set gain 0=1x, 1=16x
if self.config["gain"] == 1:
self.tsl.gain = 0
elif self.config["gain"] == 16:
self.tsl.gain = 1

# Set integration time (0=13.7ms, 1=101ms, 2=402ms, or 3=manual)
if self.config["integration_time"] == 13.7:
self.tsl.integration_time = 0
elif self.config["integration_time"] == 101:
self.tsl.integration_time = 1
elif self.config["integration_time"] == 402:
self.tsl.integration_time = 2

self.tsl.enabled = True

#print("tsl2561 Enabled = {}".format(self.tsl.enabled))
#print("tsl2561 Gain = {}".format(self.tsl.gain))
#print("tsl2561 Integration time = {}".format(self.tsl.integration_time))

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
import time
"""
Get the values from the sensor
"""
self.tsl.enabled = True # Enable sensor
time.sleep(1)

sens_type = sens_conf["type"]
data = dict(
broadband=self.tsl.broadband,
infrared=self.tsl.infrared,
lux=self.tsl.lux
)
if data[sens_type] is None: # Possible sensor underrange or overrange.
data[sens_type] = -1

self.tsl.enabled = False # Disable for energy savings

return cast(
float,
data[sens_type],
)

0 comments on commit a2fc96d

Please sign in to comment.