添加作为WIFI Station的代码

This commit is contained in:
2021-01-21 13:40:48 +08:00
parent a40f3162cf
commit 12dee25ace
11 changed files with 337 additions and 149 deletions

View File

@@ -1,4 +1,4 @@
set(COMPONENT_SRCS "main.c" "src/init.c" ) set(COMPONENT_SRCS "main.c" "src/init.c" "src/wifi_station.c")
set(COMPONENT_ADD_INCLUDEDIRS "." "include") set(COMPONENT_ADD_INCLUDEDIRS "." "include")
register_component() register_component()

View File

@@ -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

View File

@@ -1,10 +1,4 @@
/* 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 <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View File

@@ -1,4 +1,5 @@
#include "init.h" #include "init.h"
#include "wifi_station.h"
static const char *TAG = "esp8266 init"; static const char *TAG = "esp8266 init";
@@ -45,6 +46,8 @@ static void deinit_spiffs()
ESP_LOGI(TAG, "SPIFFS unmounted"); ESP_LOGI(TAG, "SPIFFS unmounted");
} }
void system_init() void system_init()
{ {
init_spiffs(); init_spiffs();
@@ -52,6 +55,11 @@ void system_init()
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
tcpip_adapter_init();
wifi_station_init();
} }
void system_deinit() void system_deinit()

147
main/src/wifi_station.c Normal file
View File

@@ -0,0 +1,147 @@
#include "wifi_station.h"
//WIFI SSID <20><> Password<72><64><EFBFBD><EFBFBD>
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));
//<2F><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD>Ҫspiffs֧<73><D6A7>
struct stat st;
if (stat("/spiffs/wifistationconfig.bin", &st) == 0)
{
if(st.st_size != sizeof(wifi_config))
{
//<2F>ļ<EFBFBD><C4BC><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD>С<EFBFBD><D0A1>һ<EFBFBD><D2BB>
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
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
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.");
}