Introduction: Using a Raspberry PI Zero W As an Access Point and MQTT Broker

Picture of Using a Raspberry PI Zero W As an Access Point and MQTT Broker

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

Picture of 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)

Picture of 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...

Picture of 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.....

Picture of 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....

Picture of 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....

Picture of 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.

Picture of 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.

Step 8: Configuring the DHCP Server (DNSMASQ)

Picture of Configuring the DHCP Server (DNSMASQ)

We are going to depart from the Raspberry Pi tutorial just a bit - but not too far.

Back on the Raspberry PI at the command prompt (pi@robotAP:~$) type:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

What this command does is makes a backup copy of the original dnsmasq configuration. Next we need to create our own custom dnsmasq.conf file: Type:

sudo nano /etc/dnsmasq.conf

I have a example in the github repository. and this is where we are going to depart from the tutorial a little by adding lines for each of our device MAC address we want to give access to the AP. We will type the following:

interface=wlan0
dhcp-range=192.168.0.2,192.168.0.20,255.255.255.0,24h dhcp-host=xx:xx:xx:xx:xx:xx,192.168.0.8 dhcp-host=xx:x:xx:xx:xx:xx,192.168.0.3 dhcp-host=xx:xx:xx:xx:xx:x,192.168.0.5 dhcp-host=xx:xx:xx:xx:xx:xx,192.168.0.10 dhcp-ignore=tag:!known

The interface line, tell the dns/dhcp server to bind to the WIFI device of the Raspberry PI.

dhcp-range tells it to provide IP addresses between 192.168.0.2 to 192.168.0.20 and give a lease time of 24 hours. (18 devices not counting the Raspberry PI)

The next line dhcp-host=xx:xx:xx:xx:xx:xx,192.168.0.8 This is what tells the dhcp server to give a specific IP address to a specific MAC address. So you need to replace the XX with your MAC addresses - you can also assign different IP addresses if you like. This is not full proof, and it can be fooled. But it should work for what we are doing.

The last line dhcp-ignore=tag:!known tells the dhcp server to reject unknown MAC addresses. Again not full proof, but good enough for what we are doing.

And is typical - there are a number of other options for dnsmasq http://www.thekelleys.org.uk/dnsmasq/doc.html

remember to save this file use CTRL-O and CTRL-X.

Step 9: Configuring the Access Point Host Software (HOSTAPD)

Picture of Configuring the Access Point Host Software (HOSTAPD)

This is the final step, and again I found that I modified it just slightly from the tutorial on the Raspberry PI website.

My example is in the repository.

At the command prompt type:

sudo nano /etc/hostapd/hostapd.conf

Next we will type the following into the editor.

interface=wlan0
ssid=robotAP hw_mode=g channel=1 #wpa=2 #wpa_passphrase=AardvarkBadgerHedgehog #wpa_key_mgmt=WPA-PSK #wpa_pairwise=TKIP #rsn_pairwise=CCMP

I found that I can get away with a minimal setup here - I don't use a password on my AP - but you can, you just need to uncomment the lines and probably change the password.

The ssid is the name of the AP - this is what will show up when you look at the wifi networks in range from another computer.

Save the file CTRL-O, CTRL-X. Now we need to tell the system where the new configuration file is.

type:

sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF - it is near the middle of the file. You will want to replace that line with:

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

The quotes are important - leave them. Finally, we need to start the dns and hostap services.

type:

sudo service hostapd start

followed by:

sudo service dnsmasq start

On your desktop computer if you look at the available networks (wifi networks) You should see a new network with the name of the ssid we used here.

You should be able to connect to this new network, and using either the CLI or PuTTY - SSH into the Raspberry PI.

you can use either the hostname we set up way back in step 2 or 3. Or you can use the IP address of the Raspberry PI (192.168.0.1)

Remember the username for the PI is still pi so you need to type something like this:

ssh pi@robotAP.local  

this tells the remote (the raspberry pi) that you want to log in with the user name pi.

Step 10: The D1 Mini WIFI Robot

Picture of The D1 Mini WIFI Robot

https://www.instructables.com/id/Wemos-D1-Mini-WIF...

This robot uses MQTT for status updates, and UDP for robot control - it works very well with this new AP.

To keep from modifing a lot of code, I left the MQTT broker we install with the default settings. There are a number of options that can be set thou. http://mosquitto.org/

The updated robot code can be found in the raspberry pi repository.

https://github.com/kd8bxp/D1-Mini-WiFi-Robot/tree/...

I may pull this branch into the master branch so if it disappears check the master!

I also updated the code so that it has a autonomous mode (starts in autonomous mode by default).

That the end, I hope you enjoyed this instructable, and got something out of it.

Comments

About This Instructable

2,282views

31favorites

License:

Bio: I am a Ham Radio operator, computer geek, currently a service tech, robotic hobbyist. I've been "playing" with microcontrolers for the last several years ... More »
More by kd8bxp:Using a Raspberry PI Zero W As an Access Point and MQTT BrokerWemos D1 Mini WIFI Robot (MQTT & UDP)ESP32/X-Project Cheerlight Badge Using MQTT
Add instructable to: