RUTX11 Modbus MQTT

Hello, i want to implement a solution for my system without Modbus MQTT Gateway… Only because my system isn’t made to work with receiving requets and responses…

What i want is to make a system that reads from the modbus requests (already setup) and send and receive data to and from the mqtt broker.
I managed to do it with “Data to Server”, but i want to read also some data and write via Modbus running on my PLC…

Is it possible to do? I’ve been looking at a solution through scripts and LUA.

Hello,

The main point at hand is that you need a way to publish the requests, but since your system doesn’t support such functionality, I have my doubts that even with the use of custom scripts, it’ll be hard to achieve such a configuration. Writing these custom scripts is out of our scope, unfortunately, so I’m unable to provide you with any examples, even if such a script were possible to create. Using a Modbus MQTT gateway would be the ideal solution here, since that’s its main purpose.

The main takeaway from this would probably be switching the system in use, using a different solution perhaps, which would be compatible with MQTT gateways and such.

Regards,
M.

Well, maybe i didn´t explain myself correctly… I want to read from MQTT and send to my PLC via Modbus. (When you say publish, you mean send from the MQTT Broker to the RUT?)

I’m already publishing using the Modbus Client and Data to Server… But now i want the backwards implementation, i want to read messages and make the connection to a modbus request.

Also i found a response on another topic from your colleague Daumantas, saying “this can be done using scripts…”

I’m going to try…

Hello,

Apologies, but could you please provide the exact thread that you were referring to?

Either way, if a custom script is required to achieve this configuration, you’ll have to look into it. If you do figure out a way, it would be amazing if you could also share the solution with the community in case someone else with a similar question/issue comes around.

Thank you,
M.

The thread is this Example.

He does not say it’s possible but suggests it might be because of another user that implements a system with some pkgs.

I’m going to try my best to make it happen, we will see… If i manage to do it i will share it.

Hey there,

After looking further, I may suggest utilizing Azure as an option. Here’s a configuration example: Azure IoT Hub Cloud Connection - Teltonika Networks Wiki

Regards,
M.

Interesting… I will check it out.
Thanks guys.

I managed to do it.

After searching this community for relevant posts i managed to insert a client on my RUTX11:

opkg -e /etc/opkg/openwrt/distfeeds.conf update
opkg -e /etc/opkg/openwrt/distfeeds.conf install mosquitto-client-ssl

this will make it possible to install the pub sub client.

After that, using WinSCP i transfered to my RUT a library called ModPoll that allows me to make modbus requests to the PLC running a server, and get returns of data.

After that i implemented this code (shell script):

# MQTT Broker IP address
BROKER="broker_ip_address"
# MQTT Broker port
PORT="1883"
# MQTT topic to subscribe to
TOPIC="testmosquitto"
PUB_TOPIC="testmosquitto/data"

# PLC IP address (Modbus TCP slave)
PLC_IP="plc_ip_address"
# Modbus slave ID on the PLC
SLAVE_ID=1 (or 16#FF = 255)

echo "Starting MQTT listener..."

# Subscribe to the MQTT topic, pipe each received message to the while loop
mosquitto_sub -h "$BROKER" -p "$PORT" -t "$TOPIC" | while read -r payload
do
    # echo "$(date) Received: $payload" >> /tmp/mqtt_log.txt  # Uncomment to enable logging

    if echo "$payload" | grep -q "on"; then
        echo "Command: READ 5 holding registers starting at 40001 (addr 0), poll once"
        # modpoll -m tcp -a $SLAVE_ID -r 1 -c 5 -t 4 -1 $PLC_IP
        # Run modpoll once, capture output
        result=$(modpoll -m tcp -a $SLAVE_ID -r 1 -c 5 -t 4 -1 $PLC_IP 2>&1)
        # pub the full data 
        echo "Publishing Modbus data to topic $PUB_TOPIC"
       mosquitto_pub -h "$BROKER" -p "$PORT" -t "$PUB_TOPIC" -m "$result"
    elif echo "$payload" | grep -q "off"; then
        echo "Command: WRITE value 1 to register 40001 (addr 0)"
        modpoll -m tcp -a $SLAVE_ID -r 1 -t 4 $PLC_IP 255
    else
        echo "Unknown payload: $payload"
    fi
done

And this is the outcome:

  • Basically i publish the word “on” and that will generate a modbus request to the server running on my PLC, and return the data to the script that will publish to my broker the information (i didn’t format the message yet…)

But feel free to implement in any way you need.
I recommend checking the tutorial on shell scripting by Teltonika itself.
(And use chatGPT :shushing_face:)

1 Like

Thanks a bunch for sharing!!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.