Get the right WiFi

One of the issues that I was facing is how to connect to my RPi when somehow the wifi router is OFF.

My rpi_bootstrapper.sh script was supposed to setup an ad-hoc network if the pre defined wifi SSID did not conntect. But somehow the ad-hoc network is not what I need, does not work, and I need to connect to my rasperry pi with more than a single device.

So I wanted my rpi to become an "host access point" when there is no wifi network.

many of the dongles you can find out there have Realtek wifi chip. They work fine unless you have to use hostapd to create your own wifi bubble. Sure, you can recompile the hostapd provided on their website, but if you are lazy like me you just get this dongle on ebay. Just search for "Wifi adapter dongle Ralink RT5370".

The key part you want to look for is the wifi chip. The one that works is RALINK RT5370. The model I've found on ebay also have a nice antenna that does help a lot to extend your range.

You can put the board quite far away from your router and still get more signal in comparison to the other "compact" dongles.

The only disadvantege I've found is that this model does block the second usb port for just few millimeters... Not sure how to solve this but I suspect that just by removing the case you can have the 2nd port back.

Having said so once you have the dongle you just have to install hostapd and configure it's config file under /etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=RPi-AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
wmm_enabled=0

This is the example to have an open network with SSID="RPi-AP" when you boot the board and there is not wifi bubble around you to connect as a normal wifi device.

Of course you also need to modify the bootstrapper script to launch hostap and the dhcpd server ... here is my updated script


#!/bin/bash

# RPi Network Conf Bootstrapper

createAdHocNetwork(){
    echo "Creating Local WiFi network"
    ifconfig wlan0 down
    echo 1 > /proc/sys/net/ipv4/ip_forward
    ifconfig wlan0 10.0.0.1 netmask 255.255.255.0 up
    /usr/sbin/dhcpd wlan0
    /usr/sbin/hostapd -B /etc/hostapd/hostapd.conf
    echo "Local WiFi network (hostap) created"
}

echo "================================="
echo "RPi Network Conf Bootstrapper 0.1"
echo "================================="
echo "Scanning for known WiFi networks"
ssids=( 'DefaultSSID' )
connected=false
for ssid in "${ssids[@]}"
do
    if iwlist wlan0 scan | grep $ssid > /dev/null
    then
        echo "First WiFi in range has SSID:" $ssid
        echo "Starting supplicant for WPA/WPA2"
        wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
        echo "Obtaining IP from DHCP"
        if dhclient -1 wlan0
        then
            echo "Connected to WiFi"
            connected=true
            break
        else
            echo "DHCP server did not respond with an IP lease (DHCPOFFER)"
            wpa_cli terminate
            break
        fi
    else
        echo "Not in range, WiFi with SSID:" $ssid
    fi
done

if ! $connected; then
    createAdHocNetwork
fi

Now you also need to configure your DHCP server ... here is my simple /etc/dhcp/dhcpd.conf

DHCPDARGS=wlan0; 

ddns-update-style none;
authoritative;

default-lease-time 600;

max-lease-time 7200;

option subnet-mask 255.255.255.0;
option broadcast-address 10.0.0.255;
option domain-name "rpi.local";

subnet 10.0.0.0 netmask 255.255.255.0 {
    range 10.0.0.20 10.0.0.200; #IP range to offer
}