--- a/src/eric7/MicroPython/Devices/RP2040Devices.py Sat Feb 18 10:11:44 2023 +0100 +++ b/src/eric7/MicroPython/Devices/RP2040Devices.py Sat Feb 18 18:12:32 2023 +0100 @@ -16,6 +16,7 @@ from PyQt6.QtWidgets import QMenu from eric7 import Globals, Preferences +from eric7.EricGui.EricOverrideCursor import EricOverrideCursor from eric7.EricWidgets import EricMessageBox from eric7.EricWidgets.EricApplication import ericApp @@ -51,6 +52,7 @@ -1: self.tr('connection failed'), 0: self.tr('idle'), 1: self.tr('connecting'), + 2: self.tr('getting IP address'), 3: self.tr('connection successful'), }, "pimoroni": { @@ -443,7 +445,7 @@ else: return super().getWifiData() - out, err = self._interface.execute(command, timeout=10000) + out, err = self._interface.execute(command)##, timeout=10000) if err: raise OSError(self._shortError(err)) @@ -464,6 +466,98 @@ ap["status"] = str(ap["status"]) return station, ap + def connectWifi(self, ssid, password): + """ + Public method to connect a device to a WiFi network. + + @param ssid name (SSID) of the WiFi network + @type str + @param password password needed to connect + @type str + @return tuple containing the connection status and an error string + @rtype tuple of (bool, str) + """ + if self._deviceData["wifi_type"] == "picow": + command = """ +def connect_wifi(ssid, password): + import network + from time import sleep + import ujson + + wifi = network.WLAN(network.STA_IF) + wifi.active(False) + wifi.active(True) + wifi.connect(ssid, password) + max_wait = 100 + while max_wait: + if wifi.status() < 0 or wifi.status() >= 3: + break + max_wait -= 1 + sleep(0.1) + status = wifi.status() + print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) + +connect_wifi({0}, {1}) +del connect_wifi +""".format(repr(ssid), repr(password if password else "")) + elif self._deviceData["wifi_type"] == "pimoroni": + # TODO: not yet implemented + pass + else: + return super().connectWifi(ssid, password) + + with EricOverrideCursor(): + out, err = self._interface.execute(command, timeout=15000) + if err: + return False, err + + result = json.loads(out.decode("utf-8").strip()) + if result["connected"]: + error = "" + else: + try: + error = self.__statusTranslations[ + self._deviceData["wifi_type"] + ][result["status"]] + except KeyError: + error = str(result["status"]) + + return result["connected"], error + + def disconnectWifi(self): + """ + Public method to disconnect a device from the WiFi network. + + @return tuple containing a flag indicating success and an error string + @rtype tuple of (bool, str) + """ + if self._deviceData["wifi_type"] == "picow": + command = """ +def disconnect_wifi(): + import network + from time import sleep + + wifi = network.WLAN(network.STA_IF) + wifi.disconnect() + wifi.active(False) + sleep(0.1) + print(not wifi.isconnected()) + +disconnect_wifi() +del disconnect_wifi +""" + elif self._deviceData["wifi_type"] == "pimoroni": + # TODO: not yet implemented + pass + else: + return super().disconnectWifi() + + out, err = self._interface.execute(command) + if err: + return False, err + + return out.decode("utf-8").strip() == "True", "" + def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): """