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

Thu, 03 Aug 2023 17:33:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 03 Aug 2023 17:33:07 +0200
branch
eric7
changeset 10153
ffe7432f716b
parent 9890
66a6d3f131cc
permissions
-rw-r--r--

MicroPython
- Added support to set the host name of the device (WiFi and Ethernet).
- Added support to set the WiFi country code (where supported by the device and the installed firmware).

try:
    import secrets

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

        country = secrets.WIFI_COUNTRY
        if country:
            try:
                network.country(country)
            except AttributeError:
                rp2.country(country)

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

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

        wifi = network.WLAN(network.STA_IF)
        wifi.active(False)
        wifi.active(True)
        wifi.connect(secrets.WIFI_SSID, secrets.WIFI_KEY if secrets.WIFI_KEY else None)
        max_wait = 140
        while max_wait:
            if wifi.status() < 0 or wifi.status() >= 3:
                break
            max_wait -= 1
            sleep(0.1)
        if wifi.isconnected():
            print("WiFi connected:", wifi.ifconfig())
        else:
            print("WiFi connection failed")

    connect_wifi()
except ImportError:
    print("WiFi secrets are kept in 'secrets.py', please add them there!")

eric ide

mercurial