Pico W connected to wifi but not the internet

January 30, 2024, 18:03

sweetbrett

hey, i have a pico w and i had it working with wifi a few weeks ago. but today the same script keeps getting EHOSTUNREACH whenever I try to connect to something outside of my network. I have confirmed that the wifi connects, I have an IP, the default gw is set, I can ping the default gw, but when i ping something like 8.8.8.8 or 1.1.1.1 or if i try to socket.connect() to something like google.com I just get EHOSTUNRECH. I've rebooted a few times. Can anyone think of what might be causing this problem? using micropython.

sweetbrett

i figured out that my AP script was running in the background and interfering. Is there a way to reset the wifi.ifconfig() back to default before changing over to client/STA mode? so the DHCP when connecting will set the DNS and gateway properly?

sweetbrett

I can't figure out why the AP mode seems to prevent the Client mode from working after the initial "provision". Trying to use Blynk to create an IOT device and do wifi provisioning.

oops.se

If you don't provide any code, its extremely hard to guess what it could be...

sweetbrett

i'm starting with this: https://github.com/vshymanskyy/blynk-library-python/blob/master/examples/Edgent_Linux_RPi/BlynkEdgent.py and removing the nmcli parts to replace them with network.WIFI and other similar parts so it will work on the pico w. this is where I am at the moment but it seems the AP is still getting in the way of the traffic from the Client (STA) https://gist.github.com/ssplatt/6c6a63c7634b736e960d489e39a0c955

sweetbrett

so the main problem right now is in the connect(), which is called on line 193

sweetbrett

got a little further, i think there is a race condition with shutting down the AP but i can't figure out the proper way to wait for it. I tried doing the reverse of the wifi client "if not wifi.isconnected(): pass" and got to a different error:
Connecting to blynk.cloud:443...
Connecting to blynk.cloud:443...
Traceback (most recent call last):
  File "<stdin>", line 77, in <module>
  File "BlynkLib.py", line 259, in run
AttributeError: 'module' object has no attribute 'timeout'

sweetbrett

def _cleanup(self):
        self.wifi_ap.disconnect()
        self.wifi_ap.active(False)
        while self.wifi_ap.isconnected():
            log('AP disconnecting...')
            time.sleep(1)
            pass
        log('wifi ap status: ', self.wifi_ap.status())

sweetbrett

connecting...
connecting...
connecting...
Wifi AP Status:
- Wifi ifconfig:  ('0.0.0.0', '255.255.255.0', '192.168.0.1', '192.168.1.1')
- No connection and no activity
Wifi Client Status:
- Wifi ifconfig:  ('192.168.1.254', '255.255.255.0', '192.168.1.1', '192.168.1.1')
- Connected!
Testing connection to google.com...
('142.250.65.206', 80)
Traceback (most recent call last):
  File "<stdin>", line 30, in <module>
  File "BlynkEdgentPico.py", line 237, in provision
OSError: [Errno 113] EHOSTUNREACH
so I do get the DNS response to obtain the IP of 'google.com', which confirms my connection to my real router, 192.168.1.1, is working. but then the actual host outside of the network is unreachable. The AP object is disconnected and deactivated. I added some more code to reset the AP object's ifconfig, so it didn't retain the old IP info.

sweetbrett

I figured out that the wifi.scan() is the problem, but not sure why or how to fix it yet

sweetbrett

well, i found that if i wifi_ap.active(True) before I wifi_client.active(True) directly after they are initialized, the scan() doesn't break the connection. so the dummy code that works is:
wifi_ap = network.WLAN(network.AP_IF)
wifi_client = network.WLAN(network.STA_IF)
wifi_ap.active(True)
wifi_client.active(True)

# scan() only works on STA (client) interface
# also, must be active(True) to scan

scan()
deactivate_all_wifi()
create_ap()
do_provisioning_things()
deactivate_all_wifi()
connect_as_wifi_client()
test_connection_to_internet()