Raspberry PiでIoTなシステム開発:nodeからwifi接続をコントロールする

nodeからRaspberry Piのwifi接続をコントロールしたいなぁ、でもexecをいちいち叩いていくのは面倒だし、と思っていたら、moduleを作ってくれている人がいました。ネットには神が沢山います。wifi-control このmoduleは実際にはnmcliのフロントエンドに過ぎないので、使用するためにはまずnmcliをインストールします。

pi@raspberrypi:~ $ sudo apt-get install network-managerReading package lists... DoneBuilding dependency treeReading state information... DoneThe following packages were automatically installed and are no longer required:  fuse libfuse2Use 'apt-get autoremove' to remove them.The following extra packages will be installed:  dconf-gsettings-backend dconf-service dns-root-data dnsmasq-base glib-networking glib-networking-common glib-networking-services  gsettings-desktop-schemas iputils-arping libdbus-glib-1-2 libdconf1 libgudev-1.0-0 libjim0.75 libmbim-glib4 libmbim-proxy  libmm-glib0 libmnl0 libndp0 libnetfilter-conntrack3 libnm-glib4 libnm-util2 libpam-systemd libpcap0.8 libpolkit-agent-1-0  libpolkit-backend-1-0 libpolkit-gobject-1-0 libproxy1 libqmi-glib1 libsoup2.4-1 libteamdctl0 modemmanager policykit-1 ppp  usb-modeswitch usb-modeswitch-dataSuggested packages:  avahi-autoipd libteam-utils comgt wvdialThe following NEW packages will be installed:  dconf-gsettings-backend dconf-service dns-root-data dnsmasq-base glib-networking glib-networking-common glib-networking-services  gsettings-desktop-schemas iputils-arping libdbus-glib-1-2 libdconf1 libgudev-1.0-0 libjim0.75 libmbim-glib4 libmbim-proxy  libmm-glib0 libmnl0 libndp0 libnetfilter-conntrack3 libnm-glib4 libnm-util2 libpam-systemd libpcap0.8 libpolkit-agent-1-0  libpolkit-backend-1-0 libpolkit-gobject-1-0 libproxy1 libqmi-glib1 libsoup2.4-1 libteamdctl0 modemmanager network-manager  policykit-1 ppp usb-modeswitch usb-modeswitch-data0 upgraded, 36 newly installed, 0 to remove and 13 not upgraded.Need to get 190 kB/5,431 kB of archives.After this operation, 20.8 MB of additional disk space will be used.Do you want to continue? [Y/n] YWARNING: The following packages cannot be authenticated!  libdbus-glib-1-2 libdconf1 libgudev-1.0-0 libmbim-glib4 libmm-glib0 libndp0 libmnl0 libnetfilter-conntrack3 libnm-util2 libnm-glib4  libpam-systemd libpcap0.8 libpolkit-gobject-1-0 libpolkit-agent-1-0 libpolkit-backend-1-0 libproxy1 libqmi-glib1  glib-networking-common glib-networking-services dconf-service dconf-gsettings-backend gsettings-desktop-schemas glib-networking  libsoup2.4-1 libteamdctl0 libjim0.75 dns-root-data dnsmasq-base libmbim-proxy modemmanager policykit-1 network-manager ppp  iputils-arping usb-modeswitch-data usb-modeswitchInstall these packages without verification? [y/N] y

「インストールするのか?本気かよ」みたいな聞き方をされると不安ではありますが、ここはレポジトリを信頼するしか無いのです。ボットになったらどうしましょ。

Setting up policykit-1 (0.105-8) ...Setting up network-manager (0.9.10.0-7) ...The following network interfaces were found in /etc/network/interfaceswhich means they are currently configured by ifupdown:- eth0- wlan0- wlan1If you want to manage those interfaces with NetworkManager insteadremove their configuration from /etc/network/interfaces.Setting up ppp (2.4.6-3.1) ...Setting up iputils-arping (3:20121221-5) ...

ご丁寧にもinterfacesに書いてあったらダメよと指摘してくれましたので、interfacesからwlan1の設定を削除しておきました。

var WiFiControl = require('wifi-control');export var init = () => {  //  Initialize wifi-control package with verbose output  WiFiControl.init({    debug: true  });  var settings = {    debug: true || false,    iface: 'wlan1',    connectionTimeout: 10000 // in ms  };  WiFiControl.configure(settings);}export var scanForWiFi = () => {  //  Try scanning for access points:  WiFiControl.scanForWiFi( function(err:any, response:any) {    if (err) console.log(err);    console.log(response);  });}

公式のサンプルに従いこんな感じ。するとこんな感じでスキャン結果が取得できました。

WiFiControl: Debug mode set to: trueWiFiControl: Determining system wireless interface...WiFiControl: Host machine is Linux.WiFiControl: Executing: nmcli -m multiline device status | grep wlanWiFiControl: Automatically located wireless interface wlan1.WiFiControl: Debug mode set to: trueWiFiControl: AP connection attempt timeout set to: 10000msWiFiControl: Wireless interface manually set to wlan1.WiFiControl: Scanning for nearby WiFi Access Points...WiFiControl: Nearby WiFi APs successfully scanned (11 found).{ success: true,  msg: 'Nearby WiFi APs successfully scanned (11 found).',  networks:   [ { ssid: 'aterm-d014ce-g',       channel: '6',       signal_level: '70',       security: 'WPA1 WPA2' },     { ssid: 'AP-2F-YA-2.4GHz',       channel: '13',       signal_level: '47',       security: 'WPA1 WPA2' },     { ssid: 'AP-2F-YU-2.4GHz',

“””

コメントをどうぞ

メールアドレスが公開されることはありません。 が付いている欄は必須項目です