Creating Your Own Sensor
This guide walks you through creating a custom sensor implementation for AtmosPyre.
What You'll Build
A sensor driver that integrates a Modbus RTU sensor with AtmosPyre's logging system.
Prerequisites
- Understanding of Sensor API and ReadTag System
- Access to your sensor's Modbus register map / communication manual
- Python 3.10+
- Sensor connected to a serial port
Implement the Following Interfaces
Attributes
YOUR_MEASUREMENT = YourMeasurementTag(ReadTagMetadata(unit='[unit]', description='[Measurement description]', precision=2, data_type='float', min_interval=1, source='[YourSensor] User Manual, Register [X]'))
module-attribute
[Measurement name] measurement tag.
This tag instance provides access to [measurement description] from
the [YourSensor] sensor.
Classes
YourMeasurementTag
Bases: ReadTag
Tag for [measurement name] measurement.
This tag class represents [measurement description] from the
[YourSensor] sensor. It is used with the dispatch mechanism to
route read operations to the appropriate register reading function.
Source code in atmospyre/sensors/_template/sensor_template.py
YourSensor
Bases: Sensor
[Manufacturer] [Model] [sensor type] with Modbus RTU communication.
The [YourSensor] is a [description of sensor capabilities and use cases].
It communicates via Modbus RTU protocol and measures [list of measurements].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
str
|
Serial port name (e.g., 'COM3' on Windows, '/dev/ttyUSB0' on Linux) |
required |
slave_address
|
int
|
Modbus slave address, by default 1 |
1
|
baudrate
|
int
|
Serial communication speed in baud, by default 19200 |
19200
|
stopbits
|
int
|
Number of stop bits, by default 1 |
1
|
bytesize
|
int
|
Number of data bits per byte, by default 8 |
8
|
parity
|
str
|
Parity checking method ('N' for None, 'E' for Even, 'O' for Odd), by default 'N' |
'N'
|
timeout
|
float
|
Read timeout in seconds, by default 0.5 |
0.5
|
Raises:
| Type | Description |
|---|---|
SerialException
|
If the serial port cannot be opened or configured |
ModbusException
|
If Modbus communication fails |
Source code in atmospyre/sensors/_template/sensor_template.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
Functions
__init__(port, slave_address=1, baudrate=19200, stopbits=1, bytesize=8, parity='N', timeout=0.5)
Initialize [YourSensor] sensor with serial communication parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
str
|
Serial port name (e.g., 'COM3' on Windows, '/dev/ttyUSB0' on Linux) |
required |
slave_address
|
int
|
Modbus slave address, by default 1 |
1
|
baudrate
|
int
|
Serial communication speed in baud, by default 19200 |
19200
|
stopbits
|
int
|
Number of stop bits, by default 1 |
1
|
bytesize
|
int
|
Number of data bits per byte, by default 8 |
8
|
parity
|
str
|
Parity checking method ('N', 'E', 'O'), by default 'N' |
'N'
|
timeout
|
float
|
Read timeout in seconds, by default 0.5 |
0.5
|
Source code in atmospyre/sensors/_template/sensor_template.py
Functions
_read(instrument, tag)
Read [measurement name] from register [X].
This function reads [measurement description] from Modbus register [X]
using [format description, e.g., 32-bit floating point].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument
|
Instrument
|
minimalmodbus Instrument instance for Modbus communication |
required |
tag
|
YourMeasurementTag
|
Tag instance identifying this measurement type |
required |
Returns:
| Type | Description |
|---|---|
float
|
|