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:
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.
| Fallback | What happens when the call fails |
|---|---|
or retry(n) | Try again, up to n times. |
or skip | Carry on without it. |
or restart | Reboot the board. |
or value | Use this value instead. |
A default value suits readings:
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:
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:
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:
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:
setup {
system.watchdog_enable(10000)
}
every 1s {
system.watchdog_feed()
}