1 改写原因
1) connect.c 和 protocol_examples_common.h 文件并不属于API库。
2) 方便后期对WIFI设置等在应用中修改。
2 改写过程
因为几个月前测试过这个项目,但只留下了相关程序,并没有总结整理,很多内容已经忘记了,回顾一下加深印象。
2.1 添加原文件到项目
一个错误插曲:一开始是直接复制以前文件到项目的main目录下,打开VSCODE将ws_echo_server.c文件内容替换为以前文件内容,编译报错,提示找不net_start(修改自start) 。
解决方法:
一开始使用的笨方法:
在VSCODE资源管理器栏中main目录下右击鼠标分别新建net.c和net.h文件,之后对net.c、net.h、ws_echo_server.c文件内容分别进行替换,再次编译正常。
正确方法是直接修改添加文件目录下CMakeLists.txt文件中加入新添加的源文件名。
后来再次遇到此问题,这次是因为函数为void类型,改为其它类型就正常了。
2.2 修改内容
对应原connect.c文件的以下两个函数做了WIFI配置相关修改。
static void start(void)
{
//读取WiFi名称和密码
nvs_handle handler;
ESP_ERROR_CHECK(nvs_open("WiFi_cfg", NVS_READWRITE, &handler));
size_t len;
len = sizeof(wifi_ssid);
ESP_ERROR_CHECK(nvs_get_str(handler,"wifi_ssid",wifi_ssid,&len));
len = sizeof(wifi_password);
ESP_ERROR_CHECK(nvs_get_str(handler,"wifi_password",wifi_password,&len));
nvs_close(handler);
#if CONFIG_EXAMPLE_CONNECT_WIFI
s_example_esp_netif = wifi_start();
s_active_interfaces++;
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
s_example_esp_netif = eth_start();
s_active_interfaces++;
#endif
#if CONFIG_EXAMPLE_CONNECT_WIFI && CONFIG_EXAMPLE_CONNECT_ETHERNET
/* if both intefaces at once, clear out to indicate that multiple netifs are active */
s_example_esp_netif = NULL;
#endif
/* create semaphore if at least one interface is active */
s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
}
static esp_netif_t *wifi_start(void)
{
char *desc;
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
// Prefix the interface description with the module TAG
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
esp_netif_config.if_desc = desc;
esp_netif_config.route_prio = 128;
esp_netif_t *netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
free(desc);
esp_wifi_set_default_wifi_sta_handlers();
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
#endif
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
//WIFI配置
wifi_config_t wifi_config;
bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid,wifi_ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password,wifi_password, sizeof(wifi_config.sta.password));
wifi_config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_connect();
return netif;
}
ws_echo_server.c的主函数增加WIFI配置写入NVS中,成功设置后可屏蔽。
void app_main(void)
{
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init());//FLASH初始化
ESP_ERROR_CHECK(esp_netif_init());//网络相关初始化
ESP_ERROR_CHECK(esp_event_loop_create_default());//创建默认的事件循环
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
//WiFi名称和密码存入NVS
// nvs_handle handler;
// ESP_ERROR_CHECK(nvs_open("WiFi_cfg", NVS_READWRITE, &handler));
// ESP_ERROR_CHECK(nvs_set_str(handler,"wifi_ssid","Tenda_123"));
// ESP_ERROR_CHECK(nvs_set_str(handler,"wifi_password","kj888888"));
// ESP_ERROR_CHECK(nvs_commit(handler));
// nvs_close(handler);
ESP_ERROR_CHECK(example_connect());
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
* and re-start it upon connection.
*/
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
//将连接和断开连接回调函数注册到系统事件循环
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
/* Start the server for the first time */
server = start_webserver();
}