Skip to content

wifi ​

ts
import {
wifi
} from 'mikro/wifi'

The default export is a Wifi singleton. Use it to connect to networks, scan for access points, and start an AP.

Connecting ​

ts
const 
result
= await
wifi
.
connect
({
ssid
: 'MyNetwork',
passphrase
: 'password123'})
if (!
result
.
ok
) {
console
.
error
('WiFi failed:',
result
.
error
)
} else {
console
.
log
('Connected, IP: %s',
result
.
value
.
ip
)
}

Station methods ​

wifi.connect(options) ​

ts
connect(options: WifiConnectOptions): Promise<Result<WifiConnectionInfo, WifiError>>

Connects to a WiFi network. Returns connection info with IP, netmask, and gateway on success. Set txPower to cap the radio's transmit power (in dBm) for this session; the driver quantizes it to an allowed level.

ts
interface WifiConnectOptions {
  ssid: string
  passphrase: string
  txPower?: number
}

wifi.disconnect(options?) ​

ts
disconnect(options?: WifiDisconnectOptions): Result<void, WifiError>

Disconnects from the current network. By default this also powers the radio down and releases its memory. Pass {shutdown: false} to keep the radio up for a faster reconnect.

ts
interface WifiDisconnectOptions {
  shutdown?: boolean // default: true
}

wifi.scan(options?) ​

ts
scan(opts?: ScanOptions): Promise<Result<ScanResult[], WifiError>>

Scans for nearby networks. Returns an array of ScanResult objects.

ts
const 
result
= await
wifi
.
scan
()
if (
result
.
ok
) {
for (const
ap
of
result
.
value
) {
console
.
log
('%s %d dBm',
ap
.
ssid
,
ap
.
rssi
)
} }

wifi.rssi() ​

ts
rssi(): Result<number, WifiError>

Returns the signal strength of the current connection in dBm.

wifi.ip() ​

ts
ip(): string | undefined

Returns the current IP address, or undefined if not connected.

wifi.status() ​

ts
status(): WifiStatus

Returns the current connection status.

wifi.ipConfig() ​

Get or set the IP configuration.

ts
// Get current config
ipConfig(): Result<IpConfig | undefined, WifiError>

// Set static IP (or re-enable DHCP)
ipConfig(opts: StaticIpConfig): Result<void, WifiError>

Station properties ​

PropertyTypeDescription
isConnectedbooleanWhether the station is connected
macstring (readonly)MAC address
hostnamestring | undefinedHostname (read/write)
txPowernumber (readonly)Transmit power in dBm; set via connect({txPower})
rssiThresholdnumberLow-RSSI event threshold (read/write)
powerSavePowerSaveModePower save mode: 'none', 'min', 'max'
countryWifiCountryCode | undefinedRegulatory country code

Station events ​

ts
wifi
.
onConnect
.
subscribe
((
info
) => {
console
.
log
('Connected: %s',
info
.
ip
)
})
wifi
.
onDisconnect
.
subscribe
((
reason
) => {
console
.
log
('Disconnected: %s',
reason
)
})
wifi
.
onRssiLow
.
subscribe
((
rssi
) => {
console
.
log
('Signal weak: %d dBm',
rssi
)
})
StreamPayloadDescription
onConnectObservable<WifiConnectionInfo>Connected to network
onDisconnectObservable<WifiDisconnectReason>Disconnected from network
onRssiLowObservable<number>Signal dropped below rssiThreshold

Each property is an Observable<T> (see mikro/observable). Subscribers fire on the JS loop thread; call unsubscribe() on the returned Subscription to stop receiving values.

Access point ​

The wifi.ap sub-interface lets you run the ESP32 as an access point.

wifi.ap.start(options) ​

ts
start(options: ApStartOptions): Result<void, WifiError>
ts
wifi
.
ap
.
start
({
ssid
: 'my-device',
passphrase
: 'secret123'}).
orPanic
('AP failed')

wifi.ap.stop() ​

ts
stop(): Result<void, WifiError>

wifi.ap properties ​

PropertyTypeDescription
isActiveboolean (readonly)Whether the AP is running
ipstring | undefined (readonly)AP IP address
stationsApStationInfo[] (readonly)Connected clients
inactiveTimeoutnumberKick idle clients after N seconds

wifi.ap.deauthStation(mac) ​

ts
deauthStation(mac: string): Result<void, WifiError>

wifi.ap events ​

ts
wifi
.
ap
.
onStationConnect
.
subscribe
((
info
) => {
console
.
log
('client connected: %s',
info
.
mac
)
})
wifi
.
ap
.
onStationDisconnect
.
subscribe
((
info
) => {
console
.
log
('client disconnected: %s',
info
.
mac
)
})
StreamPayloadDescription
onStationConnectObservable<ApStationInfo>Client connected
onStationDisconnectObservable<ApStationInfo>Client disconnected

Types ​

WifiStatus ​

ts
type WifiStatus =
  | 'STOPPED'
  | 'IDLE'
  | 'NO_SSID_AVAIL'
  | 'SCAN_COMPLETED'
  | 'CONNECTED'
  | 'CONNECT_FAILED'
  | 'CONNECTION_LOST'
  | 'DISCONNECTED'

WifiConnectionInfo ​

ts
interface WifiConnectionInfo {
  ip: string
  netmask: string
  gateway: string
}

ScanResult ​

ts
interface ScanResult {
  ssid: string
  bssid: string
  channel: number
  rssi: number
  authMode: AuthMode
  hidden: boolean
}

AuthMode ​

ts
type AuthMode = 'open' | 'wpa2-psk' | 'wpa3-psk' | 'wpa2-wpa3-psk' | 'unknown'

WifiDisconnectReason ​

ts
type WifiDisconnectReason = 'no-ssid' | 'auth-failed' | 'connection-lost' | 'disconnected'

Errors ​

WifiError ​

VariantFieldsDescription
CountryNotSet—Regulatory country not configured
StartFailedmessageFailed to start WiFi
ConnectFailedmessageConnection attempt failed
ConnectInProgress—Already connecting
DisconnectFailedmessageFailed to disconnect
ScanFailedmessageScan failed
ScanInProgress—Already scanning
NotInitialized—WiFi not started
ConfigFailedmessageIP configuration failed
ApStartFailedmessageFailed to start AP
ApStopFailedmessageFailed to stop AP
SetFailedmessageFailed to set property
GetFailedmessageFailed to get property

Mikro.js is built with AI assistance, code and docs alike. Read the AI disclosure.