55 |
55 |
56 self.__statusTranslations = { |
56 self.__statusTranslations = { |
57 200: self.tr("beacon timeout"), |
57 200: self.tr("beacon timeout"), |
58 201: self.tr("no matching access point found"), |
58 201: self.tr("no matching access point found"), |
59 202: self.tr("authentication failed"), |
59 202: self.tr("authentication failed"), |
60 203: self.tr("association failed"), |
60 203: self.tr("connection failed"), |
61 204: self.tr("handshake timeout"), |
61 204: self.tr("handshake timeout"), |
62 210: self.tr("no access point with compatible security found"), |
62 210: self.tr("no access point with compatible security found"), |
63 211: self.tr("no access point with suitable authentication mode found"), |
63 211: self.tr("no access point with suitable authentication mode found"), |
64 212: self.tr("no access point with sufficient RSSI found"), |
64 212: self.tr("no access point with sufficient RSSI found"), |
65 1000: self.tr("idle"), |
65 1000: self.tr("idle"), |
837 ap["ap_security"] = self.__securityMapping[ap["ap_security"]] |
837 ap["ap_security"] = self.__securityMapping[ap["ap_security"]] |
838 except KeyError: |
838 except KeyError: |
839 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) |
839 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) |
840 return station, ap, overall |
840 return station, ap, overall |
841 |
841 |
842 def connectWifi(self, ssid, password, hostname): |
842 def connectWifi(self, ssid, password, hostname, country=""): |
843 """ |
843 """ |
844 Public method to connect a device to a WiFi network. |
844 Public method to connect a device to a WiFi network. |
845 |
845 |
846 @param ssid name (SSID) of the WiFi network |
846 @param ssid name (SSID) of the WiFi network |
847 @type str |
847 @type str |
848 @param password password needed to connect |
848 @param password password needed to connect |
849 @type str |
849 @type str |
850 @param hostname host name of the device |
850 @param hostname host name of the device |
851 @type str |
851 @type str |
|
852 @param country WiFi country code |
|
853 @type str |
852 @return tuple containing the connection status and an error string |
854 @return tuple containing the connection status and an error string |
853 @rtype tuple of (bool, str) |
855 @rtype tuple of (bool, str) |
854 """ |
856 """ |
855 if self.hasCircuitPython(): |
857 if self.hasCircuitPython(): |
856 return self.__cpyDevice.connectWifi(ssid, password, hostname) |
858 return self.__cpyDevice.connectWifi(ssid, password, hostname, country) |
857 |
859 |
|
860 if not country: |
|
861 country = Preferences.getMicroPython("WifiCountry").upper() |
858 command = """ |
862 command = """ |
859 def connect_wifi(ssid, password, hostname): |
863 def connect_wifi(ssid, password, hostname, country): |
860 import network |
864 import network |
861 import ujson |
865 import ujson |
862 from time import sleep |
866 from time import sleep |
|
867 |
|
868 try: |
|
869 network.country(country) |
|
870 except AttributeError: |
|
871 pass |
863 |
872 |
864 if hostname: |
873 if hostname: |
865 try: |
874 try: |
866 network.hostname(hostname) |
875 network.hostname(hostname) |
867 except AttributeError: |
876 except AttributeError: |
870 wifi = network.WLAN(network.STA_IF) |
879 wifi = network.WLAN(network.STA_IF) |
871 wifi.active(False) |
880 wifi.active(False) |
872 wifi.active(True) |
881 wifi.active(True) |
873 wifi.connect(ssid, password) |
882 wifi.connect(ssid, password) |
874 max_wait = 140 |
883 max_wait = 140 |
875 while max_wait and wifi.status() == network.STAT_CONNECTING: |
884 while max_wait and wifi.status() != network.STAT_GOT_IP: |
876 max_wait -= 1 |
885 max_wait -= 1 |
877 sleep(0.1) |
886 sleep(0.1) |
878 status = wifi.status() |
887 status = wifi.status() |
879 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
888 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
880 |
889 |
881 connect_wifi({0}, {1}, {2}) |
890 connect_wifi({0}, {1}, {2}, {3}) |
882 del connect_wifi |
891 del connect_wifi |
883 """.format( |
892 """.format( |
884 repr(ssid), |
893 repr(ssid), |
885 repr(password if password else ""), |
894 repr(password if password else ""), |
886 repr(hostname), |
895 repr(hostname), |
|
896 repr(country if country else "XX"), |
887 ) |
897 ) |
888 |
898 |
889 with EricOverrideCursor(): |
899 with EricOverrideCursor(): |
890 out, err = self.executeCommands( |
900 out, err = self.executeCommands( |
891 command, mode=self._submitMode, timeout=15000 |
901 command, mode=self._submitMode, timeout=15000 |