Kaluma
Search…
Wi-Fi
THIS IS EXPERIMENTAL AND SUBJECT OF CHANGE
The wifi module supports to scan, connect and disconnect Wi-Fi networks. Use require('wifi') to access this module.
This module requires a IEEE 802.11 device driver. If the board you are using is not capable for Wi-Fi, you may need to inject external Wi-Fi device driver.

Class: WiFi

An instance of WiFi represents a Wi-Fi device.

new WiFi()

Create an instance of Wi-Fi class.
1
var WiFi = require('wifi').WiFi;
2
var wifi = new WiFi();
Copied!

wifi.reset([callback])

  • callback {Function} Called when reset complete.
Reset the Wi-Fi device.

wifi.scan([callback])

  • callback{Function}
    • err {Error}
    • scanResults {Array<Object>}
      • security {string} Security. OPEN, or multiple of WEP, WPA, PSK, WPA2, WPA2-EAP.
      • ssid {string} SSID.
      • rssi {number} Received signal strength indication.
      • bssid {string} **BSSID. (Typically MAC address)
      • channel {number} Channel.
Scan Wi-Fi networks.
1
const {WiFi} = require('wifi');
2
const wifi = new WiFi();
3
4
wifi.scan((err, scanResults) => {
5
if (err) {
6
console.error(err);
7
} else {
8
console.log(scanResults);
9
}
10
});
Copied!

wifi.connect([connectInfo][, callback])

  • connectInfo {object} Information to connect a Wi-Fi network.
    • ssid {string} SSID.
    • password {string} Password.
    • enforce {boolean} When set to true, enforce to connect even if there is already a Wi-Fi connection. Otherwise, do not try to connect if there is Wi-Fi connection. Default: false.
  • callback {Function} A callback function called when a Wi-Fi connection is established. This is also called when there is already a Wi-Fi connection.
    • err {Error}
    • connectInfo {Object}
Establish a connection to Wi-Fi network.
1
const {WiFi} = require('wifi');
2
const wifi = new WiFi();
3
4
wifi.connect({ssid:'MyHome', password:'12345678'}, err => {
5
if (err) {
6
console.error(err);
7
} else {
8
// add your code using Wi-fi connection
9
}
10
});
Copied!
If you do not want to expose your Wi-Fi connection info, you can set them in the storage. (Do not add item in the storage in the source code, add manually in Terminal)
1
storage.setItem('WIFI_SSID', 'MyHome');
2
storage.setItem('WIFI_PASSWORD', '12345678');
Copied!
And then, call connect method without connectInfo argument as below.
1
const {WiFi} = require('wifi');
2
const wifi = new WiFi();
3
4
wifi.connect(err => {
5
if (err) {
6
console.error(err);
7
} else {
8
// add your code using Wi-fi connection
9
}
10
});
Copied!

wifi.disconnect([callback])

  • callback {Function}
    • error {Error}
Disconnect from currently connected Wi-Fi network.

wifi.getConnection([callback])

  • callback {Function}
    • error {Error}
    • connectionInfo {object}
      • ssid {string}
      • bssid {string}
Get connection information of currently connected Wi-Fi network.

Event: 'associated'

This event is emitted when Wi-Fi network is connected. IP may not be assigned.

Event: 'connected'

This event is emitted when Wi-Fi network is connected with IP assignment.

Event: 'disconnected'

This event is emitted when Wi-Fi network is disconnected.
Last modified 7mo ago