Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!

### Embed the widget on your own site

Add the following snippet to your HTML:<iframe frameborder='0' height='385' scrolling='no' src='https://www.hackster.io/Shilleh/creating-a-wireless-network-with-raspberry-pi-pico-w-part-1-e3c997/embed' width='350'></iframe>

Learn how to create your own WiFi network using the Raspberry Pi Pico W in AP Mode.

Read up about this project on

#### Creating a Wireless Network with Raspberry Pi Pico W Part 1

Mahmood M. Shilleh
7
5,427
Mahmood M. Shilleh
Published July 15, 2023 © LGPL

# Creating a Wireless Network with Raspberry Pi Pico W Part 1

Learn how to create your own WiFi network using the Raspberry Pi Pico W in AP Mode.

Intermediate
Protip
15 minutes
5,431
Ad

##
Things used in this project

### Hardware components
Raspberry Pi Pico W
| × | 1 |
### Software apps and online services
MicroPython

##
Story

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 into it, consider subscribing or supporting the channel. Also, be sure to checkout our various stores to shop products for Raspberry Pi, Arduino, ESP32, and DIY electronics at ShillehTek!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

Hire me at UpWork to build your IoT projects:

https://www.upwork.com/freelancers/~017060e77e9d8a1157

Visit ShillehTek Store for Arduino and Raspberry Pi Sensors and Pre-Soldered Components:

ShillehTek Website (Exclusive Discounts):

https://shillehtek.com/collections/all

ShillehTek Amazon Store:

ShillehTek Amazon Store - US

ShillehTek Amazon Store - Canada

ShillehTek Amazon Store - Japan

###

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!

Read more

##
Credits

### Mahmood M. Shilleh

92 projects • 24 followers
Hire me at UpWork: https://www.upwork.com/freelancers/~017060e77e9d8a1157?viewMode=1 Find me on YouTube https://www.youtube.com/@mmshilleh
Contact Contact

##
Comments

Please log in or sign up to comment.

Ad

##### Related channels and tags

- communication
- embedded
- home automation
- wifi

##### Related projects

How to Run Code & Manage Files on Raspberry Pi Pico via CLI
Connect to Node App from Raspberry Pi Pico W
Connect MPU 6050 To Raspberry Pi Pico W
Send SMS with Raspberry Pi Pico W
Controlling a 5V Laser Diode with Raspberry Pi Pico W
View more related projects
1
2
3
Hey stranger! Sign up to access unlimited projects featuring Raspberry Pi Pico and more – it's free.
Not now