After reviewing relevant information found on the forum and other sources and analyzing it, in summary:
To read data from a Modbus TCP server, you can utilize the ubus call
interface available in RutOS. All available ubus
parameters and endpoints can be listed using the following command:
ubus -v list
Here is an example ubus
call to read holding registers (Function Code 3) from a Modbus TCP server:
ubus call modbus_client.rpc tcp.test '{
"id":1,
"timeout":1,
"function":3,
"first_reg":1,
"reg_count":"1",
"data_type":"16bit_int_hi_first",
"no_brackets":1,
"ip":"192.168.1.138",
"port":"502",
"delay":0
}'
In this example:
- It reads 1 register starting at address 1
- Data type is
16bit_int_hi_first
- Target Modbus server IP is
192.168.1.138
, port502
To publish or subscribe to an MQTT broker, you can use the Mosquitto client
, which can be installed on the device with the following commands:
opkg -e /etc/opkg/openwrt/distfeeds.conf update
opkg -e /etc/opkg/openwrt/distfeeds.conf install mosquitto-client-ssl
This will allow you to:
- Use
mosquitto_pub
to publish data - Use
mosquitto_sub
to subscribe and react to messages
You can then script Modbus reads and MQTT publishes in a loop, and simultaneously listen for MQTT messages and write back to Modbus registers.
Furthermore, there were similar topics/requests discussed on these threads:
In addition:
- For writing a complete automation script, you can leverage community-shared examples and further details in related forum threads.
- You can also use AI tools like chatgpt/deepseek to help draft and troubleshoot your script logic using
ubus
andmosquitto
commands.
I hope this can bring you any useful insights or hints for writing the required script.
Best regards,