Module to connect a Raspberry Pi to Wi-Fi
$ npm install rpi-wifi-connection --save
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
Constructs a new wifi connection object.
- iface - Specifies the name of the interface (default is wlan0)
Connects to the specified network.
- options.ssid - Specifies the network name.
- options.psk - Specifies the password.
- options.timeout - Specifies the number of milliseconds to wait for connection. Default is 60 seconds (60000).
- options.hidden - Should be set to
true
if the network is hidden.
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
wifi.connect({ssid:'my-network', psk:'raspberry'}).then(() => {
console.log('Connected to network.');
})
.catch((error) => {
console.log(error);
});
Return a promise containing the available networks
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
wifi.scan().then((ssids) => {
console.log(ssids);
})
.catch((error) => {
console.log(error);
});
// [ { bssid: 'f4:ca:e5:e7:de:58', signalLevel: -72, frequency: 2467, ssid: 'homo' },
{ bssid: 'f4:ca:e5:e7:de:5a', signalLevel: -72, frequency: 2467, ssid: 'deus' } ]
Returns a promise containing the network status.
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
wifi.getStatus().then((status) => {
console.log(status);
})
.catch((error) => {
console.log(error);
});
// { ssid: 'Julia', ip_address: '10.0.1.189' }
Returns a promise containing the connection state. Please note that this only returns the connection state of your Raspberry Pi for any network. To see if you are connected to a specific network, use getStatus().
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
wifi.getState().then((connected) => {
if (connected)
console.log('Connected to network.');
else
console.log('Not connected to network.');
})
.catch((error) => {
console.log(error);
});
Returns a promise containing a list of Wi-Fi networks.
var Wifi = require('rpi-wifi-connection');
var wifi = new Wifi();
wifi.getNetworks().then((networks) => {
console.log(networks);
});
// [ { id: 0, ssid: 'Julia' } ]