How to setup a Phew! Access Point

Setup your own Wifi Captive Portal for Robotics project

4 September 2022 by Kevin McAleer


Table of Contents


Video


Phew! That was too easy

You can quickly setup an Access Point / Captive Portal using MicroPython, a Raspberry Pi Pico W and the Phew! library from Pimoroni.

In fact its just a single line to setup the access point:

ap = access_point("My Wifi Access Point")

Access Point on an iPhone screen

Picture of an iPhone showing the ‘My WiFi Access Point’


About Phew!

Phew! is a Pico Http Endpoint Wrangler; its a collection of highly optimised functions for creating webpages on a MicroPython device, such as the Raspberry Pi Pico W.

Here are some of the cool things it can do:

  • Serve up Webpages
  • Templating framework, (think Jinja)
  • Logging
  • Captive Portal / WiFi Access Point
  • DNS Server

Check out the Github page for more information.


How to install Phew!

There are a couple of ways to install Phew! onto your Raspberry Pi Pico W -

  1. Download the code from https://github.com/pimoroni/phew and copy the files across 1
  2. Use uPip to install, once you have established a wifi connection:

     import upip
     upip.install("micropython-phew")
    

NB Phew is published to PyPi, making it easy to install, even within your code


The rest of the code you’ll probably need

To make the Access Portal do something useful, we need to capture all the requests from the guest computer and direct them to a route of our choice. This technique is call a Captive Portal.

DNS

We need to route any request for other websites and pages. We do this using our own DNS server; to launch this we simply add:

dns.run_catchall(<ip>)

Here is a more fuller version of the code, for context:

# Set to Accesspoint mode
ap = access_point("Wifi In The Woods")
ip = ap.ifconfig()[0]
logging.info(f"starting DNS server on {ip}")
dns.run_catchall(ip)
server.run()
logging.info("Webserver Started")

A typical application

To further explain how to use this, consider the code below:


# CyberDog 
# Kevin McAleer
# September 2022

from phew import server, template, logging, access_point, dns
from phew.template import render_template
from phew.server import redirect
import gc
gc.threshold(50000) # setup garbage collection

DOMAIN = "pico.wireless" # This is the address that is shown on the Captive Portal

@server.route("/", methods=['GET','POST'])
def index(request):
    """ Render the Index page and respond to form requests """
    if request.method == 'GET':
        logging.debug("Get request")
        return render_template("index.html")
    if request.method == 'POST':
        text = request.form.get("text", None)
        logging.debug(f'posted message: {text}')
        return render_template("index.html", text=text)

@server.route("/wrong-host-redirect", methods=["GET"])
def wrong_host_redirect(request):
  # if the client requested a resource at the wrong host then present 
  # a meta redirect so that the captive portal browser can be sent to the correct location
  body = "<!DOCTYPE html><head><meta http-equiv=\"refresh\" content=\"0;URL='http://" + DOMAIN + "'/ /></head>"
  logging.debug("body:",body)
  return body

@server.route("/hotspot-detect.html", methods=["GET"])
def hotspot(request):
    """ Redirect to the Index Page """
    return render_template("index.html")

@server.catchall()
def catch_all(request):
    """ Catch and redirect requests """
    if request.headers.get("host") != DOMAIN:
        return redirect("http://" + DOMAIN + "/wrong-host-redirect")

# Set to Accesspoint mode
ap = access_point("Wifi In The Woods")  # Change this to whatever Wifi SSID you wish
ip = ap.ifconfig()[0]                   # Grab the IP address and store it
logging.info(f"starting DNS server on {ip}")
dns.run_catchall(ip)                    # Catch all requests and reroute them
server.run()                            # Run the server
logging.info("Webserver Started")



Next Steps

Think about the possibilities of creating your own Access Points using the Pico W:

  • Robot with a webpage accessible via the Captive Portal
  • Find the Access Point in the Woods - a battery powered Pico W that shares some useful information in a remote location, maybe it gives clues for a GeoCache game.
  • Setup impromptu Information points at temporary events

Footnotes

  1. You can copy code using mpremote or Thonny 

Did you find this content useful?


If you found this high quality content useful please consider supporting my work, so I can continue to create more content for you.

I give away all my content for free: Weekly video content on YouTube, 3d Printable designs, Programs and Code, Reviews and Project write-ups, but 98% of visitors don't give back, they simply read/watch, download and go. If everyone who reads or watches my content, who likes it, helps fund it just a little, my future would be more secure for years to come. A price of a cup of coffee is all I ask.

There are a couple of ways you can support my work financially:


If you can't afford to provide any financial support, you can also help me grow my influence by doing the following:


Thank you again for your support and helping me grow my hobby into a business I can sustain.
- Kevin McAleer