Reset device Via mqtt

Hi all,
I was wondering if it is possible to force a power cycle of teltonika device via mqtt.
for example, we send a mqtt message the device is subscribed to and upon successful receipt of the message the device power cycles and sends a reconnected message back to the publisher.
I have looked and cannot find anything in the docs or here in the forum.

apologies if this has been asked before

all the best,
J Dev

Thats absolutely pssible, i use this method to remotely enable and disable pre-defined vpn connections on my RUTM30.

Use this custom script as a guideline, this assumes you are already connected to an external mqtt broker.

#!/bin/sh

============================================================

mqtt_reset_watchdog.sh

Teltonika MQTT Reset Watchdog

Behaviour:

- Subscribes to topic: router/reset

- If payload == “reset” → reboots the router

- On next boot (startup) → publishes “router restarted”

to router/reset, then starts the watchdog loop again

Place at: /etc/mqtt_reset_watchdog.sh

Make exec: chmod +x /etc/mqtt_reset_watchdog.sh

============================================================

── Configuration ────────────────────────────────────────────

BROKER=“192.168.100.10” # IP or hostname of your external MQTT broker
BROKER_PORT=“1883”
TOPIC_SUB=“router/reset”
TOPIC_PUB=“router/reset”
CLIENT_ID=“teltonika-watchdog-$$”
USERNAME=“” # Leave empty if no auth required
PASSWORD=“” # Leave empty if no auth required
REBOOT_FLAG=“/tmp/mqtt_watchdog_rebooted”
LOG_TAG=“mqtt_watchdog”

─────────────────────────────────────────────────────────────

log() {
logger -t “$LOG_TAG” “$1”
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) [$LOG_TAG] $1”
}

Build optional auth args

auth_args() {
if [ -n “$USERNAME” ]; then
echo “-u $USERNAME -P $PASSWORD”
fi
}

── Publish “router restarted” if this is a post-reboot start ─

if [ -f “$REBOOT_FLAG” ]; then
log “Post-reboot detected. Publishing ‘router restarted’ to $TOPIC_PUB …”

Wait for broker / network to be fully ready

sleep 15
mosquitto_pub
-h “$BROKER”
-p “$BROKER_PORT”
-i “${CLIENT_ID}-pub”
$(auth_args)
-t “$TOPIC_PUB”
-m “router restarted”
-r # retain flag so the value persists on the broker
if [ $? -eq 0 ]; then
log “Published ‘router restarted’ successfully.”
else
log “WARNING: Failed to publish post-reboot message. Broker may not be ready yet.”
fi
rm -f “$REBOOT_FLAG”
fi

── Main subscription loop ────────────────────────────────────

log “Starting MQTT watchdog. Subscribing to $TOPIC_SUB on $BROKER:$BROKER_PORT …”

mosquitto_sub
-h “$BROKER”
-p “$BROKER_PORT”
-i “${CLIENT_ID}-sub”
$(auth_args)
-t “$TOPIC_SUB”
-v | # -v prints topic + payload on each line
while IFS= read -r line; do

Strip the topic prefix to get just the payload

payload=“${line#”$TOPIC_SUB “}”
log “Received message: ‘$payload’”

if [ "$payload" = "reset" ]; then
    log "Reset command received! Preparing to reboot..."

    # Create flag file so the script knows it triggered the reboot
    touch "$REBOOT_FLAG"

    # Small delay so the flag file is flushed to disk
    sync
    sleep 2

    log "Rebooting now."
    reboot
    exit 0
else
    log "Ignored unknown payload: '$payload'"
fi

done

log “mosquitto_sub exited unexpectedly. Watchdog loop ended.”

@pwhooftman Amazing,
This helps a lot.
Thanks!

Greetings @J_Dev,

I hope you are doing well.

Could you please let us know whether the solution suggested by @pwhooftman resolved the issue for you, or if you still require any further assistance?

Best regards,
V.

Hi @Vilius, still working on integrating an MQTT-based reboot listener into my TRB245 setup and could use some further assistance.

The goal is to have the device subscribe to an external MQTT broker topic and execute a system reboot when a message is received on that topic. I have a shell script configured to run at startup via /etc/rc.local, and have confirmed the broker hostname and topic are correct.

When running the script manually via CLI I receive the following error:

/etc/rc.local: line 41: mosquitto_sub: not found

Running find / -name "mosquitto*" shows that only the broker binary is present at /usr/sbin/mosquitto — the client tools (mosquitto_sub / mosquitto_pub) do not appear to be installed. The installed package appears to be mosquitto-ssl.

I attempted to install the client tools via:

opkg update
opkg install mosquitto-client-ssl

Could you confirm:

  1. Whether mosquitto-client-ssl is the correct package for client tools on TRB245 firmware 07.14.5?
  2. Whether mosquitto_sub is supported on this firmware for custom shell script use?
  3. If there is an alternative recommended approach for subscribing to an external MQTT topic and triggering a shell action on the TRB245?

For reference, here is the script (identifying information removed):


#!/bin/sh

#============================================================

kaa_reboot_listener.sh

Listens on an external MQTT broker topic for a reboot command.

Any message received on the topic triggers a router reboot.

NOTE: The command will remain in “Pending” status on the

platform side since the device reboots before it can

publish a result back. This is expected behaviour.

Place at: /etc/kaa_reboot_listener.sh

Make exec: chmod +x /etc/kaa_reboot_listener.sh

#============================================================

── Configuration ───────────────────────────────────────────

BROKER=“<BROKER_HOSTNAME>”
BROKER_PORT=“1883”
APP_VERSION=“<APP_VERSION>”
ENDPOINT_TOKEN=“<ENDPOINT_TOKEN>”
USERNAME=“”
PASSWORD=“”
CLIENT_ID=“reboot-listener-$$”
LOG_TAG=“reboot_listener”

────────────────────────────────────────────────────────────

TOPIC_SUB=“kp1/${APP_VERSION}/cex/${ENDPOINT_TOKEN}/command/reboot/status”

log() {
logger -t “$LOG_TAG” “$1”
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) [$LOG_TAG] $1”
}

if [ -z “$APP_VERSION” ] || [ -z “$ENDPOINT_TOKEN” ]; then
log “ERROR: APP_VERSION and ENDPOINT_TOKEN must be set. Exiting.”
exit 1
fi

AUTH_ARGS=“”
if [ -n “$USERNAME” ]; then
AUTH_ARGS=“-u $USERNAME -P $PASSWORD”
fi

log “Starting reboot listener.”
log “Subscribing to: $TOPIC_SUB on $BROKER:$BROKER_PORT”

mosquitto_sub
-h “$BROKER”
-p “$BROKER_PORT”
-i “$CLIENT_ID”
$AUTH_ARGS
-t “$TOPIC_SUB”
-v | while IFS= read -r line; do

payload="${line#"$TOPIC_SUB "}"
log "Received message on reboot topic. Payload: '$payload'"

if [ -n "$payload" ]; then
    log "Reboot command received. Syncing and rebooting..."
    sync
    sleep 1
    reboot
    exit 0
fi

done

log “mosquitto_sub exited unexpectedly. Listener stopped.”


Device: TRB245
Firmware: 00.07.14.5

Any guidance appreciated. Thanks

Any info for me @Vilius
Thanks