Auto Start your WiFi on Raspberry Pi
So I just got my new Hawking HWUN3 USB wireless N adapter that I purchased for my Raspberry Pi, and wanted it to connect on boot being I only access it via SSH and to plug it in to a wired adapter in order to start the wireless connection so that I can unplug it just didn’t make sense. So here’s how I got it installed and starting on boot.
This network adapter is perfect, I didn’t need to compile a thing, it was recognized right out of the box. LOVE IT!
I ran ifconfig and it was right there, happy and waiting.
[codesyntax lang="bash" lines="no" title="ifconfig output"]
wlan0 Link encap:Ethernet HWaddr 00:0e:3b:1e:b8:4d
inet addr:192.168.0.51 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:464 errors:0 dropped:0 overruns:0 frame:0
TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:68999 (67.3 KiB) TX bytes:40100 (39.1 KiB)
[/codesyntax]
Then you’ll need to do a little bash scripting, you can create a script in “/etc/init.d/” and then run “update-rc.d scriptname defaults” to install it, however I decided to instead just add it to the end of “/etc/rc.local” as this will run after everything else has already loaded after a reboot. Of course you can have init start your script at the end of the boot cycle (or anywhere in between) but the rc.local just takes less planning.
By default the “/etc/rc.local” file does nothing, but I left the existing code in and just commented it out. You can do what you like of course. Here is the contents of my rc.local file
[codesyntax lang="bash" lines="normal" title="rc.local"]
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0″ on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
## Print the IP address
#_IP=$(hostname -I) || true
#if [ "$_IP" ]; then
# printf “My IP address is %s\n” “$_IP”
#fi
echo “Starting WiFi…”
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
sleep .5s
dhclient wlan0
echo “WiFi should be started”
exit 0
[/codesyntax]
The code doesn’t actually do anything until line 18. On line 19 you’ll see that we’re starting the wpa_supplicant in the background (-B) for interface wlan0 (-i wlan0) using the configuration file found at “/etc/wpa_supplicant.conf” (-c)
We will need to create that file. Also I should note, wpasupplicant was already installed on my image, however if you need it installed, just issue the following command.
[codesyntax lang="bash" lines="no" title="install wpa supplicant"]
|
1 |
sudo apt-get install wpasupplicant |
[/codesyntax]
now let’s create the ever important wpa_supplicant.conf file. This will hold your ssid name and pre-shared key (password) of course replace YOURSSID and YOURPASSWORD with your own info respectively.
[codesyntax lang="bash" lines="fancy" title="create wpa_supplicant.conf file"]
|
1 |
sudo nano /etc/wpa_supplicant.conf |
[/codesyntax]
and the contents of that file should look something like this:
[codesyntax lang="bash" lines="fancy" title="wpa_supplicant.conf"]
|
1 2 3 4 |
network={ ssid="YOURSSID" psk="YOURPASSWORD" } |
[/codesyntax]
That’s it! You should be connected! Go ahead and reboot, and then run iwconfig and you should be connected to your designated access point, all without using the GUI!
Here’s a pic of my pi, with the usb stick, and some ram heat sinks I picked up at Frys for like $10 ( and I still have 6 more lying around now.. guess I’ll need more pi!)
And here’s a screen dump showing it recognized by my pi.
[codesyntax lang="text" lines="fancy" title="pi screendump"]
Using username “pi”.
pi@192.168.0.51′s password:
Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Type ‘startx’ to launch a graphical session
Last login: Wed Jul 25 19:45:17 2012 from 192.168.0.119
pi@raspberrypi ~ $ iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11bgn ESSID:”editedformysaftey”
Mode:Managed Frequency:2.452 GHz Access Point: BC:C5:C3:6C
Bit Rate=65 Mb/s Tx-Power=20 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=70/70 Signal level=-37 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:47 Invalid misc:3 Missed beacon:0
pi@raspberrypi ~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:16:60
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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1104 (1.0 KiB) TX bytes:1104 (1.0 KiB)
wlan0 Link encap:Ethernet HWaddr 00:3b:1e:b8
inet addr:192.168.0.51 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:464 errors:0 dropped:0 overruns:0 frame:0
TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:68999 (67.3 KiB) TX bytes:40100 (39.1 KiB)
pi@raspberrypi ~ $ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0e66:0013 Hawking Technologies HWUN3 Hi-Gain Wireless-N Adapter [Ralink RT3070]
pi@raspberrypi ~ $
[/codesyntax]
One thing to note, your network led’s wont be working, because your not using your nic…. You’ll want to watch the led on the actual usb stick to see if there’s activity. If it’s up and online, the light will be steady green, and only flash with transmissions. If it’s constantly off and only blinks every few seconds, something’s wrong and you’ll need to do some troubleshooting.
Hope this helps someone, and if not, at least I can forget about how I did it until my next pi comes in a few more days!

Google Chrome 21.0.1180.83 on
Windows 7 x64 Edition
Mac OS X 10.6.8
Firefox 15.0 on
GNU/Linux x64
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
Nice article. Unfortunately, it didn’t work for me… I got these errors:
rfkill: Cannot open RFKILL control device
ioctl[SIOSCIWAP]: Device or resource busy
ioctl[SIOSCIWESSID]: Device or resource busy
I did an strace on the wpasupplicant command and it appears it is trying to access /dev/rfkill which doesn’t exist in my build. What build are you using?