Easy way to retrieve and reset Data received and Data sent through Modbus?

I am able to retrieve the various data received and sent through the Modbus registers through the MQTT Modus gateway:

Mobile data received today (SIM1) 185 186 2 32 bit unsigned integer
Mobile data sent today (SIM1) 187 188 2 32 bit unsigned integer
Mobile data received this week (SIM1) 189 190 2 32 bit unsigned integer
Mobile data sent this week (SIM1) 191 192 2 32 bit unsigned integer
Mobile data received this month (SIM1) 193 194 2 32 bit unsigned integer
Mobile data sent this month (SIM1) 195 196 2 32 bit unsigned integer
Mobile data received last 24h (SIM1) 197 198 2 32 bit unsigned integer
Mobile data sent last 24h (SIM1) 199 200 2 32 bit unsigned integer

But what i would like is to retrieve:

  • the total data received and sent as stated in the Status>Network>Mobile>Data Transmission gui.
  • a way to reset those number to 0 via modbus (the way they do when i click Network>Mobile>Limits> Clear limit . This works even if i don’t have a limit set).

@pwhooftman Hello,

There is no built-in option for this.

A possible solution is to use a custom register block as described here:
https://wiki.teltonika-networks.com/view/Modbus_custom_register_block_RutOS

You can then write a script that retrieves txbytes and rxbytes and stores them in the custom register block file (/tmp/regfile by default). Once stored, these values can be read via Modbus.

The following commands can be used in the script to retrieve data:

api get /modems/status
api get /modems/status | jsonfilter -e '@.http_body.data[0].rxbytes'
api get /modems/status | jsonfilter -e '@.http_body.data[0].txbytes'

For testing, you can manually write these values into /tmp/regfile and read them as ASCII using a Modbus client to verify that it works.

To automate this process, you can add the script to crontab so it runs at regular intervals, for example, every minute. The interval can be adjusted based on your needs.

More information on crontab and user scripts can be found here:
https://wiki.teltonika-networks.com/view/Crontabs
https://wiki.teltonika-networks.com/view/User_Scripts_examples

Hope this helps!

Kind Regards,

This works a treat!, thank you very much. For my future reference and maybe to help others:

  • MQTT Modbus gateway running om the Teltonika

    • Host & port pointing to my home MQTT server
    • Request topic “RUT241/request”
    • Response topic “RUT241/reponse”
  • "Enable custom register block’ in the Teltonika RUT241

    • Register file path: /tmp/regfile
    • First register number: 1025
    • Register count: 32
  • Script in the Teltonika (/bin/traffic.sh):

rx=$(api get /modems/status | jsonfilter -e ‘@.http_body.data[0].rxbytes’)
tx=$(api get /modems/status | jsonfilter -e ‘@.http_body.data[0].txbytes’)
rxtx=$(($rx + $tx))
rxtx=$(printf “%015d” $rxtx)
echo “$rxtx” > /tmp/regfile

This scripts runs every minute because of this crontab line:

          • /bin/traffic.sh

Now, when i post " 0 6 0 127.0.0.1 502 5 1 3 1025 8" in the topic RUT241/request on my MQTT server, the RUT241 responds with “6 OK 12336 12336 12336 12336 13872 13625 13362 13066”
In Node Red, i turn that into readable format with this code. I use different cookie numbers to recognoze different responses and use different code in NodeRed to process the responze.

var msg6 = {};
let payload = msg.payload.trim(); // Spaties verwijderen
let parts = payload.split(" “); // Opsplitsen op spaties
let response_type = Number(parts[0]);
let registers = parts.slice(2).map(Number);
if (response_type == 6) {
let asciiString = “”;
for (let reg of registers) {
let highByte = String.fromCharCode((reg >> 8) & 0xFF); // first ASCII-character
let lowByte = String.fromCharCode(reg & 0xFF); // second ASCII-character
asciiString += highByte + lowByte;
}
asciiString = asciiString.replace(/^0+/, ‘’);
let traffic = parseInt(asciiString, 10);
if (traffic > -1) {
msg6.payload = ((traffic) * 1).toFixed(1) + " Bytes”;
}
if (traffic > 1000) {
msg6.payload = ((traffic) * 0.001).toFixed(1) + " KBytes";
}
if (traffic > 1000000) {
msg6.payload = ((traffic) * 0.000001).toFixed(1) + " MBytes";
}
if (traffic > 1000000000) {
msg6.payload = ((traffic) * 0.000000001).toFixed(1) + " GBytes";
}
return [,msg6];
}

1 Like

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