connect to Micropython network if no other network is found

This commit is contained in:
Simon Pirkelmann 2019-08-13 13:20:57 +02:00
parent 4947bd4539
commit 4cc44b3cd6

View File

@ -10,14 +10,13 @@ print("networks found: {}".format(networks))
ap_found = False
# TODO: edit these lines
network_name = 'M5Wifi'
password = '<PW>'
#desired_ip = '192.168.1.101'
#desired_ip = '192.168.1.102'
desired_ip = '192.168.1.103'
network_name = '<SSID>' # existing wifi network to connect to (leave empty if unused)
password = '<PW>' # password for the network
desired_ip = '192.168.1.101' # the robot will be reachable by this IP in the network
subnet = '255.255.255.0'
gateway = '192.168.1.1'
dns = '192.168.1.1'
# TODO end edit
for n in networks:
if network_name == n[0].decode():
@ -32,10 +31,23 @@ for n in networks:
ap_found = True
ap_if.active(False)
print("disabling access point interface")
elif 'MicroPython' in n[0].decode(): # alternatively, connect to MicroPython network
print("existing network found: {}".format(n[0]))
print("connecting to the network ...")
# set static ip
sta_if.ifconfig((desired_ip, subnet, gateway, dns))
sta_if.connect(n[0].decode(), 'micropythoN')
my_ip = sta_if.ifconfig()
print("my_ip[0] = {}".format(my_ip[0]))
if my_ip[0] == desired_ip:
ap_found = True
ap_if.active(False)
print("disabling access point interface")
if not ap_found:
print("could not connect to network, becoming an access point instead")
sta_if.active(False)
ap_if.active(True)
ap_if.ifconfig((desired_ip, subnet, gateway, dns))
my_ip = ap_if.ifconfig()
print("disabling station interface")
print("my_ip = {}".format(my_ip))