mirror of
https://github.com/HEYAHONG/ESP8266ModRTOSDemo.git
synced 2025-10-15 03:48:04 +08:00
添加作为WIFI Station的代码
This commit is contained in:
@@ -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)
|
||||
|
18
Makefile
18
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
|
||||
|
||||
|
@@ -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()
|
||||
|
@@ -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.)
|
||||
|
@@ -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
|
||||
|
39
main/include/wifi_station.h
Normal file
39
main/include/wifi_station.h
Normal 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
|
44
main/main.c
44
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 <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#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 <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#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();
|
||||
|
||||
}
|
||||
|
130
main/src/init.c
130
main/src/init.c
@@ -1,61 +1,69 @@
|
||||
#include "init.h"
|
||||
|
||||
static const char *TAG = "esp8266 init";
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD>ļ<EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>洢
|
||||
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";
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD>ļ<EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>洢
|
||||
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();
|
||||
}
|
||||
|
||||
|
147
main/src/wifi_station.c
Normal file
147
main/src/wifi_station.c
Normal 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.");
|
||||
|
||||
}
|
@@ -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,
|
||||
|
|
@@ -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
|
||||
|
Reference in New Issue
Block a user