From what I found you can’t do both at the same time.
I’ve had good luck with the initial connection part for connecting to my network:
import network
from time import sleep
#import usocket as socket
def connect_wifi():
N1 = network.WLAN_SPI(network.STA_IF)
N1.active(True)
connected = False
while not connected:
N1.connect("ssid","pass")
if N1.isconnected():
connected = True
else:
print("Not Connected, trying again.")
sleep(1)
print("Connected! My IP is: {}".format(N1.ifconfig()[0]))
But I can’t get it to communicate to anything else. Sockets library seems to have issues.