This rather long post steps you through the process of setting up a WiFi router on a Raspberry Pi Zero W or Raspberry Pi 3. There are a number of reasons why you might want to do such a thing:
As an added bonus this post also describes how to install NodeJS and the AWS command line tool to created scripted workflows with AWS.
Setup the Raspberry Pi Zero W using Raspbian Lite. The version I used was 2018-06-27-raspbian-stretch-lite.img
The following instructions are Mac OSX centric but not too different for how you’d do this using other operating systems.
Locate SD card
On Mac you can do this using diskutil via the command line. Execute the command below and look for a /dev/disk that matches the size of your SD card. For me, that’s /dev/disk2
$ diskutil list
Unmount the SD card
Next unmount the SD card using the disk number.
$ diskutil unmountDisk /dev/disk2
Flash the OS image
Use the dd
command to flash the Raspbian lite image onto your SD card. Be careful when doing this!
This is a good time to mention that the dd
command can be very dangerous if you're not careful using it. Always make sure that you're pointing dd
to the correct disk drive! You wouldn't want to erase the wrong drive. After all, there's a reason why the command is known as the Disk Destroyer, but that's not what dd stands for. See this bit of [dd](https://en.wikipedia.org/wiki/Dd_%28Unix%29)
history if you're interested.
$ sudo dd bs=1m if=./2018-06-27-raspbian-stretch-lite.img of=/dev/disk2 conv=sync
While dd is running you can press ctrl-t on your keyboard to check on the status. The SD disk flash can take a while based on the speed of your SD card.
It’s recommended that you use a Class 6 or Class 10 microSDHC card for performance reasons.
Once the copy completes you can cd onto the card:
$ cd /Volumes/boot
Create an empty ssh file to tell the OS that you wish to enable SSH access:
$ touch ssh
Next create a wpa_supplicant.conf
file with the following, make sure to replace your ssid and psk fields with your router's settings.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1country=USnetwork={ ssid="YOUR-SSID" psk="YOUR-PASSWORD" scan_ssid=1}
Safely unmount the SD after your changes.
$ cd ~$ diskutil unmountDisk /dev/disk2
Connecting to and configuring your Raspberry Pi
Next, insert the SD into your Pi Zero and boot the device.
Give that a minute or so to complete then try pinging the device to locate it.
$ ping raspberrypi.local
Or locate the device using your network router or a Wifi scanner such as Who’s on My Wifi
You can ssh into the device using:
$ ssh [email protected]$ ssh [email protected]
The default password is raspberry
.
If you encounter the following error:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
then simply edit your .ssh/known_hosts
file and remove the line that has the raspberrypi
entry.
$ vi /Users/${USER}/.ssh/known_hosts
So moving right along, once you sign in, the first order of business is to change the password!
$ passwd
The username will still remain pi
but you'll be able to sign in using your new password.
You can optionally proceed to make other tweaks using the raspi-config utility if you’d like.
$ sudo raspi-config
Next make sure the OS is up to date:
$ sudo apt-get update
**_E: Unable to fetch some archives, maybe run apt-get update or try with — fix-missing?_**
Then rerun **_sudo apt-get update_**
and retry the package update that failed. This has proven to be successful.Lastly, here take a moment to setup your timezone:
$ sudo dpkg-reconfigure tzdata
For the Pi ZeroW use:
$ mkdir nodejs; cd nodejs$ wget https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-armv6l.tar.xz$ tar -xvf node-v8.12.0-linux-armv6l.tar.xz$ cd /usr/local/bin$ sudo cp /home/pi/nodejs/node-v8.12.0-linux-armv6l/bin/node .$ sudo ln -sf /home/pi/nodejs/node-v8.12.0-linux-armv6l/bin/npx npx$ sudo ln -sf /home/pi/nodejs/node-v8.12.0-linux-armv6l/bin/npm npm$ cd ~
If using a Raspberry Pi 3 use:
$ mkdir nodejs; cd nodejs$ wget https://nodejs.org/dist/v10.14.0/node-v10.14.0-linux-armv7l.tar.xz$ tar -xvf node-v10.14.0-linux-armv7l.tar.xz$ cd /usr/local/bin$ sudo cp /home/pi/nodejs/node-v10.14.0-linux-armv7l/bin/node .$ sudo ln -sf /home/pi/nodejs/node-v10.14.0-linux-armv7l/bin/npx npx$ sudo ln -sf /home/pi/nodejs/node-v10.14.0-linux-armv7l/bin/npm npm$ cd ~
You can test the NodeJS install by:
$ node --version
This is an optional step but is useful if you have files in an S3 bucket that you’d like to copy into the Raspberry Pi.
$ sudo apt-get install awscli
Next configure AWS access:
$ aws configureAWS Access Key ID [None]: ************AWS Secret Access Key [None]: ***************************Default region name [None]: us-west-2Default output format [None]: json
Download assets script
#!/bin/bashmkdir -p $1aws s3 cp s3://bucketname/$1 ./$1 — recursive
Usage:
./download.sh 303A0FA/teb002/3A0FA-2018/10/18/90918447–2fnaodyxhbr
Remove WPA Supplicant
$ sudo apt-get purge wpasupplicant
Install an DHCP server$ sudo apt-get install isc-dhcp-server
Setup DHCP$ sudo vi /etc/dhcp/dhcpd.conf
Add to end of file:
subnet 172.16.1.0 netmask 255.255.255.0 {range 172.16.1.25 172.16.1.50;option domain-name-servers 8.8.4.4;option routers 172.16.1.1;interface wlan0;}
Install the host access point daemon$ sudo apt-get install hostapd
Configure hostapd
Create a hostapd.conf file$ sudo vi /etc/hostapd/hostapd.conf
add these lines to hostapd.conf file
interface=wlan0#driver=nl80211ssid=YOUR_STATIONhw_mode=gchannel=5wpa=1wpa_passphrase=SECRETPASSWORDwpa_key_mgmt=WPA-PSKwpa_pairwise=TKIP CCMPwpa_ptk_rekey=600macaddr_acl=0
Moving right along…
$ sudo ifconfig wlan0 172.16.1.1$ sudo /etc/init.d/isc-dhcp-server restart
-d is for debug mode so you can see if any errors appear$ sudo hostapd -d /etc/hostapd/hostapd.conf
Might need to ctrl-c after ^^^
Now lets make these changes persistent$ sudo vi /etc/network/interfaces
Next, add or edit these following line
auto wlan0iface wlan0 inet staticaddress 172.16.1.1netmask 255.255.255.0
Edit rc.local …
$ sudo vi /etc/rc.local
and add two lines before the last line exit 0
hostapd -B /etc/hostapd/hostapd.confiptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Then uncomment the following line in /etc/sysctrl.conf
$ sudo vi /etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4#net.ipv4.ip_forward=1
So above should read:
net.ipv4.ip_forward=1
That’s it. We can now reboot the box.
$ sudo reboot
At this stage you should be able to access the WiFi network.
Thanks for reading! If you like what you read, hold the clap button below so that others may find this. You can also follow me on Twitter.