793 else: |
793 else: |
794 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) |
794 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) |
795 |
795 |
796 return station, ap, overall |
796 return station, ap, overall |
797 |
797 |
798 def connectWifi(self, ssid, password, hostname): |
798 def connectWifi(self, ssid, password, hostname, country=""): |
799 """ |
799 """ |
800 Public method to connect a device to a WiFi network. |
800 Public method to connect a device to a WiFi network. |
801 |
801 |
802 @param ssid name (SSID) of the WiFi network |
802 @param ssid name (SSID) of the WiFi network |
803 @type str |
803 @type str |
804 @param password password needed to connect |
804 @param password password needed to connect |
805 @type str |
805 @type str |
806 @param hostname host name of the device |
806 @param hostname host name of the device |
807 @type str |
807 @type str |
|
808 @param country WiFi country code |
|
809 @type str |
808 @return tuple containing the connection status and an error string |
810 @return tuple containing the connection status and an error string |
809 @rtype tuple of (bool, str) |
811 @rtype tuple of (bool, str) |
810 """ |
812 """ |
811 if self._deviceData["wifi_type"] == "picow": |
813 if self._deviceData["wifi_type"] == "picow": |
812 country = Preferences.getMicroPython("WifiCountry").upper() |
814 if not country: |
|
815 country = Preferences.getMicroPython("WifiCountry").upper() |
813 command = """ |
816 command = """ |
814 def connect_wifi(ssid, password, hostname, country): |
817 def connect_wifi(ssid, password, hostname, country): |
815 import network |
818 import network |
816 import rp2 |
|
817 import ujson |
819 import ujson |
818 from time import sleep |
820 from time import sleep |
819 |
821 |
820 rp2.country(country) |
822 try: |
|
823 network.country(country) |
|
824 except AttributeError: |
|
825 import rp2 |
|
826 rp2.country(country) |
821 |
827 |
822 if hostname: |
828 if hostname: |
823 try: |
829 try: |
824 network.hostname(hostname) |
830 network.hostname(hostname) |
825 except AttributeError: |
831 except AttributeError: |
828 wifi = network.WLAN(network.STA_IF) |
834 wifi = network.WLAN(network.STA_IF) |
829 wifi.active(False) |
835 wifi.active(False) |
830 wifi.active(True) |
836 wifi.active(True) |
831 wifi.connect(ssid, password) |
837 wifi.connect(ssid, password) |
832 max_wait = 140 |
838 max_wait = 140 |
833 while max_wait: |
839 while max_wait and wifi.status() != network.STAT_GOT_IP: |
834 if wifi.status() < 0 or wifi.status() >= 3: |
|
835 break |
|
836 max_wait -= 1 |
840 max_wait -= 1 |
837 sleep(0.1) |
841 sleep(0.1) |
838 status = wifi.status() |
842 status = wifi.status() |
839 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
843 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
840 |
844 |
1944 ( |
1948 ( |
1945 self.tr("Hostname"), |
1949 self.tr("Hostname"), |
1946 ethStatus["hostname"] if ethStatus["hostname"] else self.tr("unknown"), |
1950 ethStatus["hostname"] if ethStatus["hostname"] else self.tr("unknown"), |
1947 ), |
1951 ), |
1948 (self.tr("MAC-Address"), ethStatus["mac"]), |
1952 (self.tr("MAC-Address"), ethStatus["mac"]), |
1949 (self.tr("Preferred IP Version"), str(ethStatus["prefer"])) |
1953 (self.tr("Preferred IP Version"), str(ethStatus["prefer"])), |
1950 ] |
1954 ] |
1951 addressInfo = { |
1955 addressInfo = { |
1952 "ipv4": ethStatus["ipv4_addr"], |
1956 "ipv4": ethStatus["ipv4_addr"], |
1953 "ipv6": ethStatus["ipv6_addr"], |
1957 "ipv6": ethStatus["ipv6_addr"], |
1954 } |
1958 } |