Using the Raspberry Pi Zero as aWiFi range extender and an IOT device
The Raspberry pi zero is an amazing device with outstanding features. With in built wifi and bluetooth it can easily be turned into an IOT device to control various peripherals. In this article i will explain, how i turned my pi zero into a wifi range extender from scratch.
The initial setup instructions are for Mac OS , but they can be easily used in any flavor of linux also. To start off with we need to burn the latest version of raspbian lite os on a micro sd card. You can get it from here https://downloads.raspberrypi.org/raspbian_lite_latest. Download it and unzip the .img file from it. Insert the micro sd card in one of the USB drives using a micro sd card reader. Open a terminal window and list all the disks by typing the following
You will get a list of all attached drives and can easily identify the newly inserted card. In my case the device was listed as /dev/disk2 Once done , proceed to unmount the device by typing the following
diskutil unmountDisk /dev/disk2
Next we burn the .img file that we downloaded to the disk
sudo dd bs=1m if=filename.img of=/dev/disk2
After a few minutes, the image file will have been written to the micro sd card.
We then mount the sd card back(Mac OS does this automatically for us) and create a blank file named “ssh”. This is so that we can ssh into our freshly minted Pi zero. On the Mac, the sd card will show up as /Volumes/boot , we need to create the “ssh” file in this folder. Next we need to setup internet on our Pi to download the required packages. We can do this in 2 ways,
- via Wifi using wpa_supplicant
- via USB using RNDIS
For the first way, it is fairly simple, create a file named wpa_supplicant.conf in the same folder as the blank “ssh” file and put in the following details, replace the value of your router’s SSID and password
For the 2nd way you will need to setup your pi +mac to enable internet sharing over USB. Check this link for more details.
The next step is to put the micro SD card on the Pi zero and wait for a few seconds , for it to boot up. We will now ssh into the Pi and install 2 packages,
Hostapd : hostapd is a user space daemon software enabling a network interface card to act as an access point and authentication server….
Dnsmasq:dnsmasq is free software providing Domain Name System caching, a Dynamic Host Configuration Protocol server…
We do this in the following way on the ssh terminal
sudo apt-get update
sudo apt-get install hostapd dnsmasq
Once installed , we disable hostapd and dnsmasq so that they do not start automatically on booting up.
sudo systemctl disable hostapd
sudo systemctl disable dnsmasq
Next step is editing the /etc/hostapd/hostapd.conf file .Enter the following details
You can change the ssid and wpa_passphrase as per your requirement.
Next to enable hostapd to use this file, we will edit /etc/default/hostapd and add the following line to it
Next edit /etc/dnsmasq.conf and add the following lines at the end
Our access point will give out IP addresses in the range of 192.168.2.2 to 192.168.2.50 and we will use Google’s DNS server(22.214.171.124) for name resolution.
Next edit /etc/network/interfaces to add our virtual device
iface uap0 inet static
Next add a script which will start the hostapd and dnsmasq service , the script is named /usr/local/bin/piapstart and has the following contents
# Create the virtual device
/sbin/iw dev wlan0 interface add uap0 type __ap
ifup uap0# Route all the packets coming on this interface, this will enable #us to access internet through wlan0
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 ! -d 192.168.2.0/24 -j MASQUERADE
systemctl start dnsmasq
systemctl start hostapd
I wanted my Pi zero to be completely portable and have the ability to edit the SSID easily through a web page ,that i can access via the Pi zero access point. To do this i installed apache and php on the pi zero.
sudo apt-get install apache php
I wrote a simple php script which will get the required details and write it to a wpa_supplicant.conf file. You can find the script and other files here. We can easily access this from a web browser like this
Finally we need to put all of this in the startup script /etc/rc.local
#Start the Access point daemon
/bin/bash /usr/local/bin/piapstart# Enable wlan0 using wpa_supplicant and use the conf file created by #our php script
wpa_supplicant -Dnl80211 -iwlan0 -c/var/www/html/wpa_supplicant.conf -B -d#delay by 10 seconds so that the wlan0 connection comes upsleep 10#get an ip address from the wifi routerdhcpcd wlan0
Enabling wlan0 automatically using /etc/network/interfaces does not work , the wlan0 link is always down, so starting it via the startup script is the only option i could find. Wpa_supplicant helps establish the connection to a router and dhcpcd gets an ip address from the router.
Every time we change the SSID details using the php script, we need to reboot our Pi zero for the changes to take effect. Using the Pi zero in this mode, we can use it for many production grade real life situations where the user can control all his devices using the Pi zero.
Thanks to the following links for inputs
Source files for the php script and sample .conf file can be downloaded from here