Weigh scale Modbus Ethernet server daemon
Project description
Table of Contents
[[TOC]]
1. Introduction
Modbus Scale Server provides the server-side capabilities required by the Mazzer and Rancilio weigh scales of project UC-02-2024 Coffee Cart Modifications.
A Raspberry Pi 4 Model B hosts the Modbus Ethernet server daemon that Modbus clients may query to obtain the current scale weight (grams), or tare (zero) the scale.
The daemon uses non-privileged port 2593, which is unofficially assigned to Ultima Online servers. This port has been chosen as use of official Modbus port 502 would require elevated user privileges.
2. Preparation
The Raspberry Pi communicates with the downstream Arduino Uno using the two-wire serial I2C (Inter-Integrated Circuit) protocol. This is not enabled by default within Raspberry Pi OS (Operating System), so it is necessary to use the raspi-config terminal application to enable the I2C protocol before continuing.
3. Dependencies
Modbus Scale Server is part of the UC-02-2024 Coffee Cart Modifications software suite. Scale software has been split along hardware component lines, with the software for each hardware component residing in a separate GitLab repository.
-
modbus_scale_broker. The Arduino sketch that must be downloaded to the Uno.
-
modbus_scale_server. The Modbus Ethernet server daemon that runs on the Raspberry Pi.
-
modbus_scale_client. The Modbus Ethernet client that queries the server.
-
modbus_scale_ui. A Textual UI (User Interface) that displays the output of the Mazzer and Rancilio scales, and which can be used to tare (zero) either scale.
Because these repositories are designed to be deployed together as part of a comprehensive weigh scale solution, both hardware and software dependencies exist between them. These dependencies must be borne in mind when deciding whether or not to employ Modbus Scale Server in isolation.
Python packages are also available for the following software components.
These packages may be installed using pip. Note however that the caveat regarding hardware and software dependencies still applies.
4. Installation
Two installation methods are available, with the most appropriate depending on whether the intent is to use the code base as-is, or to modify it.
4.1. PyPI Package
Those who wish to use the code base as-is should install the Python package via pip.
Whilst it is not strictly necessary to create a venv (virtual environment) in order to deploy the package, doing so provides a Python environment that is completely isolated from the system-wide Python install. The practical upshot of this is that the venv can be torn-down and recreated multiple times without issue.
$ python -m venv ~/venv
Next, activate the venv and install the package. Once activated, the name of the venv will be prepended to the terminal prompt.
$ source ~/venv/bin/activate
(venv) $ python -m pip install modbus-scale-server
[!important] The smbus package is a dependency of modbus_scale_server, and will cause the installation of the later to fail unless the Python development library upon which the former relies has been installed prior. To determine which version of the library to install, inspect the
~/venv/lib/site-packages/directory, and select based on the Python version installed into the venv.
$ sudo apt install libpython3.11-dev
4.2. GitLab Repository
Those who wish to modify the code base should clone the GitLab repository instead. Again, whilst not strictly necessary to create a venv in order to modify the code base, doing so is still recommended.
$ python -m venv ~/venv
$ source ~/venv/bin/activate
(venv) $ cd ~/venv
(venv) $ git clone https://gitlab.com/uc_mech_wing/robotics_control_lab/uc-02-2024/modbus_scale_server.git
Irrespective of whether or not a venv has been created, the requirements.txt file may be used to ensure that the correct module dependencies are installed.
(venv) $ cd ~/venv/modbus_scale_server
(venv) $ python -m pip install -r ./requirements.txt
5. Verification
In order to verify that Modbus Scale Server has been installed correctly, it is advisable to create a minimal working example.
$ touch ~/venv/example.py
If installed from the Python package, add the following.
from modbus_scale_server import modbus_scale_server
server = modbus_scale_server.ModbusScaleServer(host = "<host>", msgs = True)
server.daemon()
If cloned from the GitLab repository, replace the import statement with the following.
from modbus_scale_server.src.modbus_scale_server import modbus_scale_server
In either case, be sure to replace <host> with the IPv4 (Internet Protocol
version 4) address of your physical Raspberry Pi. All going well, running the
example code will produce output similar to the following.
(venv) $ python ~/venv/example.py
[INFO] Starting daemon...
[DATA] Scale weight = 123.4g
[DATA] Scale weight = 123.4g
[DATA] Scale weight = 123.4g
...
[DATA] Scale weight = 123.4g
[INFO] Stopping daemon...
Note that verification assumes that all of the requisite hardware and software dependencies have been met.
6. Operation
In order to be able to respond to client queries, the Modbus Scale Server daemon must run continuously in the background. This is best achieved by writing a custom systemd service that is started whenever the Raspberry Pi boots.
$ sudo touch /etc/systemd/system/modbus_scale_server.service
Add the following.
[Unit]
Description=Weigh scale Modbus Ethernet server daemon
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=10
User=<user>
ExecStart=/home/<user>/venv/bin/python /home/<user>/venv/example.py
[Install]
WantedBy=multi-user.target
Be sure to replace <user> with the name of a suitable Raspberry Pi user.
[!note] All this service does is run the example code using the Python executable from the venv. Be sure to change the value of the
msgsinput argument in the example code fromTruetoFalsebefore continuing.
Finally, start the service and ensure that it starts automatically on boot.
$ sudo systemctl start modbus_scale_server.service
$ sudo systemctl enable modbus_scale_server.service
7. Further Information
For further information about Coffee Cart Modifications please refer to the project UC-02-2024 group README.
8. Documentation
Code has been documented using Doxygen.
9. License
Modbus Scale Server is released under the GNU General Public License.
10. Authors
Code by Rodney Elliott, rodney.elliott@canterbury.ac.nz
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modbus_scale_server-1.0.10.tar.gz.
File metadata
- Download URL: modbus_scale_server-1.0.10.tar.gz
- Upload date:
- Size: 45.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f4e11c3516ed26ac4dba91f8e48748188eef7ee6bf7d94f16b1e6017c159ae0
|
|
| MD5 |
375c1c30d04c031810bcbc7d1f6b76b4
|
|
| BLAKE2b-256 |
44e0e6a84a2922ab1769a2d1c75902bae5963dc75c22fe5a2434d319d156c874
|
File details
Details for the file modbus_scale_server-1.0.10-py3-none-any.whl.
File metadata
- Download URL: modbus_scale_server-1.0.10-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
401efd5338470499e408b8dded51de2a5c12120d872fe736b1b94850bf150ee1
|
|
| MD5 |
ef1d27a6a47046330318d28700658094
|
|
| BLAKE2b-256 |
d013237e2b12d8cf53a55884d195f3187a213bf3bf8417051ef1ea54a627a047
|