Inconsistent behavior with network connectivity? #12260
Replies: 9 comments 4 replies
-
I have no experience with the Pico WiFi, so these are suggestions based on my ESP32 trials. After # seems more reliable to start with a fresh connect()
if nic.isconnected():
nic.disconnect()
print ('started in the connected state, but now disconnected')
else:
print ('started in the disconnected state')
utime.sleep(0.1) For Unit 2 use a try/except around your connect and see if errors are being thrown. Apparently, it doesn't on the ESP32 but who knows. For the ESP32 getting a `WiFi internal error' was a show-stopper for me. Have you tried re-booting the units in a different order? |
Beta Was this translation helpful? Give feedback.
-
Did you check that all three boards have different MAC addresses and get different IP addresses, when connecting to the Router? |
Beta Was this translation helpful? Give feedback.
-
You might want to try the test script below. I've tested it with 2 Pico-W and got good results. I only have 2 Pico-W boards. They were connected to a powerd USB hub (radio needs power). They were very close together (interference test) and about 5 metres (radio power test) from a cheap d-link WiFi router. import time, network, socket
ap = network.WLAN(network.AP_IF); ap.active(False) # Disable AP!
nic = network.WLAN(network.STA_IF); nic.active(True)
nic_mac = nic.config('mac')
print("MAC Address:", nic_mac)
while True:
nic.connect("wifi-ssid", "wifi-pwd")
cnt =0
while cnt<10:
if not nic.isconnected():
print(f'wait {cnt}')
time.sleep(2)
cnt+=1
else:
break
if nic.isconnected():
print(nic.ifconfig())
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n')
data = s.recv(1000)
print(data[:80])
s.close() # don't forget to close the socket
nic.disconnect() # and disconnect from WiFi
else:
print('No WiFi')
time.sleep(5) |
Beta Was this translation helpful? Give feedback.
-
The issue is unfortunately not solved. Getting ~50cm close to the router (acting as both a router and a Wifi access point) does not help. So again, I don't think the wireless signal is the issue. Since the issue is persistent even after re-installing the firmware and after several resets, I think that there is either an issue with the Wifi chip, or with the TCP/IP stack in Micropython (at least the implementation on the pico w). |
Beta Was this translation helpful? Give feedback.
-
If you can reliably connect to the router/AP and get the IP address by DHCP, then the hardware of the pico W is fine, and the tcp/ip stack is working as well. |
Beta Was this translation helpful? Give feedback.
-
May I ask how you connect your three Pico-Ws when you run your test? Since you want to see the output, I assume you connect all three to three USB ports on a laptop. Could it be that the laptop simply cannot supply enough power to drive all three Pico-Ws? |
Beta Was this translation helpful? Give feedback.
-
@robert-hh No the connection to the router/AP is not reliable. Just in some cases, nic.ifconfig() showed an IP address, I found the same IP address on the web console of my router under the list of connected devices. One of the reasons, why I think there is an issue with the hardware or the tcp/ip stack, is that quite often, the nic.isconnected() is True, I get an IP configuration, with an IP address, network mask, default gateway and DNS server, but the default gateway is not reachable and DNS is not resoling (i.e., socket.getaddrinfo is hanging or triggers an OSError -1) For example, since in many cases, nic.isconnected() returns True, but socket.getaddrinfo fails, I am using the uping package to ping the gateway. and noticed that the gateway is not reachable. See code below. How can the Wifi network interface be connected, but the default gateway not reachable? In my opinion, this is inconsistent networking behavior. And yes, I rebooted the router. and I reset the picow several times. import time, network, socket
from uping import ping
ap = network.WLAN(network.AP_IF); ap.active(False) # Disable AP!
nic = network.WLAN(network.STA_IF); nic.active(True)
while True:
nic.connect("mySSID", "myKey")
for i in range(10):
if nic.isconnected():
break
else:
print(f'wait {i}')
time.sleep(1)
if nic.isconnected():
print(nic.ifconfig())
ip, mask, gateway, dns = nic.ifconfig()
n_trans, n_recv = ping(gateway, count = 10)
if n_recv > 0: # gateway is responding
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n')
data = s.recv(1000)
print(data[:80])
s.close() # don't forget to close the socket
break
else:
print('Wifi seems to be connected. But gateway not reachable!')
print('No WiFi. Will try in 5 seconds.')
nic.disconnect() # and disconnect from WiFi before the next trial.
time.sleep(5) |
Beta Was this translation helpful? Give feedback.
-
I would like to add that examining the wifi signal strength using nic.scan() returns very good values for all 3 boards. However, what I find strange, is that the returned tuples from nic.scan() do not fit to the spec of .scan() as described in https://docs.micropython.org/en/latest/library/network.WLAN.html
The first 4 output parameters look good:
However, the last 2 output parameters do not look conform to the spec: For example, according to the spec, the last output parameter should be "hidden" and should be a value of either 0 or 1. So why am I getting 4 or 5? Also the "security" values should be between 0 and 4 according to the spec. So why am I getting 5? Am I using the right spec? |
Beta Was this translation helpful? Give feedback.
-
rp2 board:
The rp2 network module is in micropython/extmod/network_cyw43.c, if I'm not mistaken. The network_cyw43_scan_cb() function shows this:
The esp32 gives the following results
I'm running out of ideas. Maybe the results of the scan are not important. Do you have another AP you can try? (public library, internet cafe, friend's house). I wonder if all 3 pico-w's behave similarly or if 1 is consistently good and the other 2 have problems? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I have experienced inconsistent behavior with wifi networking with the pico w (no it's not because of weak signal strength!).
So for debugging purposes, I am now trying a very simple script (See code below) based on the examples from https://docs.micropython.org/en/latest/library/network.html#common-network-adapter-interface
I have 3 picow's. and for whatever reason,
I am using the firmware v1.20.0 (2023-04-26) downloaded from micropython.org. As I mentioned, the wifi can very likely not be the issue, because all 3 devices are in the same distance to the router (which is around 5meters!)
Here is the code
Beta Was this translation helpful? Give feedback.
All reactions