Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new module for sensor adxl345 #223

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Hardware support is provided by specific GPIO, Sensor and Stream modules. It's e
- HCSR04 ultrasonic range sensor (connected to the Raspberry Pi on-board GPIO) (`hcsr04`)
- LM75 temperature sensor (`lm75`)
- MCP3008 analog to digital converter (`mcp3008`)
- ADXl345 3-axis accelerometer up to ±16g (`adxl345`)

### Streams

Expand Down Expand Up @@ -111,4 +112,4 @@ stream_modules:
module: serial
device: /dev/ttyUSB0
baud: 9600
```
```
68 changes: 68 additions & 0 deletions mqtt_io/modules/sensor/adxl345.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
ADXL345 Digital Accelerometer Sensor

Mandatory:
- chip_addr

Optional:
- output_g (set True if output in g). default:m*s²

Output:
- x (in m*s²)
- y (in m*s²)
- z (in m*s²)
"""

from json import dumps
from typing import cast

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

REQUIREMENTS = ("adxl345",)
CONFIG_SCHEMA: CerberusSchemaType = {
"chip_addr": dict(type="integer", required=True, empty=False),
"output_g": dict(type="boolean", required=False, empty=False),
}


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

SENSOR_SCHEMA: CerberusSchemaType = {
"type": dict(
type="string",
required=False,
empty=False,
default="all",
allowed=["all", "x", "y", "z"],
)
}

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
# pylint: disable=import-error,no-member
from adxl345 import ADXL345 # type: ignore

self.i2c_addr: int = self.config["chip_addr"]
self.adxl345 = ADXL345(self.i2c_addr)

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
sens_type = sens_conf["type"]

if "output_g" in self.config and self.config["output_g"]:
all_axes = self.adxl345.get_axes(True)
else:
all_axes = self.adxl345.get_axes()

return cast(
float,
dict(
x=all_axes["x"],
y=all_axes["y"],
z=all_axes["z"],
all_axes=dumps(all_axes),
)[sens_type],
)
Loading