This is a setup so that that Raspberry Pi will connect to recognized networks automatically, and when a recognized network can’t be found, it generates it’s own network. This allows the pi to be connected to in the “field”.
Install the Dependencies.
sudo apt-get install hostapd isc-dhcp-server
Configure DHCP (/etc/dhcp/dhcpd.conf)
PDARGS=wlan0; #args for the dhcpd daemon -> limit DHCP to the wlan0 interface 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-network"; option routers 10.0.0.1; #default gateway subnet 192.168.42.0 netmask 255.255.255.0 { range 192.168.42.10 192.168.42.50; option broadcast-address 192.168.42.255; option routers 192.168.42.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; } #static IP-assignment host myLaptop { hardware ethernet 11:aa:22:bb:33:cc; fixed-address 10.0.0.100; }
Edit /etc/default/isc-dhcp-server and scroll down to INTERFACES=”” and update it to say
INTERFACES="wlan0"
Modify /etc/network/interfaces
#start interface upon start of the system auto lo wlan0 # register looback interfact iface lo inet loopback # use dhcp and allow interface to hotplug allow-hotplug eth0 iface eth0 inet dhcp # use manual ip config for wanl0 allow-hotplug wlan0 iface wlan0 inet manual iface default inet dhcp
Create /etc/hostapd/hostapd.conf and Modify the WIFI_NETWORK_NAME and WIFI_NETWORK_PASSWORD
interface=wlan0 driver=rtl871xdrv ssid=WIFI_NETWORK_NAME hw_mode=g channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=WIFI_NETWORK_PASSWORD wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
Add the following to /etc/rc.local. Be sure to exit 0 is at the end of file.
# RPi Network Conf Bootstrapper createAdHocNetwork(){ echo "Creating ad-hoc network" ifconfig wlan0 down ifconfig wlan0 192.168.42.1 service isc-dhcp-server restart service hostapd restart echo "Created Access Point" } echo "=================================" echo "RPi Network Conf Bootstrapper 0.1" echo "=================================" echo "Scanning for known WiFi networks" ssids=( 'WIFINETWORK1' 'ANOTHERWIFINETWORK' ) 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 echo "ADHOC GO!!" createAdHocNetwork fi #reroute port 80 to 8080 iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Source
http://www.bernaschina.com/en/blog/raspberrypi/cloud9-raspberry-pi/