Setting WiFi up via the command line on Raspberry Pi, using a USB Wireless Adapter
These are notes on how to setup WiFi on the Raspberry Pi. The R-Pi is a model 2 running Raspbian 4.1.19+.
In my case and in the example that follows, the Raspberry Pi is connected to an Ethernet network, static IP at 192.168.1.17. This is the address that I am logging in via SSH to get to the command line to configure the USB WiFi. Adapter.
USB WiFi Adapters
Two USB Wifi’s were tried a Belkin N300 and an Edimax EW7811Un. Both use the Realtek RTL8192CU chip and work well with the R-pi. Initial testing was with the Belkin and the output from this device is used in this post for the command line examples.
Edimax EW7811Un 150Mbps 11n WiFi USB Adapter, it is nano size and has a blue activity light. It works well, can’t imagine how small the antenna is in there and how they get RF to work out OK with these sub wavelength antennas!
NOTE: Originally the adapters were tried by plugging them into a powered USB hub which plugged into the R-pi. This allows for hot-plugging. If a device is hot-plugged directly into the R-Pi it will force a reboot, at least on the one that I have (R-Pi Model 2B). This is probably due to an inrush current that pulls down the power bus momentarily, I am guessing. The powered USB hub isolates the R-Pi from the devices connected to it as far as power is concerned and things will hot plug fine using it. I did realize later on that when I plugged the USB WiFi adapter directly into the R-Pi, I got more stable behavior with it. As in less strange dropouts of the WiFi from the network. It maintains a network connection better for me at least, plugged in directly.
Initial Testing
The first steps involve checking to see that the adapter is detected, registered and the correct device driver is loaded. They are just confirmation that all is well. They can be skipped and then ran later if problems arise and troubleshooting is needed.
Plug in USB WiFi adapter and run a lsusb and dmesg…
lsusb
erick@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 2101:8500 ActionStar
Bus 001 Device 005: ID 2101:8501 ActionStar
Bus 001 Device 006: ID 154b:005b PNY
Bus 001 Device 008: ID 050d:2103 Belkin Components F7D2102 802.11n N300 Micro Wireless Adapter v3000 [Realtek RTL8192CU]
Bus 001 Device 007: ID 174c:1153 ASMedia Technology Inc.
The device shows up fine using lsusb. Now on to dmesg to see if the correct driver loaded…
[156238.694964] usb 1-1.3.3: new high-speed USB device number 8 using dwc_otg
[156238.797146] usb 1-1.3.3: New USB device found, idVendor=050d, idProduct=2103
[156238.797188] usb 1-1.3.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[156238.797207] usb 1-1.3.3: Product: Belkin Wireless Adapter
[156238.797225] usb 1-1.3.3: Manufacturer: Realtek
[156238.797242] usb 1-1.3.3: SerialNumber: 00e04c000001
[156239.201673] usbcore: registered new interface driver rtl8192cu
Kernel driver rtl8192cu is loaded, all should be well with the adapter!
lsmod
The following lsmod was run and it confirms the kernel module is loaded for the 8192cu driver. It is just added confirmation that all is well.
erick@raspberrypi ~ $ lsmod
Module Size Used by
xt_state 1434 1
ipt_MASQUERADE 1220 1
nf_nat_masquerade_ipv4 2814 1 ipt_MASQUERADE
iptable_nat 2120 1
nf_nat_ipv4 6162 1 iptable_nat
nf_nat 17132 2 nf_nat_ipv4,nf_nat_masquerade_ipv4
8192cu 556175 0
nfsd 263815 11
nf_conntrack_ipv4 14388 3
nf_defrag_ipv4 1766 1 nf_conntrack_ipv4
xt_conntrack 3420 1
nf_conntrack 95316 6 nf_nat,xt_state,nf_nat_ipv4,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_ipv4
iptable_filter 1698 1
ip_tables 12362 2 iptable_filter,iptable_nat
x_tables 18590 5 ip_tables,ipt_MASQUERADE,xt_state,xt_conntrack,iptable_filter
i2c_dev 6386 4
snd_bcm2835 22502 0
snd_pcm 92861 1 snd_bcm2835
snd_seq 58152 0
snd_seq_device 5142 1 snd_seq
snd_timer 22156 2 snd_pcm,snd_seq
snd 67534 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
sg 20575 0
i2c_bcm2708 5988 0
bcm2835_gpiomem 3703 0
bcm2835_rng 2207 0
uio_pdrv_genirq 3526 0
uio 10078 1 uio_pdrv_genirq
Setup the WiFi Connection
In this example this is a WPA type of security. I know the SSID and password and just put them into the wpa_supplicant configuration file. If you need to see what WiFi nodes are available on the network consider using the scan …
( IF Needed )
sudo iwlist wlan0 scan
Check /etc/network/interfaces
Check to see that the section exists in the file that will allow the USB apapter to hot plug and also the wpa-roam line that points to the wpa_supplicant.conf file where the SSID and password will be stored in the next step.
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
…any changes to the interfaces file will require a reboot or a restart of networking to take effect, via….
sudo service networking restart
If you are running the R-Pi headless it will disconnect from SSH and will require a re-login. If a mistake is made in the interfaces file, it might not come back and require connecting a keyboard and monitor to reconnect. The good news about having both a running eth0 and wlan0 is that if you make a mistake in only one of them it will be possible to connect via the other one. Less chances of being totally locked out by a small typo in the interfaces file. Sometimes a restart of network services will cause a non-recoverable dropout which requires a reboot to get an SSH connection going again.
Config wpa_supplicant
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Go to the bottom of the file and add the following type of entry, putting in the correct SSID and pwd:
network={
ssid="SprintHotspot2.4-example"
psk="thepassword"
}
Save and Exit
Then execute the follow to apply the new configuration….
wpa_cli -i wlan0 reconfigure
TEST IT:
ifconfig wlan0
Results show that the interface is up and and running
erick@raspberrypi ~/Music/music-test $ ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr 74:df:e0:e0:0e:e0
inet addr:192.168.128.46 Bcast:192.168.128.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4023854 errors:0 dropped:1664207 overruns:0 frame:0
TX packets:2955774 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3003299485 (2.7 GiB) TX bytes:1956045437 (1.8 GiB)
It should be working at this stage. Trying to reach the net using something like ping google.com should give good results. If not more troubleshooting is required. I had to do the next step to get it to reach the net on the wireless network as it was trying to use the Ethernet connection to the router to get out, set as the default gateway, which was not hooked up to the web at all. Just a router at 192.168.1.1 and no WAN port connection.
Additional Step
The following bit may or may not apply for everyone. But, I am adding here as it was not obvious at the moment I got WiFi up. I had to think on it a bit! Basically a default route has to exist that takes it to a gateway to the Internet.
IN ORDER TO GET PI OUT ON INTERNET NEEDED TO DO A
sudo route add default gw 192.168.128.1 wlan0
As there was no route out and it must pick eth0 by priority! Needs a route to the Sprint Box that I am connected to the net on the 192.168.128.0 network.
THE ABOVE WOULD HAVE TO HAPPEN ON EVERY REBOOT OR NETWORK REFRESH! Or just get rid of default gateway on ETH0 and it might just pick up the gateway on the wlan0 all of the time! If both are dhcp the eth0 gateway will always be treated as preferred, so going static will get rid of it as I was planning on using this R-pi as a bridge from LAN to WLAN! This required editing /etc/network/interfaces to remove the 192.168.1.1 router as a gateway. The router was reconfigured by turning off DHCP on it, effectively making it an access point for WiFi. Essentially it becomes another path to the Internet along with the WiFi hotspot.
erick@raspberrypi ~ $ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.128.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
erick@raspberrypi ~ $ sudo route add default gw 192.168.128.1 wlan0
erick@raspberrypi ~ $ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.128.1 0.0.0.0 UG 0 0 0 wlan0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.128.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
SHOWS UP IN /etc/resolv.conf as well….
erick@raspberrypi ~ $ cat /etc/resolv.conf
domain router
search router
nameserver 192.168.128.1
ip show route as well…
erick@raspberrypi ~ $ ip route show
default via 192.168.128.1 dev wlan0
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.17
192.168.128.0/24 dev wlan0 proto kernel scope link src 192.168.128.46
/etc/network/interfaces
The following /etc/network/interfaces file was edited to make the wlan0 connection which connected to the Internet work for the R-Pi.
Note the eth0 connection is setup static on the 192.168.1.0 wired network. The wlan0 connection is set for dhcp on the 192.168.128.0. The default gateway at 192.168.1.1 ( the router ) is commented out to allow the default to fall on the 192.168.128.1 WiFi router, which is a ZTE WiFi hotspot, basically a repeater from 4G LTE cell to WiFi.
Note the wpa-roam points to the wpa_supplicant file that has the SSID and password entered earlier in this post to get the WiFi going.
erick@raspberrypi ~/Music/music-test $ sudo cat /etc/network/interfaces
[sudo] password for erick:
auto lo
iface lo inet loopback
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.17
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
# Remove gateway to see if it fails-over to wlan0 gateway on 192.168.128.1 12242017
# gateway 192.168.1.1
# nameservers
dns-nameservers 8.8.8.8 8.8.4.4
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
ALL UP AND RUNNING OK
Running ip a shows all interfaces. Note that the R-pi is connected to wlan0 on the 192.168.128.46 address and eth0 is connected at 192.168.1.17, both networks are now available.
erick@raspberrypi ~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether b8:2a:eb:2a:a4:2a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.17/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 08:8a:3c:ba:83:8a
brd ff:ff:ff:ff:ff:ff
inet 192.168.128.46/24 brd 192.168.128.255 scope global wlan0
valid_lft forever preferred_lft forever
The connection to the WiFi node can be confirmed via iwconfig wlan0…
erick@raspberrypi ~ $ iwconfig wlan0
IEEE 802.11bg ESSID:"SprintHotspot2.4-example" Nickname:"<WIFI@REALTEK>"
Mode:Managed Frequency:2.452 GHz Access Point: 34:3A:87:3A:BA:3A
Bit Rate:54 Mb/s Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality=100/100 Signal level=63/100 Noise level=0/100
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
lo no wireless extensions.
eth0 no wireless extensions.