element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Need help Setting up the Pico W up as an access point
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 3 replies
  • Subscribers 661 subscribers
  • Views 2646 views
  • Users 0 members are here
Related

Need help Setting up the Pico W up as an access point

gregaryb
gregaryb over 2 years ago

I am in the process of setting up a wrapper class for setting the Pico up as either an access point or just connecting to an existing network.

Forget about 'BeginHTTP(self, bIsAccessPoint, strSSID, strPassword):' - I have not tested that function yet.

I am only working on ' BeginUDP(self, bIsAccessPoint, strSSID, strPassword, nPort):'

This begin function is being called like so:

BeginUDP(True, "AARONS_ROBOT", "PICO123456", 10002)

The function exits with no error message.

My message stating that the access point has been started and is listening on port 10002 is displayed.

The access point appears on my mobile phone.

But when I try and connect to it, the connection times out and fails.

Why?

=====================================================


import socket
import network

class CWiFi:

    strSSID = ""
    strPassword = ""
    WiFi = 0
    nPort = 0
    Socket = 0
    strIPAddr = ""
    strIPAddrRequest = ""
    nReceiveDataSize = 2048
    Socket = 0
    Client = 0
    
    # Function overloading not allowed in Python so the constructor does nothing useful
    def __init_():
        strSSID = ""
        
    # Common setup tasks
    def Begin(self, bIsAccessPoint, strSSID, strPassword):
        self.strSSID = strSSID
        self.strPassword = strPassword
        self.WiFi = network.WLAN(network.AP_IF) # Access point
        
        # Start Access Point
        if (bIsAccessPoint):
            self.WiFi.config(essid = strSSID, password = strPassword)
            self.WiFi.active(True)

            while self.WiFi.active == False:
                pass

            self.strIPAddr = str(self.WiFi.ifconfig()[0])
            print("Access point started with IP address '" + self.strIPAddr + "'")
        # Connect to existing network
        else:
            self.WiFi.active(True)
            self.WiFi.connect(strSSID, strPassword)
            while (self.WiFi.isconnected() == False):
                print('Waiting for connection...')
                sleep(1)
            self.strIPAddr = self.WiFi.ifconfig()[0]
            print("Connected to SSID: '" + strSSID + ", with IP address: '" + self.strIPAddr + "'")

    # Setup the AP for HTTP requests
    def BeginHTTP(self, bIsAccessPoint, strSSID, strPassword):
        self.Begin(bIsAccessPoint, strSSID, strPassword)
        self.nPort = 80
        self.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #creating socket object
        self.Socket.bind((self.strIPAddr, nPort))
        self.Socket.listen(1)
        print("Waiting for request on port '" + str(self.nPort) + "'...")
        
    # Setup the AP for UDP messages
    def BeginUDP(self, bIsAccessPoint, strSSID, strPassword, nPort):
        self.Begin(bIsAccessPoint, strSSID, strPassword)
        self.nPort = nPort
        self.Socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.Socket.bind((self.strIPAddr, self.nPort))
        print("Waiting for request on port '" + str(self.nPort) + "'...")

  • Sign in to reply
  • Cancel
  • shabaz
    0 shabaz over 2 years ago

    Hi,

    Regarding "The access point appears on my mobile phone. But when I try and connect to it, the connection times out and fails." are you referring to a WiFi connection failure, or some application timeout/failure to send UDP?

    If it's the former, then you could try the very stripped down code here: https://www.recantha.co.uk/blog/?p=21398 (under the heading "Initial access point" and if that doesn't work, it could be a hardware or power issue, or perhaps something to do with your setup (a photo may be needed) or old micropython firmware possibly.

    If it's the latter, is there some code missing from the end? There should be a self.Socket.udp_recv or something. 

    I have not tried it myself, but an example here contains that: https://forums.raspberrypi.com/viewtopic.php?t=340208

    There could even be an issue with the mobile app with UDP perhaps, since there are often slight changes in different releases of app and mobile OS. If there's any doubt, then it could be worthwhile attempting UDP client from a PC instead (and make sure it's allowed in the firewall configuration). 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gregaryb
    0 gregaryb over 2 years ago in reply to shabaz

    I don't get as far as Socket.udp_recv mate. The login fails before that. My phone asks me to change the password.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 2 years ago

    I gave it a quick shot, and this functioned for me, it is simpler code (no class and no switching between existing WLAN and WAP modes, it is WAP mode only) just to see if it works:

    import socket
    import network
    
    ssid = "PicoW"
    password = "b0bb0bb0b"
    sock = None
    
    ap = network.WLAN(network.AP_IF)
    ap.config(essid=ssid, password=password) 
    ap.active(True)
    
    while ap.active == False:
      pass
    
    print("Access point active")
    print(ap.ifconfig())
    
    def udp_server_setup(portnum):
        global sock
        if not ap.active:
            print("error, AP is not active yet")
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        server_ip = socket.getaddrinfo("0.0.0.0", portnum, socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind(server_ip[0][4])
        print(f"Waiting for request on {server_ip}:{portnum}")
        while True:
            pkt = sock.recv(4)
            if len(pkt)<1:
                print(f"packet length is {len(pkt)}")
            else:
                print(f"packet is {pkt}")
    
    
    
    udp_server_setup(1250)
    
    

    With the above code (I used the latest micropython linked to from the Pi page)

    I see this:

    MicroPython v1.19.1 on 2022-10-11; Raspberry Pi Pico W with RP2040
    Type "help()" for more information.
    >>> import pico_wap_test
    Access point active
    ('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')
    Waiting for request on [(2, 1, 0, '', ('0.0.0.0', 1250))]:1250
    packet is b'Abcd'

    I sent a UDP packet from my Windows laptop, because I don't have an app on my mobile, the debug above shows the packet contained "Abcd".

    The mobile (running Android) did connect to the Pico's SSID, even though I didn't test the UDP packet.

    image

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube