MicroPython-ESP32之更合理的建立wifi连接-1Z实验室

96
1Z实验室How
2018.08.17 15:14* 字数 1374

出品:1Z实验室 1zlab.com
1ZLAB: Make Things Easy

导引

作为一款支持wifi的物联网芯片,ESP32的联网方式自然是要重点掌握的.在MicroPython下,联网更是一件轻松Easy的事情, 在MicroPython tutorial for ESP8266中,官方给出了MicroPython的WiFi连接示例代码.但是在实际的应用场景中,很多基于物联网的工程应用,第一要义就是建立网络连接.按照这份示例代码,WIFI确实可以几步就连接妥当.但WIFI连接有哪些坑需要注意,如何编写更合理的WIFI连接代码呢?

注: 如果你想直接获取最终解决方案的代码,请直接跳转至文章尾部.

初试网络连接

我们先来看看官方给的示例代码(部分变量名笔者有所改动):

def do_connect():
    import network #WIFI连接需要引入network包
    wifi = network.WLAN(network.STA_IF)  #创建连接对象 如果让ESP32接入WIFI的话使用STA_IF模式,若以ESP32为热点,则使用AP模式
    if not wifi.isconnected(): #判断WIFI连接状态
        print('connecting to network...')
        wifi.active(True) #激活WIFI
        wifi.connect('<essid>', '<password>') #essid为WIFI名称,password为WIFI密码
        while not wifi.isconnected():
            pass # WIFI没有连接上的话做点什么,这里默认pass啥也不做
    print('network config:', wifi.ifconfig())

我们对其稍作改动,将WIFI名字和密码改为do_connect的参数

def do_connect(essid, password):
    import network 
    wifi = network.WLAN(network.STA_IF)  
    if not wifi.isconnected(): 
        print('connecting to network...')
        wifi.active(True) 
        wifi.connect(essid, password) 
        while not wifi.isconnected():
            pass 
    print('network config:', wifi.ifconfig())

Ctrl+E开启MicroPython ESP32的粘贴模式,将以上代码copy进去:

paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== def do_connect(essid, password):
===     import network 
===     wifi = network.WLAN(network.STA_IF)  
===     if not wifi.isconnected(): 
===         print('connecting to network...')
===         wifi.active(True) 
===         wifi.connect(essid, password) 
===         while not wifi.isconnected():
===             pass 
===     print('network config:', wifi.ifconfig())
>>> 

调用do_connect函数:

>>> do_connect('ChinaNet-Q5uk','0921608677')
I (88874) wifi: wifi driver task: 3ffcb8b8, prio:23, stack:4096, core=0
I (88874) wifi: wifi firmware version: ac8d7b4
I (88874) wifi: config NVS flash: enabled
I (88874) wifi: config nano formating: disabled
I (88884) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (88894) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (88904) wifi: Init dynamic tx buffer num: 32
I (88904) wifi: Init data frame dynamic rx buffer num: 64
I (88914) wifi: Init management frame dynamic rx buffer num: 64
I (88914) wifi: Init static rx buffer size: 1600
I (88924) wifi: Init static rx buffer num: 10
I (88924) wifi: Init dynamic rx buffer num: 0
connecting to network...
I (88984) phy: phy_version: 3910, c0c45a3, May 21 2018, 18:07:06, 0, 0
I (88994) wifi: mode : sta (30:ae:a4:84:22:64)
I (88994) wifi: STA_START
I (89234) wifi: n:2 0, o:1 0, ap:255 255, sta:2 0, prof:1
I (89804) wifi: state: init -> auth (b0)
I (89814) wifi: state: auth -> assoc (0)
I (89824) wifi: state: assoc -> run (10)
I (90124) wifi: connected with ChinaNet-Q5uk, channel 2
I (90124) wifi: pm start, type: 1

I (90134) network: CONNECTED
I (91414) event: sta ip: 192.168.2.189, mask: 255.255.255.0, gw: 192.168.2.1
I (91414) network: GOT_IP
network config: ('192.168.2.189', '255.255.255.0', '192.168.2.1', '192.168.2.1')
>>> 

打印一大堆日志,然后成功连接到WIFI.

听说你想开机自动连接WIFI?

大部分读者的内心独白:
大佬: 这还用你教,我写在main.py里不就好了吗.
小白: 那每次都要开机自动输入一遍WIFI名字和密码,岂不是很麻烦?
大佬: 写个配置文件,专门放置WIFI密码和名字不就好了吗?

现在我们按照大佬的思路来安排一波:
首先我们创建 main.py,并写入do_connect函数的代码,我们将以读取配置文件的方式来读取WIFI的名称和密码,因此代码做如下改动:

def do_connect():
    import json
    import network
    # 尝试读取配置文件wifi_confi.json,这里我们以json的方式来存储WIFI配置
    # wifi_config.json在根目录下
    
    # 若不是初次运行,则将文件中的内容读取并加载到字典变量 config
    try:
        with open('wifi_config.json','r') as f:
            config = json.loads(f.read())
    # 若初次运行,则将进入excpet,执行配置文件的创建        
    except:
        essid = input('wifi name:') # 输入essid
        password = input('wifi passwrod:') # 输入password
        config = dict(essid=essid, password=password) # 创建字典
        with open('wifi_config.json','w') as f:
            f.write(json.dumps(config)) # 将字典序列化为json字符串,存入wifi_config.json
            
    #以下为正常的WIFI连接流程        
    wifi = network.WLAN(network.STA_IF)  
    if not wifi.isconnected(): 
        print('connecting to network...')
        wifi.active(True) 
        wifi.connect(config['essid'], config['password']) 
        while not wifi.isconnected():
            pass 
    print('network config:', wifi.ifconfig()) 

if __name__ == '__main__':
    do_connect()

然后就可以愉快的重启了,试试咱们的开机自动连接WIFI的代码

wifi name:ChinaNet-Q5uk
wifi passwrod:0921608677
I (69669) wifi: wifi driver task: 3ffc6678, prio:23, stack:4096, core=0
I (69669) wifi: wifi firmware version: ac8d7b4
I (69669) wifi: config NVS flash: enabled
I (69669) wifi: config nano formating: disabled
...此处省略若干行日志

I (71139) network: CONNECTED
I (72089) event: sta ip: 192.168.2.189, mask: 255.255.255.0, gw: 192.168.2.1
I (72089) network: GOT_IP
network config: ('192.168.2.189', '255.255.255.0', '192.168.2.1', '192.168.2.1')
MicroPython v1.9.4-429-ge755bd493 on 2018-08-03; ESP32 module with ESP32
Type "help()" for more information.
>>>

再看看我们的wifi_config.json是否已创建完毕:

>>> import os
>>> os.listdir()
['boot.py', 'main.py', 'wifi_config.json']
>>> 

再重启试试吧

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
...此处省略若干行日志

I (1882) network: CONNECTED
I (4842) event: sta ip: 192.168.2.189, mask: 255.255.255.0, gw: 192.168.2.1
I (4842) network: GOT_IP
network config: ('192.168.2.189', '255.255.255.0', '192.168.2.1', '192.168.2.1')
MicroPython v1.9.4-429-ge755bd493 on 2018-08-03; ESP32 module with ESP32
Type "help()" for more information.
>>> 

问题:WIFI环境发生改变后,WIFI连接陷入了死循环

小白: 大佬真棒,上面的代码没毛病,但是如果WIFI环境一变,好像就陷入了死循环,比如接下来的这波操作:
我们把 wifi_config.json文件删掉,然后重启,输入错误的wifi名称和密码,以此来模拟WIFI环境的改变:

>>> import os
>>> os.remove('wifi_config.json')

然后重启,并输入错误的essid和password:

connecting to network...
I (26468) phy: phy_version: 3910, c0c45a3, May 21 2018, 18:07:06, 0, 0
I (26468) wifi: mode : sta (30:ae:a4:84:22:64)
I (26468) wifi: STA_START
I (28878) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (31288) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (33708) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (36118) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (38528) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (40938) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (43348) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (45758) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (48168) wifi: STA_DISCONNECTED, reason:201
no AP found                                                                                                                                  
I (50578) wifi: STA_DISCONNECTED, reason:201
no AP found
I (52998) wifi: STA_DISCONNECTED, reason:201
no AP found
I (55408) wifi: STA_DISCONNECTED, reason:201
no AP found
Traceback (most recent call last):
  File "main.py", line 31, in <module>
  File "main.py", line 26, in do_connect
KeyboardInterrupt: 
                     Ctrl+C也没用
MicroPython v1.9.4-429-ge755bd493 on 2018-08-03; ESP32 module with ESP32
Type "help()" for more information.
>>> I (57818) wifi: STA_DISCONNECTED, reason:201
no AP found
I (60228) wifi: STA_DISCONNECTED, reason:201
no AP found
I (62638) wifi: STA_DISCONNECTED, reason:201
no AP found
I (65048) wifi: STA_DISCONNECTED, reason:201
no AP found
                   .....没有尽头的刷屏
>>> I (168678) wifi: STA_DISCONNECTED, reason:201
no AP found
                     repl已经被刷屏了
>>> I (171088) wifi: STA_DISCONNECTED, reason:201
no AP found
I (173498) wifi: STA_DISCONNECTED, reason:201
no AP found
I (175908) wifi: STA_DISCONNECTED, reason:201
no AP found

TIPS:当你陷入这种repl的输出死循环的时候,不要慌张,解决方案还是有的,请先冷静以下,默默的一字一句输入import os回车os.remove('main.py')回车,在此过程中请无视repl的输出,只需保证自己的输入无误就OK, 以此删除你的main.py文件,当然你也可以去对症下药,但是删掉main.py重启是最简单的方式...

问题已经暴露的一览无余了,更合理的WIFI连接姿势我们立马给出.

更合理的建立wifi连接

先让我们回顾之前的代码:

wifi = network.WLAN(network.STA_IF)  
    if not wifi.isconnected(): 
        print('connecting to network...')
        wifi.active(True) 
        wifi.connect(config['essid'], config['password']) 
        while not wifi.isconnected():
            pass 
    print('network config:', wifi.ifconfig())

在 while循环中,,执行循环的条件是wifi没有连接成功.我们看到目前的做法是pass,什么也没干.因此我们在此做点文章应该就OK了.
可能大部分人和我一开始的思路一样,删掉wifi_config.json,重新调用do_connect函数进行wifi的初始化配置.但这样的话,可能你永远也无法走出这个循环,因为经过我多次踩坑之后发现:

connect()函数执行完毕之后并不会等待WIFI连接成功之后才继续执行,而是直接继续往下执行.而且connect()本身不会返回任何数据.

所以接下来的探讨分为两种情况:

  • 1 essid 和 password 正确
    在WIFI没有真正的建立起连接的时候,wifi.is_connected()一直都返回False,while循环中的代码会被循环执行.当wifi连接成功建立之后,wifi.is_connected()返回True,跳出while循环,WIFI连接宣告完毕

  • 2 essid 和 password 不正确
    wifi.is_connected()一直都返回False,while循环中的代码会被循环执行.并且伴随着repl的一顿刷屏输出.

为了解决WIFI连接过程需要耗费一定的时间的这个问题,我们需要加上一段延时,在一定延时之后我们再进行is_connected()判断,此时若is_connected()==False,则视为连接失败,便接着执行我们应对WIFI环境改变或其他原因导致的essid与password认证失败的代码.
为了阻止repl由于wifi连接失败产生的刷屏,我们需要将wifi关闭,调用wifi.active(False),然后我们删除wifi_config.json,再递归调用do_connect()

更合理的wifi连接代码如下:
import sys

# 添加路径
sys.path.append('examples')

def is_legal_wifi(essid, password):
    '''
    判断WIFI密码是否合法
    '''
    if len(essid) == 0 or len(password) == 0:
        return False
    return True

def do_connect():
    import json
    import network
    # 尝试读取配置文件wifi_confi.json,这里我们以json的方式来存储WIFI配置
    # wifi_config.json在根目录下
    
    # 若不是初次运行,则将文件中的内容读取并加载到字典变量 config
    try:
        with open('wifi_config.json','r') as f:
            config = json.loads(f.read())
    # 若初次运行,则将进入excpet,执行配置文件的创建        
    except:
        essid = ''
        password = ''

        while True:
            essid = input('wifi name:') # 输入essid
            password = input('wifi passwrod:') # 输入password

            if is_legal_wifi(essid, password):
                config = dict(essid=essid, password=password) # 创建字典
                with open('wifi_config.json','w') as f:
                    f.write(json.dumps(config)) # 将字典序列化为json字符串,存入wifi_config.json
                break
            else:
                print('ERROR, Please Input Right WIFI')
    
    #以下为正常的WIFI连接流程        
    wifi = network.WLAN(network.STA_IF)  
    if not wifi.isconnected(): 
        print('connecting to network...')
        wifi.active(True) 
        wifi.connect(config['essid'], config['password']) 
        import utime

        for i in range(200):
            print('第{}次尝试连接WIFI热点'.format(i))
            if wifi.isconnected():
                break
            utime.sleep_ms(100) #一般睡个5-10秒,应该绰绰有余
        
        if not wifi.isconnected():
            wifi.active(False) #关掉连接,免得repl死循环输出
            print('wifi connection error, please reconnect')
            import os
            # 连续输错essid和password会导致wifi_config.json不存在
            try:
                os.remove('wifi_config.json') # 删除配置文件
            except:
                pass
            do_connect() # 重新连接
        else:
            print('network config:', wifi.ifconfig()) 

if __name__ == '__main__':
    do_connect()

更改了main.py后我们重新启动一下:

connecting to network...
...省略若干行日志...
wifi connection error, please reconnect
wifi name:ChinaNet-Q5uk
wifi passwrod:0921608677
connecting to network...
...省略若干行日志...
I (256217) network: CONNECTED
I (258397) event: sta ip: 192.168.2.189, mask: 255.255.255.0, gw: 192.168.2.1
I (258397) network: GOT_IP
network config: ('192.168.2.189', '255.255.255.0', '192.168.2.1', '192.168.2.1')
MicroPython v1.9.4-429-ge755bd493 on 2018-08-03; ESP32 module with ESP32
Type "help()" for more information.
>>> 

总结

  • MicroPython ESP32 wifi连接connect()函数不是同步执行的,为了正确的判断wifi的连接状态,需要一定的延时
  • connect()函数从错误的essid和password连接时,会在repl中进行输出死循环,需要将连接自行断开以解决此问题.
  • 1ZLAB为你贴出了更加合理的wifi连接代码,请拿去开心.

交流

如果你对我写的文章中的内容感到困惑,欢迎评论区参与讨论或指教
没有账号的话可以邮箱联系:Fuermohao@outlook.com

推广

ad.png
MicroPython ESP32
Web note ad 1