Volt Docs

Language

Storage

Keep values in flash so they survive a restart or a power cut.

A storage variable is kept in the chip’s flash. It’s loaded when the board starts, so it carries on from where it was:

main.volt
storage boots: int = 0

setup {
    boots += 1
    storage.save("boots", boots)
    log("boot", "this is boot number {boots}")
}

Try it. Press Check, then delete the storage.save line. The variable would reset on every boot, and the warning says so.

The value after = is used the very first time, before anything has been saved.

Saving

Changing a storage variable changes it in memory only. To keep the change, save it with storage.save("name", value), using the variable’s name as the key. A storage variable that’s never saved gets a warning, since it would reset on every boot.

Saves are collected in memory and written to flash together, within 30 seconds. Call storage.flush() to write at once, for example just before deep_sleep or a restart:

main.volt
storage setpoint: float = 21.5

setup {
    setpoint = 22.0
    storage.save("setpoint", setpoint)
    storage.flush()
}

Flash wears out after many thousands of writes to the same place. Batching saves is what lets a device save often and still last for years; don’t call flush() on every change.

Keys and lists

storage.save and storage.load also work with keys you choose, for values you don’t need as variables:

main.volt
setup {
    storage.save("last_error", 7)
    let code: int = storage.load("last_error")
    log("boot", "last error was {code}")
}

Named lists keep values in order, for store-and-forward: readings taken while offline, sent when the network comes back. There can be 8 lists of up to 64 values each.

main.volt
hardware {
    device env: BME280
}

every 1min {
    storage.push("readings", int(env.temperature() * 10.0))
}

every 5min when wifi.is_connected() {
    while storage.list_count("readings") > 0 {
        mqtt.send_int("home/temp", storage.drain("readings"))
    }
}

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