Ejemplo n.º 1
0
def get_lux_readings(logger):
    tsl_dict = {}
    lux_dict = {}
    try:
        i2c = busio.I2C(board.SCL, board.SDA)
        tca = adafruit_tca9548a.TCA9548A(i2c)
    except Exception as e:
        logger.error("error at initiating i2c devices :{}".format(str(e)))
        return lux_dict
    for i in range(8):
        try:
            tsl_dict["tsl_{}".format(i)] = adafruit_bh1750.BH1750(tca[i])
        except Exception as e:
            logger.error(
                "error in initiating lux sensor at {}th pin :{}".format(
                    i, str(e)))
            pass
    for k, tsl in tsl_dict.items():
        try:
            lux_dict[k] = tsl.lux
        except Exception as e:
            logger.error("error in reading lux at {}th sensor :{}".format(
                i, str(e)))
            lux_dict[k] = -1
    return lux_dict
Ejemplo n.º 2
0
 def _init_sensor(self):
     """
     Initialize sensor (simply save the module), no complicated init needed.
     """
     import adafruit_bh1750
     import board
     i2c = board.I2C()
     self._sensor_driver = adafruit_bh1750.BH1750(i2c)
Ejemplo n.º 3
0
 def __init__(self, scl: _LsPin, sda: _LsPin) -> None:
     try:
         import adafruit_bh1750
     except ImportError:
         raise RuntimeError(
             'Adafruit_BH1750 module is not installed. Run `apt install python3-adafruit-circuitpython-bh1750` as root to install.'
         ) from ImportError
     self._sensor = adafruit_bh1750.BH1750(
         _busio.I2C(scl=DigitalInputOutput.lspin_to_libgpiod_pin(scl),
                    sda=DigitalInputOutput.lspin_to_libgpiod_pin(sda)))
Ejemplo n.º 4
0
 def read_bh1750(self):
     """
     @brief Read bh1750 raspberry pi i2c bus
     Get lux
     """
     try:
         with I2C(self._bus) as i2c:
             sensor = adafruit_bh1750.BH1750(i2c, address=self._addr)
             lux = sensor.lux
             if self._debug:
                 print("Light: %.3f lx" % lux)
             return lux
     except Exception as e:
         print("cannot read bh1750, An exception occurred: {}".format(e))
async def read_sensors(n_try=1, time_sleep=.5):
    results = {}
    await sleep(time_sleep)
    try:
        i2c = busio.I2C(board.SCL, board.SDA, frequency=10000)
        ads0 = ADS.ADS1115(i2c, address=const(0x48))
        ads1 = ADS.ADS1115(i2c, address=const(0x49))
        air_meter = adafruit_am2320.AM2320(i2c)
        light_meter = adafruit_bh1750.BH1750(i2c)

        soil_sensors = [AnalogIn(ads0, i) for i in range(4)]
        soil_sensors.append(AnalogIn(ads1, 0))
        for i, sensor in enumerate(soil_sensors):
            results[f'soil_sensor_{i}'] = sensor.value
        results['air_temp'] = air_meter.temperature * (9 / 5) + 32
        results['air_humidity'] = air_meter.relative_humidity
        results['light_sensor'] = light_meter.lux
    except Exception as e:
        logger.error(e)
        if n_try > 3:
            return {}
        return await read_sensors(n_try=n_try + 1)
    return results
Ejemplo n.º 6
0
    def __init__(self, i2c=None):
        super().__init__('rtf_bh1750')

        if i2c is None:
            self.i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
        else:
            self.i2c = i2c

        self.sensor = adafruit_bh1750.BH1750(i2c)
        # resolution: LOW(4 lx), MEDIUM(1 lx), HIGH (0.5 lx)
        self.sensor.resolution = adafruit_bh1750.Resolution.HIGH

        # at high and medium, it can take <= 180msec to collect data
        self.timer = self.create_timer(Rate.hertz_5, self.callback)
        # self.timer = self.create_timer(0.2, self.callback)

        self.pub = self.create_publisher(Illuminance, 'illuminance', 10)
        # self.pub_pressure = self.create_publisher(FluidPressure, 'pressure', 10)

        frame_id = self.declare_parameter('frame_id', "base_imu_link").value

        self.msg = Illuminance()
        self.msg.header.frame_id = frame_id
        self.msg.variance = 0.01
Ejemplo n.º 7
0
import adafruit_bh1750
import board
from ISStreamer.Streamer import Streamer

# --------- User Settings ---------
BUCKET_NAME = "INSERT BUCKET NAME HERE"
BUCKET_KEY = "INSERT BUCKET KEY HERE"
ACCESS_KEY = "INSERT ACCESS KEY HERE"
MINUTES_BETWEEN_READS = 1
# ---------------------------------

# BH1750 settings
i2c = board.I2C()
sensor = adafruit_bh1750.BH1750(i2c)
sensor.resolution = 1

# Initial State settings
streamer = Streamer(bucket_name=BUCKET_NAME,
                    bucket_key=BUCKET_KEY,
                    access_key=ACCESS_KEY)

previousReading = 0

while True:

    if (sensor.lux > 5 and previousReading == 0):
        previousReading = 1
        print("high prev reading", str(previousReading))
        print(sensor.lux)
        streamer.log("Lux", 1)
        streamer.flush()
Ejemplo n.º 8
0
import adafruit_fona.adafruit_fona_network as network
import adafruit_fona.adafruit_fona_socket as cellular_socket
from secrets import secrets
print("V1")
JSON_URL = "http://www.snerz.at/iot/index.php?light=222"

# Create a serial connection for the FONA connection
uart = busio.UART(board.GP4, board.GP5)
rst = digitalio.DigitalInOut(board.GP3)

analog_in = AnalogIn(board.A3)

print("Create Sens!")
i2c = busio.I2C(scl=board.GP21, frequency=50000, sda=board.GP20)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
lux_sens = adafruit_bh1750.BH1750(i2c)
pth_sens = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)

start = time.monotonic()
display.fill(0)
display.show()

display.show()

pth_sens.sea_level_pressure = 1013.25

#while True:
now = time.monotonic()
ft = now - start
start = time.monotonic()
voltage = round(analog_in.value / 6553.6, 3)
Ejemplo n.º 9
0
def get_lux_readings():
    i2c = busio.I2C(board.SCL, board.SDA)
    sensor = adafruit_bh1750.BH1750(i2c)
    return {"tsl_9": sensor.lux}