Volt Docs

Language

Handling failure

Say what happens when a network, a sensor or a connection fails, with an or fallback or a try block.

Networks drop, brokers go away and sensors come loose. In Volt, each call that can fail says what happens when it does, right where it’s made.

or

A call that can fail takes an or fallback:

main.volt
setup {
    wifi.connect("home", "secret") or retry(5)
    mqtt.connect("mqtt://broker.local") or skip
}

Try it. Press Check, then delete or retry(5). The warning says what would happen, and the fix puts a fallback back.

FallbackWhat happens when the call fails
or retry(n)Try again, up to n times.
or skipCarry on without it.
or restartReboot the board.
or valueUse this value instead.

A default value suits readings:

main.volt
hardware {
    device env: BME280
}

every 5s {
    let t: float = env.temperature() or 0.0
    log("env", "temp={t}")
}

wifi.connect or mqtt.connect with no fallback gets a warning, because without one the program carries on offline as if nothing happened. The warning’s fix adds or retry(5) or or skip.

try

try puts one policy on several steps. If any of them fails, the rest are skipped and the policy applies:

main.volt
hardware {
    device env: BME280
}

every 10s {
    try {
        let t: float = env.temperature()
        mqtt.send_float("home/temp", t)
    } or skip
}

setup {
    wifi.connect("ssid", "password") or retry(5)
    mqtt.connect("mqtt://broker.local") or retry(3)
}

try takes or skip, or restart, or a retry with a last resort:

main.volt
setup {
    try {
        wifi.connect("home-ssid", "password")
        mqtt.connect("mqtt://broker.local")
    } or retry 5 times else restart
}

A try with nothing inside that can fail gets a warning: its policy could never apply.

Timeouts

timeout gives a block a time limit, with a fallback if it runs over:

main.volt
setup {
    timeout 5s {
        wifi.connect("home", "secret") or skip
    } else {
        log("wifi", "gave up after 5 seconds")
    }
}

The watchdog

For the failure you didn’t foresee, a hang, system.watchdog_enable(timeout_ms) reboots the board if system.watchdog_feed() isn’t called in time. Feed it from a block that only runs while things are healthy:

main.volt
setup {
    system.watchdog_enable(10000)
}

every 1s {
    system.watchdog_feed()
}