src/eric7/MicroPython/Devices/MCUScripts/esp32WiFiConnect.py

Mon, 20 Nov 2023 15:38:21 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 20 Nov 2023 15:38:21 +0100
branch
eric7
changeset 10326
d83e1fea5ea6
parent 10153
ffe7432f716b
child 11038
918bc08e1f89
permissions
-rw-r--r--

Fixed an issue in the MicroPython interface related to Pi Pico with Pimoroni 'pico wireless'.

def connect_wifi():
    import esp32
    import network
    from time import sleep

    try:
        nvs = esp32.NVS("wifi_creds")
        buf = bytearray(1024)
        size = nvs.get_blob("ssid", buf)
        ssid = buf[:size].decode()
        size = nvs.get_blob("password", buf)
        password = buf[:size].decode()
        size = nvs.get_blob("hostname", buf)
        hostname = buf[:size].decode()
        size = nvs.get_blob("country", buf)
        country = buf[:size].decode()

        print("Connecting WiFi to '{0}'...".format(ssid))

        if country:
            try:
                network.country(country)
            except AttributeError:
                pass

        if hostname:
            try:
                network.hostname(hostname)
            except AttributeError:
                pass

        wifi = network.WLAN(network.STA_IF)
        wifi.active(False)
        wifi.active(True)
        wifi.connect(ssid, password)
        max_wait = 140
        while max_wait and wifi.status() == network.STAT_CONNECTING:
            max_wait -= 1
            sleep(0.1)
        print("Connection status:", wifi.isconnected())
    except:
        print("WiFi secrets are kept in NVM. Please store them there!")

connect_wifi()

eric ide

mercurial