MODBUS TCP Ethernet Temperature Sensor
using USB Thermometer and Linux as hostThis note consists of the following steps:
- Preparing Linux host
- Software installation
- Testing thermometer
- Enabling MODBUS service
- Testing MODBUS connectivity
Prepare Linux host
Since the temperature probe has a USB connection a host with Ethernet port is needed. For sake of simplicity a Raspberry Pi single board computer is used in this note as an example host.
Basic steps includes:
- Install Raspberry Pi OS Lite to micro SD card, using Raspberry Pi Imager.
- Start Raspberry Pi, configure user and login to a console.
- Set static IP or fix static IP for host on your DHCP server.
- Plug in USB Thermometer and obtain its character device, see Driver and software installation guide for details.
At this point you should have working thermometer using utmp-cli
or digitemp
binary on the host.
For the rest of the note it is assumed the thermometer has been assigned to /dev/ttyUSB0
character device. For robustness use character device assiged to the thermometer serial number, like /dev/serial/by-id/???
.
Install software
Since the software is distributed as source code compiling tools and libraries are needed. Install compiling tools with needed libraries on Linux host, e.g. sudo apt-get install -y gcc libmodbus-dev libsystemd-dev libc6-dev make
for Debian based distros.
Source code is offered with GPL-2 licence and uses libmodbus library licenced under LGPL-2.1+ licence.
Obtain source code (e-mail to support) and compile.
unzip -d~ utmpwatcher.zip cd ~/utmpwatcher/ make clean && make MODBUS=1 SYSTEMD=1 sudo make install
After issuing last command, the binaries (server: uws
, client: uwc
) are installed into /usr/local/sbin
and /usr/local/bin
directories respectively.
Test thermometer
For testing purpose a server binary uws
is ran as superuser using verbose and foreground switch. The process output should be like the following.
#
sudo uws -f -v -p 0 -P 0 -s /dev/ttyUSB0
UTMP Watcher Server v0.071 Copyright 2024 usbtemp.com et al. Licensed under GPL-2.0-only licence.
USBTEMP> Device 0 at serial port /dev/ttyUSB0.
BINARY> Using 2001 for BINARY port.
FCGI> Disabled.
MODBUS> Using 502 for Modbus port.
TELNET> Disabled.
Starting UTMP Watcher Server ...
In another console emulator run client binary.
#
uwc
UTMP Watcher Client v0.01 Copyright 2024 usbtemp.com et al. Licensed under GPL-2.0-only licence.
ROM: 282fb6e50c00002c
Temperature: 19.250
Stop server by pressing Ctrl-C in first console.
Software supports without any source code modification up to 9 thermometers. Just list theirs character devices with -s
switch or list theirs character devices at the end, e.g. uws -f -v -p 0 -P 0 /dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2
. Order is preserved.
Enable MODBUS and start at boot
First create nonprivileged dedicated user for daemon and grant it access to USB Thermometer (as member of dialout group).
sudo groupadd -r utmpwatcher sudo useradd -g utmpwatcher -M -N -r -s /usr/sbin/nologin utmpwatcher sudo usermod -a -G dialout utmpwatcher
Install systemd configuration
cd ~/utmpwatcher/ sudo make install-systemd
To configure daemon (persistent) configuration edit file /etc/uws.args, e.g. sudo nano /etc/uws.args
to disable FastCGI and TELNET interface.
ARGS="-p 0 -P 0 -s /dev/ttyUSB0"
Because default port number of MODBUS TCP, 502 is lower that 1024, daemon ran by nonprivileged user cannot obtain such socket for itself, therefor a feature in systemd, called socket-based activation, is exploited. Instead of running uws.service a systemd uws.socket is enabled and started.
sudo systemctl daemon-reload sudo systemctl enable uws.socket sudo systemctl start uws.socket
Once systemd detects incomming connection on port 502, it creates socket and passes it to the service it has just started.
You can check service's status at any time with systemctl status uws.socket
.
Test MODBUS connectivity
For testing purposes mbpoll
binary is used. It is available inside a package for Debian based systems after installing mbpoll package.
For example if your host has IP of 198.18.41.105, first input register holds the temperature from first thermometer encoded as DCBA float.
mbpoll -t 3:float -c 1 198.18.41.105
Presence (status) of first temperature probe is available from first discrete input.
At this point you should have a working temperature sensor available over MODBUS TCP (502) through Linux host.
Troubleshooting
Write to support e-mail on main website. Thanks.