47 |
47 |
48 self.__createRP2040Menu() |
48 self.__createRP2040Menu() |
49 |
49 |
50 self.__statusTranslations = { |
50 self.__statusTranslations = { |
51 "picow": { |
51 "picow": { |
52 -3: self.tr('authentication failed'), |
52 -3: self.tr("authentication failed"), |
53 -2: self.tr('no matching access point found'), |
53 -2: self.tr("no matching access point found"), |
54 -1: self.tr('connection failed'), |
54 -1: self.tr("connection failed"), |
55 0: self.tr('idle'), |
55 0: self.tr("idle"), |
56 1: self.tr('connecting'), |
56 1: self.tr("connecting"), |
57 2: self.tr('connected, waiting for IP address'), |
57 2: self.tr("connected, waiting for IP address"), |
58 3: self.tr('connected'), |
58 3: self.tr("connected"), |
59 }, |
59 }, |
60 "pimoroni": { |
60 "pimoroni": { |
61 # TODO: not yet implemented |
61 # TODO: not yet implemented |
62 }, |
62 }, |
63 } |
63 } |
64 self.__securityTranslations = { |
64 self.__securityTranslations = { |
65 0: self.tr("open", "open WiFi network"), |
65 0: self.tr("open", "open WiFi network"), |
66 1: "WEP", |
66 1: "WEP", |
67 2: "WPA", |
67 2: "WPA", |
68 3: "WPA2", |
68 3: "WPA2", |
69 4: "WPA/WPA2", |
69 4: "WPA/WPA2", |
70 5: "WPA2 (CCMP)", |
70 5: "WPA2 (CCMP)", |
71 6: "WPA3", |
71 6: "WPA3", |
72 7: "WPA2/WPA3" |
72 7: "WPA2/WPA3", |
73 } |
73 } |
74 |
74 |
75 def setButtons(self): |
75 def setButtons(self): |
76 """ |
76 """ |
77 Public method to enable the supported action buttons. |
77 Public method to enable the supported action buttons. |
419 raise OSError(self._shortError(err)) |
419 raise OSError(self._shortError(err)) |
420 return ast.literal_eval(out.decode("utf-8")) |
420 return ast.literal_eval(out.decode("utf-8")) |
421 |
421 |
422 def getWifiData(self): |
422 def getWifiData(self): |
423 """ |
423 """ |
424 Public method to get data related to the current WiFi status |
424 Public method to get data related to the current WiFi status. |
425 |
425 |
426 @return tuple of two dictionaries containing the WiFi status data |
426 @return tuple of two dictionaries containing the WiFi status data |
427 for the WiFi client and access point |
427 for the WiFi client and access point |
428 @rtype tuple of (dict, dict) |
428 @rtype tuple of (dict, dict) |
|
429 @exception OSError raised to indicate an issue with the device |
429 """ |
430 """ |
430 if self._deviceData["wifi_type"] == "picow": |
431 if self._deviceData["wifi_type"] == "picow": |
431 command = """ |
432 command = """ |
432 def wifi_status(): |
433 def wifi_status(): |
433 import ubinascii |
434 import ubinascii |
469 # TODO: not yet implemented |
470 # TODO: not yet implemented |
470 pass |
471 pass |
471 else: |
472 else: |
472 return super().getWifiData() |
473 return super().getWifiData() |
473 |
474 |
474 out, err = self._interface.execute(command)##, timeout=10000) |
475 out, err = self._interface.execute(command) |
475 if err: |
476 if err: |
476 raise OSError(self._shortError(err)) |
477 raise OSError(self._shortError(err)) |
477 |
478 |
478 stationStr, apStr = out.decode("utf-8").splitlines() |
479 stationStr, apStr = out.decode("utf-8").splitlines() |
479 station = json.loads(stationStr) |
480 station = json.loads(stationStr) |
483 self._deviceData["wifi_type"] |
484 self._deviceData["wifi_type"] |
484 ][station["status"]] |
485 ][station["status"]] |
485 except KeyError: |
486 except KeyError: |
486 station["status"] = str(station["status"]) |
487 station["status"] = str(station["status"]) |
487 try: |
488 try: |
488 ap["status"] = self.__statusTranslations[ |
489 ap["status"] = self.__statusTranslations[self._deviceData["wifi_type"]][ |
489 self._deviceData["wifi_type"] |
490 ap["status"] |
490 ][ap["status"]] |
491 ] |
491 except KeyError: |
492 except KeyError: |
492 ap["status"] = str(ap["status"]) |
493 ap["status"] = str(ap["status"]) |
493 return station, ap |
494 return station, ap |
494 |
495 |
495 def connectWifi(self, ssid, password): |
496 def connectWifi(self, ssid, password): |
528 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
529 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
529 |
530 |
530 connect_wifi({0}, {1}, {2}) |
531 connect_wifi({0}, {1}, {2}) |
531 del connect_wifi |
532 del connect_wifi |
532 """.format( |
533 """.format( |
533 repr(ssid), |
534 repr(ssid), |
534 repr(password if password else ""), |
535 repr(password if password else ""), |
535 repr(country if country else "XX"), |
536 repr(country if country else "XX"), |
536 ) |
537 ) |
537 elif self._deviceData["wifi_type"] == "pimoroni": |
538 elif self._deviceData["wifi_type"] == "pimoroni": |
538 # TODO: not yet implemented |
539 # TODO: not yet implemented |
539 pass |
540 pass |
540 else: |
541 else: |
541 return super().connectWifi(ssid, password) |
542 return super().connectWifi(ssid, password) |
548 result = json.loads(out.decode("utf-8").strip()) |
549 result = json.loads(out.decode("utf-8").strip()) |
549 if result["connected"]: |
550 if result["connected"]: |
550 error = "" |
551 error = "" |
551 else: |
552 else: |
552 try: |
553 try: |
553 error = self.__statusTranslations[ |
554 error = self.__statusTranslations[self._deviceData["wifi_type"]][ |
554 self._deviceData["wifi_type"] |
555 result["status"] |
555 ][result["status"]] |
556 ] |
556 except KeyError: |
557 except KeyError: |
557 error = str(result["status"]) |
558 error = str(result["status"]) |
558 |
559 |
559 return result["connected"], error |
560 return result["connected"], error |
560 |
561 |
627 if self._deviceData["wifi_type"] == "picow": |
628 if self._deviceData["wifi_type"] == "picow": |
628 country = Preferences.getMicroPython("WifiCountry").upper() |
629 country = Preferences.getMicroPython("WifiCountry").upper() |
629 secrets = "WIFI_SSID = {0}\nWIFI_KEY = {1}\nWIFI_COUNTRY={2}".format( |
630 secrets = "WIFI_SSID = {0}\nWIFI_KEY = {1}\nWIFI_COUNTRY={2}".format( |
630 repr(ssid), |
631 repr(ssid), |
631 repr(password) if password else '""', |
632 repr(password) if password else '""', |
632 repr(country) if country else '""' |
633 repr(country) if country else '""', |
633 ) |
634 ) |
634 wifiConnectFile = "picowWiFiConnect.py" |
635 wifiConnectFile = "picowWiFiConnect.py" |
635 else: |
636 else: |
636 secrets = "WIFI_SSID = {0}\nWIFI_KEY = {1}\n".format( |
637 secrets = "WIFI_SSID = {0}\nWIFI_KEY = {1}\n".format( |
637 repr(ssid), |
638 repr(ssid), |
641 try: |
642 try: |
642 # write secrets file |
643 # write secrets file |
643 self.putData("/secrets.py", secrets.encode("utf-8")) |
644 self.putData("/secrets.py", secrets.encode("utf-8")) |
644 # copy auto-connect file |
645 # copy auto-connect file |
645 self.put( |
646 self.put( |
646 os.path.join( |
647 os.path.join(os.path.dirname(__file__), "MCUScripts", wifiConnectFile), |
647 os.path.dirname(__file__), "MCUScripts", wifiConnectFile |
|
648 ), |
|
649 "/wifi_connect.py", |
648 "/wifi_connect.py", |
650 ) |
649 ) |
651 except OSError as err: |
650 except OSError as err: |
652 return False, str(err) |
651 return False, str(err) |
653 |
652 |
845 sleep(0.1) |
848 sleep(0.1) |
846 print(ap.isconnected()) |
849 print(ap.isconnected()) |
847 |
850 |
848 start_ap() |
851 start_ap() |
849 del start_ap |
852 del start_ap |
850 """.format(repr(ssid), security, repr(password), repr(country if country else "XX")) |
853 """.format( |
|
854 repr(ssid), security, repr(password), repr(country if country else "XX") |
|
855 ) |
851 elif self._deviceData["wifi_type"] == "pimoroni": |
856 elif self._deviceData["wifi_type"] == "pimoroni": |
852 # TODO: not yet implemented |
857 # TODO: not yet implemented |
853 pass |
858 pass |
854 else: |
859 else: |
855 return super().startAccessPoint(ssid, security=security, password=password) |
860 return super().startAccessPoint(ssid, security=security, password=password) |
928 Preferences.setMicroPython("WifiCountry", country) |
933 Preferences.setMicroPython("WifiCountry", country) |
929 |
934 |
930 command = """ |
935 command = """ |
931 import rp2 |
936 import rp2 |
932 rp2.country({0}) |
937 rp2.country({0}) |
933 """.format(repr(country)) |
938 """.format( |
|
939 repr(country) |
|
940 ) |
934 |
941 |
935 out, err = self._interface.execute(command) |
942 out, err = self._interface.execute(command) |
936 if err: |
943 if err: |
937 self.microPython.showError("rp2.country()", err) |
944 self.microPython.showError("rp2.country()", err) |
938 |
945 |