src/eric7/MicroPython/Devices/EspDevices.py

branch
mpy_network
changeset 9797
3be7b2326e2c
parent 9795
11b4d39d7584
child 9798
4402d76c5fa9
equal deleted inserted replaced
9795:11b4d39d7584 9797:3be7b2326e2c
683 'status': wifi.status(), 683 'status': wifi.status(),
684 'ifconfig': wifi.ifconfig(), 684 'ifconfig': wifi.ifconfig(),
685 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 685 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
686 } 686 }
687 if wifi.active(): 687 if wifi.active():
688 station['txpower'] = wifi.config('txpower') 688 try:
689 else: 689 station['txpower'] = wifi.config('txpower')
690 station['txpower'] = 0 690 except ValueError:
691 pass
691 print(ujson.dumps(station)) 692 print(ujson.dumps(station))
692 693
693 wifi = network.WLAN(network.AP_IF) 694 wifi = network.WLAN(network.AP_IF)
694 ap = { 695 ap = {
695 'active': wifi.active(), 696 'active': wifi.active(),
699 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), 700 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
700 'channel': wifi.config('channel'), 701 'channel': wifi.config('channel'),
701 'essid': wifi.config('essid'), 702 'essid': wifi.config('essid'),
702 } 703 }
703 if wifi.active(): 704 if wifi.active():
704 ap['txpower'] = wifi.config('txpower') 705 try:
705 else: 706 ap['txpower'] = wifi.config('txpower')
706 ap['txpower'] = 0 707 except ValueError:
708 pass
707 print(ujson.dumps(ap)) 709 print(ujson.dumps(ap))
708 710
709 wifi_status() 711 wifi_status()
710 del wifi_status 712 del wifi_status
711 """ 713 """
1015 if err: 1017 if err:
1016 return False, err 1018 return False, err
1017 else: 1019 else:
1018 return out.decode("utf-8").strip() == "True", "" 1020 return out.decode("utf-8").strip() == "True", ""
1019 1021
1020 def startAccessPoint(self, ssid, security=None, password=None): 1022 def startAccessPoint(self, ssid, security=None, password=None, ifconfig=None):
1021 """ 1023 """
1022 Public method to start the access point interface. 1024 Public method to start the access point interface.
1023 1025
1024 @param ssid SSID of the access point 1026 @param ssid SSID of the access point
1025 @type str 1027 @type str
1026 @param security security method (defaults to None) 1028 @param security security method (defaults to None)
1027 @type int (optional) 1029 @type int (optional)
1028 @param password password (defaults to None) 1030 @param password password (defaults to None)
1029 @type str (optional) 1031 @type str (optional)
1032 @param ifconfig IPv4 configuration for the access point if not default
1033 (IPv4 address, netmask, gateway address, DNS server address)
1034 @type tuple of (str, str, str, str)
1030 @return tuple containing a flag indicating success and an error message 1035 @return tuple containing a flag indicating success and an error message
1031 @rtype tuple of (bool, str) 1036 @rtype tuple of (bool, str)
1032 """ 1037 """
1033 if security is None or password is None: 1038 if security is None or password is None:
1034 security = 0 1039 security = 0
1035 password = "" 1040 password = ""
1036 if security > 4: 1041 if security > 4:
1037 security = 4 # security >4 cause an error thrown by the ESP32 1042 security = 4 # security >4 cause an error thrown by the ESP32
1038 1043
1039 command = """ 1044 command = """
1040 def start_ap(): 1045 def start_ap(ssid, authmode, password, ifconfig):
1041 import network 1046 import network
1042 1047
1043 ap = network.WLAN(network.AP_IF) 1048 ap = network.WLAN(network.AP_IF)
1044 ap.active(False) 1049 ap.active(False)
1050 if ifconfig:
1051 ap.ifconfig(ifconfig)
1045 ap.active(True) 1052 ap.active(True)
1046 try: 1053 try:
1047 ap.config(ssid={0}, authmode={1}, password={2}) 1054 ap.config(ssid=ssid, authmode=authmode, password=password)
1048 except: 1055 except:
1049 ap.config(essid={0}, authmode={1}, password={2}) 1056 ap.config(essid=ssid, authmode=authmode, password=password)
1050 1057
1051 start_ap() 1058 start_ap({0}, {1}, {2}, {3})
1052 del start_ap 1059 del start_ap
1053 """.format( 1060 """.format(
1054 repr(ssid), security, repr(password) 1061 repr(ssid), security, repr(password), ifconfig
1055 ) 1062 )
1056 1063
1057 out, err = self._interface.execute(command, timeout=15000) 1064 out, err = self._interface.execute(command, timeout=15000)
1058 if err: 1065 if err:
1059 return False, err 1066 return False, err

eric ide

mercurial