Creating a Wireless Network with Raspberry Pi Pico W: AP Mode Walkthrough (Part 1)

The Raspberry Pi Pico W is a powerful microcontroller that can be used for a variety of projects. One of its most interesting features is its ability to function as an access point (AP). This means that you can use the Pico W to create your own wireless network, which can be used to host a web server, control other devices, or simply share files.

In this blog post, I will show you how to use the Pico W in AP mode to host a web server.

What is AP mode?

AP mode is a mode in which the Pico W can function as a wireless access point. This means that the Pico W can create its own wireless network, which other devices can connect to.

Potential use cases

There are a number of potential use cases for using AP mode on the Pico W. Here are a few examples:

  • Hosting a web server: You can use the Pico W to host a simple web server, which can be used to display information or control other devices. (Which we will show in this tutorial)
  • Controlling other devices: You can use the Pico W to control other devices over the network. For example, you could use it to control a light bulb or a motor.
  • Sharing files: You can use the Pico W to share files with other devices over the network.
  • Creating a temporary network: You could use the Pico W to create a temporary network for debugging or testing.

Before we get started, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

Step 1-) Code

In order to get started all you will need is a Raspberry Pi Pico W with some sort of MicroPython environment to code on. I am using Thonny and it is probably the most common IDE to write MicroPython code.

In Thonny we use the following code:

import network
import time
import socket


def web_page():
  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
            <body><h1>Hello World</h1></body></html>
         """
  return html

# if you do not see the network you may have to power cycle
# unplug your pico w for 10 seconds and plug it in again
def ap_mode(ssid, password):
    """
        Description: This is a function to activate AP mode
        
        Parameters:
        
        ssid[str]: The name of your internet connection
        password[str]: Password for your internet connection
        
        Returns: Nada
    """
    # Just making our internet connection
    ap = network.WLAN(network.AP_IF)
    ap.config(essid=ssid, password=password)
    ap.active(True)
    
    while ap.active() == False:
        pass
    print('AP Mode Is Active, You can Now Connect')
    print('IP Address To Connect to:: ' + ap.ifconfig()[0])
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #creating socket object
    s.bind(('', 80))
    s.listen(5)

    while True:
      conn, addr = s.accept()
      print('Got a connection from %s' % str(addr))
      request = conn.recv(1024)
      print('Content = %s' % str(request))
      response = web_page()
      conn.send(response)
      conn.close()
      
ap_mode('NAME',
        'PASSWORD')

Explanation of Code:

  • The code begins by importing the required modules: network, time, and socket.
  • There is a function defined called web_page(), which returns an HTML string. This is what the WebServer we create will show once we visit the server from an external device. You can customize this as you like.
  • The ap_mode() function is defined. It is responsible for activating the Access Point (AP) mode on a device and creating the web server on the Pico W.
  • The function takes two parameters: ssid (the name of the internet connection) and password (password for the internet connection). The name of the network along with the password is up to you, if you are concerned about security be more creative with the names.
  • Inside the ap_mode() function, an Access Point (AP) object is created using network.WLAN(network.AP_IF). Very simple to do this and any Pico W should be able to do this.
  • The configuration for the AP is set using the provided ssid and password values as you defined.
  • The AP is activated by setting ap.active(True). Otherwise, it will not be shown in other devices.
  • The code enters a while loop and waits for the AP to become active, this while loop usually never runs because AP mode activates pretty quickly.
  • Once the AP is active, it displays a message indicating that AP mode is active and provides the IP address to connect to.
  • The remainder of the code is very simple client-server code
  • A socket object s is created using socket.socket(socket.AF_INET, socket.SOCK_STREAM).
  • The socket is bound to port 80 with s.bind(('', 80)). This is a general port.
  • The socket starts listening for incoming connections with s.listen(5). We can connect up to 5 devices in AP mode.
  • The code enters another while loop to continuously handle incoming connections. This will run forever until we stop the code or unplug the Pico W.
  • When a connection is received, it prints the address of the client.
  • The received request from the client is stored in the request variable using conn.recv(1024).
  • The web_page() function is called to generate an HTML response, which is stored in the response variable.
  • The response is sent back to the client using conn.send(response).
  • The connection is closed with conn.close().
  • Finally, the ap_mode() function is called with the desired ssid and password values.

This code essentially sets up a device to act as an Access Point, allowing clients to connect to it. When a client connects, it receives a simple "Hello World" HTML page as a response.

Step 2-) Connect from a Device

In order to connect to the network simply go to the WiFi on your device (Computer or Phone) and find the network. If the code runs and the network can’t be found just simply Power Cycle the Pico W by turning it off and leaving it off for 10 seconds, then rerun the code. In this case, I see the following WiFi network.

On a web browser, I go to the HTTP webpage as follows. Just use any web browser. Your IP address for your Pico W will be different; you can get it from the console in the code provided.

The result:

Conclusion:

That is the first part of using a Raspberry Pi Pico W in AP mode, you can use this to host a Web Server without access to the World Wide Web. Next, we will be using it to relay messages between Pico W’s. It is pretty interesting because, in its base form, this is essentially how the internet we all know and love works, just a series of networks intertwined and routed to different IP addresses serving different clients. If you are impressed with the Pico W or enjoyed this content, a subscription to the channel would be awesome. Let us know if you have any questions. Thanks!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

How to Code with Lua on ESP32 with XEdge32

How to Code with Lua on ESP32 with XEdge32

Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

Stream Audio From Raspberry Pi to Local Computer

Stream Audio From Raspberry Pi to Local Computer

Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

SSH Raspberry Pi via Cell Phone

SSH Raspberry Pi via Cell Phone

This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

Remotely Control Raspberry Pi via SSH from External Network

Remotely Control Raspberry Pi via SSH from External Network

Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

Stream Video from Raspberry Pi Camera to YouTube Live

Stream Video from Raspberry Pi Camera to YouTube Live

Learn how to stream to YouTube from a Raspberry Pi Camera.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.