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) + "'...")