3

I want to have multiple Pi-powered devices that will automatically connect to an existing single wireless AP opened on one of them if it exists. If not, the Pi should create a new AP for other to connect to. I already have hostapd running fine along with a DHCP server to supply IP addresses to each Pi. The last piece of the puzzle that I am having issues with though is checking if a network already exists. I would like to run this in either Bash or Python.

Sidenote: I cannot get the Ethernet connection to be tunneled through hostapd to the AP. I set net.ipv4.ip_forward to 1 and added the necessary stuff to iptables according to Setting up a Raspberry Pi as a WiFi access point.

2 Answers 2

1

I would like to run this in either Bash or Python.

I'm not going to write the code for you, but the simplest thing to do here, presuming that by "checking if a network already exists" you mean one to which the pi has already connected, i.e., an AP started by the first pi to not find anything to connect to, would be to parse output from ip addr. The man page for this is man ip-address. If you know the interface (presumably, wlan0), you can focus in:

 ip addr show dev wlan0

What you are looking for is an inet address. If the device has no inet address, it's not on a network. I think hostapd can also work with inet6 addresses, beware if this if it may apply BUT: Note that current versions of ip will show the inet6 version of the inet (v4) address regardless, which is probably irrelevant to you you.1 It's only a genuine-ipv6-only address if there's no plain inet address.

Beware interface lo will always have an inet address, 127.0.0.1, but this is not a true external network connection.

The same information is shown for eth0, etc. The first time you look at ip addr output you may want to use ip -c addr for some color but don't use that if you are machine parsing the output.

There's also stuff in /proc and /sys (in fact I'd guess that's where ip gets its info from) but again, this is probably the simplest way.


If you mean a network to which the pi is NOT connected, then you'll have to parse output from, e.g., iw dev wlan scan. There's not much of a man iw, look at iw help scan.

Referring to "looking for an ethernet network" to which the pi is not connected is not worth discussion so I leave it at at that.


1. IPv4 addresses have corresponding IPv6 versions (but not vice versa).

1

I interpret your question differently to Goldilocks, so in case my interpretation is the correct one...

To see the available access points you can do

sudo iwlist wlan0 scan

If your Pi access points are all called, say, Pi-blah, Pi-foo, Pi-bar etc., it's a simple matter to parse the list and see if you're the first.

Two notes:

  1. It's worth thinking about security. If i rock up outside your house and set up an access point called Pi-mark, will all your stuff eventually connect to it?

  2. The problem I predict you'll have will be race conditions: power them all on together, they'll all start at the same speed, they'll all scan, they'll all find no other access points, they'll all think they're the first and they'll all start their own access point. You'll have to think of a way to get out of that hole.

6
  • In fact I gave this second interpretation in the second half of the my answer ;) I think iwlist is being depreciated in favor of iw but that is not very important, whichever one you find easier to use and parse will do. WRT #2, you could use something like the random backoff algorithm used with all wifi transmissions, except unless your adapter can scan and act in AP mode (probably not) you'd have to do a variation along the lines of: Wait a random period, start AP. Wait a random period, check for connections. If there are none, stop the AP and start over.
    – goldilocks
    Commented Mar 20, 2016 at 11:16
  • ...Actually that's more a description of how wired networks have traditionally worked. With wifi, there is the further (and related to this) problem that a node can't detect other nodes starting simultaneous or slightly after them because they drown each other out when broadcasting. So they either use time slicing (not possible here), beacons from a defined center (probably not possible here), or a variation on what I just described (which would work, although it may take several minutes for say 5-10 pis to work themselves out that way, or it may be much quicker).
    – goldilocks
    Commented Mar 20, 2016 at 11:33
  • BTW #1 is not an issue, hostapd can use WPA2, etc., for authentication and encryption.
    – goldilocks
    Commented Mar 20, 2016 at 11:39
  • @goldilocks So you did :-) I started writing more about the race and realised it was harder than it first seems: you need to make sure they don't all fragment themselves into two or more separate groups, for instance. I'm sure there's a way though. Regarding security, what if my "rogue" access point is open, no WPA2? I'm sure you can stop the Pis from connecting to such APs, but you need to think to do it. :-)
    – Mark Smith
    Commented Mar 20, 2016 at 11:45
  • You could have one periodically drop out, scan, report. Personally, if I wanted to do something like this I'd explore ad hoc networking although I'm not sure to what extent most wifi adapters are capable of this or what software is required on the pi.
    – goldilocks
    Commented Mar 20, 2016 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.