Skip to content

A C utility for reading temperature data from R4DCB08 sensor modules via Modbus RTU over serial port. Supports up to 8 channels, device configuration, correction values, and optional digital filtering. Includes MQTT daemon for home automation integration.

License

Notifications You must be signed in to change notification settings

chmelat/r4dcb08

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

R4DCB08 Temperature Sensor Utility

V1.13 (2026-01-28)

A command-line utility for communicating with R4DCB08 temperature sensor modules via serial port.

Overview

This utility allows you to:

  • Read temperature data from up to 8 channels in real-time
  • Configure device settings (address, baudrate)
  • Read and set temperature correction values
  • Monitor temperature with configurable intervals

Installation

Prerequisites

  • C compiler (GCC/Clang)
  • Make utility
  • Serial port support (Linux/Unix-based systems)

Compilation

make

System-wide Installation (optional)

sudo make install    # Installs to /usr/local/bin
sudo make uninstall  # Removes from /usr/local/bin

Quick Start

  1. Connect the R4DCB08 module to your computer via USB-RS485 adapter
  2. Run the utility:
./r4dcb08

Example output (reading 4 channels every 2 seconds):

$ ./r4dcb08 -n 4 -t 2
# Date                  Ch1  Ch2  Ch3  Ch4
2026-01-21 14:30:15.42  22.5 23.1 21.8 22.0
2026-01-21 14:30:17.42  22.6 23.0 21.9 22.1
2026-01-21 14:30:19.42  22.5 23.1 21.8 22.0
^C
Received SIGINT (Ctrl+C), measurement stopped

Output format: timestamp Ch1 Ch2 Ch3 ... - channels are printed in order from 1 to n, separated by spaces.

Press Ctrl+C to stop continuous measurements.

Usage

Basic Examples

  1. Read temperature from channel 1 (default):
./r4dcb08
  1. Read 4 channels every 2 seconds:
./r4dcb08 -n 4 -t 2
  1. Single measurement without timestamp (useful for scripts):
./r4dcb08 -n 4 -f
# Output: 22.5 23.1 21.8 22.0
  1. Use different serial port:
./r4dcb08 -p /dev/ttyUSB1
  1. Enable median filter (smooths out noise/spikes):
./r4dcb08 -m
  1. Enable MAF filter (moving average with trapezoidal weights):
./r4dcb08 -M 5
  1. Combine filters (median → MAF pipeline):
./r4dcb08 -m -M 5 -n 4 -t 2

Digital Filters

The utility provides two digital filters that can be used separately or combined:

Median Filter (-m)

  • Three-point median filter
  • Removes isolated spikes while preserving signal edges
  • Introduces 1 sample delay

MAF Filter (-M n)

  • Moving Average Filter with trapezoidal weights
  • Window size n must be odd (3, 5, 7, ... 15)
  • Weights: [0.5, 1, 1, ..., 1, 0.5]
  • Formula: MAF = (0.5·x[0] + x[1] + ... + x[n-2] + 0.5·x[n-1]) / (n-1)
  • Smooths high-frequency noise while reducing edge distortion compared to simple moving average

Filter Workflow

When both filters are enabled, data flows through the pipeline:

Raw data → Median Filter → MAF Filter → Output

Example with 4 channels, 2-second interval, median + MAF(5):

$ ./r4dcb08 -m -M 5 -n 4 -t 2
# Active three-point median filter for all data ...
# Active MAF filter (window size 5) for all data ...
#
# Date                  Ch1  Ch2  Ch3  Ch4
2026-01-28 10:15:03.12  22.5 23.1 21.8 22.0
2026-01-28 10:15:05.12  22.5 23.0 21.9 22.0
2026-01-28 10:15:07.12  22.5 23.1 21.8 22.0
^C
Received SIGINT (Ctrl+C), measurement stopped

Device Configuration

  1. Read current temperature correction values:
./r4dcb08 -c
  1. Set temperature correction for channel 3 to +1.5°C:
./r4dcb08 -s 3,1.5

Temperature correction is useful when sensor readings need calibration (e.g., sensor shows 21.0°C but actual temperature is 22.5°C → set correction to +1.5).

  1. Change device address from 1 to 5:
./r4dcb08 -a 1 -w 5

Note: -a 1 specifies current address, -w 5 sets new address.

  1. Change device baudrate to 19200:
./r4dcb08 -x 4

Baudrate codes: 0=1200, 1=2400, 2=4800, 3=9600, 4=19200. Change takes effect after device power cycle.

  1. Factory reset:
./r4dcb08 -r

Writes factory defaults to EEPROM (address 1, baudrate 9600, all corrections 0). Power cycle needed to apply.

  1. Scan RS485 bus for devices:
./r4dcb08 -S

Scans addresses 1-254 and reports all responding devices. Useful for discovering device addresses on the bus.

Example output:

Found 2 device(s):
  1
  15

Command Line Options

Option Description Default
-p [port] Serial port device /dev/ttyUSB0
-a [1-254] Device address (for multi-device setups) 1
-b [baud] Serial port baudrate {1200, 2400, 4800, 9600, 19200} 9600
-t [seconds] Interval between measurements 1
-n [1-8] Number of channels to read 1
-c Read temperature correction values -
-w [1-254] Write new device address -
-x [0-4] Write device baudrate (0=1200, 1=2400, 2=4800, 3=9600, 4=19200) -
-s [ch,value] Set temperature correction for channel (e.g., -s 3,1.5) -
-m Enable three-point median filter (reduces noise) Off
-M [3-15] Enable MAF filter with window size (must be odd) Off
-f One-shot measurement without timestamp Off
-r Factory reset (resets to address 1, baudrate 9600, corrections 0) -
-S Scan RS485 bus for devices (addresses 1-254) -
-h or -? Display help -

Understanding -b vs -x

  • -b sets baudrate for this session (how fast your computer talks to the device)
  • -x changes baudrate stored in the device (permanent, survives power cycle)

Normally you only need -b if default 9600 doesn't work. Use -x only when you want to permanently change device speed.

Notes

  • Temperature readings are in degrees Celsius
  • Valid range: -55.0°C to 125.0°C (values outside show as NaN)
  • Press Ctrl+C to stop continuous measurements
  • Multiple devices can share one RS485 bus using different addresses

MQTT Daemon

For continuous monitoring and integration with home automation systems, there's a separate MQTT daemon that publishes temperature readings to an MQTT broker.

Features:

  • Publishes temperatures to MQTT broker (Mosquitto, etc.)
  • TLS/SSL support
  • Runs as systemd service or standalone
  • Auto-reconnect, LWT for offline detection
  • Works on systems without systemd (Alpine, FreeBSD)

Quick example:

cd mqtt_daemon
make
./r4dcb08-mqtt -p /dev/ttyUSB0 -H mqtt.local -v

See mqtt_daemon/README.md for full documentation.

Changelog

V1.13 (2026-01-28)

  • Added MAF (Moving Average Filter) with trapezoidal weights (-M option)
  • Window size configurable (odd values 3-15)
  • Filters can be chained: median → MAF

V1.12 (2026-01-28)

  • Minor code improvements

V1.11 (2026-01-23)

  • Added RS485 bus scan feature (-S option)
  • Scans addresses 1-254 with 100ms timeout per address
  • Reports all responding Modbus RTU devices

V1.10 (2026-01-21)

  • Added factory reset command (-r option)
  • Resets device to address 1, baudrate 9600, all corrections 0

V1.9 (2026-01-21)

  • Replaced deprecated signal() with POSIX sigaction()
  • Removed unsafe printf() from signal handler
  • Added NULL check after now() call
  • Replaced usleep() with POSIX nanosleep()
  • Fixed type punning using portable bitwise operations

V1.8 (2025-08-29)

  • Added exclusive file locking (flock) for serial port
  • Improved reliability in multi-process environments

V1.7 (2025-06-06)

  • Unified error handling system
  • Centralized constants
  • Fixed resource leak in port initialization

V1.6 (2025-06-06)

  • Fixed baud rate table bug (1200 baud)
  • Code cleanup and optimization

V1.5 (2025-04-17)

  • Implemented AppStatus error codes
  • Modularized configuration
  • Improved signal handling

V1.4 (2025-04-14/16)

  • Added median filter
  • Enhanced input validation

Technical Reference

This section is for developers and advanced users.

Protocol

Modbus RTU over serial port:

  • Function 0x03: Read holding registers
  • Function 0x06: Write single register

Register Map

Register Description R/W Values
0x0000-0x0007 Temperature readings (channels 1-8) R signed int16 / 10
0x0008-0x000F Temperature correction values R/W signed int16 / 10
0x00FD Automatic report interval R/W 0-255 (seconds, 0=disabled)
0x00FE Device address R/W 1-254
0x00FF Baudrate setting R/W 0-4 (see codes below)
0x00FF Factory reset W 5 (triggers reset)

Baudrate codes: 0=1200, 1=2400, 2=4800, 3=9600, 4=19200

Factory reset: Writing value 5 to register 0x00FF writes default values to EEPROM (address=1, baudrate=9600, corrections=0). Power cycle needed to load new settings from EEPROM.

Error Codes

  • 0: Success
  • -10 to -19: Argument errors
  • -20 to -29: Communication errors
  • -30 to -39: Operation errors

About

A C utility for reading temperature data from R4DCB08 sensor modules via Modbus RTU over serial port. Supports up to 8 channels, device configuration, correction values, and optional digital filtering. Includes MQTT daemon for home automation integration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors