Mqtt message at boot

Hello everybody,

i would like to send the state of the 3 digital outputs of a TRB255 via mqtt at boot.

I tried it with the modbus alarm function, but i get random results…

Any suggestions?

Thanks in advance!

PiM

Hello!

Thank you for your post!

To fully achieve what you have described, you will require a custom script, which is outside of our scope.

However, you can obtain the status of input and output pins via the command line (CLI or SSH). List of possible ubus values in TRB255 devices:

ioman.adc.adc0  - Analog input
ioman.gpio.dio0 - Configurable input/output (PIN2)
ioman.gpio.dio1 - Configurable input/output (PIN3)
ioman.gpio.dio2 - Configurable input/output (PIN4)

If you do make a script, make sure to include these values in it.

Full article can be found here: TRB255 Input/Output - Teltonika Networks Wiki

Let us know if you have any more questions.

Kind regards,
Laura.

A tip for all users here: the free version of Claude.ai generates great answers and examples for questions like this. I asked Claude: “i would like to send the state of the 3 digital outputs of a Teltonika TRB255 via mqtt at boot. The address of my mqtt server is https://my.mqtt.server. Please take into consideration the default mqtt capabilities of the TRB255”, and this is the result:

Important limitation to address first

The built-in MQTT Publisher on TRB devices supports a fixed set of query-able parameters. Looking at the supported parameter table, digital1 and digital2 are only listed for RUT9, and pin3/pin4 are not listed for TRB255 either. In short, the TRB255’s native MQTT Publisher does not natively expose the D1/D2/D3 digital output states as queryable MQTT parameters. Teltonika Networks

To publish digital output states at boot, you have two practical approaches:


Approach 1 — Shell script via System → Custom Scripts (recommended)

Since the TRB255 runs RutOS (OpenWrt-based Linux), you can write a shell script that runs at boot, reads the D1/D2/D3 states, and publishes them via mosquitto_pub (which is available as part of the Mosquitto package).

Step 1 — Install MQTT package

Go to System → Package Manager and install the mosquitto-client package if not already present.

Note: Your broker address https://my.mqtt.server uses HTTPS — MQTT itself runs over TCP (port 1883) or TLS/TCP (port 8883), not HTTP/HTTPS. The correct hostname to use in the script would be my.mqtt.server with port 8883 (TLS) or 1883 (plain).

Step 2 — Write the boot script

Go to System → Administration → Custom Scripts and add a script in the Boot section:

sh

#!/bin/sh

# Wait for network to be ready
sleep 15

BROKER="my.mqtt.server"
PORT="1883"            # or 8883 for TLS
SERIAL=$(uci get system.@system[0].serial 2>/dev/null || cat /proc/sys/kernel/hostname)

# Read D1, D2, D3 output states
# On TRB255, digital I/O is accessible via /sys/class/gpio or iomux UCI
D1=$(gpiocmd get pin1 2>/dev/null || echo "unknown")
D2=$(gpiocmd get pin2 2>/dev/null || echo "unknown")
D3=$(gpiocmd get pin3 2>/dev/null || echo "unknown")

# Publish each output state
mosquitto_pub -h "$BROKER" -p "$PORT" -t "device/$SERIAL/digital_out/1" -m "$D1"
mosquitto_pub -h "$BROKER" -p "$PORT" -t "device/$SERIAL/digital_out/2" -m "$D2"
mosquitto_pub -h "$BROKER" -p "$PORT" -t "device/$SERIAL/digital_out/3" -m "$D3"

Topic structure used: device/<SERIAL>/digital_out/<N> — this follows Teltonika’s convention where TRB device type is device and the serial number is used in the topic path. Teltonika Networks

GPIO note: The exact GPIO command on TRB255 may be gpiocmd, gpioget, or reading from /sys/class/gpio/. You may need to check which pin numbers map to D1/D2/D3 in your firmware version. You can find this in Services → Input/Output in the WebUI or via ls /sys/class/gpio/ over SSH.

Approach 2 — Use the built-in MQTT Publisher + I/O Juggler

If you want a pure-WebUI solution without scripting:

The MQTT Publisher is configured under Services → MQTT. Enable it, set the Hostname to my.mqtt.server, set the Port (1883 or 8883), and optionally configure TLS and credentials. Teltonika Networks

Then use Services → Input/Output → I/O Juggler to create actions triggered by the “System startup” event, and set the action to send an MQTT message for each of D1, D2, D3. However, I/O Juggler actions are primarily designed to react to input changes, not to proactively read and report output states — so this is less clean than the script approach.


Summary recommendation

Method Complexity Reliability No coding
Custom boot script Low–Medium :white_check_mark: Best :cross_mark:
I/O Juggler + Publisher Low :warning: Limited :white_check_mark:

The custom boot script is the most reliable path. If you share your exact firmware version or SSH access details I can refine the GPIO pin commands to match your specific setup.

Hello!

Thank you for sharing this with our community.

Our apologies for the late response.

We would like to note that AI can make mistakes, and it is worth checking the scripts it writes before running them on your device.

Using custom-made scripts, you do so at your own discretion.

Kind regards,
Laura.