async_2.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:micropython-dev-kit 作者: db4linq 项目源码 文件源码
def wifi_config():
    global cfg
    global client
    global CLIENT_ID
    print('load configuration')
    f = open('config.json')
    cfg = json.loads(f.read())
    f.close()
    print(cfg)
    print('starting wifi connection')
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(cfg['ap']['ssid'], cfg['ap']['pwd'])
    while not wlan.isconnected():
        print('wait connection...')
        await asyncio.sleep(1)
    wlan.ifconfig()
    mqtt_cfg = cfg['mqtt']
    client = MQTTClient(
        CLIENT_ID, mqtt_cfg["broker"], 
        port=mqtt_cfg["port"], 
        keepalive=mqtt_cfg["keepalive"])
    will_msg = {'id': CLIENT_ID, 
        'status': False, 
        'msg': 'The connection from this device is lost:('
    }
    client.set_last_will('/device/will/status', json.dumps(will_msg))
    client.set_callback(on_message)
    client.connect()
    client.subscribe('/device/{0}/switch'.format(CLIENT_ID.decode("utf-8")), 0)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号