snsary.contrib.adafruit.generic
Wrapper for most Adafruit sensor objects, which usually have one or more properties to retrieve the current values of the sensor. GenericSensor polls a given sensor object and publishes the latest value of each property as a Reading.
Most Adafruit sensors come with a driver written for CircuitPython, which can be used in pure Python via the Adafruit Blinka library. You will need to separately install the CircuitPython library for each sensor you want to scrape. For each sensor, you will then need to manually create and configure an instance of GenericSensor.
Example for the BH1750 light sensor:
# install the sensor library
pip3 install adafruit-circuitpython-bh1750
# create the sensor object
import board
i2c = board.I2C()
bh1750 = adafruit_bh1750.BH1750(i2c)
# prepare to poll / scrape
GenericSensor(bh1750)
Although GenericSensor will only scrape numerical values, these aren’t always wanted. For example, the Adafruit SCD30 sensor class exposes an unwanted self_calibration_enabled property. You can use Stream filters to clean up the output e.g.:
GenericSensor(scd30).stream.filter_names('CO2', 'temperature', 'relative_humidity')
Warning: the Adafruit I2C library does have a locking feature, but it’s not thread-safe. This means it’s possible for multiple sensors to conflict if they poll for data around the exact same time.
Module Contents
- class snsary.contrib.adafruit.generic.GenericSensor(device, period_seconds=10)
Bases:
snsary.sources.PollingSensor- property device
- property name
- ready(**kwargs)
Returns
Trueif the device is ready to be sampled, based on the kwargs tosample. The decision to sample could equally be made based on thedeviceitself. The default implementation returnsTrue.
- sample(**kwargs)