Reference
Built-in objects
The objects and functions every Volt program has: Wi-Fi, MQTT, HTTP, storage, the system, Bluetooth, channels, serial buses and the built-in functions.
These are always there: no declaration, no import. Call them as wifi.connect(…), system.uptime(), log(…).
wifi
Joins a Wi-Fi network. Using it also tells the compiler that analog pins on ADC2 won’t work.
-
wifi.connect(ssid: string, password: string)
Joins a network. It can fail, so give it an
orfallback. -
wifi.disconnect()
Leaves the network.
-
wifi.is_connected() -> bool
Whether the board is on the network.
-
wifi.get_ip() -> string
The board’s IP address, as text.
-
wifi.get_rssi() -> int
Signal strength in dBm (closer to 0 is stronger).
mqtt
Publishes and subscribes to an MQTT broker.
-
mqtt.connect(url: string)
Connects to a broker, e.g.
"mqtt://broker.local". Give it anorfallback. -
mqtt.disconnect()
Disconnects from the broker.
-
mqtt.send(topic: string, data: string)
Publishes text to a topic.
-
mqtt.send_float(topic: string, value: float)
Publishes a number to a topic.
-
mqtt.send_int(topic: string, value: int)
Publishes a whole number to a topic.
-
mqtt.on_receive(topic: string)
Subscribes to a topic.
-
mqtt.is_connected() -> bool
Whether the broker connection is up.
-
mqtt.has_message() -> bool
Whether an unread message has arrived.
-
mqtt.payload() -> string
The latest message’s text, and marks it read.
-
mqtt.topic() -> string
The topic the latest message came on.
http
Makes HTTP requests.
-
http.get(url: string) -> string
Fetches a URL and returns the body (empty on failure, cut at 1023 characters).
-
http.post(url: string, body: string) -> string
Posts a body to a URL and returns the response body.
storage
Keeps values in flash, so they survive a restart. See Storage in the language guide.
-
storage.save(key: string, value: any)
Saves a value under a key. Writes are batched to flash within 30 seconds, or at once with
flush(). -
storage.load(key: string) -> int
Loads a whole number saved under a key (
0if there is none). -
storage.load_float(key: string) -> float
Loads a number saved under a key.
-
storage.load_str(key: string) -> string
Loads text saved under a key.
-
storage.push(list_name: string, value: int)
Appends a value to a named list kept in flash (up to 8 lists of 64 values).
-
storage.drain(list_name: string) -> int
Takes the oldest value off a named list.
-
storage.list_count(list_name: string) -> int
How many values a named list holds.
-
storage.list_clear(list_name: string)
Empties a named list.
-
storage.flush()
Writes pending saves to flash now.
system
The board itself: restarting, uptime, memory, the watchdog.
-
system.restart()
Reboots the board.
-
system.id() -> int
The chip’s unique ID.
-
system.uptime() -> int
Seconds since the board started.
-
system.free_heap() -> int
Free memory, in bytes.
-
system.micros() -> int
Microseconds since the board started (wraps after about 71 minutes).
-
system.min_free_heap() -> int
The least free memory seen since the board started.
-
system.watchdog_enable(timeout_ms: int)
Reboots the board if
watchdog_feed()isn’t called withintimeout_ms. -
system.watchdog_feed()
Resets the watchdog’s countdown.
-
system.watchdog_disable()
Turns the watchdog off.
ble
Bluetooth Low Energy: advertising, values a phone can read, scanning.
-
ble.advertise(name: string)
Starts advertising under a name.
-
ble.stop()
Stops Bluetooth.
-
ble.is_connected() -> bool
Whether a phone or computer is connected.
-
ble.expose_int(name: string, value: int) -> int
Publishes a readable value and returns its id, for
update_int. -
ble.expose_float(name: string, value: float) -> int
Publishes a readable value and returns its id, for
update_float. -
ble.expose_string(name: string, value: string) -> int
Publishes a readable value and returns its id, for
update_string. -
ble.update_int(id: int, value: int)
Changes a published value.
-
ble.update_float(id: int, value: float)
Changes a published value.
-
ble.update_string(id: int, value: string)
Changes a published value.
-
ble.scan(duration: Duration) -> int
Scans for nearby devices for
durationand returns how many were found. -
ble.scan_name(index: int) -> string
The name of a device found by the last scan.
-
ble.scan_rssi(index: int) -> int
The signal strength of a device found by the last scan.
ota
Firmware updates over the network.
-
ota.enable()
Accepts firmware updates over the network.
Channels
Every channel you declare (channel events: int[8]) has these methods.
-
events.send(value: any)
Puts a value in the channel. If the channel is full, the value is dropped.
-
events.receive() -> int
Takes the oldest value out, or returns
0when the channel is empty. It never waits. -
events.try_receive() -> int
The same as
receive. -
events.count() -> int
How many values are waiting.
-
events.has_data() -> bool
Whether a value is waiting.
-
events.is_empty() -> bool
Whether no value is waiting.
-
events.is_full() -> bool
Whether the buffer is full, so a
sendwould be dropped. -
events.clear()
Drops every waiting value.
Serial buses
A UART you declare in the hardware block (uart modem(tx: 17, rx: 16, baud: 9600)) has these methods.
-
modem.print(text: string)
Sends text.
-
modem.read_byte() -> int
The next byte received, as a number.
-
modem.read_line() -> string
The next line received, without its line ending.
-
modem.available() -> int
How many bytes are waiting.
-
modem.flush()
Waits until everything sent has gone out.
Functions
-
log(tag: string, message: string)
Prints a line to the serial console: the time since boot, the tag, and the message.
-
sleep(duration: Duration)
Pauses this block for
duration; other tasks keep running. -
deep_sleep(duration: Duration)
Powers down for
duration, then restarts from the beginning. RAM is lost;storagesurvives. -
light_sleep(duration: Duration)
Sleeps for
durationand carries on where it left off. -
float(value: int) -> float
Converts a whole number to a number with a fraction.
-
int(value: float) -> int
Converts a number to a whole number, dropping the fraction.
-
len(text: string) -> int
The length of a string.
-
contains(text: string, part: string) -> bool
Whether
partappears intext. -
starts_with(text: string, prefix: string) -> bool
Whether
textbegins withprefix. -
ends_with(text: string, suffix: string) -> bool
Whether
textends withsuffix. -
to_int(text: string) -> int
Reads a whole number from text.
-
to_float(text: string) -> float
Reads a number from text.