Watchdog

obnizOS has a communication watchdog. It treats an active connection with data coming from the server as the healthy state. When no communication happens for a certain time, the OS assumes it is stuck, logs Hang Detect. Rebooting..., and restarts the device.

State Time until restart without communication
Online (connected to the server) 3 minutes. Data received from the server resets the timer automatically, so you do not need to do anything.
Not connected (offline, connecting, or disconnected with cloud.disconnect()) 10 minutes

This is the communication monitor, one of the three levels of monitoring in obnizOS. wdt.feed() affects only this level. The hardware watchdog and the task monitor are handled by the OS. See Error Detection and Watchdogs.

When a plugin keeps the device offline on purpose, this restart gets in the way. Call wdt.feed() to tell the OS that the device is working normally.


wdt.feed()

Resets the communication watchdog timer. The OS treats the moment of the call as the last communication, so the restart is postponed by the time in the table above.

There are no arguments and no return value.

wdt.feed();
  • Call it at a regular interval shorter than the timeout, for example once per second in on_offline_loop().
  • While online, the server keeps the timer reset. Do not call wdt.feed() while online: if the connection hangs, the OS can no longer detect it and recover by restarting.
  • The net functions reset the timer automatically while they wait for the network, so a long transfer does not cause a restart.

Example: Staying Offline and Connecting After 10 Seconds

The following plugin disconnects from the cloud at startup and keeps the device offline. While offline, it calls wdt.feed() every second, and it connects to the cloud 10 seconds after going offline. When a command arrives from obniz.js, it disconnects again.

function on_event(event)
  if event == "power_on" then
    os.resetOnDisconnect(false); -- keep peripherals running when offline
    cloud.disconnect();
  elseif event == "online" then
    os.log(" - Lua Online");
  end
end

local lastTick = os.getTick();
local connecting = false;
local offlineTick = 0;

function on_offline_loop()
  -- Feed the watchdog every second while offline
  if os.getTick() - lastTick > 1000 then
    lastTick = os.getTick();
    wdt.feed();
  end
  -- Connect to the cloud 10 seconds after going offline
  if os.getTick() - offlineTick > 10 * 1000 and not connecting then
    cloud.connect();
    connecting = true;
  end
end

function on_command(command)
  cloud.disconnect();
  connecting = false;
  offlineTick = os.getTick();
end

⚠️ Important Caution

Use cloud.disconnect() and wdt.feed() with care. Depending on your Lua code, the device may stay offline forever and its script can no longer be updated from the cloud.

We strongly recommend the following safeguards:

  • Connect to the internet at least once after startup.
  • Connect to the internet again after a certain period.
  • Stop calling wdt.feed() after a certain period, so that the OS restarts the device if something goes wrong.

See Internet for how to control the cloud connection with cloud.connect() and cloud.disconnect().