• 伟大的micropython smartconfig 配网它来了!!!


    我这其实只是实验和搬运,还是感谢伟大的walkline群主,他弄好的,我只是负责搬运发布给新手看。
    之前一大堆人问我配网的事儿,输入下wifi名称密码这么麻烦吗,好吧,有求必应,之前的配网是通过ap模式建立热点来搞定的,确实不够优雅,现在智能配网他来了!

    首先纯mpy解决不了,别想了,因为mpy的network不支持混合模式,无法解析网络数据,门都给你焊死了!
    所以要自己打固件,方便顾及小白,我直接上代码了:
    改动地方有两处:
    1 ports/esp32/main/CMakeLists.txt
    ${PROJECT_DIR}/machine_sdcard.c
    下面一行 大约84行左右
    插入一行新的

    ${PROJECT_DIR}/smartconfig.c
    

    2 新建 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密码并连接了
    在这里插入图片描述

    emmm。。。肯定还有人问我固件,我打了个esp32的,需要的自己来拿吧
    esp32自动配网固件

  • 相关阅读:
    使用BitLocker实现磁盘加密、u盘加密、移动硬盘加密
    Spring Boot异步请求处理框架
    java计算机毕业设计郑工校园二手交易平台网站源程序+mysql+系统+lw文档+远程调试
    前端研习录(11)——CSS3新特性——圆角及阴影讲解及示例说明
    VSCode python extension loading 终极解决方案
    总结使用React做过的一些优化
    如何指定JVM target 为1.8
    快速入门Spring Cloud Hystrix(服务降级、服务熔断、服务监控)
    我把 CPU 三级缓存的秘密,藏在这 8 张图里
    深挖 Python 元组 pt.2
  • 原文地址:https://blog.csdn.net/jd3096/article/details/127098603