Auto-connect to wireless nets?


31 posts   Page 1 of 2   1, 2
by AaplMike » Tue Jul 17, 2012 5:46 pm
Hey all.
So, I have a wireless dongle and can attach fine to my home wireless network, having set up /etc/network/interfaces to connect to that one ESSID using WPA. Of course, that relies on a very precise configuration.

However, I'll be depending often on mobile use with the RPi, which means no handy monitor and keyboard, so I want to simply power on the RPi and have it automatically join whichever open wireless network it can reach. Since I have the RPi registering itself via Bonjour/Avahi, I can then SSH to it using raspberrypi.local.

And so, how would I configure Debian to auto-connect to any open wireless AP?
Posts: 3
Joined: Fri Jun 29, 2012 4:12 pm
Location: Central California
by SirLagz » Fri Jul 20, 2012 5:41 pm
Crazy idea off the top of my head

Have a script that is run every minute that will -
Check to see if wlan0 is connected with the
Code: Select all
iwconfig
command
if it is then do nothing
if it is not connected
do a wifi scan with
Code: Select all
iwlist wlan0 scan

to
get the name of an open network
and then
Code: Select all
iwconfig wlan0 essid NETWORK_ID
dhclient wlan0

to connect
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by paulk » Fri Jul 20, 2012 9:31 pm
wpa-roam does this exactly.

Have a quick google, otherwise I'll dig out an example config when I can.
Posts: 12
Joined: Wed Jun 27, 2012 10:32 am
by r1cht3r » Sun Jul 22, 2012 4:19 am
This config has been working for me on my Raspbian wheezy install.

/etc/network/interfaces
Code: Select all
auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
   wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface home inet dhcp
iface work inet dhcp
iface default inet dhcp

/etc/wpa_supplicant/wpa_supplicant.conf
Code: Select all
network={
        ssid="Acme Corp"
        scan_ssid=1
        psk=abc123
        id_str="work"
        priority=5

network={
        ssid="Linksys"
        scan_ssid=1
        psk=abc123
        id_str="home"
        priority=5

network={
   key_mgmt=NONE
}
Posts: 1
Joined: Sun Jul 22, 2012 4:09 am
by shawnanastasio » Wed Jul 25, 2012 12:09 am
Although its probably inefficient and takes uses more CPU, I use Wicd. Just install with "apt-get install wicd-gtk" It provides a nice GUI for configuring wireless networks, and there is an option to automatically connect to certain networks
Posts: 19
Joined: Mon May 28, 2012 7:44 pm
by kobzar » Wed Jul 25, 2012 6:22 am
you can used wicd in console to (wicd-cli)
No Fate!
Posts: 37
Joined: Tue Jul 17, 2012 1:09 pm
Location: Ukraine
by Michi » Wed Jul 25, 2012 8:44 am
I'm using wicd-curses...takes 1-2 minutes but works great.

Greets,
Michael
Posts: 13
Joined: Sun Jul 08, 2012 4:06 pm
Location: Germany
by pjc123 » Thu Jul 26, 2012 12:44 am
r1cht3r wrote:This config has been working for me on my Raspbian wheezy install.


Automatically selecting a wireless router with a specific priority is exactly what I need. So I tried your example (modified for my SSID, Password, etc.) and I am getting the following errors. A Google search indicates that something is possibly wrong with the format of the file:

wpa_supplicant: /sbin/wpa_supplicant daemon failed to start
run-parts: /etc/network/if-pre-up.d/wpasupplicant exited with return code 1
Failed to connect to wpa_supplicant - wpa_ctrl_open: No such file or directory
wpa_supplicant: /sbin/wpa_cli daemon failed to start
run-parts: /etc/network/if-up.d/wpasupplicant exited with return code 1

I can connect wirelessly with a simple setup (without using wpa_supplicant), so I know that everything is in working condition.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by pjc123 » Thu Jul 26, 2012 4:56 pm
pjc123 wrote:
r1cht3r wrote:This config has been working for me on my Raspbian wheezy install.


Automatically selecting a wireless router with a specific priority is exactly what I need. So I tried your example (modified for my SSID, Password, etc.) and I am getting the following errors. A Google search indicates that something is possibly wrong with the format of the file:

wpa_supplicant: /sbin/wpa_supplicant daemon failed to start
run-parts: /etc/network/if-pre-up.d/wpasupplicant exited with return code 1
Failed to connect to wpa_supplicant - wpa_ctrl_open: No such file or directory
wpa_supplicant: /sbin/wpa_cli daemon failed to start
run-parts: /etc/network/if-up.d/wpasupplicant exited with return code 1

I can connect wirelessly with a simple setup (without using wpa_supplicant), so I know that everything is in working condition.


Well, solved that problem. The network sections of the wpa_supplicant.conf file are missing the closing curly brackets. However, I still cannot connect to my routers.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by pjc123 » Thu Aug 02, 2012 9:06 pm
Between reading all the /usr/share/doc/wpa_supplicant documentation and some experimenting with the various parameters, I finally have everything working. Now:

1) During bootup, wpa_supplicant searches through each router I have listed in wpa_supplicant.conf (more importantly in the proper order), then chooses the first active router it finds, and finally assigns a DHCP ip address to wlan0. If there is no wifi dongle connected, it uses the wired connection eth0. This operation was critical for my headless pi configuration.

2) I can now connect to my routers that have a hidden SSID (SSID not broadcasted), which was not possible with just the /etc/network file by itself without using wpa_supplicant.conf. I disable broadcasts for security reasons.

3) As an added bonus, the wpa_supplicant file does not think that the character "\" is an escape character in the password field, unlike the /etc/network file by itself without using wpa_supplicant.conf, which required that it be escaped with another escape character "\\".
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by lizdainis » Wed Oct 10, 2012 12:59 pm
pjc123 can you share your script ? autoconnect
Posts: 2
Joined: Wed Oct 10, 2012 12:57 pm
by pjc123 » Wed Oct 10, 2012 9:36 pm
lizdainis wrote:pjc123 can you share your script ? autoconnect


I haven't used the pi in a couple of months, so let me see if it still works. The cat plays with the wiring, so no guarantee. What have you tried so far? Have you read any of the documentation that I pointed to?
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by pjc123 » Thu Oct 11, 2012 12:02 am
lizdainis wrote:pjc123 can you share your script ? autoconnect


First of all, don't expect to just copy my scripts and expect them to magically work for your application. You might find it a lot easier to use one of the gui applications like wicd, although I don't know their capabilities. For me, I like to know what is going on, so I usually work with simple files whenever I configure something. Because I have these files set up for my specific routers, and to perform certain specific tasks, there are probably items you do not need and/or you need to modify for your case. OK, now that the disclaimer is behind us......Oh, wait a minute, one more......The following was set up and tested with Wheezy Debian dated 06-18-2012. OK, some items you might need to change:

1) The config files scan for multiple routers, in the order given in the wpa_supplicant file. I am not sure if that is what you mean by “autoconnect”. If so, the sections labeled win7 and duane in both files are what are used in both files to differentiate each router (Change the names to what you feel is appropriate, as long as they match in both files). If what you mean by “autoconnect” is to connect to public routers that may be available, then you need to un-comment the last four lines of the wpa_supplicant file (Remove the pound symbols). Note that I have not tested that feature. I have seen it used with and without the ssid=”” parameter.

2) Scan for routers with a hidden SSID (Not broadcasted). There are several lines in the files that are specifically set up for this including setting ap_scan=2 and scan_ssid=1. Note that the priority field is ignored when setting ap_scan=2, and the routers are searched for in the order that they appear in the config file. Also, the following four security parameters must be set explicity for each router, meaning not having multiple values on each line, and the values must be exactly correct: key_mgmt, pairwise, group and proto.

3) I have two different wifi dongles that I use (An Edimax and a TP-Link). If you only have one wireless dongle for your pi, then you do not need the wlan1 section in the /etc/network/interfaces file.

4) I am using DHCP. If you are using a static address you will have to modify these files.

5) I am using WPA2. If you are using different authentication (WPA, WEP, etc) you will have to modify these files.

6) You need to enter your SSID (and PSK if using one of the WPA protocols). Do not type in all CAPS like I did, use whatever case you used in your router.

7) Just a side note that I set several other parameters explicitly, rather than relying on multiple or default settings.

On your pi, the files in the directory /usr/share/doc/wpa_supplicant explain what all these parameters mean, and there are various example files for different configurations. In particular, the file associated with the wpa_supplicant.conf file is located at /usr/share/doc/wpa_supplicant/examples/wpa_supplicant.conf.gz.

Here are my config files:

A) /etc/network/interfaces:

Code: Select all
auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
   wpa-driver wext
   wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
   wpa-driver wext
   wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface win7 inet dhcp
iface duane inet dhcp
iface default inet dhcp



B) /etc/wpa_supplicant/wpa_supplicant.conf:

Code: Select all
update_config=1
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
eapol_version=1

network={
   ssid="ENTER SSID HERE"
   scan_ssid=1
   mode=0
   proto=WPA2
   auth_alg=OPEN
   pairwise=CCMP
   group=CCMP
   key_mgmt=WPA-PSK
   psk="ENTER PASSKEY HERE"
   id_str="win7"
   priority=1
}

network={
   ssid="ENTER SSID HERE"
   scan_ssid=1
   mode=0
   proto=WPA2
   auth_alg=OPEN
   pairwise=CCMP
   group=CCMP
   key_mgmt=WPA-PSK
   psk="ENTER PASSKEY HERE"
   id_str="duane"
   priority=0
}

# network={
#   ssid=""
#   key_mgmt=NONE
#}
Last edited by pjc123 on Wed Jul 17, 2013 11:30 am, edited 2 times in total.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by Polydnj » Fri Oct 26, 2012 2:48 pm
After setting up my raspberry pi to wirelessly connect to the router wireless with WiFi manager tool I tried again on a new computer, with a fresh Debian installed on the raspberrypi. It is helpful to know what is going on on the LAN to troubleshoot.

One, I used my router webpage by connecting to the default IP address.
Second, I SSH'ed through the router to the pi.
Third, Simply edited the /etc/wpa_supplicant/wpa_supplicant.conf: file with what you wrote
Fourth, Rebooted and was able wireless SSH by the IP adress the my routers DHCP server gave.
User avatar
Posts: 25
Joined: Wed Oct 10, 2012 1:59 am
by Polydnj » Fri Nov 30, 2012 1:01 am
If your confused on the purpose of the properties and values.. here is a usefull cheat sheet http://www.nowiressecurity.com/articles/configure_8021x_authentication_in_linux.htm
User avatar
Posts: 25
Joined: Wed Oct 10, 2012 1:59 am
by miguel_mike » Wed Dec 12, 2012 11:38 pm
SirLagz wrote:Crazy idea off the top of my head

Have a script that is run every minute that will -
Check to see if wlan0 is connected with the
Code: Select all
iwconfig
command
if it is then do nothing
if it is not connected
do a wifi scan with
Code: Select all
iwlist wlan0 scan

to
get the name of an open network
and then
Code: Select all
iwconfig wlan0 essid NETWORK_ID
dhclient wlan0

to connect






Hey man, can you share that script with me?
I am new to bash scripting and want a script that runs:
ifup wlan0
if my pi disconnects, which it does very often

how could i check if my pi is connected?
Posts: 6
Joined: Mon Dec 10, 2012 10:27 pm
by pjc123 » Thu Dec 13, 2012 12:13 pm
There is a thread here on that topic:

viewtopic.php?f=26&t=16054

See the script by Dweeber at the very end. Although I have not tried it yet, if it works I plan on adding it once I finish building my hardware. My pi is headless and will be moving in and out of the range of a wifi signal occasionally, so I also need a way to reconnect without reinserting the dongle.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by penn » Sun Sep 22, 2013 7:55 am
got an answer. add these lines. /etc/network interfaces
Code: Select all
allow-hotplug wlan0
iface wlan0 inet manual
        wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
/etc/wpa_supplicant/wpa_supplicant.conf
Code: Select all
network{
key_mgmt=NONE
}
Posts: 1
Joined: Sun Sep 22, 2013 7:23 am
by sbhunia » Thu Oct 03, 2013 9:13 pm
Hello pjc123,

Could you please tell me how did you install 2 wireless card to Raspberry Pi? As soon as I add 2 cards, it hang up.
Posts: 1
Joined: Thu Oct 03, 2013 8:47 pm
by pjc123 » Thu Oct 03, 2013 9:51 pm
sbhunia wrote:Hello pjc123,

Could you please tell me how did you install 2 wireless card to Raspberry Pi? As soon as I add 2 cards, it hang up.


Unfortunately I never tried two wifi dongles at the same time, only one at a time using the settings above, which works fine. The first thing that comes to mind if you are trying to do that and either the OS or the wifi startup is hanging up, is that you might be exceeding the power requirements of the pi, unless of course you are using a powered hub. I know I would not even attempt to connect two of my high gain dongles at the same time, as they would never start up properly; this is even more the case on a stock Model B Rev 1 board, as it will not even run one of my wifi dongles without removing the USB fuses.

EDIT: OK, just tried two dongles with the above setting with a powered hub, I can confirm that it will only let me run one dongle at a time. As soon as I enable the second dongle and it gets an ip address, the first one loses its ip address, and vice-versa; I never needed to run two dongles at the same time, so I never tried it before. I don't know offhand how to change it to work for your case.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by SirLagz » Fri Oct 04, 2013 8:02 am
I've run 2 dongles at once, 2 RT5370s and they both connected and got IP addresses.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by pjc123 » Fri Oct 04, 2013 10:37 am
SirLagz wrote:I've run 2 dongles at once, 2 RT5370s and they both connected and got IP addresses.


Some useful information on how you did it would be nice (network files setup), and were you able to get it working with roaming?
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by SirLagz » Sat Oct 05, 2013 4:48 am
I had it setup in /etc/network/interfaces, one with a static IP address and one with a dynamic IP Address, both interfaces were configured to use ssid/psk from /etc/network/interfaces
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Posts: 1704
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
by pjc123 » Sat Oct 05, 2013 10:40 am
SirLagz wrote:I had it setup in /etc/network/interfaces, one with a static IP address and one with a dynamic IP Address, both interfaces were configured to use ssid/psk from /etc/network/interfaces


Unfortunately, no good for my application.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm
by vivekanand » Sat Dec 07, 2013 5:37 am
my question is : i want operate WiFi dongle with raspberry pi so i need to write python script for automatically scan WiFi networks and need to be connected automatically .
with known ssid and known password .
that means i need to provide password for WiFi network in script only
remain thing is need to scan and connect automatically .

so please help me to write python script. for this .
Posts: 4
Joined: Thu Nov 28, 2013 7:54 am