File size: 2,655 Bytes
4bec42e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#              .';:cc;.
#            .,',;lol::c.
#            ;';lddddlclo
#            lcloxxoddodxdool:,.
#            cxdddxdodxdkOkkkkkkkd:.
#          .ldxkkOOOOkkOO000Okkxkkkkx:.
#        .lddxkkOkOOO0OOO0000Okxxxxkkkk:
#       'ooddkkkxxkO0000KK00Okxdoodxkkkko
#      .ooodxkkxxxOO000kkkO0KOxolooxkkxxkl
#      lolodxkkxxkOx,.      .lkdolodkkxxxO.
#      doloodxkkkOk           ....   .,cxO;
#      ddoodddxkkkk:         ,oxxxkOdc'..o'
#      :kdddxxxxd,  ,lolccldxxxkkOOOkkkko,
#       lOkxkkk;  :xkkkkkkkkOOO000OOkkOOk.
#        ;00Ok' 'O000OO0000000000OOOO0Od.
#         .l0l.;OOO000000OOOOOO000000x,
#            .'OKKKK00000000000000kc.
#               .:ox0KKKKKKK0kdc,.
#                      ...
#
# Author: peppe8o
# Date: Jul 24th, 2022
# Version: 1.0
# https://peppe8o.com

# modified by @sgbaird from source:
# https://peppe8o.com/getting-started-with-wifi-on-raspberry-pi-pico-w-and-micropython/

import time

import network
import rp2
from ubinascii import hexlify


def connectWiFi(ssid, password, country=None, wifi_energy_saver=False, retries=3):
    for _ in range(retries):
        try:
            if country is not None:
                # https://www.google.com/search?q=wifi+country+codes
                rp2.country(country)
            wlan = network.WLAN(network.STA_IF)
            if not wifi_energy_saver:
                wlan.config(pm=0xA11140)  # avoid the energy-saving WiFi mode
            wlan.active(True)

            mac = hexlify(network.WLAN().config("mac"), ":").decode()
            print(f"MAC address: {mac}")

            wlan.connect(ssid, password)
            # Wait for connect or fail
            max_wait = 10
            while max_wait > 0:
                if wlan.status() < 0 or wlan.status() >= 3:
                    break
                max_wait -= 1
                print("waiting for connection...")
                time.sleep(1)

            # Handle connection error
            if wlan.status() != 3:
                raise RuntimeError("network connection failed")
            else:
                print("connected")
                status = wlan.ifconfig()
                print("ip = " + status[0])
            return status
        except RuntimeError as e:
            print(f"Attempt failed with error: {e}. Retrying...")
    raise RuntimeError(
        "All attempts to connect to the network failed. Ensure you are using a 2.4 GHz WiFi network with WPA-2 authentication. See the additional prerequisites section from https://doi.org/10.1016/j.xpro.2023.102329 or the https://github.com/sparks-baird/self-driving-lab-demo/issues/76 for additional troubleshooting help."
    )