Skip to content

wifi

ts
import wifi from 'mikrojs/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('MyNetwork', 'password123')
if (!result.ok) {
  console.error('WiFi failed:', result.error.name)
} else {
  console.log('Connected, IP:', result.value.ip)
}

Station methods

wifi.connect(ssid, passphrase)

ts
connect(ssid: string, passphrase: string): Promise<Result<WiFiConnectionInfo, WifiError>>

Connects to a WiFi network. Returns connection info with IP, netmask, and gateway on success.

wifi.disconnect()

ts
disconnect(): Result<void, WifiError>

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(ap.ssid, ap.rssi, 'dBm')
  }
}

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)
txPowernumberTransmit power (read/write)
rssiThresholdnumberLow-RSSI event threshold (read/write)
powerSavePowerSaveModePower save mode: 'none', 'min', 'max'
countryWiFiCountryCode | undefinedRegulatory country code

Station events

ts
wifi.on('connect', (info) => {
  console.log('Connected:', info.ip)
})

wifi.on('disconnect', (reason) => {
  console.log('Disconnected:', reason)
})

wifi.on('rssi-low', (rssi) => {
  console.log('Signal weak:', rssi, 'dBm')
})
EventPayloadDescription
'connect'WiFiConnectionInfoConnected to network
'disconnect'WiFiDisconnectReasonDisconnected from network
'rssi-low'numberSignal dropped below rssiThreshold

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

EventPayloadDescription
'station-connect'ApStationInfoClient connected
'station-disconnect'ApStationInfoClient 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
CountryNotSetRegulatory country not configured
StartFailedmessageFailed to start WiFi
ConnectFailedmessageConnection attempt failed
ConnectInProgressAlready connecting
DisconnectFailedmessageFailed to disconnect
ScanFailedmessageScan failed
ScanInProgressAlready scanning
NotInitializedWiFi not started
ConfigFailedmessageIP configuration failed
ApStartFailedmessageFailed to start AP
ApStopFailedmessageFailed to stop AP
SetFailedmessageFailed to set property
GetFailedmessageFailed to get property