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

Fri, 28 Apr 2023 12:07:41 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 28 Apr 2023 12:07:41 +0200
branch
mpy_network
changeset 9990
54c614d91eff
parent 9890
66a6d3f131cc
child 10153
ffe7432f716b
permissions
-rw-r--r--

MicroPython
- Refactored the code by introducing a device interface base class and changed the interface instantiation logic to prepare the basis for developing the 'webrepl' interface.

try:
    import secrets

    def connect_wifi():
        import network
        from time import sleep

        print("Connecting WiFi to '{0}' ...".format(secrets.WIFI_SSID))
        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