Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
micropython smartconfig 配网
我这其实只是实验和搬运,还是感谢伟大的walkline群主,他弄好的,我只是负责搬运发布给新手看。
之前一大堆人问我配网的事儿,输入下wifi名称密码这么麻烦吗,好吧,有求必应,之前的配网是通过ap模式建立热点来搞定的,确实不够优雅,现在智能配网他来了!
首先纯mpy解决不了,别想了,因为mpy的network不支持混合模式,无法解析网络数据,门都给你焊死了!
所以要自己打固件,方便顾及小白,我直接上代码了:
改动地方有两处:
1 ports/esp32/main/CMakeLists.txt
${PROJECT_DIR}/machine_sdcard.c
下面一行 大约84行左右
插入一行新的
${PROJECT_DIR}/smartconfig.c
新建 ports/esp32/smartconfig.c
内容为
#include "py/obj.h" #include "py/runtime.h" #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_smartconfig.h" static EventGroupHandle_t s_wifi_event_group; static const int ESPTOUCH_DONE_BIT = BIT1; static const char *TAG = "smartconfig"; static int found_ssid_and_password = false; static uint8_t ssid[33] = {0}; static uint8_t password[65] = {0}; static uint8_t type = -1; static uint8_t token = 0; static void smartconfig_task(void * parm); static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) { ESP_LOGI(TAG, "Scan done"); } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) { ESP_LOGI(TAG, "Found channel"); } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) { ESP_LOGI(TAG, "Got SSID and password"); smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data; memcpy(ssid, evt->ssid, sizeof(evt->ssid)); memcpy(password, evt->password, sizeof(evt->password)); type = evt->type; token = evt->token; ESP_LOGI(TAG, "SSID:%s", ssid); ESP_LOGI(TAG, "PASSWORD:%s", password); ESP_LOGI(TAG, "TYPE:%d", type); ESP_LOGI(TAG, "TOKEN:%d", token); found_ssid_and_password = true; xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); } } static void initialise_wifi(void) { s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); xTaskCreate(smartconfig_task, "smartconfig_task", 4096, NULL, 3, NULL); } static void smartconfig_task(void * parm) { EventBits_t uxBits; ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS)); smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); while (1) { uxBits = xEventGroupWaitBits(s_wifi_event_group, ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY); if(uxBits & ESPTOUCH_DONE_BIT) { ESP_LOGI(TAG, "smartconfig over"); ESP_ERROR_CHECK(esp_smartconfig_stop()); vTaskDelete(NULL); } } } STATIC mp_obj_t smartconfig_start(void) { initialise_wifi(); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(smartconfig_start_obj, smartconfig_start); STATIC mp_obj_t smartconfig_success(void) { return mp_obj_new_bool(found_ssid_and_password); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(smartconfig_success_obj, smartconfig_success); STATIC mp_obj_t smartconfig_info(void) { mp_obj_t info[] = { mp_obj_new_str((const char *)ssid, strlen((const char *)ssid)), mp_obj_new_str((const char *)password, strlen((const char *)password)), mp_obj_new_int(type), mp_obj_new_int(token) }; return mp_obj_new_tuple(4, info); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(smartconfig_info_obj, smartconfig_info); STATIC const mp_rom_map_elem_t smartconfig_module_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_smartconfig)}, {MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&smartconfig_start_obj)}, {MP_ROM_QSTR(MP_QSTR_success), MP_ROM_PTR(&smartconfig_success_obj)}, {MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&smartconfig_info_obj)}, {MP_ROM_QSTR(MP_QSTR_TYPE_UNKNOWN), MP_ROM_INT(-1)}, {MP_ROM_QSTR(MP_QSTR_TYPE_ESPTOUCH), MP_ROM_INT(SC_TYPE_ESPTOUCH)}, {MP_ROM_QSTR(MP_QSTR_TYPE_AIRKISS), MP_ROM_INT(SC_TYPE_AIRKISS)}, }; STATIC MP_DEFINE_CONST_DICT(smartconfig_module_globals, smartconfig_module_globals_table); const mp_obj_module_t smartconfig_user_cmodule = { .base = {&mp_type_module}, .globals = (mp_obj_dict_t *)&smartconfig_module_globals, }; MP_REGISTER_MODULE(MP_QSTR_smartconfig, smartconfig_user_cmodule);
完事儿了,make吧
make好了之后就可以import smartconfig了,测试py在此:
from utime import sleep import network import socket import smartconfig def inet_pton(ip_str:str): '''convert ip address from string to bytes''' ip_bytes = b'' ip_segs = ip_str.split('.') for seg in ip_segs: ip_bytes += int(seg).to_bytes(1, 'little') return ip_bytes def send_ack(local_ip, local_mac): '''sent ack_done event to phone''' udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) data = smartconfig.info()[3].to_bytes(1, 'little') + local_mac port = 10000 # airkiss port if smartconfig.info()[2] == smartconfig.TYPE_ESPTOUCH: data += inet_pton(local_ip) port = 18266 # esptouch port print( f"""- sending ack: type: {'esptouch' if smartconfig.info()[2] == smartconfig.TYPE_ESPTOUCH else 'airkiss'} port: {port} data: {data} length: {len(data)} """ ) for _ in range(20): sleep(0.1) try: udp.sendto(data, ('255.255.255.255', port)) except OSError: pass print('- ack was sent') station = network.WLAN(network.STA_IF) station.active(True) print('- start smartconfig...') smartconfig.start() print('- waiting for success...') while not smartconfig.success(): sleep(0.5) print('- got smartconfig info') ssid, password, sc_type, token = smartconfig.info() print(smartconfig.info()) print('- connecting to wifi...') station.connect(ssid, password) while not station.isconnected(): sleep(0.5) print('- wifi connected') while station.ifconfig()[0] == '0.0.0.0': sleep(0.5) print('- got ip') print(station.ifconfig()) send_ack(station.ifconfig()[0], station.config('mac'))
运行后提示等待配网,这时打开微信——小程序——搜airkiss配网 有个esp模组airkiss配网那个,或者关注乐鑫公众号——产品资源——airkiss设备
配网吧 然后会发现esp32 自动获取wifi密码并连接了
————————————————
原文链接:https://blog.csdn.net/jd3096/article/details/127098603
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0087008476257324 seconds