BBB WiFi

From Klaus' wiki
Revision as of 09:46, 25 May 2015 by Klaus (Talk | contribs)

Jump to: navigation, search

If you are going to add a WiFi adapter you should search eBay, Alibaba or your local HW pusher for a Realtek RTL8188CUS or similar adapter. They will usually work right out of the box without any external power.

Plug in the adapter into the USB interface.

Issue

]$ lsusb

expect a result like this

root@beaglebone:~# lsusb
Bus 001 Device 002: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Next issue

]$ ifconfig -a

expect something like this output

eth0      Link encap:Ethernet  HWaddr 1c:ba:8c:e7:40:03  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:40 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr f6:7f:36:73:b5:93  
          inet addr:192.168.7.2  Bcast:192.168.7.3  Mask:255.255.255.252
          inet6 addr: fe80::f47f:36ff:fe73:b593/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:774 errors:0 dropped:0 overruns:0 frame:0
          TX packets:540 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:62696 (61.2 KiB)  TX bytes:94951 (92.7 KiB)

wlan0     Link encap:Ethernet  HWaddr 00:13:ef:20:06:33  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:5 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

From this we can see that the WiFi adapter was recognised by the Linux OS and given the device name wlan0.

In order to have the wiFi to auto connect at boot time issue

]$ vi /etc/network/interfaces

In this file uncomment the lines following the #WiFi example

auto wlan0
iface wlan0 inet dhcp
   wpa-ssid "YourSSID"
   wpa-psk  "YourVerySecretPassword"

save the file and execute this command

]$ ifconfig wlan0 up

This will start the wlan0, but nothing is output for your information.

Issue

]$ ifup wlan0

This will give you an output like the one shown below

ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/00:NN:NN:NN:NN:NN
Sending on   LPF/wlan0/00:NN:NN:NN:NN:NN
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 7
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 12
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.10.1
DHCPACK from 192.168.10.1
bound to 192.168.10.110 -- renewal in 35998 seconds.

You have got a new IP address for the WiFi interface. Note it, most accesspoints re-issue the same IP address if the device reconnects frequently - otherwise it may be an idea to consult the accesspoint to see if it is possible to configure that a specific MAC addresse is connected to a specific IP addresse often it is possible.

A DHCP client can ask for being assigned to a specific IP address. In order to increase the chances for having the same IP address at every boot you can add two extra lines in the /etc/network/interfaces file just below the settings for wlan0

auto wlan0
iface wlan0 inet dhcp
   wpa-ssid "YourSSID"
   wpa-psk  "YourVerySecretPassword"
   address 192.168.10.110
   gateway 192.168.10.1

Now in order to check if the WiFi comes up automagically after a reboot perform a reboot of the system.

Sometime my BBB does not get an IP address. After digging into the log files I found that the driver does not always get the device ready for DHCP'ing in time.

I stumbled upon this on AdaFruit.

]$ apt-get update && apt-get install git
]$ git clone https://github.com/adafruit/wifi-reset.git
]$ cd wifi-reset
]$ chmod +x install.sh
]$ ./install.sh

after installing this the BBB often come up on WiFi - but sometimes it takes minutes to get everything registered and running.

Sometimes this is not enough, so I improved the wifi-reset.sh script and run it every five minutes from cron. So now the waiting time is no more than five minutes.

The improved script look like this, where I also added a lock functionality in order to have only one cope running at any time

#!/bin/bash
# Bring all wifi interfaces down.
# Identify wifi interfaces as rows from standard output of iwconfig (NOT standard
# error, those are non-wifi interfaces) which start without whitespace.
 
# Only run one copy
lock=/tmp/cleanup_tcpserver.LCK
[ -f ${lock} ] && ps -e | awk '{ if( $1 == PID ) {EX=0;exit} }
END {exit EX}' EX=1 PID="`cat ${lock}`" && exit 2
echo " $$ " > ${lock}
 
ASSOC=`iwconfig wlan0|grep -o "Not-Associated"`
while [ ! -z ${ASSOC} ]; do
        echo "Running wlan setup"
        iwconfig 2> /dev/null | grep -o '^[[:alnum:]]\+' | while read x; do ifdown $x; done
        # Bring all wifi interfaces up.
        iwconfig 2> /dev/null | grep -o '^[[:alnum:]]\+' | while read x; do ifup $x; done
        sleep 30
        ASSOC=`iwconfig wlan0|grep -o "Not-Associated"`
done

the script looks for the phrase "Not-Associated" in the output from iwconfig. If there is no association with an accesspoint we will reset all the wlans and after that enable them once again. The script will loop every thirty seconds until we get an association with an accesspoint. The looping is necessary because I encountered accesspoints that did not issue a new IP address until after some unspecified delay time.

In /etc/crontab add this line

#m h dom mon dow user  command
*/5  *    * * *   root   /opt/wifi-reset/wifi-reset.sh

which will cause cron to execute the script every five minutes. It can probably run more often if needed but this works for me and is OK.