src/eric7/MicroPython/Devices/RP2Devices.py

branch
eric7
changeset 11236
75c26fe1d1c7
parent 11208
f776db7cc222
child 11237
c1c31b861d54
equal deleted inserted replaced
11235:b984f0085bed 11236:75c26fe1d1c7
96 7: self.tr("open", "open WiFi network"), 96 7: self.tr("open", "open WiFi network"),
97 8: self.tr("automatic"), 97 8: self.tr("automatic"),
98 }, 98 },
99 } 99 }
100 100
101 self.__securityMapping = {
102 "SEC_DPP": "DPP",
103 "SEC_OPEN": self.tr("Open"),
104 "SEC_OWE": "OWE",
105 "SEC_WAPI": "WAPI",
106 "SEC_WEP": "WEP",
107 "SEC_WPA": "WPA",
108 "SEC_WPA_WPA2": "WPA/WPA2",
109 "SEC_WPA2": "WPA2",
110 "SEC_WPA2_ENT": "WPA2 Enterprise",
111 "SEC_WPA2_WPA3": "WPA2/WPA3",
112 "SEC_WPA2_WPA3_ENT": "WPA2/WPA3 Enterprise",
113 "SEC_WPA3": "WPA3",
114 "SEC_WPA3_ENT": "WPA3 Enterprise",
115 "SEC_WPA3_ENT_192": "WPA3 Enterprise (192-bit)",
116 "SEC_WPA3_EXT_PSK": "WPA3 Extended",
117 "SEC_WPA3_EXT_PSK_MIXED_MODE": "WPA3 Extended, Mixed Mode",
118 }
119
101 def setButtons(self): 120 def setButtons(self):
102 """ 121 """
103 Public method to enable the supported action buttons. 122 Public method to enable the supported action buttons.
104 """ 123 """
105 super().setButtons() 124 super().setButtons()
558 import ubinascii 577 import ubinascii
559 import ujson 578 import ujson
560 import network 579 import network
561 import rp2 580 import rp2
562 581
582 def security_str(mode):
583 for sm in [e for e in dir(network.WLAN) if e.startswith('SEC_')]:
584 if getattr(network.WLAN, sm, 0) == mode:
585 return sm
586 return ""
587
563 wifi = network.WLAN(network.STA_IF) 588 wifi = network.WLAN(network.STA_IF)
564 station = { 589 station = {
565 'active': wifi.active(), 590 'active': wifi.active(),
566 'connected': wifi.isconnected(), 591 'connected': wifi.isconnected(),
567 'status': wifi.status(), 592 'status': wifi.status(),
580 'ifconfig': wifi.ifconfig(), 605 'ifconfig': wifi.ifconfig(),
581 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 606 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
582 'channel': wifi.config('channel'), 607 'channel': wifi.config('channel'),
583 'txpower': wifi.config('txpower'), 608 'txpower': wifi.config('txpower'),
584 'essid': wifi.config('essid'), 609 'essid': wifi.config('essid'),
610 'ap_security': security_str(wifi.config('security')),
585 } 611 }
586 print(ujson.dumps(ap)) 612 print(ujson.dumps(ap))
587 613
588 overall = { 614 overall = {
589 'active': station['active'] or ap['active'] 615 'active': station['active'] or ap['active']
696 ][station["ap_security"]] 722 ][station["ap_security"]]
697 except KeyError: 723 except KeyError:
698 station["ap_security"] = self.tr("unknown ({0})").format( 724 station["ap_security"] = self.tr("unknown ({0})").format(
699 station["ap_security"] 725 station["ap_security"]
700 ) 726 )
727 if "ap_security" in ap and self._deviceData["wifi_type"] == "picow":
728 try:
729 ap["ap_security"] = self.__securityMapping[ap["ap_security"]]
730 except KeyError:
731 ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"])
732
701 return station, ap, overall 733 return station, ap, overall
702 734
703 def connectWifi(self, ssid, password, hostname): 735 def connectWifi(self, ssid, password, hostname):
704 """ 736 """
705 Public method to connect a device to a WiFi network. 737 Public method to connect a device to a WiFi network.
1217 """ 1249 """
1218 Public method to start the access point interface. 1250 Public method to start the access point interface.
1219 1251
1220 @param ssid SSID of the access point 1252 @param ssid SSID of the access point
1221 @type str 1253 @type str
1222 @param security security method (defaults to None) 1254 @param security security mode (defaults to None) (unused)
1223 @type int (optional) 1255 @type str (optional)
1224 @param password password (defaults to None) 1256 @param password password (defaults to None)
1225 @type str (optional) 1257 @type str (optional)
1226 @param hostname host name of the device (defaults to None) 1258 @param hostname host name of the device (defaults to None)
1227 @type str (optional) 1259 @type str (optional)
1228 @param ifconfig IPv4 configuration for the access point if not default 1260 @param ifconfig IPv4 configuration for the access point if not default
1230 @type tuple of (str, str, str, str) 1262 @type tuple of (str, str, str, str)
1231 @return tuple containing a flag indicating success and an error message 1263 @return tuple containing a flag indicating success and an error message
1232 @rtype tuple of (bool, str) 1264 @rtype tuple of (bool, str)
1233 """ 1265 """
1234 if security is None or password is None: 1266 if security is None or password is None:
1235 security = 0 1267 security = "SEC_OPEN"
1236 password = "" # secok 1268 password = "" # secok
1237 1269
1238 if self._deviceData["wifi_type"] == "picow": 1270 if self._deviceData["wifi_type"] == "picow":
1239 country = Preferences.getMicroPython("WifiCountry").upper() 1271 country = Preferences.getMicroPython("WifiCountry").upper()
1240 if security:
1241 security = 4 # Pico W supports just WPA/WPA2
1242 command = """ 1272 command = """
1243 def start_ap(ssid, security, password, hostname, ifconfig, country): 1273 def start_ap(ssid, password, hostname, ifconfig, country):
1244 import network 1274 import network
1245 import rp2 1275 import rp2
1246 from time import sleep 1276 from time import sleep
1247 1277
1248 rp2.country(country) 1278 rp2.country(country)
1255 1285
1256 ap = network.WLAN(network.AP_IF) 1286 ap = network.WLAN(network.AP_IF)
1257 ap.active(True) 1287 ap.active(True)
1258 if ifconfig: 1288 if ifconfig:
1259 ap.ifconfig(ifconfig) 1289 ap.ifconfig(ifconfig)
1260 ap.config(ssid=ssid, security=security, password=password) 1290 ap.config(ssid=ssid, security=network.WLAN.{5}, password=password)
1261 sleep(0.1) 1291 sleep(0.1)
1262 print(ap.status() == network.STAT_GOT_IP) 1292 print(ap.status() == network.STAT_GOT_IP)
1263 1293
1264 start_ap({0}, {1}, {2}, {3}, {4}, {5}) 1294 start_ap({0}, {1}, {2}, {3}, {4})
1265 del start_ap 1295 del start_ap
1266 """.format( 1296 """.format(
1267 repr(ssid), 1297 repr(ssid),
1268 security,
1269 repr(password), 1298 repr(password),
1270 repr(hostname), 1299 repr(hostname),
1271 ifconfig, 1300 ifconfig,
1272 repr(country if country else "XX"), 1301 repr(country if country else "XX"),
1302 security,
1273 ) 1303 )
1274 elif self._deviceData["wifi_type"] == "picowireless": 1304 elif self._deviceData["wifi_type"] == "picowireless":
1275 if ifconfig: 1305 if ifconfig:
1276 return ( 1306 return (
1277 False, 1307 False,

eric ide

mercurial