Skip to content

Commit

Permalink
deinit the network device to clear vestigial configs
Browse files Browse the repository at this point in the history
  • Loading branch information
n1kdo committed Dec 9, 2023
1 parent 128f937 commit 10c6118
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/kpa500-remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def value(self):
machine = Machine()

onboard = machine.Pin('LED', machine.Pin.OUT, value=0)
onboard.on()
morse_led = machine.Pin(2, machine.Pin.OUT, value=0) # status LED
reset_button = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)

Expand Down Expand Up @@ -183,8 +182,12 @@ def connect_to_network(config):
if access_point_mode:
print('Starting setup WLAN...')
wlan = network.WLAN(network.AP_IF)
wlan.active(False)
wlan.config(pm=0xa11140) # disable power save, this is a server.
wlan.deinit()
wlan.config(pm=wlan.PM_NONE) # disable power save, this is a server.
# wlan.deinit turns off the onboard LED because it is connected to the CYW43
# turn it on again.
onboard = machine.Pin('LED', machine.Pin.OUT, value=0)
onboard.on()

hostname = config.get('hostname')
if hostname is not None:
Expand Down Expand Up @@ -214,8 +217,33 @@ def connect_to_network(config):
else:
print('Connecting to WLAN...')
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
wlan.config(pm=0xa11140) # disable power save, this is a server.
wlan.deinit()
# wlan.deinit turns off the onboard LED because it is connected to the CYW43
# turn it on again.
onboard = machine.Pin('LED', machine.Pin.OUT, value=0)
onboard.on()

wlan.active(True)
wlan.config(pm=wlan.PM_NONE) # disable power save, this is a server.

is_dhcp = config.get('dhcp')
if is_dhcp is None:
is_dhcp = True
if not is_dhcp:
ip_address = config.get('ip_address')
netmask = config.get('netmask')
gateway = config.get('gateway')
dns_server = config.get('dns_server')
if ip_address is not None and netmask is not None and gateway is not None and dns_server is not None:
print('Configuring network with static IP')
wlan.ifconfig((ip_address, netmask, gateway, dns_server))
else:
print('Cannot use static IP, data is missing.')
print('Configuring network with DHCP....')
#wlan.ifconfig('dhcp')
else:
print('Configuring network with DHCP...')
#wlan.ifconfig('dhcp')

hostname = config.get('hostname')
if hostname is not None:
Expand All @@ -225,7 +253,8 @@ def connect_to_network(config):
except ValueError:
print('Failed to set hostname.')

wlan.active(True)
# print(f'ifconfig={wlan.ifconfig()}')

max_wait = 10
wl_status = wlan.status()
print(f' ifconfig={wlan.ifconfig()}')
Expand All @@ -243,28 +272,9 @@ def connect_to_network(config):
print('Network did not connect!')
morse_code_sender.set_message('ERR')
return None, None

print(f' connected, ifconfig={wlan.ifconfig()}')

is_dhcp = config.get('dhcp')
if is_dhcp is None:
is_dhcp = True
if not is_dhcp:
ip_address = config.get('ip_address')
netmask = config.get('netmask')
gateway = config.get('gateway')
dns_server = config.get('dns_server')
if ip_address is not None and netmask is not None and gateway is not None and dns_server is not None:
print('Configuring network with static IP')
wlan.ifconfig((ip_address, netmask, gateway, dns_server))
else:
print('Cannot use static IP, data is missing.')
print('Configuring network with DHCP....')
wlan.ifconfig('dhcp')
else:
print('Configuring network with DHCP...')
wlan.ifconfig('dhcp')

onboard.on() # turn on the LED, WAN is up.
wl_config = wlan.ifconfig()
ip_address = wl_config[0]
netmask = wl_config[1]
Expand Down

0 comments on commit 10c6118

Please sign in to comment.