I’m working on a project based on a Raspberry Pi, controlled by an external device (laptop, smartphone…) and therefore I was looking for the most universal way to connect the device to the Raspberry. Thanks to a small usb->wifi adapter you can transform the Raspberry in a wireless access point that broadcasts a wireless network your device can connect to: in this post I’m going to explain you how to do that.
Wifi module
The Raspberry Pi doesn’t include a wifi adapter, so you need to buy a usb->wifi module.
I chose the Edimax EW-7811Un, a very small and cheap (you can buy it for less than 10 euros from Amazon) adapter:
Thanks to its small size, the adapter juts out slightly when inserted in one of the Raspberry’s USB ports:
I installed the latest version of the Raspbian Linux distro and the adapter is automatically recognized (you can verify it running the dmesg command):
A new network device is created, named wlan0:
Network configuration
The new access point we’re going to configure broadcasts a network with the following settings:
- SSID: Pi_AP
- protection: WPA2-PSK
- password: PiPasswd
- IP addresses: DHCP (range 192.168.10.11-20, mask 255.255.255.0)
First you have to set a static IP address for the wlan0 interface, editing the /etc/network/interfaces file:
Reload the configuration with the following commands:
sudo ifdown wlan0 sudo ifup wlan0 |
DHCP and DNS
I chose dnsmasq as daemon for the DHCP and DNS services.
Before installing it, make sure your distribution is updated running the following commands:
sudo apt-get update sudo apt-get upgrade |
then install the software:
sudo apt-get install dnsmasq |
Now change its config file (/etc/dnsmasq.conf) adding the following lines at the end:
interface=wlan0 dhcp-range=192.168.10.11,192.168.10.20,,255.255.255.0,24h |
The first configures dnsmasq to listen only on the wifi interface, while the second configures the range of the IP addresses that dnsmasq provides and the lease time (24h).
Restart the deamon with:
sudo /etc/init.d/dnsmasq restart |
Access Point
The access point functionalities are provided by hostapd:
sudo apt-get install hostapd |
Create a new config file (/etc/hostapd/hostapd.conf) and add the following content:
interface=wlan0 driver=rtl871xdrv ssid=Pi_AP hw_mode=g channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=PiPasswd wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP |
Edit the /etc/default/hostapd file and uncomment the line that defines the path of the config file you’ve just created:
Unfortunately the hostapd shipped with Raspian does not support my adapter: you have to compile the version provided by Realtek (that is the manufacturer of the chipset the adapter uses).
Download from the official website the Linux version of the driver (scroll down the page until you find the RTL8188CUS chipset):
Copy the downloaded file into the /tmp directory. Run the following commands (change the filename with the one you downloaded if different) to unzip the different packages:
sudo unzip RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/ cd wpa_supplicant_hostapd/ sudo tar -xvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812 cd hostapd |
Now you can start the compilation process and wait until it ends:
sudo make sudo make install |
Copy the compiled version of hostapd overwriting the old one:
sudo mv hostapd /usr/sbin/hostapd sudo chown root.root /usr/sbin/hostapd sudo chmod 755 /usr/sbin/hostapd |
Now you can test the configuration running the hostapd daemon in console mode:
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf |
If everything works, the new wifi network will be available and you’ll be able to connect:
At the end, you can configure Linux to run the hostapd daemon at the boot:
sudo update-rc.d hostapd enable |
Ciao Luca,
interessante.. se posso chiederti, cosa farai di preciso con il Pi?
Ciao! Sto lavorando (quasi terminato) ad un display gestito da Raspberry e configurabile via wifi sfruttando appunto la modalità AP descritta in questo articolo…
Ciao, ho un raspberry che ho configurato con hostapd, bridge tra lan e wifi usb RTL8188CUS.
Sembra funzionare senza usare dnsmasq.
Quello che mi succede però è che mi ritrovo con la rete wifi del router e quella del raspberry con ssid diversi.
A volte il cellulare rimane connesso a quella del raspberry anche se il segnale è decisamente più basso rispetto a quello del router.
Ho letto che dovrei metter lo stesso SSID e channel diverso alle due reti wifi per ottenere il passaggio del cellulare android da una all’altra senza nessun problema.
Il fatto è che alla rete wifi del router ho impostato il mac address filter e la wifi è senza protezione mentre per il raspberry ho impostato la password nel file hostapd.
Come posso fare?
ciao, volevo sapere se hai utilizzato anche il cavo ethernet per fare il tuo progetto. ti perchè io voglio fare un access point col rasp…solo che la gente mi dice che devo utilizzare due chiavette wifi, perchè ho un router portatile senza ethernet. mi dai dei consigli? grazie
Ciao Luca, spiegami meglio cosa vuoi ottenere… sicuramente puoi fare un access point con Raspberry utilizzando il wifi + la scheda ethernet ma se poi questo cavo ethernet non hai modo di collegarlo al tuo router ADSL che te ne fai?
ciao, volevo chiederti perchè il mio access point non funzia…
praticamente io vedo l’ap col telefono però quando mi collego mi va in loop continuo e non accede. non capisco più niente aiutami per favore.
Ciao, volevo chiederti se e possibile in questo modo accedere al raspberry anche senza connessione internet……
Cercherò di farmi capire meglio che posso.
E possibile connettersi via wireless da cell. al raspberry per pilotare le GPIO senza router/internet. ad esempio allesterno?
ciao! certo, se lo configuri come access point ti puoi collegare alla rete wifi che lui pubblica e sfruttare tale collegamento per accedere al Raspberry.