Adapters¶
Hardware adapter implementations for the MagnetometerPort protocol.
QMC5883L (Production)¶
gas2mqtt.adapters.qmc5883l
¶
QMC5883L magnetometer adapter — production I2C implementation.
Reads from the QMC5883L 3-axis digital magnetometer via the I2C bus using smbus2. The QMC5883L is a multi-chip module containing a 3-axis magnetic sensor with on-die temperature compensation.
I2C address: 0x0D (default for QMC5883L) Data register: 0x00 (9 bytes) Bytes 0-1: X axis (little-endian signed 16-bit) Bytes 2-3: Y axis (little-endian signed 16-bit) Bytes 4-5: Z axis (little-endian signed 16-bit) Byte 6: Status register Bytes 7-8: Temperature (little-endian signed 16-bit)
Configuration
Register 0x09: Control Register 1 Register 0x0B: SET/RESET Period Register
Note: Despite some legacy references to "HMC5883", the actual hardware is a QMC5883L — a lower-cost successor with a different register map.
Qmc5883lAdapter
¶
Qmc5883lAdapter(settings: Gas2MqttSettings)
Production adapter for the QMC5883L magnetometer over I2C.
Implements MagnetometerPort protocol. Reads magnetic field (X, Y, Z) and temperature from the QMC5883L via smbus2.
The QMC5883L data output register layout (starting at 0x00): Bytes 0-1: X axis (little-endian signed 16) Bytes 2-3: Y axis (little-endian signed 16) Bytes 4-5: Z axis (little-endian signed 16) Byte 6: Status register Bytes 7-8: Temperature (little-endian signed 16)
Source code in packages/src/gas2mqtt/adapters/qmc5883l.py
initialize
¶
Open I2C bus and configure the QMC5883L.
Control Register 1 (0x09): 0b11010001 - Continuous measurement mode (bits 0-1: 01) - Output data rate: 10 Hz (bits 2-3: 00) - Full scale range: 8 Gauss (bits 4-5: 01) - Over sample ratio: 64 (bits 6-7: 11)
SET/RESET Period Register (0x0B): 0b00000001 - Recommended value per datasheet.
Source code in packages/src/gas2mqtt/adapters/qmc5883l.py
read
¶
read() -> MagneticReading
Read magnetic field (X, Y, Z) and temperature from the QMC5883L.
Reads 9 bytes from register 0x00: X, Y, Z axes (little-endian) plus status byte and temperature (little-endian).
Returns:
| Type | Description |
|---|---|
MagneticReading
|
MagneticReading with bx, by, bz, and temperature_raw. |
Raises:
| Type | Description |
|---|---|
IOError
|
If I2C communication fails. |
RuntimeError
|
If the bus has not been initialized. |
Source code in packages/src/gas2mqtt/adapters/qmc5883l.py
close
¶
__aenter__
async
¶
Enter async context: initialize the sensor.
Enables cosalette 0.1.5 adapter lifecycle management.
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit async context: close the I2C bus.
Fake (Test/Dry-Run)¶
gas2mqtt.adapters.fake
¶
Fake magnetometer adapter for testing and dry-run mode.
Provides deterministic readings for unit tests and --dry-run operation. The bz value can be controlled to simulate trigger events.
FakeMagnetometer
¶
Test double for MagnetometerPort.
Returns configurable, deterministic readings. Useful for: - Unit tests (set specific bz values to test Schmitt trigger) - Dry-run mode (returns safe default values)
Attributes:
| Name | Type | Description |
|---|---|---|
bx |
int
|
Configurable X-axis value (default 0). |
by |
int
|
Configurable Y-axis value (default 0). |
bz |
int
|
Configurable Z-axis value (default 0). |
temperature_raw |
int
|
Configurable raw temperature value (default 0). |
initialized |
bool
|
Whether initialize() has been called. |
closed |
bool
|
Whether close() has been called. |
Source code in packages/src/gas2mqtt/adapters/fake.py
initialize
¶
close
¶
__aenter__
async
¶
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Exit async context: close the fake sensor.