How to setup the Wifi on a Raspberry Pi using an Access Point?

This tutorial will help you set up an access point that you can connect to in order to setup Wifi on the Raspberry Pi.  This is very useful especially if you are planning to use the raspberry pi in a product or a project that someone else will use but is not tech savvy. 

Download and Install Raspbian

  1. Download the latest image of Raspbian from www.raspberrypi.org website. I’m using the “RASPBIAN STRETCH LITE” version for this tutorial.

Configure SSH & Wifi

You can follow this tutorial that walks you thru this process of configuring SSH and Wifi.

How to setup WiFi and SSH on a Raspberry Pi Zero W without a monitor or a keyboard?

Configure The Raspberry Pi

At this point you should have inserted the SD card to your Raspberry Pi and connected the power USB cable to power it ON.  The login info for a freshly installation of Raspbian is:

username: pi
password: raspberry

Update and upgrade the latest updates for Raspbian. First, let’s update the system’s package list using the following command.

sudo apt-get update

Next, upgrade all of the packages to their latest version using the following command.

sudo apt-get dist-upgrade

Install Git

In order to clone repositories from github.com, you need to have Git installed on the raspberry pi. Run the following command to install it.

sudo apt install git

Install NodeJS & NPM

You will need to install NodeJs and NPM in order to run the setup in the next step. Copy and paste the following command to download and install NodeJS.

wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | sudo bash

Clone the Gateway Wifi Setup repository

Make sure you are in the home directory of the pi user. You can confirm that by typing the commandpwd.  That should display. /home/pi – Now clone the repository using the following command.

git clone https://github.com/eddieespinal/gateway-wifi-setup.git

Setup

Once you finish cloning the repository, change directory to the gateway-wifi-setup by typing:

cd gateway-wifi-setup

Now, you need to install the NodeJs dependancies for this project. Run the following command:

npm install

Install AP mode

Install the software we need to host an access point but make sure it does not run by default each time we boot.  Run the following commands, one at a time.

sudo apt-get install hostapd
sudo apt-get install dnsmasq

Now, let’s disable them.

sudo systemctl disable hostapd
sudo systemctl disable dnsmasq

Configuration Files

Let’s configure the hostapd and dnsmasq that we just installed.  Edit `/etc/default/hostapd

sudo nano /etc/default/hostapd

and uncomment DEAMON_CONF="" add the following line:

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

You can save the changes by pressing Ctrl+x then press Enter


Next, copy config/hostapd.conf to /etc/hostapd/hostapd.conf.  This config file defines the access point name “DASH Wifi Setup“. Edit it if you want to use a different name. 

Make sure you are inside the gateway-wifi-setup folder. /home/pi/gateway-wifi-setup then run the following command.

sudo cp config/hostapd.conf /etc/hostapd/hostapd.conf

Next, copy config/dnsmasq.conf to /etc/dnsmasq.conf

sudo cp config/dnsmasq.conf /etc/dnsmasq.conf

Configure server to run on startup

Copy config/mozilla-gateway-wifi-setup.service to /lib/systemd/system, edit it to set the correct paths for node and for the server code, and then enable the service with systemd:

sudo cp config/mozilla-gateway-wifi-setup.service /lib/systemd/system

Enable it:

sudo systemctl enable mozilla-gateway-wifi-setup

Test it out

Let’s make sure that we have setup everything correctly.  You need to remove the current wifi info we provided in the wpa_supplicant.conf file. Todo that, let’s edit that file, but first let’s create a back up of it:

sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant_bkup.conf

Now, let’s edit it and remove the network info.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
    ssid="YOUR-WIFI-NAME"
    psk="YOUR-WIFI-PASSWORD"
}

Remove the network info. It should look like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

IMPORT: Make sure that this file hasupdate_config=1 in order to be able to modify it later using the gateway-wifi-setup AP.

Reboot

sudo reboot

Once your Raspberry Pi reboots, you should see a new Wifi network on your computer or phone. It will look something like this:

Connect to the DASH Wifi Setup Access Point.  Then open your browser and navigate to:

http://192.168.220.1:8080

If everything was setup correctly, you should see the following page in your browser.

Once you enter your WiFi password, the access point will be disabled and it will automatically disconnect you from it.  At this point, your Raspberry Pi should be connected to the internet and you should be able to SSH to it.


DONE!