Files
fastbee/firmware/esp-idf/wumei-smart-firmware/main/wifi.c
2021-07-13 17:15:20 +08:00

202 lines
6.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************
* 作者kerwincui
* 时间2021-06-08
* 邮箱164770707@qq.com
* 源码地址https://gitee.com/kerwincui/wumei-smart
* author: kerwincui
* create: 2021-06-08
* email164770707@qq.com
* source:https://github.com/kerwincui/wumei-smart
******************************************************************************/
#include "wifi.h"
#include "common.h"
#include "led.h"
#include "mqtt.h"
#include "http_server.h"
static const char *TAG = "wifi";
static int s_retry_num = 0;
static EventGroupHandle_t s_wifi_event_group;
static esp_event_handler_instance_t instance_any_id;
static esp_event_handler_instance_t instance_got_ip;
// 站点回调函数
static void station_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
printf("station event handler begin \n\n\n");
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) {
esp_wifi_connect();
// if (s_retry_num < MAXIMUM_RETRY) {
// esp_wifi_connect();
// s_retry_num++;
// ESP_LOGI(TAG, "retry to connect to the AP");
// } else {
// xEventGroupSetBits(s_wifi_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: " IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
// AP回调函数
static void soft_ap_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid);
}
}
//站点模式
static void wifi_station_init(void)
{
esp_netif_create_default_wifi_sta();
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &station_event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &station_event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = "",
.password = "",
.listen_interval = LISTEN_INTERVAL,
/* 设置密码意味着工作站将连接到包括WEP/WPA在内的所有安全模式。但是这些模式已被弃用不建议使用。
* 如果您的接入点不支持WPA2可以通过在下面的行注释来启用这些模式 */
//.threshold.authmode = WIFI_AUTH_WPA2_PSK,
//PMF功能
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
strcpy((char *)wifi_config.sta.ssid,(char *)ssid);
strcpy((char *)wifi_config.sta.password,(char *)pwd);
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
}
//启动wifi站点模式
void wifi_start(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_station_init();
ESP_ERROR_CHECK(esp_wifi_start());
// 省电模式
esp_wifi_set_ps(POWER_SAVE_MODE);
//STATION模式等待建立连接或者超过连接最大次数后连接失败比特位是通过事件处理程序设置
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",ssid, pwd);
wifi_status=1;
led_status();
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", ssid, pwd);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
// 停止wifi
void wifi_mqtt_stop(void)
{
if(is_wifi_offline==0){
// 停止mqtt和wifi
mqtt_stop();
esp_wifi_stop();
wifi_status=0;
led_status();
}
}
// 继续wifi站点模式
void wifi_continue(void)
{
ESP_ERROR_CHECK(esp_wifi_start());
//STATION模式等待建立连接或者超过连接最大次数后连接失败比特位是通过事件处理程序设置
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",ssid, pwd);
wifi_status=1;
led_status();
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", ssid, pwd);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
//启动AP
void ap_start(void)
{
wifi_mqtt_stop();
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
ESP_ERROR_CHECK(esp_netif_init());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&soft_ap_event_handler,
NULL,
NULL));
wifi_config_t wifi_config = {
.ap = {
.ssid = AP_SSID,
.ssid_len = strlen(AP_SSID),
.channel = AP_CHANNEL,
.password = AP_PASS,
.max_connection = AP_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
if (strlen(AP_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", AP_SSID, AP_PASS, AP_CHANNEL);
//启动web服务
http_server_start();
}