Difference between revisions of "BBB WiFi"

From Klaus' wiki
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
Next issue
 
Next issue
 
<source lang=bash>
 
<source lang=bash>
root@beaglebone:~# ifconfig -a
+
]$ ifconfig -a
 
</source>
 
</source>
 
expect something like this output
 
expect something like this output
Line 55: Line 55:
 
In order to have the wiFi to auto connect at boot time issue
 
In order to have the wiFi to auto connect at boot time issue
 
<source lang=bash>
 
<source lang=bash>
root@beaglebone:~# vi /etc/network/interfaces
+
]$ vi /etc/network/interfaces
 
</source>
 
</source>
 
In this file uncomment the lines following the #WiFi example
 
In this file uncomment the lines following the #WiFi example
Line 68: Line 68:
 
]$ ifconfig wlan0 up
 
]$ ifconfig wlan0 up
 
</source>
 
</source>
This will start the wlano, but nothing is output for your information.
+
This will start the wlan0, but nothing is output for your information.
  
 
Issue
 
Issue
Line 94: Line 94:
  
 
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.
 
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.
 
Now in order to check if the WiFi comes up automagically after a reboot perform a reboot of the system.
Line 109: Line 118:
 
</source>
 
</source>
  
after installing this the BBB always come up on WiFi - but sometimes it takes minutes to get everything registered and running.
+
after installing this the BBB often come up on WiFi - but sometimes it takes minutes to get everything registered and running.
  
Sometimes this is 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.
+
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
+
The improved script look like this, where I also added a lock functionality in order to have only one cope running at any time
 
<source lang=bash>
 
<source lang=bash>
#!/bin/bash
+
!/bin/bash
 
# Bring all wifi interfaces down.
 
# Bring all wifi interfaces down.
 
# Identify wifi interfaces as rows from standard output of iwconfig (NOT standard
 
# Identify wifi interfaces as rows from standard output of iwconfig (NOT standard
 
# error, those are non-wifi interfaces) which start without whitespace.
 
# error, those are non-wifi interfaces) which start without whitespace.
ASSOC=`iwconfig wlan0|grep -o "Not-Associated"`
+
 
 +
# Only run one copy
 +
lock=/tmp/wifireset.LCK
 +
[ -f ${lock} ] && ps -e | awk '{ if( $1 == PID ) {EX=0;exit} }
 +
END {exit EX}' EX=1 PID="`cat ${lock}`" && exit 2
 +
echo " $$ " > ${lock}
 +
 
 +
WLAN=`ifconfig -a|grep wlan|cut -d" " -f1`
 +
 
 +
# Get status of $WLAN
 +
ASSOC=`iwconfig ${WLAN}|grep -o "Not-Associated"`
 +
 
 +
# Try to shutdown and restart the interface
 
while [ ! -z ${ASSOC} ]; do
 
while [ ! -z ${ASSOC} ]; do
 
         echo "Running wlan setup"
 
         echo "Running wlan setup"
Line 126: Line 147:
 
         iwconfig 2> /dev/null | grep -o '^[[:alnum:]]\+' | while read x; do ifup $x; done
 
         iwconfig 2> /dev/null | grep -o '^[[:alnum:]]\+' | while read x; do ifup $x; done
 
         sleep 30
 
         sleep 30
         ASSOC=`iwconfig wlan0|grep -o "Not-Associated"`
+
         ASSOC=`iwconfig ${WLAN}|grep -o "Not-Associated"`
 
done
 
done
 +
 
</source>
 
</source>
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 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 cron add this line
+
In /etc/crontab add this line
 
  #m h dom mon dow user  command
 
  #m h dom mon dow user  command
  /5  *    * * *  root  /opt/wifi-reset/wifi-reset.sh
+
  */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 i OK.
+
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.

Latest revision as of 16:12, 31 October 2015

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/wifireset.LCK
[ -f ${lock} ] && ps -e | awk '{ if( $1 == PID ) {EX=0;exit} }
END {exit EX}' EX=1 PID="`cat ${lock}`" && exit 2
echo " $$ " > ${lock}
 
WLAN=`ifconfig -a|grep wlan|cut -d" " -f1`
 
# Get status of $WLAN
ASSOC=`iwconfig ${WLAN}|grep -o "Not-Associated"`
 
# Try to shutdown and restart the interface
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 ${WLAN}|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.