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 )