atomvm_gleam/network

Types

SoftAP options for start_with.

Event callbacks send these messages to notify:

  • ap_started
  • {sta_connected, Mac} (6-byte MAC binary)
  • {sta_disconnected, Mac}
  • {sta_ip_assigned, Address} (Erlang {A, B, C, D})
pub type ApConfig {
  ApConfig(
    ssid: option.Option(String),
    psk: option.Option(String),
    ap_channel: option.Option(Int),
    ap_ssid_hidden: option.Option(Bool),
    ap_max_connections: option.Option(Int),
    notify: process.Pid,
  )
}

Constructors

Errors from the network driver.

pub type Error {
  Failed
  NotSupported
  Badarg
  Timeout
  Disconnected
  Other(String)
}

Constructors

  • Failed
  • NotSupported
  • Badarg
  • Timeout
  • Disconnected
  • Other(String)

Address / netmask / gateway triple from DHCP (got_ip / wait_for_sta).

pub type IpInfo {
  IpInfo(
    address: Ipv4Address,
    netmask: Ipv4Address,
    gateway: Ipv4Address,
  )
}

Constructors

IPv4 address as four octets.

pub type Ipv4Address {
  Ipv4Address(a: Int, b: Int, c: Int, d: Int)
}

Constructors

  • Ipv4Address(a: Int, b: Int, c: Int, d: Int)

mDNS options passed through network:start/1 as {mdns, […]}.

ttl is optional; omit to use the AtomVM default.

pub type MdnsConfig {
  MdnsConfig(host: String, ttl: option.Option(Int))
}

Constructors

SNTP options for start / start_with.

On sync, notify receives {synchronized, {Sec, Usec}}.

pub type SntpConfig {
  SntpConfig(host: String, notify: process.Pid)
}

Constructors

STA options for start / start_with.

When managed is True, the radio starts without joining until sta_connect / sta_connect_to.

Event callbacks send these messages to notify:

  • connected
  • {got_ip, IpInfo} (Erlang {IP, Netmask, Gateway} tuples)
  • disconnected
  • {scan_results, Results} when scan_done is wired to notify
pub type StaConfig {
  StaConfig(
    managed: Bool,
    ssid: option.Option(String),
    psk: option.Option(String),
    dhcp_hostname: option.Option(String),
    notify: process.Pid,
  )
}

Constructors

STA connection status from sta_status.

Prefixed constructors avoid clashing with Error variants; FFI maps AtomVM atoms (associated, connected, …) to these values.

pub type StaStatus {
  StaAssociated
  StaConnected
  StaConnecting
  StaDegraded
  StaDisconnected
  StaDisconnecting
  StaInactive
}

Constructors

  • StaAssociated
  • StaConnected
  • StaConnecting
  • StaDegraded
  • StaDisconnected
  • StaDisconnecting
  • StaInactive

Values

pub fn error_to_string(error: Error) -> String

Format an Error for logging.

pub fn sta_connect() -> Result(Nil, Error)

Connect using credentials from the last start / sta_connect_to config.

See sta_connect.

pub fn sta_connect_to(
  ssid: String,
  psk: String,
) -> Result(Nil, Error)

Connect to ssid with psk (empty string for open networks).

See sta_connect.

pub fn sta_disconnect() -> Result(Nil, Error)

Disconnect from the current access point.

pub fn sta_rssi() -> Result(Int, Error)

RSSI of the associated AP in dBm ({ok, Dbm}).

See sta_rssi.

pub fn sta_status() -> Result(StaStatus, Error)

Current STA interface status.

See sta_status.

pub fn sta_status_to_string(status: StaStatus) -> String

Format a StaStatus for logging.

pub fn start(
  sta: StaConfig,
  sntp: option.Option(SntpConfig),
) -> Result(Nil, Error)

Start the network interface (STA + optional SNTP).

Compatibility wrapper around start_with with no AP or mDNS.

See network:start/1 and the 0.7 guide.

pub fn start_with(
  sta: option.Option(StaConfig),
  ap: option.Option(ApConfig),
  sntp: option.Option(SntpConfig),
  mdns: option.Option(MdnsConfig),
) -> Result(Nil, Error)

Start the network with optional STA, AP, SNTP, and mDNS sections.

At least one of sta or ap should be provided. STA+AP is supported.

See AP mode and STA+AP mode.

pub fn stop() -> Result(Nil, Error)

Stop the network interface.

pub fn wait_for_ap(
  ssid: option.Option(String),
  psk: option.Option(String),
  timeout_ms: Int,
) -> Result(Nil, Error)

Blocking convenience: start AP and wait until ready (network:wait_for_ap/2).

Omit ssid / psk (None) to use AtomVM defaults (generated SSID / open AP).

See AP Mode Convenience Functions.

pub fn wait_for_ap_default() -> Result(Nil, Error)

Start AP with empty config and default 15_000 ms timeout (network:wait_for_ap/0).

See AP Mode Convenience Functions.

pub fn wait_for_ap_timeout(timeout_ms: Int) -> Result(Nil, Error)

Equivalent to wait_for_ap(None, None, timeout_ms) (network:wait_for_ap/1 timeout clause).

pub fn wait_for_sta(
  ssid: String,
  psk: String,
  timeout_ms: Int,
) -> Result(IpInfo, Error)

Blocking convenience: start STA with ssid/psk and wait for DHCP (network:wait_for_sta/2).

Useful for simple apps; badge-style UIs should prefer start + event messages.

See STA Mode Convenience Functions.

pub fn wait_for_sta_config(
  ssid: option.Option(String),
  psk: option.Option(String),
) -> Result(IpInfo, Error)

Blocking convenience: start STA with optional credentials and default 15_000 ms timeout (network:wait_for_sta/1 config clause).

Omit ssid / psk (None) for an empty STA property list (NVS / last config).

See STA Mode Convenience Functions.

pub fn wait_for_sta_default() -> Result(IpInfo, Error)

Blocking convenience: start STA with empty config and default 15_000 ms timeout (network:wait_for_sta/0).

Useful for simple apps; badge-style UIs should prefer start + event messages.

See STA Mode Convenience Functions.

pub fn wait_for_sta_timeout(
  timeout_ms: Int,
) -> Result(IpInfo, Error)

Blocking convenience: start STA with empty config and timeout_ms (network:wait_for_sta/1 timeout clause).

See STA Mode Convenience Functions.

pub fn wifi_scan(results: Int) -> Result(Nil, Error)

Start an async Wi-Fi scan requesting up to results APs (network:wifi_scan/1).

Requires scan_done → notify from start. Results arrive as {scan_results, …} on the notify pid.

See wifi_scan.

pub fn wifi_scan_default() -> Result(Nil, Error)

Start a Wi-Fi scan with defaults from the running STA config (network:wifi_scan/0).

Uses default_scan_results, dwell, passive, and hidden settings from the last start STA config when available. With scan_done → notify, results arrive as {scan_results, …} on the notify pid.

See wifi_scan.

✨ Search Document