Send RS485 to remote MQTT broker using TRB245

Hi, my use case is as follows: I have a QR code scanner connected to RS485 on a TRB245. Whenever a scan is done, the data should be sent in realtime to a remote MQTT broker. How can I do this? I’ve tried all combinations of modbus client/server/ setups but nothing is working. I have confirmed using CLI that the device is working correctly and I am receiving the data in the console.

Hello,

Apologies for the delayed response.

Would it be possible for you to explain what kind of scanner are you using? What protocols does it support?

You mentioned Modbus. Does you scanner support it?

Can the scanner be configured in any way?

Kind Regards,

Currently using the CLI command ‘cat /dev/RS485’ whenever I scan a qr code it displays the result as a text string - which is what I want. So my console configuration is correct. But now how to publish this text string to an MQTT Broker, this is the question.

Hello,

Could you please tell me the model of the scanner?

Does it just output data/strings? Its not Modbus RTU, is it?

Kind Regards,

its the NEDAP Nvite, and on the datasheet it says:

I think it’s only RS485 over serial.

actually it can be configured to output hex or decimals if needed:

Hello,

Apologies for the delayed response.

In this case, functioalities like Modbus, MQTT broker/publisher, or Data to Server will not be an option.

I would suggest writing a script that will get data from /dev/rs485 and send it via MQTT to your broker. To send data to MQTT within your script, you can use mosquitto. Please, take a look at this post here. You can also find different script examples on this forum using search functionality (ChatGPT can also help!). Make sure to disable all RS485 services that you may have configured on the device so that it can be used in your script

Another option is to send data to some other server over TCP, and then forward it your MQTT broker from that server. For this, you can use the Serial over IP fucntionality. This will allow you to forward all data from RS485 towards your server via TCP/IP.

Kind Regards,

so I receive data when running ‘cat /dev/rs485’ in the CLI, but when I use a bash script I get no output:


#!/bin/bash

# RS485 Device Configuration
RS485_DEVICE="/dev/ttyUSB0"

# Main loop to read from RS485 and output to console
while true; do
    # Read data from RS485 using cat
    RS485_DATA=$(cat $RS485_DEVICE 2>/dev/null)

    # Check if the read operation was successful
    if [ $? -eq 0 ]; then
        # Output the received data to the console
        echo "Received RS485 data: $RS485_DATA"
    else
        # Print an error message and the exit status for debugging
        echo "Error reading from RS485 device. Exit status: $?"
    fi

    # Add a delay if needed to control the rate of reading
    sleep 1
done

This topic was automatically closed after 19 days. New replies are no longer allowed.

Hello,

First of all, make sure you use shell: #!/bin/sh

I would suggest using python (pyserial) for this, but it will likely not fit on TRB245 due to the lack of flash memory. For shell, you can try reading via dd or via read (like read -r line< /dev/rs485). Though I am not sure if this will work.

Make sure you add execution rights to the script via chmod +x /etc/yourscript.sh, and that there are no other services running on TRB that use RS485.

If needed, you can run the script in the background like so: /etc/yourscript.sh &

Also, once you confirmed that the script is working, you can put it into the custom scripts via WebUI. This will allow you to start your script automatically after each reboot. In this case, I would suggest doing it like this:

(sleep 120 && /etc/yourscript.sh) &

This will allow some time for other services to initialize.

Kind Regards,

thanks, scanning now works from the CLI however the custom script after reboot doesn’t work. Should the script have the ‘exit 0’ at the end?

I have similar (commercial) app developed to run on RUT955, using “official” openwrt. App is done in LUA, which perfectly fits into available flash. MQTT is used for comms to AWS-IoT.

Hello,

Yes, you should put it just above the ‘exit 0’ line.

Have you tried the following (can try to increase sleep timer to allow more time for other services to start)?

(sleep 120 && /etc/yourscript.sh) &

and:

(sleep 120 && /etc/yourscript.sh & ) &

Kind Regards,

If nothing should be entered into the ‘serial utilities’ of the webui, then how does the device know which baudrate to communicate with the rs485 via the CLI or custom scripts?

Hello,

You can take a look at stty. For example, enable RS485 service in WebUI, set baudrate to value A and:

stty -F /dev/rs485

This should show you the baudrate for this device. You can then try changing baudrate to value B and checking it again.

To set the device, try:

stty -F /dev/rs485 9600 cs8 -cstopb -parenb
#check
stty -F /dev/rs485
#help
stty --help

Kind Regards,

the startup script will run, but is not able to read from the device. The issue is something to do with the reboot, because after a reboot if I go to the CLI and run ‘cat /dev/rs485’ it will also not read anything. It will ONLY start reading from the device after I add a value in the ‘serial utilities’ → ‘console’; but then ONLY if I toggle the value to ON then to OFF. Very strange indeed.

Hello,

Not sure. Perhaps you could try the following before reading with cat:

stty -F /dev/rs485 -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

Does it make any difference?

If is does not help, could you test by restarting RS console using UCI before using cat? From the startup scrip or from the script itself:

/etc/init.d/rs_console restart

Or something like:

uci set rs_console.1.enabled='1'
uci commit
/etc/init.d/rs_console restart

uci set rs_console.1.enabled='0'
uci commit
/etc/init.d/rs_console restart

Kind Regards,

yes! the uci commands work and enable the rs485 connection after reboot using the startup script. Thanks!

This topic was automatically closed after 10 days. New replies are no longer allowed.