src/eric7/MicroPython/Devices/EspDevices.py

branch
eric7
changeset 11236
75c26fe1d1c7
parent 11167
a3f5af773bc7
equal deleted inserted replaced
11235:b984f0085bed 11236:75c26fe1d1c7
73 3: "WPA2", 73 3: "WPA2",
74 4: "WPA/WPA2", 74 4: "WPA/WPA2",
75 5: "WPA2 (CCMP)", 75 5: "WPA2 (CCMP)",
76 6: "WPA3", 76 6: "WPA3",
77 7: "WPA2/WPA3", 77 7: "WPA2/WPA3",
78 }
79 self.__securityMapping = {
80 "SEC_DPP": "DPP",
81 "SEC_OPEN": self.tr("Open"),
82 "SEC_OWE": "OWE",
83 "SEC_WAPI": "WAPI",
84 "SEC_WEP": "WEP",
85 "SEC_WPA": "WPA",
86 "SEC_WPA_WPA2": "WPA/WPA2",
87 "SEC_WPA2": "WPA2",
88 "SEC_WPA2_ENT": "WPA2 Enterprise",
89 "SEC_WPA2_WPA3": "WPA2/WPA3",
90 "SEC_WPA2_WPA3_ENT": "WPA2/WPA3 Enterprise",
91 "SEC_WPA3": "WPA3",
92 "SEC_WPA3_ENT": "WPA3 Enterprise",
93 "SEC_WPA3_ENT_192": "WPA3 Enterprise (192-bit)",
94 "SEC_WPA3_EXT_PSK": "WPA3 Extended",
95 "SEC_WPA3_EXT_PSK_MIXED_MODE": "WPA3 Extended, Mixed Mode",
78 } 96 }
79 97
80 def __createCpyDevice(self): 98 def __createCpyDevice(self):
81 """ 99 """
82 Private method to create a CircuitPython device interface. 100 Private method to create a CircuitPython device interface.
716 def wifi_status(): 734 def wifi_status():
717 import ubinascii 735 import ubinascii
718 import ujson 736 import ujson
719 import network 737 import network
720 738
739 def security_str(mode):
740 for sm in [e for e in dir(network.WLAN) if e.startswith('SEC_')]:
741 if getattr(network.WLAN, sm, 0) == mode:
742 return sm
743 return ""
744
721 wifi = network.WLAN(network.STA_IF) 745 wifi = network.WLAN(network.STA_IF)
722 station = { 746 station = {
723 'active': wifi.active(), 747 'active': wifi.active(),
724 'connected': wifi.isconnected(), 748 'connected': wifi.isconnected(),
725 'status': wifi.status(), 749 'status': wifi.status(),
740 'status': wifi.status(), 764 'status': wifi.status(),
741 'ifconfig': wifi.ifconfig(), 765 'ifconfig': wifi.ifconfig(),
742 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 766 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
743 'channel': wifi.config('channel'), 767 'channel': wifi.config('channel'),
744 'essid': wifi.config('essid'), 768 'essid': wifi.config('essid'),
769 'ap_security': security_str(wifi.config('security')),
745 } 770 }
746 if wifi.active(): 771 if wifi.active():
747 try: 772 try:
748 ap['txpower'] = wifi.config('txpower') 773 ap['txpower'] = wifi.config('txpower')
749 except ValueError: 774 except ValueError:
781 station["status"] = str(station["status"]) 806 station["status"] = str(station["status"])
782 try: 807 try:
783 ap["status"] = self.__statusTranslations[ap["status"]] 808 ap["status"] = self.__statusTranslations[ap["status"]]
784 except KeyError: 809 except KeyError:
785 ap["status"] = str(ap["status"]) 810 ap["status"] = str(ap["status"])
811 if "ap_security" in ap:
812 try:
813 ap["ap_security"] = self.__securityMapping[ap["ap_security"]]
814 except KeyError:
815 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"])
786 return station, ap, overall 816 return station, ap, overall
787 817
788 def connectWifi(self, ssid, password, hostname): 818 def connectWifi(self, ssid, password, hostname):
789 """ 819 """
790 Public method to connect a device to a WiFi network. 820 Public method to connect a device to a WiFi network.
1186 """ 1216 """
1187 Public method to start the access point interface. 1217 Public method to start the access point interface.
1188 1218
1189 @param ssid SSID of the access point 1219 @param ssid SSID of the access point
1190 @type str 1220 @type str
1191 @param security security method (defaults to None) 1221 @param security security mode (defaults to None) (unused)
1192 @type int (optional) 1222 @type str (optional)
1193 @param password password (defaults to None) 1223 @param password password (defaults to None)
1194 @type str (optional) 1224 @type str (optional)
1195 @param hostname host name of the device (defaults to None) 1225 @param hostname host name of the device (defaults to None)
1196 @type str (optional) 1226 @type str (optional)
1197 @param ifconfig IPv4 configuration for the access point if not default 1227 @param ifconfig IPv4 configuration for the access point if not default
1208 hostname=hostname, 1238 hostname=hostname,
1209 ifconfig=ifconfig, 1239 ifconfig=ifconfig,
1210 ) 1240 )
1211 1241
1212 if security is None or password is None: 1242 if security is None or password is None:
1213 security = 0 1243 security = "SEC_OPEN"
1214 password = "" # secok 1244 password = "" # secok
1215 if security > 4:
1216 security = 4 # security >4 cause an error thrown by the ESP32
1217 1245
1218 command = """ 1246 command = """
1219 def start_ap(ssid, authmode, password, hostname, ifconfig): 1247 def start_ap(ssid, password, hostname, ifconfig):
1220 import network 1248 import network
1221 1249
1222 if hostname: 1250 if hostname:
1223 try: 1251 try:
1224 network.hostname(hostname) 1252 network.hostname(hostname)
1229 ap.active(False) 1257 ap.active(False)
1230 if ifconfig: 1258 if ifconfig:
1231 ap.ifconfig(ifconfig) 1259 ap.ifconfig(ifconfig)
1232 ap.active(True) 1260 ap.active(True)
1233 try: 1261 try:
1234 ap.config(ssid=ssid, authmode=authmode, password=password) 1262 ap.config(ssid=ssid, authmode=network.WLAN.{4}, password=password)
1235 except: 1263 except:
1236 ap.config(essid=ssid, authmode=authmode, password=password) 1264 ap.config(essid=ssid, authmode=network.WLAN.{4}, password=password)
1237 1265
1238 start_ap({0}, {1}, {2}, {3}, {4}) 1266 start_ap({0}, {1}, {2}, {3})
1239 del start_ap 1267 del start_ap
1240 """.format( 1268 """.format(
1241 repr(ssid), security, repr(password), repr(hostname), ifconfig 1269 repr(ssid), repr(password), repr(hostname), ifconfig, security
1242 ) 1270 )
1243 1271
1244 out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) 1272 out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000)
1245 if err: 1273 if err:
1246 return False, err 1274 return False, err

eric ide

mercurial