← index #11381Issue #9071
Related · high · value 0.303
QUERY · ISSUE

RasperryPi Pico W - Wifi issue with hidden SSID ?

openby Korhmopened 2023-04-30updated 2023-05-05
bug

Hello,

I have stability issues with my Raspi Pico W connected to a wifi network with SSID hidden
When I start the programm (with Thonny IDE run command, or ampy, or directly by plugin the Pico W to power supply), it does not always connect to wifi.

Most of the time after few seconds, I run into the error network.STAT_CONNECT_FAIL or network.STAT_WRONG_PASSWORD (but password is correct)
If it finally connect (after 3 or 4 physical or logical reset), it's not satable at all. ping command may more than 50% of the packet.

I did not find in the documentation of micropython (or of the Pico W) that hidden SSID may cause issue.

If I switch to a more classical Wifi netrowk (SSID not hidden), there is no issue

Is if something somebody already faced ?
Is it recommanded to use a not hidden SSID ?

I use the latestet micropython git branch (master - commit 867e4dd3dc9235974974fd5dab204371616d6f49)

MicroPython v1.20.0-24-g867e4dd3d on 2023-04-28; Raspberry Pi Pico W with RP2040

with latest submodule versions

➜  micropython git:(master) git submodule status
 84f56af13292d8f32c40acbd949bde698ddd4507 lib/asf4 (remotes/origin/circuitpython)
 531cab9c278c947d268bd4c94ecab9153a961b43 lib/axtls (axtls-2.1.5-44-g531cab9)
 35aaec4418ad78628a3b935885dd189d41ce779b lib/berkeley-db-1.xx (heads/master-7-g35aaec4)
 1635e36d06821af8b61302509e91bfcc1ade84c4 lib/btstack (v1.4-1660-g1635e36d0)
 36144f94bee69ad5e7b46fef06ce0c2405bafee5 lib/cyw43-driver (v1.0.0)
 55bffd3a71cbeed2104cf30e7a39b641d8c1ff48 lib/fsp (v2.3.0-1-g55bffd3a)
 e9de7e35f2339598b16cbb375f9992643ed81209 lib/libffi (v3.2.1-146-ge9de7e3)
 5c5d513093075f7245ea522101b17c50aa579af2 lib/libhydrogen (5c5d513)
 6ca936f6b588cee702c638eee75c2436e6cf75de lib/lwip (STABLE-2_1_3_RELEASE)
 1bc2c9cb8b8fe4659bd94b8ebba5a4c02029b7fa lib/mbedtls (v2.16.10-61-g1bc2c9cb8)
 c113611765278b2fc8dcf8b2f2c3513b35a69b39 lib/micropython-lib (v0.1-1154-gc113611)
 42849560ba7906f023f61e5f7ff3709ba2c1dfca lib/mynewt-nimble (nimble_1_4_0_tag-2-g42849560)
 7a4c9d946cf1801771fc180acdbf7b878f270093 lib/nrfx (v2.0.0)
 fa5a554c7944d2a196626f8d3631e44943f9abcc lib/nxp_driver (fa5a554)
 f396d05f8252d4670d4ea05c8b7ac938ef0cd381 lib/pico-sdk (1.5.0-1-gf396d05)
 fdb1ec6879bc8b7bb54ded73bac04839003c5a07 lib/stm32lib (F0-1.9.0+F4-1.16.0+F7-1.7.0+G0-1.5.1+G4-1.3.0+H7-1.11.0+L0-1.11.2+L1-1.10.3+L4-1.17.0+WB-1.10.0+WL-1.1.0-35-gfdb1ec6)
 868f2bcda092b0b8d5f7ac55ffaef2c81316d35e lib/tinyusb (0.13.0-345-g868f2bcda)
 0803fc519ad7227e841287fb3638d6c8b2f111a1 lib/wiznet5k (W5500_PHY_LINK-92-g0803fc5)

I build a custom firmware by adding 2 python modules with a manifest. The commands to bulld are :

cd ports/rp2
make FROZEN_MANIFEST=/home/damien/dev/upiscine/boards/rp2_PCIO-W_manifest.py BOARD=PICO_W submodules
make FROZEN_MANIFEST=/home/damien/dev/upiscine/boards/rp2_PCIO-W_manifest.py BOARD=PICO_W clean
make FROZEN_MANIFEST=/home/damien/dev/upiscine/boards/rp2_PCIO-W_manifest.py BOARD=PICO_W

with p2_PCIO-W_manifest.py

include("$(PORT_DIR)/boards/PICO_W/manifest.py")
module("microdot.py", base_path="./libs/microdot/src")
module("microdot_asyncio.py", base_path="./libs/microdot/src")

Code to connect to network:

import network
from secrets import secrets
from time import sleep
from rp2 import country

status_map = {
    network.STAT_IDLE: ('STAT_IDLE', 'no connection and no activity'),
    network.STAT_CONNECTING: ('STAT_CONNECTING', 'connecting in progress'),
    network.STAT_WRONG_PASSWORD: ('STAT_WRONG_PASSWORD', 'failed due to incorrect password'),
    network.STAT_NO_AP_FOUND: ('STAT_NO_AP_FOUND', 'failed because no access point replied'),
    network.STAT_CONNECT_FAIL: ('STAT_CONNECT_FAIL', 'failed due to other problems'),
    network.STAT_GOT_IP: ('STAT_GOT_IP', 'connection successful')
}

def wlan_connect():

    country('FR')

    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(False)

    wlan = network.WLAN(network.STA_IF)
    wlan.deinit()

    if not wlan.isconnected():
        print('connecting to network {} ...'.format(secrets.get('wifi_sid')))
        wlan.active(True)
        wlan.connect(secrets.get('wifi_sid'), secrets.get('wifi_password'))
        while not wlan.isconnected():
            print('Error: {} ({})'.format(status_map.get(wlan.status()), wlan.status()))
            sleep(1)
    print('network config:', wlan.ifconfig())

Thank you!

CANDIDATE · ISSUE

TLS/SSL module does not work on its own (Pico w)

closedby christophigelopened 2022-08-18updated 2023-12-12
bugport-rp2

Today I tested the new Raspberry Pico W board and especially the network connectivity. I am aware that every version for that board is marked as "unstable" but I still wanted to Issue a report, because it is a very strange error and I couldn't find any resource on the Internet describing similar issues.
My initial plan was to implement a small IoT service with the Pico W. It should connect to a server with a valid SSL/TLS certificate via TLS1.2, send info about the current power state and also receive commands.
Here is my code for the pico w:

from machine import Pin
import network
import socket
import json
import time
import ubinascii
import sys
import os
import ussl
import _thread

WIFI_SSID = "PLACEHOLDER"
WIFI_PASSWORD = "PLACEHOLDER"
ID = "PLACEHOLDER"

SERVER_HOSTNAME = "PLACEHOLDER_HOSTNAME"
SERVER_PORT = 4001

ca_data_base64 = "PLACEHOLDER_CA_CERT_DER_BASE64"
ca_data = ubinascii.a2b_base64(ca_data_base64.encode("utf-8"))

outp = Pin(16, Pin.OUT)
inp = Pin(17, Pin.IN, Pin.PULL_DOWN)
led = Pin("LED", Pin.OUT)

server_led = Pin(15, Pin.OUT)
wifi_led = Pin(14, Pin.OUT)
    
def getData():
    d = {"power": False}
    if inp.value() == 1:
        d["power"] = True
    return d

def init_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.config(pm = 0xa11140)
    
    return wlan
    
def connect_to_wifi(wlan):
    wlan.connect(WIFI_SSID, WIFI_PASSWORD)
    while not wlan.isconnected():
        if wlan.status() == network.STAT_IDLE:
            print("Error while Connecting to Wifi: STAT_IDLE")
        elif wlan.status() == network.STAT_CONNECTING:
            print("Connecting to Wifi... STAT_CONNECTING")
        elif wlan.status() == network.STAT_NO_AP_FOUND:
            print("Error while Connecting to Wifi: STAT_NO_AP_FOUND")
        elif wlan.status() == network.STAT_CONNECT_FAIL:
            print("Error while Connecting to Wifi: STAT_CONNECT_FAIL")
        elif wlan.status() == network.STAT_GOT_IP:
            print("Error while Connecting to Wifi: STAT_GOT_IP")
        else:
            print(f"Connecting to Wifi...")
            print("Status: " + str(wlan.status()))
        if wlan.status() < 0:
            raise OSError("Wifi not connected!")
        time.sleep(1)
    
    print("Wifi Connection stable")
    wifi_led.value(1)
    
def connect_to_server():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    print("Connecting to server...")
    
    s.connect(socket.getaddrinfo(SERVER_HOSTNAME, SERVER_PORT)[0][-1])
    tls_socket = ussl.wrap_socket(s, server_side=False, cert_reqs=ussl.CERT_REQUIRED, cadata=ca_data, server_hostname=SERVER_HOSTNAME, do_handshake=True)
    tls_socket.setblocking(False)

    tls_socket.write(bytes(ID, "utf-8"))
    
    server_led.value(1)
    
    print("Connection to server successful")
    
    return tls_socket

def receiving_loop(s):
    while True:
        try:
            re = s.read(512)
            if re != None:
                string = re.decode("utf-8")
                obj = json.loads(string)
                
                if obj["power"]:
                    print("Receiving 1")
                    outp.value(1)
                else:
                    print("Receiving 0")
                    outp.value(0)
        except ValueError:
            print("Error while decoding json")
        

def sending_loop(s):
    global confirm
    while True:
        data = getData()

        s.write(bytes(json.dumps(data), "utf-8"))
        print(f"Sent {json.dumps(data)}")
        
        time.sleep(1)

outp.value(0)

server_led.value(0)
wifi_led.value(0)
    
led.value(0)
time.sleep(0.3)
led.value(1)

wlan= init_wifi()

time.sleep(3)

try:
    connect_to_wifi(wlan)
    s = connect_to_server()
                
    _thread.start_new_thread(receiving_loop, [s])
    sending_loop(s)
except KeyboardInterrupt:
    print("Interrupting")
except OSError as e:
    print(e)

After implementing the backend and writing the pico w script in Thonny everything worked perfectly fine. The CA certificate I hardcoded into the software got accepted and a valid TLS connection could be established. I saved the script as main.py on the pico's flash and triple checked that it actually gets run at startup. I disconnected the pico from my Pc and plugged it into my wall adapter. I waited and I realized the pico wouldn't connect to the server at all. I checked the logs (the server is written in Java) and got "Received fatal alert: certificate_unknown" as the error message on server side. The next thing I did was to connect the pico back to my Pc and watching the execution from another COM reader other than Thonny. It still didn't work as expected with the same error on server side and the error (-9984, 'MBEDTLS_ERR_X509_CERT_VERIFY_FAILED') printed by the Pico.

When I run the script from Thonny I get this output:

Wifi Connection stable
Connecting to server...
Connection to server successful
Sent {"power": false}
Sent {"power": false}
Sent {"power": false}
[...]

When it starts by itself this is the output:

Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Wifi Connection stable
Connecting to server...
(-9984, 'MBEDTLS_ERR_X509_CERT_VERIFY_FAILED')

When started over and over by another script this is the output:

Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Connecting to Wifi... STAT_CONNECTING
Wifi Connection stable
Connecting to server...
(-9984, 'MBEDTLS_ERR_X509_CERT_VERIFY_FAILED')
Wifi Connection stable
Connecting to server...
(-9984, 'MBEDTLS_ERR_X509_CERT_VERIFY_FAILED')
[...]

First I thought this might happen because Thonny restarts scripts by performing a soft-reset and to reset it normally I would cut the power which is not just a soft reset. Therefore, I implemented a soft reset with sys.exit() when the error happens. But this didn't work either. This documentation is clear about that sys.exit() should perform a soft reset, the same reset I wanted to implement but it just doesn't reset. It just exits to the normal python shell and doesn't restart main.py or boot.py. And even when I do a machine reset and the pico is somehow connected to Thonny it does work. So I assume it is not the machine reset that causes the problem but I am not entirely sure.

After 8 hours of trial and error, I can say most certainly that the Pico W would only establish a valid connection to the server when the program is run or reset by the Thonny IDE. I have no idea why that happens, or how I can solve the problem and I would really appreciate help.

The firmware is rp2-pico-w-20220815-unstable-v1.19.1-284-ga16a330da.uf2

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied