663 |
663 |
664 def getWifiData(self): |
664 def getWifiData(self): |
665 """ |
665 """ |
666 Public method to get data related to the current WiFi status. |
666 Public method to get data related to the current WiFi status. |
667 |
667 |
668 @return tuple of two dictionaries containing the WiFi status data |
668 @return tuple of three dictionaries containing the WiFi status data |
669 for the WiFi client and access point |
669 for the WiFi client, access point and overall data |
670 @rtype tuple of (dict, dict) |
670 @rtype tuple of (dict, dict, dict) |
671 @exception OSError raised to indicate an issue with the device |
671 @exception OSError raised to indicate an issue with the device |
672 """ |
672 """ |
673 command = """ |
673 command = """ |
674 def wifi_status(): |
674 def wifi_status(): |
675 import ubinascii |
675 import ubinascii |
706 ap['txpower'] = wifi.config('txpower') |
706 ap['txpower'] = wifi.config('txpower') |
707 except ValueError: |
707 except ValueError: |
708 pass |
708 pass |
709 print(ujson.dumps(ap)) |
709 print(ujson.dumps(ap)) |
710 |
710 |
|
711 overall = { |
|
712 'active': station['active'] or ap['active'] |
|
713 } |
|
714 print(ujson.dumps(overall)) |
|
715 |
711 wifi_status() |
716 wifi_status() |
712 del wifi_status |
717 del wifi_status |
713 """ |
718 """ |
714 |
719 |
715 out, err = self._interface.execute(command) |
720 out, err = self._interface.execute(command) |
716 if err: |
721 if err: |
717 raise OSError(self._shortError(err)) |
722 raise OSError(self._shortError(err)) |
718 |
723 |
719 stationStr, apStr = out.decode("utf-8").splitlines() |
724 stationStr, apStr, overallStr = out.decode("utf-8").splitlines() |
720 station = json.loads(stationStr) |
725 station = json.loads(stationStr) |
721 ap = json.loads(apStr) |
726 ap = json.loads(apStr) |
|
727 overall = json.loads(overallStr) |
722 try: |
728 try: |
723 station["status"] = self.__statusTranslations[station["status"]] |
729 station["status"] = self.__statusTranslations[station["status"]] |
724 except KeyError: |
730 except KeyError: |
725 station["status"] = str(station["status"]) |
731 station["status"] = str(station["status"]) |
726 try: |
732 try: |
727 ap["status"] = self.__statusTranslations[ap["status"]] |
733 ap["status"] = self.__statusTranslations[ap["status"]] |
728 except KeyError: |
734 except KeyError: |
729 ap["status"] = str(ap["status"]) |
735 ap["status"] = str(ap["status"]) |
730 return station, ap |
736 return station, ap, overall |
731 |
737 |
732 def connectWifi(self, ssid, password): |
738 def connectWifi(self, ssid, password): |
733 """ |
739 """ |
734 Public method to connect a device to a WiFi network. |
740 Public method to connect a device to a WiFi network. |
735 |
741 |
758 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
764 print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) |
759 |
765 |
760 connect_wifi({0}, {1}) |
766 connect_wifi({0}, {1}) |
761 del connect_wifi |
767 del connect_wifi |
762 """.format( |
768 """.format( |
763 repr(ssid), |
769 repr(ssid), |
764 repr(password if password else ""), |
770 repr(password if password else ""), |
765 ) |
771 ) |
766 |
772 |
767 with EricOverrideCursor(): |
773 with EricOverrideCursor(): |
768 out, err = self._interface.execute(command, timeout=15000) |
774 out, err = self._interface.execute(command, timeout=15000) |
769 if err: |
775 if err: |
770 return False, err |
776 return False, err |
829 nvs.set_blob('password', password) |
835 nvs.set_blob('password', password) |
830 nvs.commit() |
836 nvs.commit() |
831 |
837 |
832 save_wifi_creds({0}, {1}) |
838 save_wifi_creds({0}, {1}) |
833 del save_wifi_creds |
839 del save_wifi_creds |
834 """.format(repr(ssid), repr(password) if password else "''") |
840 """.format( |
|
841 repr(ssid), repr(password) if password else "''" |
|
842 ) |
835 bootCommand = """ |
843 bootCommand = """ |
836 def modify_boot(): |
844 def modify_boot(): |
837 add = True |
845 add = True |
838 try: |
846 try: |
839 with open('/boot.py', 'r') as f: |
847 with open('/boot.py', 'r') as f: |
1056 ap.config(essid=ssid, authmode=authmode, password=password) |
1064 ap.config(essid=ssid, authmode=authmode, password=password) |
1057 |
1065 |
1058 start_ap({0}, {1}, {2}, {3}) |
1066 start_ap({0}, {1}, {2}, {3}) |
1059 del start_ap |
1067 del start_ap |
1060 """.format( |
1068 """.format( |
1061 repr(ssid), security, repr(password), ifconfig |
1069 repr(ssid), security, repr(password), ifconfig |
1062 ) |
1070 ) |
1063 |
1071 |
1064 out, err = self._interface.execute(command, timeout=15000) |
1072 out, err = self._interface.execute(command, timeout=15000) |
1065 if err: |
1073 if err: |
1066 return False, err |
1074 return False, err |
1067 else: |
1075 else: |