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

diskutil list

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,

  1. via Wifi using wpa_supplicant
  2. 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

country=US

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

network={

ssid=”MyRouterSSID”

psk=”MyRouterPasswd"

}

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

interface=uap0
ssid=PiAP
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

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

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Next edit /etc/dnsmasq.conf and add the following lines at the end

interface=uap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.2.2,192.168.2.50,12h

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(8.8.8.8) for name resolution.

Next edit /etc/network/interfaces to add our virtual device


iface uap0 inet static
address 192.168.2.1
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
gateway 192.168.2.1

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

#!/bin/bash
# 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
sysctl net.ipv4.ip_forward=1
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

http://raspberrypi.local/ssidconfig.php

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

Pi Zero headless setup

Raspberry Pi Zero W as both wifi client and access point

Source files for the php script and sample .conf file can be downloaded from here

The Middle Aged Programmer

Written by

You guessed it, i am a middle aged programmer who has been writing code for the last 20+ years. Current platforms include C, Java, PHP and nodejs.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store