sensor.mcp3008
#
- class platypush.plugins.sensor.mcp3008.MCP3008Mode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
Enum
MPC3008 mode enum (
hardware
orsoftware
).
- class platypush.plugins.sensor.mcp3008.SensorMcp3008Plugin(*_, **__)[source]#
Bases:
SensorPlugin
Plugin to read analog sensor values from an MCP3008 chipset. The MCP3008 chipset is a circuit that allows you to read measurements from multiple analog sources (e.g. sensors) and multiplex them to a digital device like a Raspberry Pi or a regular laptop. See https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008 for more info.
Requires:
adafruit-mcp3008
(pip install adafruit-mcp3008
)
Triggers:
- __init__(CLK=None, MISO=None, MOSI=None, CS=None, spi_port=None, spi_device=None, channels=None, Vdd=3.3, **kwargs)[source]#
The MCP3008 can be connected in two modes:
- Hardware SPI mode: advised if you have enough GPIO pins available
(and slightly faster)
- Software SPI mode: useful if you don’t have all the required GPIO
PINs for hardware SPI available. Slightly slower, as the conversion is done via software, but still relatively performant.
See https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008#wiring for info
- Parameters:
CLK (int) – (software SPI mode) CLK GPIO PIN
MISO (int) – (software SPI mode) MISO GPIO PIN
MOSI (int) – (software SPI mode) MOSI GPIO PIN
CS (int) – (software SPI mode) CS GPIO PIN
spi_port (int) – (hardware SPI mode) SPI port
spi_device (str) – (hardware SPI mode) SPI device name
channels (dict) –
name-value mapping between MCP3008 output PINs and sensor names. This mapping will be used when you get values through
get_measurement()
. Example:channels = { "0": { "name": "temperature", "conv_function": 'round(x*100.0, 2)' # T = Vout / (10 [mV/C]) }, "1": { "name": "light", # ALS-PT9 "conv_function": 'round(x*1000.0, 6)' # ALS-PT9 has a 10 kOhm resistor } }
Note that you can also pass a conversion function as
conv_function
that will convert the output voltage to whichever human-readable value you wish. In the case above I connected a simple temperature sensor to the channel 0 and a simple ALS-PT9 light sensor to the channel 1, and passed the appropriate conversion functions to convert from voltage to, respectively, temperature in Celsius degrees and light intensity in lumen. Note that we reference the current voltage asx
inconv_function
.Vdd (float) – Input voltage provided to the circuit (default: 3.3V, Raspberry Pi default power source)
- get_measurement()[source]#
Returns a measurement from the sensors connected to the MCP3008 device. If channels were passed to the configuration, the appropriate sensor names will be used and the voltage will be converted through the appropriate conversion function. Example:
output = { "temperature": 21.0, # Celsius "humidity": 45.1 # % }
Otherwise, the output dictionary will contain the channel numbers as key and the row voltage (between 0 and 255) will be returned. Example:
output = { "0": 145, "1": 130 }
- transform_entities(entities: Dict[str, float | int]) List[Device] [source]#
This method takes a list of entities in any (plugin-specific) format and converts them into a standardized collection of Entity objects. Since this method is called by
publish_entities()
before entity updates are published, you may usually want to extend it to pre-process the entities managed by your extension into the standard format before they are stored and published to all the consumers.