Project WolverineBroadcasting LIVE from the orbiting command centre

I’ve had a world of pain trying to get any kind of mesh networking up and running on the Raspberry Pi.

What I wanted: 3 Raspberry Pies (Pi’s?), with no DHCP server and no LAN or Internet connectivity, to be able to connect to each other over WiFi. I also want each RPi to act as an access point so I can log in via SSH without needing an ethernet connection.

Why: I wanted to set up a replicated database (CouchDB in this case).

The equipment:

  • 3 x Raspberry Pi Model B+ (Running Weezy or Jessie, it doesn’t matter)
  • 3 x Edimax EW-7811un (like this: PB Tech, or Amazon)
  • 3 x TP-Link TL-WN723N version 3 (like this: PB Tech, or Amazon)

Chipsets: The Edimax adaptor is running rt8192cu chipset, and the TP-Link adaptor is running the rt8188cu.

The rt8188cu chipset WILL NOT WORK in ad-hoc mode, but it makes a fine access point, which is why I kept it. It has good range, especially compared to the Edimax, which is shit (a tiny adaptor obviously has a tiny antenna).

The rt8192cu chipset does support ad-hoc mode, and the Edimax adaptor works perfectly except the range is quite short.

The reason I have two different adaptors is because I bought quite a few different types in order to find one that worked. If I was starting from scratch, I might have just bought 6 Edimax adaptors 🙂

So, here are the steps you need to take, to get ad-hoc connectivity working, along with access points on each RPi. wlan0 will be reserved for the Edimax and the ad-hoc network, wlan1 will be for the access point.

Note: These steps are taken from https://www.maketecheasier.com/set-up-raspberry-pi-as-wireless-access-point/ . I’ve reproduced them here in case this link fails.

First, specify the wlan locations. Because we’ve got two WiFi adaptors with different chipsets, we need to be sure that they are assigned to the same USB port each time the RPi is booted.

Step 1: Assign the wlan devices

edit /lib/udev/rules.d/75-persistent-net-generator.rules

sudo nano /lib/udev/rules.d/75-persistent-net-generator.rules

Change this line:

# device name whitelist
KERNEL!="ath*|msh*|ra*|sta*|ctc*|lcs*|hsi*", \
GOTO="persistent_net_generator_end"

To:
# device name whitelist
KERNEL!="ath*|wlan*[0-9]|msh*|ra*|sta*|ctc*|lcs*|hsi*", \
GOTO="persistent_net_generator_end"

Now (VERY IMPORTANT), remove the TP-Link adaptor, and leave just the Edimax adaptor. We want this to be permanently assigned to wlan0. With this plugged in, reboot the RPi.

When it’s loaded, check that /etc/udev/rules/70-persistent-net.rules has an entry for wlan0 with this adaptor listed.

nano /etc/udev/rules/70-persistent-net.rules

Now plug the TP-Link adaptor into another USB port, leaving the Edimax adaptor where it is. Reboot the Pi.

Again, check the persistent net rules file – there should be two entries. If you don’t rearrange the adaptors, then they will always be assigned to the same wlan0 and wlan1 roles.

Step 2: Create the access point on wlan1:

Edit “/etc/network/interfaces” and add the static IP address information for wlan1.

sudo nano /etc/network/interfaces

I like to comment out the existing interface configuration in case you need to revert back or to compare your changes, so place a ‘#’ sign in front of all the existing wlan0 and wlan1 entries, and make sure your interfaces file now looks like this:

allow-hotplug wlan1
iface wlan1 inet static
address 192.168.3.10
netmask 255.255.255.0
gateway 192.168.3.1

We’ll be assigning IP addresses starting from 192.168.3.10 – I’ve reserved the first 10 numbers for the gateway, but you can change these values to be anything you want as long as you remember to be consistent.

Lastly, definitely make sure that there are no mentions of wpa_supplication.conf files.

Reboot your machine (sudo reboot).

Step 3: Install and configure a DHCP server

Install and configure the DHCP server:

sudo apt-get install isc-dhcp-server

Ignore any error messages about not being able to start the DHCP server, but I think Jessie suppresses these.

Now edit the configuration file:

sudo nano /etc/dhcp/dhcpd.conf

Add a ‘#’ character in front of the ‘option domain-name’ lines, like this:

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Remove the ‘#’ sign in front of the ‘authoritative;’ statement, like this:

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

At the bottom of the file, add the following lines:

subnet 192.168.3.0 netmask 255.255.255.0 {
range 192.168.3.10 192.168.3.210;
option broadcast-address 192.168.3.255;
option routers 192.168.3.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Exit from nano with ‘Ctrl+X’.

Make the wireless adapter the default for the DHCP request:

sudo nano /etc/defualt/isc-dhcp-server

Change ‘INTERFACES=""‘ to ‘INTERFACES="wlan1"

Exit from nano with “Ctrl+X”

Restart the DHCP server:

sudo service isc-dhcp-server restart

If you get errors at this point, check the configuration changes we’ve made – a small error will prevent the DHCP server from starting.

Step 4: Install and configure the access point daemon

Install hostapd:

sudo apt-get install hostapd

Edit the hostapd configuration file and create a wireless network:

sudo nano /etc/hostapd/hostapd.conf

Add the following lines (this file will be empty to start with) – change ‘MyPi’ to your preferred access point name, and change the password.

Because we’ll be creating 3 access points, I’d recommend making the ssid to be something unique each time.

interface=wlan1
driver=rtl871xdrv
ssid=MyPi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
This will create a password protected network called 'MyPi' on channel 6 with the password 'raspberry'.

Tell hostapd where to find its configuration file by setting the default location:

sudo nano /etc/default/hostapd

Remove the ‘#’ in front of ‘DAEMON_CONF’ and alter the line to read:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Step 5: Configure IP routing between the wireless adaptor and ethernet

This is not technically part of my basic requirements, but we’ll do it anyway.

Edit ‘/etc/sysctl.conf‘ to enable IP forwarding:

sudo nano /etc/sysctl.conf

Find the line which reads “Uncomment the next line to enable packet forwarding for IPv4” and uncomment the next line like this:

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Run the following command to activate forwarding now:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Now turn the Pi into a router with the follow commands:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan1 -o eth0 -j ACCEPT

And save the routing tables into the file ‘/etc/iptables.ipv4.nat

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit ‘/etc/network/interfaces‘:

sudo nano /etc/network/interfaces

And add the following line to the end of the file. This line will restore the routing table whenever the Pi is booted:
pre-up iptables-restore < /etc/iptables.ipv4.nat

Step 6: Update the drivers.

This is very important. This chipset will not work without changing the drivers for the adaptor. We’ve already indicated in the hostapd.conf file that we want to use rtl871.xdrv, so now we need to go and get it.

Adafruit has a pre-compiled version of hostapd for the rtl871xdrv driver. To install it use the following commands (use this link if the adafruit location fails: http://www.projectwolverine.com/wp-content/uploads/2015/10/adafruit_hostapd.zip):

wget http://www.adafruit.com/downloads/adafruit_hostapd.zip
unzip adafruit_hostapd.zip
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG
sudo mv hostapd /usr/sbin
sudo chmod 755 /usr/sbin/hostapd

Now reboot the Pi. Using a different machine, you should see this appear as an access point, and you should be able to connect with the password you specified.

Step 7: Getting ad-hoc connectivity going

For the ad-hoc network, we’ll be adding nodes on the 192.168.4.x range. I recommend numbering your nodes and assigning an IP address which matches this number, like this: MeshNode1 -> 192.168.4.1, MeshNode2 -> 192.168.4.2 etc

Edit the interfaces file again:

sudo nano /etc/network/interfaces

Add these lines, for wlan0:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wireless-essid MeshNode1
wireless-channel 3
wireless-mode ad-hoc
wireless-ap 02:12:34:56:78:90
address 192.168.4.1
netmask 255.255.255.0

Now run ifconfig and write down the IP and MAC address for this node. The MAC address is found under the HWaddr value for each wlan section.

Repeat these steps for each node you want to add to the ad-hoc network, and write down the IP and MAC address for each machine.

Once you’ve got all the details, you need to add each of these to the arp tables on each RPi.

On each RPi, run this command:

sudo arp -s IP_ADDRESS MAC_ADDRESS

So, if you have three RPis, it would look like this (replacing the mac addresses with your own):

MeshNode1 (192.168.4.1)

sudo arp -s 192.168.4.2 80:1f:02:f4:4b:de
sudo arp -s 192.168.4.3 80:1f:02:f4:4b:df

MeshNode2 (192.168.4.2)

sudo arp -s 192.168.4.1 80:1f:02:f4:4b:e3
sudo arp -s 192.168.4.3 80:1f:02:f4:4b:df

MeshNode3 (192.168.4.3)

sudo arp -s 192.168.4.1 80:1f:02:f4:4b:e3
sudo arp -s 192.168.4.2 80:1f:02:f4:4b:de

Doing this will make each RPi aware of each other, and you can now ping them individually.

Note: every time you reboot an RPi, you’ll need to add these values back so I recommend making a startup script to do this.

If you’ve completed all of these steps, then you now have 3 Raspberry Pies with no ethernet connectivity, able to ping and ssh into each other, and available as separate access points.

This entry was posted in Cool stuff. Bookmark the permalink.

Comments are closed.

Browse by Topic