From 4d7c0e7be70a7f7d70e3e2ebe12d20309a0cc4b5 Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Wed, 5 Jun 2019 08:03:31 +0200 Subject: [PATCH] robot now tries to automatically connect to an existing wifi network. if the network is not found then the robot will become an access point instead --- micropython_firmware/boot.py | 40 +++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/micropython_firmware/boot.py b/micropython_firmware/boot.py index dc21b03..60d4a58 100644 --- a/micropython_firmware/boot.py +++ b/micropython_firmware/boot.py @@ -3,11 +3,41 @@ import time # connect to local wifi network sta_if = network.WLAN(network.STA_IF) -sta_if.active(False) -#sta_if.active(True) -# TODO: edit this line -#sta_if.connect("", "") -#sta_if.ifconfig() +ap_if = network.WLAN(network.AP_IF) +sta_if.active(True) +networks = sta_if.scan() +print("networks found: {}".format(networks)) +ap_found = False + +# TODO: edit these lines +network_name = 'M5Wifi' +password = '' +#desired_ip = '192.168.1.101' +desired_ip = '192.168.1.102' +subnet = '255.255.255.0' +gateway = '192.168.1.1' +dns = '192.168.1.1' + +for n in networks: + if network_name == n[0].decode(): + 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(network_name, password) + 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) + my_ip = ap_if.ifconfig() + print("disabling station interface") +print("my_ip = {}".format(my_ip)) # start terminal over wifi time.sleep(5) # wait for wifi to connect