The goal of this project is to create a wireless access point using a Raspberry PI Zero W. The access point or AP (for short) will not need access to the internet. Instead it will provide a private (intranet) network that can be used for robot control, and sensor processing. We will be using a private MQTT broker with this project.
Original for use with my D1 Mini WIFI Robot â https://www.instructables.com/id/Wemos-D1-Mini-WIFâŠ
The ideas presented here can be used for other web services (IE: web server, FTP server, gopher (yup showing my age here), irc, mail). And it can be bridged to the internet (but that is beyond the project goal).
We will be setting up a MAC filter, so that only our robot and control systems will be able to connect to the AP. (This isnât full prove, but works good enough for us).
The project setup files can be found here (in the raspberrypi branch)
https://github.com/kd8bxp/D1-Mini-WiFi-Robot
* NOTES: At some point I may pull the branches together into the master branch, and may or may not remember to up this instructable.
https://github.com/kd8bxp/D1-Mini-WiFi-Robot/tree/âŠ
* Also Note: these setup files are what I used to get the robot to work, the instructable will go over what is needed. You may want to make some changes to my setup, depending on what you change you may need to also make changes to the robot sketches, if you are planning on using the robot. đ
Hardware Needed:
Raspberry PI Zero W
* A Raspberry PI 3 should work with out any changes.
We will need a OTG usb cable, a HDMI adapter.
HDMI cable, micro SD card (I used a 16gb, which is more than I need for this project. A 8gb would work just as well). Youâll need a keyboard and mouse (a wireless one that uses the same dongle is ideal. but a small USB hub can be used).
And finally youâll need a power supply for your Raspberry PI. A 5v 2A supply is prefered, a good cell phone charger, or the official Raspberry PI power supply.
*** A Raspberry PI 2, or Raspberry PI Zero could also be used, with the correct wireless adapter (The adapter needs to support AP mode). Setup maybe slightly different (different drivers, and different ways to get the wifi to work in the first place). Iâve not tried this with a PI2 or ZERO â so if you use one of these having some troubleshooting skills will come in handy.
** This may work with other SBC (single board computers) â OrangePi, C.H.I.P., Beaglebone. Setup will be similar but different for each SBC, and may not work at all â depending on the type of WIFI adapter they have or you are using. Lots of troubleshoot skills will be needed if you attempt this with a different board.
Step 1: Download the Raspbian Jessie Image & Install to SD Card
As of Oct 25, 2017 â the Raspbian Stretch Image (September 2017 image) has some issues with WiFi and the Zero W. And until itâs fixed we need to use the Raspbian Jessie image. Some things to be aware of, Raspberry Pi foundation only supports the current image â so the image we are using is out of date. For me this isnât a big deal, since I want to create my own intranet and only for my robot projects. for now we can still get the updates, and install software from the repositories, this might change at any point. While you can only find the current image from the download page, the previous images are still available. (Although they are hidden a bit, and take a little hunting to find â lucky you â you found this instructable, and I found them.)
http://downloads.raspberrypi.org/raspbian/images/Â This has images dating back to 2012 (Wheezy) â so a good collection, and for those of us who still us are original raspberry pis itâs good to know.
The image specifically we want is here:
http://downloads.raspberrypi.org/raspbian/images/râŠ
I used the 2017-07-05-raspbian-jessie.zip.torrent since itâs a rather large file, and torrents are least are fast and have download recovery should your download be interrupted. (youâll need a torrent client)
Depending on your connection speed this could take several minutes or longer.
Mean while (while we wait) â we can install Etcher â https://etcher.io/
Etcher makes it easy to âburnâ a hard drive image to a SD card. It is super easy to use, and has a installer or a portable version for all major OSes. So download the version for your OS, and run the installer (if you choose a version with install option). Linux users, there is an option to install as well â I missed it the first time, so I thought Iâd post the link https://github.com/resin-io/etcher#debian-and-ubunâŠ
(Wait over) Once the download is complete, youâll want to unzip
I normally choose to unzip the image, but etcher does support burning the image from a zip file â up to you.
Put the SD card into your computers reader. Depending on your OS and setting it may or may not open the file explorer. Just close it if it does, we will not need it yet.
Start Etcher, select the image you just unzipped (or the zip file), typically the zip file will be in your downloads folder, and the unzipped file will be in itâs own directory/folder under the downloads folder.
Make sure Etcher has selected the correct SD card (it usually does, but just make sure), and finally click âFlashâ
* Linux users will be prompted for your password Etcher has SuperCow, so extra important to make sure it has the correct SD card.
NOTE: Iâve not used the Windows or Mac version of Etcher, it should be very similar. I am not sure youâll be asked for your password or not at this point.
It doesnât take long to burn the image, and Etcher is set by default to verify the brunt image.
Step 2: Going HeadlessâŠ. (Or How to Use a Raspberry Pi Without a Monitor or Keyboard/mouse)
At some point we will want to use our Raspberry PI in a Headless setup, The good news is itâs fairly easy to setup and you donât need anything special to do it. Strictly speaking we do need some additional software depending on your OS.
A really good tutorial for setup can be found here:
https://core-electronics.com.au/tutorials/raspberrâŠ
Adafruit also has a good tutorial. https://learn.adafruit.com/raspberry-pi-zero-creatâŠ
(Iâll be following the core-electronics tutorial, as it fits more with our goals) Adafruit provides some addition troubleshooting methods, which shouldnât be needed.
Additional software needed:
We need a SSH client â Linux & Mac users, should have a basic SSH client from the shell by default.
Windows users will need to install one â PuTTY is probably one of the best (There is a version of PuTTY for Linux as well) â https://www.chiark.greenend.org.uk/~sgtatham/puttyâŠ
Download and install PuTTY for your OS, we will be using it (or another SSH client later).
To Start:
First we need to mount the âbootâ partition off the SD card, one way to do this, is to remove and reinsert the card in the computer.
You should see a list of files like the picture above.
To get the Zero W on your wifi â we need to create a file in the boot folder called â âwpa_supplicant.confâ
Now open that file, and type the following:
network={ ssid="YourWiFiSSIDname" psk="YourWiFipassword" key_mgmt=WPA-PSK }
Put your WIFI SSID in place of YourWiFiSSIDname â leave the quotes. And replace YourWiFipassword with your network password. This assumes you are using a WPA-PSK password, security for your network. There are a number of different things that can be set in the wpa_supplicant.conf file.
for more information: https://w1.fi/cgit/hostap/plain/wpa_supplicant/wpaâŠ
There is a lot of information contained in that example file, itâs not a great thing to read, but the information is great!
One, thing I might note at this point â If for some reason your network doesnât use any password â itâs a little frustration finding out how to set this up â but pretty simple once you do.
network={
ssid="YourWiFiSSIDname" key_mgmt=NONE }
Enable SSH â by default SSH is disabled, but itâs easy to enable it.
All we need to do is create an empty file called âsshâ with no file extension. The file is placed on the boot partition.
We are done, Safely remove the SD card (IE: donât just pull it out, make sure itâs safe to do so).
Step 3: Booting Up the Raspberry PiâŠ
For this next step, we will need to hook up the HDMI adapter, keyboard and mouse, and use a HDMI monitor or TV. Insert the SD card into the Raspberry Pi Zero W.
Connect every thing up, and apply power.
On First boot you should see a message saying something along the lines of âExpanding File Systemâ.
The system will reboot, and after a few minutes you should see the Raspbain Desktop. â IF everything went well from the previous step you should also be connected to your network.
From here we need to setup a few things, so click on the Raspberry PI in the upper left hand corner.
Goto âPreferencesâ and âRaspberry Pi Configurationâ. When the Configuration tool comes up, you should be on the system tab. You should see a section that says Boot â we need to click on âTo CLIâ. This will boot the system to a command line interface. For our project we donât need the GUI interface (Desktop).
Next Click on the âLocalisationâ tab â youâll want to set each selection for your country, timezone, and keyboard type (Yes it makes a difference).
When you set everything, click OK and you should get a popup saying that a reboot is needed. â Go ahead and Reboot.
*** NOTES: Look in the upper right hand corner, and verify you are connected to the internet should see a couple of arrows if you are connect. IF not, click on that network connection icon, and setup your connection. If you are on the CLI and are not connected, use this tutorial. https://www.raspberrypi.org/documentation/configurâŠ
Step 4: Setting a New HostnameâŠ..
We should now be at the command line interface (pi@raspberrypi:~$ )
You should see a message that says something about having SSH enabled and the default password is still set.
We will change that and the system HOSTNAME.
type:
pi@raspberrypi:~$ sudo raspi-config
This will bring up the Raspberry Pi Command Line Configuration tool. Youâll notice it is a little different from the GUI version. The first option is to set a new password, it should already be highlighted (RED), if not use the arrow keys to highlight it. Hit âenterâ and follow the instructions (The default password is âraspberryâ without the quotes). Set a new password to anything you like, but remember it -youâll use this to SSH into the device should you need to.
You should be returned to the menu, and the 2nd option is âHostnameâ, use the arrow keys to highlight it, and hit âenterâ. You should get a informational message about how the Hostname should be. Hit âenterâ.
Use the backspace key, and remove the name âraspberrypiâ â You can call this device almost anything you want â Since mine is being used for my robotic projects I called it ârobotAPâ, hit âenterâ again, and you should be returned to the menu.
Now hit the âtabâ key, that should get you to the âselectâ and the arrow key to get to âfinishâ. Hit âenterâ and the system should reboot.
Step 5: Installing Additional SoftwareâŠ.
We are at the point where we need to install some additional software that will allow us to create an AP.
There are two packages we need, they are âhostapdâ and âdnsmasqâ, We are also going to install the MQTT broker âmosquittoâ now.
We will be following the tutorial on the Raspberry Pi website (I found that some of the settings are not needed, and I will be adding some settings to the dnsmasq.conf file more on that later) â https://www.raspberrypi.org/documentation/configurâŠ
We donât need internet access, so we can stop at the âUsing the Raspberry PI as an access point to share an internet connectionâ.
* NOTE: after the AP is setup, we will not have access to the internet from this device. So if there is any addition software youâd like to install, now is a good time to do it. â There is a way to get internet back, which I will go over later.
You will also notice that the prompt has changed, and reflects the new hostname you set in the previous step.
pi@robotAP:~$
At the command prompt type:
sudo apt-get update
followed by a
sudo apt-get dist-upgrade
These two commands will update the package manager with the latest information for all the various packages, including if any package needs or has upgrades.
The dist-upgrade, will upgrade all the packages marked for upgrade â and it will do it on a distribution level. Which is a bit more then a standard upgrade. It will bring everything to the latest.
After the upgrade, you should probably do a reboot one more time.
* Other Notes: sudo is powerful, this is a admin command, it can make changes to everything, and used with other commands can cause damage to your system. sudo stands for super user do. In linux the super user is the root user (or a super admin). Caution be used, and any command you are unaware of, or unsure what it does should be googled.
apt-get (apt can also be used without the -get) â apt is the package manager used by Raspbian (Debian, Ubuntu, Linux Mint, etc) Itâs very popular package manager â update, dist-upgrade, install, remove are all options for apt-get (There are a lot more)
sudo shutdown -r now
* We are having the super user shutdown the system, this time one of the options is -r (which is the reboot flag), and a time which for us is now â we want the restart to happen quickly.
The command ârebootâ can also be used.
After the reboot we should return to the prompt (pi@robotAP:~$) type
sudo apt-get install dnsmasq hostapd mosquitto
We now need to configure our system.
We have to stop the AP (hostapd) and the DNS server (dnsmasq) type
sudo systemctl stop dnsmasq
followed by
sudo systemctl stop hostapd
** IF any of the commands above fail because of no internet connection, follow this tutorial
https://www.raspberrypi.org/documentation/configurâŠ
Step 6: Configure a Static IPâŠ.
In linux the /etc folder contains most of the system software configuration files, and setting. Care should be taken to follow these steps.
* As far as I am aware nothing we are doing will stop the PI from booting, but it could cause the PI to not connect to the internet, or create an AP â since we are using the HDMI/Keyboard/Monitor this issues are minimal, because we still have access to the CLI *
We are going to edit some configuration files, we are going to use a CLI editor called ânanoâ. Since /etc is a protected folder we need to use sudo to create or edit the files.
* In the github repository I included the files from my setup *
type:
sudo nano /etc/dhcpcd.conf
use the arrow keys to get to the bottom of the file. And type the following (followed by the âenterâ key)
denyinterfaces wlan0
* Note: IF you have modified this file before and have âinterfaceâ line anywhere, then âdenyinterfaces wlan0â needs to be above those, otherwise the end of the file works well.
To save and exit nano, hit âCTRL-Oâ and âenterâ, followed by âCTRL-Xâ
Next we will setup the static IP address, I did modified this slightly from the tutorial on the Raspberrypi website.
I commented out the lines that allow the WiFi adapter to connect to the internet â If we ever need to have the device connect to the internet, we just uncomment this lines, comment the lines we are about to add, and restart the network interface (But I am getting ahead of myself)
type:
sudo nano /etc/network/interfaces
We are looking for the section that starts with:
allow-hotplug wlan0
You should see there are already a couple of lines under that section â these lines let the interface connect to your âhomeâ network. I commented them out, so mine will end up looking like this.
allow-hotplug wlan0 #iface wlan0 inet dhcp # wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf iface wlan0 inet static address 192.168.0.1 netmask 255.255.255.0 network 192.168.0.0
We set this up for the private IP address space of 192.168.0.x
Other IPv4 private address spaces are:
24-Bit block 10.0.0.0 - 10.255.255.255 20-Bit block 172.16.0.0 - 172.31.255.255 16-Bit block 192.168.0.0 - 192.168.255.255
You can choose to use another address space if you wish, but remember it we will be using the IP numbers later.
Itâs now time to restart the DHCP daemon with our new configuration.
type each of the following in.
sudo service dhcpcd restart sudo ifdown wlan0 sudo ifup wlan0
ifdown will bring the wlan0 interface down (turn off, but not really), ifup will bring the interface up (bring it online)
dhcpcd is a service, so to start it, restart it, stop it â we can use the âserviceâ command.
Step 7: Finding the MAC Addresses of Your Devices.
At this point, we need the MAC addresses of the devices you want to allow a connection to your AP.
In the github respository Iâve included an arduino sketch for finding the MAC of the ESP8266 and the ESP32 devices. You will need to run the sketch on each device, and make a note to yourself of the address.
You will want to include in your list at least one desktop or laptop computer â this is so you can SSH into the Raspberry PI and make changes as needed. Or if you need your desktop computer to interact with the robot at some point.
To find the mac address of your computers, open a terminal (Linux) or Command from Windows (CMD)
For windows at the cmd prompt type:
ipconfig /all
You are looking for the Physical Address of your WIFI adapter.
On a Linux machine at the terminal (shell â CLI) type:
ifconfig -a
You are looking for the HWaddr of your WIFI adapter (For me this is wlp1s0 â it will probably be different for you or on a different linux machine)
Youâll want to make a note of the MAC address.
I have four devices, Two D1 Mini Controllers (ESP8266), one ESP8266 with OLED display, and the Desktop Computer I am on. You can add as many MAC as youâd like â but be aware the AP we are creating can only handle a dozen or so devices connect to it at one time.
Source: Using a Raspberry PI Zero W As an Access Point and MQTT Broker