Tuesday, February 19, 2013

Wifi autoconnect RaspberryPi

I wanted my RaspberryPi to autoconnect through wifi on a spcified Access Point (AP).
To do this, first I copied /etc/wpa_supplicant/wpa_supplicant.conf to /etc/ .
You need to tell RaspberryPi to connect to your wifi hotspot (for example, your wifi router).
You can than output the wpa_passphrase to /etc/wpa_supplicant.conf by issuing the following command:
sudo wpa_passphrase myssid "this is test" >> /etc/wpa_supplicant.conf 
myssid could be your wifi's ssid and "this is test" could be your wifi's password or code.

In /etc/wpa_supplicant.conf I added the following lines after the psk line:
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN


...so it will look something like:
network={
        ssid="myssid"
        #psk="this is test"
        psk=fa5d92f2b069636fd3fe65e2376f2061b37467f8908ca373bbf10f7c92cd37b9

        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
}


So that's the part of wpa_supplicant.conf part, now we have to modify /etc/network/interfaces.
We add the following lines:
allow-hotplug wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant.conf -B


So, this is it. I have an Android phone, which has a wifi hotspot feature. If you turn a wifi hotspot on (or your own wifi router at home), your RaspberryPi should automatically connect. Make sure you rebooted your Raspberry and make sure you used the right SSID and passphrase of your wifi AP.

A small problem I encountered is that I had no connection on the tcp/ip layer when turning off and on my AP while RasPi was running. There was a physical connection though, but somehow the upper layers was dead. It turns out that wpa_supplicant wasn't cleaned up properly (not killed) and there somehow went wrong. After googling around for solutions, I could only find selfmade scripts which I don't really like (like checking for connection status every 60 sec. or something). I also didn't try netcfg, because I was afraid it will conflict with other default network managers (I'm not sure about that).
The solution I found is as follows:
  • Copy /etc/wpa_supplicant/ifupdown.sh to /etc/ifplugd/action.d/ifupdown (make sure you make a copy of /etc/ifplugd/action.d/ifupdown, before you overwrite it).
  •  sudo reboot
That's it! I do see some error messages in /var/log/syslog, but besides that, it works. Now I can turn off and on AP, my RasPi will automatically reconnect with a working the tcp/ip layer.

2 comments:

  1. Just for the records:
    With this solution no files are overwritten and no error messages in /var/log/syslog:

    http://forum.xbian.org/newreply.php?tid=455&pid=5072

    ReplyDelete
    Replies
    1. I'm not allowed to see the page as I'm not registered. Is there another way you can show me what you exactly mean?

      Delete

NSNotification example