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)
评论列表
文章目录