How to get a raspberry Pi to set up a wireless ad-hoc network, but only if it is not already connected
This tutorial post will show you how to get a raspberry pi to set up a wireless ad-hoc network, but only if it not already connected. The wireless hotspot will also Dynamically assign IP Addresses to clients that connect.
Things that you should already have
- A Raspberry Pi + SD Card (already set up)
- A wireless dongle (that supports monitor / master mode)
- Your raspberry pi set up to automatically connect to at least one wireless network (The default wireless configuration utility works fine)
If you do not have a screen and are controlling your raspberry pi via ssh or rpd, you will probably want to connect your raspberry pi to the nearest network with an ethernet wire, just in case.
Step 1 – Install the required packages
Install the following packages with the command below:
- hostapd – The WiFi hotspot manager
- udhcpd – The dhcp server that will hand out the IP Addresses to clients that connect
sudo apt-get install hostapd udhcpd
Step 2 – Configure udhcpd (The bit that hands out IP addresses)
Open /etc/udhcp.conf in your favourite text editor.
sudo nano /etc/udhcpd.conf
The sudo is rather important. Then enter the following example configuration:
start 192.168.0.2 # This is the range of IPs that the hotspot will give to client devices. end 192.168.0.20 interface wlan0 # The device udhcp listens on. remaining yes opt dns 220.127.116.11 18.104.22.168 # The DNS servers client devices will use. opt subnet 255.255.255.0 opt router 192.168.0.1 # The Pi's IP address on wlan0 which we will set up shortly. opt lease 864000 # 10 day DHCP lease time in seconds
The next step is to ‘enable’ the uDHCPd server. To do this, edit the
/etc/default/udhcpd file and comment out the following line by prepending a hash symbol:
This will enable the udhcpd server. Later, we will prevent it from starting automatically, just in case we do actually manage to connect to another wireless network.
Step 3 – Configuring hostapd (The wireless access point bit)
The next step is to configure hostapd. Edoit the following file (creating it if it doesn’t exist):
/etc/hostapd/hostapd.conf. It should contain the following text:
interface=wlan0 driver=nl80211 ssid=Raspi_wifi hw_mode=g channel=11 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=somepassword wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
The above configuration will create an ad-hoc wireless g network on channel 11. You should change the channel to one that is not in use in the area where you will be using your Raspberry Pi. It will be secured with WPA-2, and password will be “somepassword”. Again, you should change this to a more secure password.
Step 4 – Checking the connectivity on boot (and performing the appropriate action)
Run the following commands:
sudo update-rc.d hostapd remove sudo update-rc.d udhcpd remove
This will prevent udhcpd and hostapd from running on boot and preventing the raspberry pi from connecting to other wireless networks automatically.
Next, edit the following file:
/etc/rc.local. It should look something like this:
#[...a lot of stuff commented here...] # 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 exit 0
Change it to look something like this:
#!/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 sleep 5 _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi if [ "$_IP" ]; then echo "[Access Point Setup] - The wifi is already connected, no access point needed" else echo "[Access Point Setup] - The wifi is not connected, firing up an access point..." sudo ifconfig wlan0 192.168.0.1 sudo service hostapd start sudo service udhcpd start fi
sleep 5 bit makes sure that the raspberry pi has time to connect to any wireless networks it can.
The bit starting after the “My IP Address is…” is the bit that outputs a message telling the console whether or not we are setting up an ad-hoc wireless access point.
That concludes this tutorial! At this point, your raspberry pi should be configured to set up a wireless access point, but only if it is not already connected.
If you have any problems or questions, please post a comment below.