Files
xiaozhi-esp32/main/boards/common/wifi_board.cc

185 lines
5.6 KiB
C++
Raw Normal View History

2024-11-05 20:15:00 +08:00
#include "wifi_board.h"
2024-12-06 11:08:49 +08:00
#include "display.h"
2024-11-05 20:15:00 +08:00
#include "application.h"
#include "system_info.h"
2024-11-18 06:17:39 +08:00
#include "font_awesome_symbols.h"
2024-11-25 02:27:21 +08:00
#include "settings.h"
2025-02-19 23:54:59 +08:00
#include "assets/lang_config.h"
2024-10-29 00:22:29 +08:00
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2024-11-05 20:15:00 +08:00
#include <esp_http.h>
2024-11-14 23:15:43 +08:00
#include <esp_mqtt.h>
#include <esp_udp.h>
2024-11-05 20:15:00 +08:00
#include <tcp_transport.h>
#include <tls_transport.h>
#include <web_socket.h>
2024-10-29 00:22:29 +08:00
#include <esp_log.h>
2024-11-05 20:15:00 +08:00
#include <wifi_station.h>
#include <wifi_configuration_ap.h>
2025-01-12 10:25:43 +08:00
#include <ssid_manager.h>
2024-11-05 20:15:00 +08:00
2024-10-29 00:22:29 +08:00
static const char *TAG = "WifiBoard";
2025-01-12 10:25:43 +08:00
WifiBoard::WifiBoard() {
Settings settings("wifi", true);
wifi_config_mode_ = settings.GetInt("force_ap") == 1;
if (wifi_config_mode_) {
ESP_LOGI(TAG, "force_ap is set to 1, reset to 0");
settings.SetInt("force_ap", 0);
}
}
2025-02-04 14:24:51 +08:00
std::string WifiBoard::GetBoardType() {
return "wifi";
}
2025-01-12 10:25:43 +08:00
void WifiBoard::EnterWifiConfigMode() {
2024-10-29 00:22:29 +08:00
auto& application = Application::GetInstance();
2025-01-12 10:25:43 +08:00
application.SetDeviceState(kDeviceStateWifiConfiguring);
auto& wifi_ap = WifiConfigurationAp::GetInstance();
wifi_ap.SetLanguage(Lang::CODE);
2025-01-12 10:25:43 +08:00
wifi_ap.SetSsidPrefix("Xiaozhi");
wifi_ap.Start();
// 显示 WiFi 配置 AP 的 SSID 和 Web 服务器 URL
2025-02-19 23:54:59 +08:00
std::string hint = Lang::Strings::CONNECT_TO_HOTSPOT;
2025-01-12 10:25:43 +08:00
hint += wifi_ap.GetSsid();
2025-02-19 23:54:59 +08:00
hint += Lang::Strings::ACCESS_VIA_BROWSER;
2025-01-12 10:25:43 +08:00
hint += wifi_ap.GetWebServerUrl();
hint += "\n\n";
2025-01-12 10:25:43 +08:00
2025-01-16 05:43:07 +08:00
// 播报配置 WiFi 的提示
2025-02-19 23:54:59 +08:00
application.Alert(Lang::Strings::WIFI_CONFIG_MODE, hint.c_str(), "", Lang::Sounds::P3_WIFICONFIG);
2025-01-16 05:43:07 +08:00
2025-01-12 10:25:43 +08:00
// Wait forever until reset after configuration
while (true) {
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
ESP_LOGI(TAG, "Free internal: %u minimal internal: %u", free_sram, min_free_sram);
vTaskDelay(pdMS_TO_TICKS(10000));
}
}
void WifiBoard::StartNetwork() {
// User can press BOOT button while starting to enter WiFi configuration mode
if (wifi_config_mode_) {
EnterWifiConfigMode();
return;
}
// If no WiFi SSID is configured, enter WiFi configuration mode
auto& ssid_manager = SsidManager::GetInstance();
auto ssid_list = ssid_manager.GetSsidList();
if (ssid_list.empty()) {
wifi_config_mode_ = true;
EnterWifiConfigMode();
return;
}
2024-10-29 00:22:29 +08:00
auto& wifi_station = WifiStation::GetInstance();
2025-01-12 10:25:43 +08:00
wifi_station.OnScanBegin([this]() {
auto display = Board::GetInstance().GetDisplay();
2025-02-18 19:33:07 +08:00
display->ShowNotification(Lang::Strings::SCANNING_WIFI, 30000);
2025-01-12 10:25:43 +08:00
});
wifi_station.OnConnect([this](const std::string& ssid) {
auto display = Board::GetInstance().GetDisplay();
2025-02-19 23:54:59 +08:00
std::string notification = Lang::Strings::CONNECT_TO;
notification += ssid;
notification += "...";
display->ShowNotification(notification.c_str(), 30000);
2025-01-12 10:25:43 +08:00
});
wifi_station.OnConnected([this](const std::string& ssid) {
auto display = Board::GetInstance().GetDisplay();
2025-02-19 23:54:59 +08:00
std::string notification = Lang::Strings::CONNECTED_TO;
notification += ssid;
display->ShowNotification(notification.c_str(), 30000);
2025-01-12 10:25:43 +08:00
});
2024-10-29 00:22:29 +08:00
wifi_station.Start();
2025-01-05 19:34:28 +08:00
2025-01-12 10:25:43 +08:00
// Try to connect to WiFi, if failed, launch the WiFi configuration AP
if (!wifi_station.WaitForConnected(60 * 1000)) {
wifi_station.Stop();
wifi_config_mode_ = true;
EnterWifiConfigMode();
return;
2024-10-29 00:22:29 +08:00
}
}
Http* WifiBoard::CreateHttp() {
return new EspHttp();
}
WebSocket* WifiBoard::CreateWebSocket() {
2024-11-16 05:49:35 +08:00
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
2024-10-29 00:22:29 +08:00
std::string url = CONFIG_WEBSOCKET_URL;
if (url.find("wss://") == 0) {
2024-11-16 05:49:35 +08:00
return new WebSocket(new TlsTransport());
} else {
return new WebSocket(new TcpTransport());
}
#endif
return nullptr;
2024-10-29 00:22:29 +08:00
}
2024-11-14 23:15:43 +08:00
Mqtt* WifiBoard::CreateMqtt() {
return new EspMqtt();
}
Udp* WifiBoard::CreateUdp() {
return new EspUdp();
}
2024-11-18 06:17:39 +08:00
const char* WifiBoard::GetNetworkStateIcon() {
if (wifi_config_mode_) {
return FONT_AWESOME_WIFI;
}
auto& wifi_station = WifiStation::GetInstance();
if (!wifi_station.IsConnected()) {
return FONT_AWESOME_WIFI_OFF;
}
int8_t rssi = wifi_station.GetRssi();
2025-02-01 23:08:41 +08:00
if (rssi >= -60) {
2024-11-18 06:17:39 +08:00
return FONT_AWESOME_WIFI;
2025-02-01 23:08:41 +08:00
} else if (rssi >= -70) {
2024-11-18 06:17:39 +08:00
return FONT_AWESOME_WIFI_FAIR;
} else {
return FONT_AWESOME_WIFI_WEAK;
}
}
2024-11-03 05:54:15 +08:00
std::string WifiBoard::GetBoardJson() {
2024-10-29 00:22:29 +08:00
// Set the board type for OTA
auto& wifi_station = WifiStation::GetInstance();
std::string board_json = std::string("{\"type\":\"" BOARD_TYPE "\",");
board_json += "\"name\":\"" BOARD_NAME "\",";
2024-11-01 14:26:02 +08:00
if (!wifi_config_mode_) {
board_json += "\"ssid\":\"" + wifi_station.GetSsid() + "\",";
board_json += "\"rssi\":" + std::to_string(wifi_station.GetRssi()) + ",";
board_json += "\"channel\":" + std::to_string(wifi_station.GetChannel()) + ",";
board_json += "\"ip\":\"" + wifi_station.GetIpAddress() + "\",";
}
2024-10-29 00:22:29 +08:00
board_json += "\"mac\":\"" + SystemInfo::GetMacAddress() + "\"}";
return board_json;
}
2024-11-15 23:07:20 +08:00
void WifiBoard::SetPowerSaveMode(bool enabled) {
auto& wifi_station = WifiStation::GetInstance();
wifi_station.SetPowerSaveMode(enabled);
}
2024-11-25 02:27:21 +08:00
void WifiBoard::ResetWifiConfiguration() {
2025-02-19 23:54:59 +08:00
// Set a flag and reboot the device to enter the network configuration mode
2024-11-25 02:27:21 +08:00
{
Settings settings("wifi", true);
2025-01-12 10:25:43 +08:00
settings.SetInt("force_ap", 1);
2024-11-25 02:27:21 +08:00
}
2025-02-19 23:54:59 +08:00
GetDisplay()->ShowNotification(Lang::Strings::ENTERING_WIFI_CONFIG_MODE);
2024-11-25 02:27:21 +08:00
vTaskDelay(pdMS_TO_TICKS(1000));
// Reboot the device
esp_restart();
}