From 12dee25ace2ef32021d2574e85345e060113cadc Mon Sep 17 00:00:00 2001 From: HEYAHONG <2229388563@qq.com> Date: Thu, 21 Jan 2021 13:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=9C=E4=B8=BAWIFI=20Stat?= =?UTF-8?q?ion=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 12 +-- Makefile | 18 ++--- main/CMakeLists.txt | 8 +- main/component.mk | 8 +- main/include/init.h | 46 +++++------ main/include/wifi_station.h | 39 ++++++++++ main/main.c | 44 +++++------ main/src/init.c | 130 ++++++++++++++++--------------- main/src/wifi_station.c | 147 ++++++++++++++++++++++++++++++++++++ partitions.csv | 16 ++-- sdkconfig.defaults | 18 ++--- 11 files changed, 337 insertions(+), 149 deletions(-) create mode 100644 main/include/wifi_station.h create mode 100644 main/src/wifi_station.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f649a2..95f4e9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ -# The following lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(ESP8266Mod) +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(ESP8266Mod) diff --git a/Makefile b/Makefile index 3a564ff..8ff6c55 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := esp8266 - -include $(IDF_PATH)/make/project.mk - +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := esp8266 + +include $(IDF_PATH)/make/project.mk + diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f63c031..92b9f01 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMPONENT_SRCS "main.c" "src/init.c" ) -set(COMPONENT_ADD_INCLUDEDIRS "." "include") - -register_component() +set(COMPONENT_SRCS "main.c" "src/init.c" "src/wifi_station.c") +set(COMPONENT_ADD_INCLUDEDIRS "." "include") + +register_component() diff --git a/main/component.mk b/main/component.mk index a98f634..d1eb1e5 100644 --- a/main/component.mk +++ b/main/component.mk @@ -1,4 +1,4 @@ -# -# "main" pseudo-component makefile. -# -# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/main/include/init.h b/main/include/init.h index 38e53a2..66c071d 100644 --- a/main/include/init.h +++ b/main/include/init.h @@ -1,23 +1,23 @@ -#ifndef INIT_H_INCLUDED -#define INIT_H_INCLUDED -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event.h" -#include "esp_netif.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "lwip/sockets.h" -#include "lwip/dns.h" -#include "lwip/netdb.h" -#include "esp_log.h" -#include "mqtt_client.h" -#include "esp_err.h" -#include "esp_log.h" -#include "esp_spiffs.h" - -void system_init(); - -#endif // INIT_H_INCLUDED +#ifndef INIT_H_INCLUDED +#define INIT_H_INCLUDED +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" +#include "esp_log.h" +#include "mqtt_client.h" +#include "esp_err.h" +#include "esp_log.h" +#include "esp_spiffs.h" + +void system_init(); + +#endif // INIT_H_INCLUDED diff --git a/main/include/wifi_station.h b/main/include/wifi_station.h new file mode 100644 index 0000000..936f5a0 --- /dev/null +++ b/main/include/wifi_station.h @@ -0,0 +1,39 @@ +#ifndef WIFI_STATION_H_INCLUDED +#define WIFI_STATION_H_INCLUDED + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "esp_event.h" +#include "esp_wifi.h" +#include "nvs.h" +#include "nvs_flash.h" + +#include "lwip/err.h" +#include "lwip/sys.h" +#include "string.h" + +#define WIFI_STATION_SSID "Test" +#define WIFI_STATION_PASSWORD "12345678" + +extern EventGroupHandle_t s_wifi_station_event_group; + +/* The event group allows multiple bits for each event, but we only care about two events: + * - we are connected to the AP with an IP + * - we failed to connect after the maximum amount of retries */ +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +void wifi_station_init(); + +void wifi_station_setconfig(const wifi_config_t *cfg); + +const wifi_config_t * wifi_station_getconfig(); + +void wifi_station_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data); + +#endif // WIFI_STATION_H_INCLUDED diff --git a/main/main.c b/main/main.c index bbf753d..e3a40c8 100644 --- a/main/main.c +++ b/main/main.c @@ -1,25 +1,19 @@ -/* SPIFFS filesystem example. - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ - -#include -#include -#include -#include -#include "esp_err.h" -#include "esp_log.h" -#include "esp_spiffs.h" -#include "init.h" - -static const char *TAG = "esp8266 main"; - -void app_main(void) -{ - - system_init(); - -} + + +#include +#include +#include +#include +#include "esp_err.h" +#include "esp_log.h" +#include "esp_spiffs.h" +#include "init.h" + +static const char *TAG = "esp8266 main"; + +void app_main(void) +{ + + system_init(); + +} diff --git a/main/src/init.c b/main/src/init.c index 4901176..b60a001 100644 --- a/main/src/init.c +++ b/main/src/init.c @@ -1,61 +1,69 @@ -#include "init.h" - -static const char *TAG = "esp8266 init"; - -//初始化文件系统用于文件存储 -static void init_spiffs() -{ - ESP_LOGI(TAG, "Initializing SPIFFS"); - - esp_vfs_spiffs_conf_t conf = { - .base_path = "/spiffs", - .partition_label = NULL, - .max_files = 100, - .format_if_mount_failed = true - }; - - // Use settings defined above to initialize and mount SPIFFS filesystem. - // Note: esp_vfs_spiffs_register is an all-in-one convenience function. - esp_err_t ret = esp_vfs_spiffs_register(&conf); - - if (ret != ESP_OK) { - if (ret == ESP_FAIL) { - ESP_LOGE(TAG, "Failed to mount or format filesystem"); - } else if (ret == ESP_ERR_NOT_FOUND) { - ESP_LOGE(TAG, "Failed to find SPIFFS partition"); - } else { - ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); - } - return; - } - - size_t total = 0, used = 0; - ret = esp_spiffs_info(NULL, &total, &used); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); - } else { - ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); - } -} - -static void deinit_spiffs() -{ - // All done, unmount partition and disable SPIFFS - esp_vfs_spiffs_unregister(NULL); - ESP_LOGI(TAG, "SPIFFS unmounted"); -} - -void system_init() -{ - init_spiffs(); - - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); -} - -void system_deinit() -{ - deinit_spiffs(); -} - +#include "init.h" +#include "wifi_station.h" + +static const char *TAG = "esp8266 init"; + +//初始化文件系统用于文件存储 +static void init_spiffs() +{ + ESP_LOGI(TAG, "Initializing SPIFFS"); + + esp_vfs_spiffs_conf_t conf = { + .base_path = "/spiffs", + .partition_label = NULL, + .max_files = 100, + .format_if_mount_failed = true + }; + + // Use settings defined above to initialize and mount SPIFFS filesystem. + // Note: esp_vfs_spiffs_register is an all-in-one convenience function. + esp_err_t ret = esp_vfs_spiffs_register(&conf); + + if (ret != ESP_OK) { + if (ret == ESP_FAIL) { + ESP_LOGE(TAG, "Failed to mount or format filesystem"); + } else if (ret == ESP_ERR_NOT_FOUND) { + ESP_LOGE(TAG, "Failed to find SPIFFS partition"); + } else { + ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); + } + return; + } + + size_t total = 0, used = 0; + ret = esp_spiffs_info(NULL, &total, &used); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); + } else { + ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); + } +} + +static void deinit_spiffs() +{ + // All done, unmount partition and disable SPIFFS + esp_vfs_spiffs_unregister(NULL); + ESP_LOGI(TAG, "SPIFFS unmounted"); +} + + + +void system_init() +{ + init_spiffs(); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + tcpip_adapter_init(); + + wifi_station_init(); + +} + +void system_deinit() +{ + deinit_spiffs(); +} + diff --git a/main/src/wifi_station.c b/main/src/wifi_station.c new file mode 100644 index 0000000..54e1e33 --- /dev/null +++ b/main/src/wifi_station.c @@ -0,0 +1,147 @@ +#include "wifi_station.h" + +//WIFI SSID 及 Password缓存 +static wifi_config_t wifi_config = {0}; +static const char *TAG = "wifi station"; + +static int s_retry_num = 0; +EventGroupHandle_t s_wifi_station_event_group; + + +static void save_wifi_station_config() +{ + FILE* f = fopen("/spiffs/wifistationconfig.bin", "wb"); + if (f == NULL) + { + ESP_LOGE(TAG, "Failed to open wifistationconfig.bin for writing"); + return; + } + + fwrite(&wifi_config,sizeof(wifi_config),1,f); + + ESP_LOGI(TAG, "save file wifistationconfig.bin"); + + fclose(f); +} + +static void load_wifi_station_config() +{ + memset(&wifi_config.sta.ssid,0,sizeof(wifi_config.sta.ssid)); + memcpy(wifi_config.sta.ssid,WIFI_STATION_SSID,sizeof(WIFI_STATION_SSID)); + memset(&wifi_config.sta.password,0,sizeof(wifi_config.sta.password)); + memcpy(wifi_config.sta.password,WIFI_STATION_PASSWORD,sizeof(WIFI_STATION_PASSWORD)); + + //以下均需要spiffs支持 + struct stat st; + if (stat("/spiffs/wifistationconfig.bin", &st) == 0) + { + if(st.st_size != sizeof(wifi_config)) + { + //文件大小与结构结构体大小不一致 + save_wifi_station_config(); + } + else + { + FILE* f = fopen("/spiffs/wifistationconfig.bin", "rb"); + if (f == NULL) + { + ESP_LOGE(TAG, "Failed to open wifistationconfig.bin for read"); + return; + } + + fread(&wifi_config,sizeof(wifi_config),1,f); + + ESP_LOGI(TAG, "load file wifistationconfig.bin"); + + + fclose(f); + } + } + else + { + //保存文件 + save_wifi_station_config(); + } +} + + + + +void wifi_station_setconfig(const wifi_config_t *cfg) +{ + if(cfg!=NULL && cfg!=&wifi_config) + memcpy(&wifi_config,cfg,sizeof(wifi_config)); + save_wifi_station_config(); +} + +const wifi_config_t * wifi_station_getconfig() +{ + load_wifi_station_config(); + return &wifi_config; +} + +void wifi_station_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) + { + esp_wifi_connect(); + } + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) + { + if (s_retry_num < 100) + { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } + else + { + xEventGroupSetBits(s_wifi_station_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } + else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) + { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip:%s", + ip4addr_ntoa(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(s_wifi_station_event_group, WIFI_CONNECTED_BIT); + } +} + + +void wifi_station_init() +{ + load_wifi_station_config(); + + s_wifi_station_event_group = xEventGroupCreate(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_station_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_station_event_handler, NULL)); + + + + + + + /* Setting a password implies station will connect to all security modes including WEP/WPA. + * However these modes are deprecated and not advisable to be used. Incase your Access point + * doesn't support WPA2, these mode can be enabled by commenting below line */ + + if (strlen((char *)wifi_config.sta.password)) + { + wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; + } + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + +} diff --git a/partitions.csv b/partitions.csv index 4078e4d..ed903fd 100644 --- a/partitions.csv +++ b/partitions.csv @@ -1,8 +1,8 @@ -# Name, Type, SubType, Offset, Size, Flags -# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild -nvs, data, nvs, 0x9000, 0x4000 -otadata, data, ota, 0xd000, 0x2000 -phy_init, data, phy, 0xf000, 0x1000 -ota_0, 0, ota_0, 0x10000, 0xF0000 -ota_1, 0, ota_1, 0x110000,0xF0000 -storage, data, spiffs, 0x200000,2M, +# Name, Type, SubType, Offset, Size, Flags +# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild +nvs, data, nvs, 0x9000, 0x4000 +otadata, data, ota, 0xd000, 0x2000 +phy_init, data, phy, 0xf000, 0x1000 +ota_0, 0, ota_0, 0x10000, 0xF0000 +ota_1, 0, ota_1, 0x110000,0xF0000 +storage, data, spiffs, 0x200000,2M, diff --git a/sdkconfig.defaults b/sdkconfig.defaults index a323a15..7b9f8ed 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -1,9 +1,9 @@ -CONFIG_PARTITION_TABLE_CUSTOM=y -CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" -CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" -CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y -CONFIG_ESPTOOLPY_FLASHSIZE="4MB" -CONFIG_SPI_FLASH_SIZE=0x400000 -CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE=y -CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y -CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_ESPTOOLPY_FLASHSIZE="4MB" +CONFIG_SPI_FLASH_SIZE=0x400000 +CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE=y +CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y +CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200