Skip to content

Commit 3404180

Browse files
committed
更新Wi-Fi组件版本,从OTA接口读取Websocket服务器
1 parent c380617 commit 3404180

15 files changed

+67
-58
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# CMakeLists in this exact order for cmake to work correctly
55
cmake_minimum_required(VERSION 3.16)
66

7-
set(PROJECT_VER "1.6.0")
7+
set(PROJECT_VER "1.6.1")
88

99
# Add this line to disable the specific warning
1010
add_compile_options(-Wno-missing-field-initializers)

docs/websocket.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
2. **建立 WebSocket 连接**
1515
- 当设备需要开始语音会话时(例如用户唤醒、手动按键触发等),调用 `OpenAudioChannel()`
16-
- 根据编译配置获取 WebSocket URL`CONFIG_WEBSOCKET_URL`
16+
- 根据配置获取 WebSocket URL
1717
- 设置若干请求头(`Authorization`, `Protocol-Version`, `Device-Id`, `Client-Id`
1818
- 调用 `Connect()` 与服务器建立 WebSocket 连接
1919

main/CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(SOURCES "audio_codecs/audio_codec.cc"
1111
"display/lcd_display.cc"
1212
"display/oled_display.cc"
1313
"protocols/protocol.cc"
14+
"protocols/mqtt_protocol.cc"
15+
"protocols/websocket_protocol.cc"
1416
"iot/thing.cc"
1517
"iot/thing_manager.cc"
1618
"system_info.cc"
@@ -148,12 +150,6 @@ file(GLOB BOARD_SOURCES
148150
)
149151
list(APPEND SOURCES ${BOARD_SOURCES})
150152

151-
if(CONFIG_CONNECTION_TYPE_MQTT_UDP)
152-
list(APPEND SOURCES "protocols/mqtt_protocol.cc")
153-
elseif(CONFIG_CONNECTION_TYPE_WEBSOCKET)
154-
list(APPEND SOURCES "protocols/websocket_protocol.cc")
155-
endif()
156-
157153
if(CONFIG_USE_AUDIO_PROCESSOR)
158154
list(APPEND SOURCES "audio_processing/audio_processor.cc")
159155
endif()

main/Kconfig.projbuild

+3-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
menu "Xiaozhi Assistant"
22

3-
config OTA_VERSION_URL
4-
string "OTA Version URL"
3+
config OTA_URL
4+
string "Default OTA URL"
55
default "https://api.tenclass.net/xiaozhi/ota/"
66
help
7-
The application will access this URL to check for updates.
7+
The application will access this URL to check for new firmwares and server address.
88

99

1010
choice
@@ -23,32 +23,6 @@ choice
2323
bool "Japanese"
2424
endchoice
2525

26-
27-
choice CONNECTION_TYPE
28-
prompt "Connection Type"
29-
default CONNECTION_TYPE_MQTT_UDP
30-
help
31-
网络数据传输协议
32-
config CONNECTION_TYPE_MQTT_UDP
33-
bool "MQTT + UDP"
34-
config CONNECTION_TYPE_WEBSOCKET
35-
bool "Websocket"
36-
endchoice
37-
38-
config WEBSOCKET_URL
39-
depends on CONNECTION_TYPE_WEBSOCKET
40-
string "Websocket URL"
41-
default "wss://api.tenclass.net/xiaozhi/v1/"
42-
help
43-
Communication with the server through websocket after wake up.
44-
45-
config WEBSOCKET_ACCESS_TOKEN
46-
depends on CONNECTION_TYPE_WEBSOCKET
47-
string "Websocket Access Token"
48-
default "test-token"
49-
help
50-
Access token for websocket communication.
51-
5226
choice BOARD_TYPE
5327
prompt "Board Type"
5428
default BOARD_TYPE_BREAD_COMPACT_WIFI

main/application.cc

+16-6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void Application::CheckNewVersion() {
7878
ESP_LOGE(TAG, "Too many retries, exit version check");
7979
return;
8080
}
81+
82+
char buffer[128];
83+
snprintf(buffer, sizeof(buffer), Lang::Strings::CHECK_NEW_VERSION_FAILED, retry_delay, ota_.GetCheckVersionUrl().c_str());
84+
Alert(Lang::Strings::ERROR, buffer, "sad", Lang::Sounds::P3_EXCLAMATION);
85+
8186
ESP_LOGW(TAG, "Check new version failed, retry in %d seconds (%d/%d)", retry_delay, retry_count, MAX_RETRY);
8287
for (int i = 0; i < retry_delay; i++) {
8388
vTaskDelay(pdMS_TO_TICKS(1000));
@@ -372,11 +377,16 @@ void Application::Start() {
372377

373378
// Initialize the protocol
374379
display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
375-
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
376-
protocol_ = std::make_unique<WebsocketProtocol>();
377-
#else
378-
protocol_ = std::make_unique<MqttProtocol>();
379-
#endif
380+
381+
if (ota_.HasMqttConfig()) {
382+
protocol_ = std::make_unique<MqttProtocol>();
383+
} else if (ota_.HasWebsocketConfig()) {
384+
protocol_ = std::make_unique<WebsocketProtocol>();
385+
} else {
386+
ESP_LOGW(TAG, "No protocol specified in the OTA config, using MQTT");
387+
protocol_ = std::make_unique<MqttProtocol>();
388+
}
389+
380390
protocol_->OnNetworkError([this](const std::string& message) {
381391
SetDeviceState(kDeviceStateIdle);
382392
Alert(Lang::Strings::ERROR, message.c_str(), "sad", Lang::Sounds::P3_EXCLAMATION);
@@ -529,7 +539,7 @@ void Application::Start() {
529539
SetDeviceState(kDeviceStateConnecting);
530540
wake_word_detect_.EncodeWakeWordData();
531541

532-
if (!protocol_->OpenAudioChannel()) {
542+
if (!protocol_ || !protocol_->OpenAudioChannel()) {
533543
wake_word_detect_.StartDetection();
534544
return;
535545
}

main/assets/en-US/language.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"ERROR": "Error",
99
"VERSION": "Ver ",
1010
"LOADING_PROTOCOL": "Logging in...",
11-
"CHECKING_NEW_VERSION": "Checking for new version...",
1211
"INITIALIZING": "Initializing...",
1312
"PIN_ERROR": "Please insert SIM card",
1413
"REG_ERROR": "Unable to access network, please check SIM card status",
1514
"DETECTING_MODULE": "Detecting module...",
1615
"REGISTERING_NETWORK": "Waiting for network...",
16+
"CHECKING_NEW_VERSION": "Checking for new version...",
17+
"CHECK_NEW_VERSION_FAILED": "Check for new version failed, will retry in %d seconds: %s",
1718

1819
"STANDBY": "Standby",
1920
"CONNECT_TO": "Connect to ",

main/assets/ja-JP/language.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"DETECTING_MODULE": "モジュールを検出中...",
1515
"REGISTERING_NETWORK": "ネットワーク接続待機中...",
1616
"CHECKING_NEW_VERSION": "新しいバージョンを確認中...",
17+
"CHECK_NEW_VERSION_FAILED": "更新確認に失敗しました。%d 秒後に再試行します: %s",
1718

1819
"STANDBY": "待機中",
1920
"CONNECT_TO": "接続先 ",

main/assets/zh-CN/language.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"DETECTING_MODULE":"检测模组...",
1515
"REGISTERING_NETWORK":"等待网络...",
1616
"CHECKING_NEW_VERSION":"检查新版本...",
17+
"CHECK_NEW_VERSION_FAILED":"检查新版本失败,将在 %d 秒后重试:%s",
1718

1819
"STANDBY":"待命",
1920
"CONNECT_TO":"连接 ",

main/assets/zh-TW/language.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"DETECTING_MODULE": "檢測模組...",
1515
"REGISTERING_NETWORK": "等待網絡...",
1616
"CHECKING_NEW_VERSION": "檢查新版本...",
17+
"CHECK_NEW_VERSION_FAILED": "檢查新版本失敗,將在 %d 秒後重試:%s",
1718

1819
"STANDBY": "待命",
1920
"CONNECT_TO": "連接 ",

main/boards/common/wifi_board.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,13 @@ Http* WifiBoard::CreateHttp() {
114114
}
115115

116116
WebSocket* WifiBoard::CreateWebSocket() {
117-
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
118-
std::string url = CONFIG_WEBSOCKET_URL;
117+
Settings settings("websocket", false);
118+
std::string url = settings.GetString("url");
119119
if (url.find("wss://") == 0) {
120120
return new WebSocket(new TlsTransport());
121121
} else {
122122
return new WebSocket(new TcpTransport());
123123
}
124-
#endif
125124
return nullptr;
126125
}
127126

main/idf_component.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ dependencies:
88
espressif/esp_io_expander_tca9554: "==2.0.0"
99
espressif/esp_lcd_panel_io_additions: "^1.0.1"
1010
78/esp_lcd_nv3023: "~1.0.0"
11-
78/esp-wifi-connect: "~2.3.2"
11+
78/esp-wifi-connect: "~2.4.1"
1212
78/esp-opus-encoder: "~2.3.1"
1313
78/esp-ml307: "~1.9.0"
1414
78/xiaozhi-fonts: "~1.3.2"
1515
espressif/led_strip: "^2.4.1"
1616
espressif/esp_codec_dev: "~1.3.2"
17-
espressif/esp-sr: "^2.0.3"
17+
espressif/esp-sr: "^2.0.5"
1818
espressif/button: "^3.3.1"
1919
espressif/knob: "^1.0.0"
2020
lvgl/lvgl: "~9.2.2"

main/ota.cc

+20-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323

2424

2525
Ota::Ota() {
26-
SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
26+
{
27+
Settings settings("wifi", false);
28+
check_version_url_ = settings.GetString("ota_url");
29+
if (check_version_url_.empty()) {
30+
check_version_url_ = CONFIG_OTA_URL;
31+
}
32+
}
2733

2834
#ifdef ESP_EFUSE_BLOCK_USR_DATA
2935
// Read Serial Number from efuse user_data
@@ -42,10 +48,6 @@ Ota::Ota() {
4248
Ota::~Ota() {
4349
}
4450

45-
void Ota::SetCheckVersionUrl(std::string check_version_url) {
46-
check_version_url_ = check_version_url;
47-
}
48-
4951
void Ota::SetHeader(const std::string& key, const std::string& value) {
5052
headers_[key] = value;
5153
}
@@ -144,6 +146,19 @@ bool Ota::CheckVersion() {
144146
has_mqtt_config_ = true;
145147
}
146148

149+
has_websocket_config_ = false;
150+
cJSON *websocket = cJSON_GetObjectItem(root, "websocket");
151+
if (websocket != NULL) {
152+
Settings settings("websocket", true);
153+
cJSON *item = NULL;
154+
cJSON_ArrayForEach(item, websocket) {
155+
if (item->type == cJSON_String) {
156+
settings.SetString(item->string, item->valuestring);
157+
}
158+
}
159+
has_websocket_config_ = true;
160+
}
161+
147162
has_server_time_ = false;
148163
cJSON *server_time = cJSON_GetObjectItem(root, "server_time");
149164
if (server_time != NULL) {

main/ota.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class Ota {
1313
Ota();
1414
~Ota();
1515

16-
void SetCheckVersionUrl(std::string check_version_url);
1716
void SetHeader(const std::string& key, const std::string& value);
1817
bool CheckVersion();
1918
esp_err_t Activate();
2019
bool HasActivationChallenge() { return has_activation_challenge_; }
2120
bool HasNewVersion() { return has_new_version_; }
2221
bool HasMqttConfig() { return has_mqtt_config_; }
22+
bool HasWebsocketConfig() { return has_websocket_config_; }
2323
bool HasActivationCode() { return has_activation_code_; }
2424
bool HasServerTime() { return has_server_time_; }
2525
void StartUpgrade(std::function<void(int progress, size_t speed)> callback);
@@ -29,13 +29,15 @@ class Ota {
2929
const std::string& GetCurrentVersion() const { return current_version_; }
3030
const std::string& GetActivationMessage() const { return activation_message_; }
3131
const std::string& GetActivationCode() const { return activation_code_; }
32+
const std::string& GetCheckVersionUrl() const { return check_version_url_; }
3233

3334
private:
3435
std::string check_version_url_;
3536
std::string activation_message_;
3637
std::string activation_code_;
3738
bool has_new_version_ = false;
3839
bool has_mqtt_config_ = false;
40+
bool has_websocket_config_ = false;
3941
bool has_server_time_ = false;
4042
bool has_activation_code_ = false;
4143
bool has_serial_number_ = false;

main/protocols/websocket_protocol.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "board.h"
33
#include "system_info.h"
44
#include "application.h"
5+
#include "settings.h"
56

67
#include <cstring>
78
#include <cJSON.h>
@@ -67,10 +68,18 @@ bool WebsocketProtocol::OpenAudioChannel() {
6768
delete websocket_;
6869
}
6970

71+
Settings settings("websocket", false);
72+
std::string url = settings.GetString("url");
73+
std::string token = settings.GetString("token");
74+
7075
busy_sending_audio_ = false;
7176
error_occurred_ = false;
72-
std::string url = CONFIG_WEBSOCKET_URL;
73-
std::string token = "Bearer " + std::string(CONFIG_WEBSOCKET_ACCESS_TOKEN);
77+
78+
// If token not starts with "Bearer " or "bearer ", add it
79+
if (token.empty() || (token.find("Bearer ") != 0 && token.find("bearer ") != 0)) {
80+
token = "Bearer " + token;
81+
}
82+
7483
websocket_ = Board::GetInstance().CreateWebSocket();
7584
websocket_->SetHeader("Authorization", token.c_str());
7685
websocket_->SetHeader("Protocol-Version", "1");
@@ -109,6 +118,7 @@ bool WebsocketProtocol::OpenAudioChannel() {
109118
}
110119
});
111120

121+
ESP_LOGI(TAG, "Connecting to websocket server: %s with token: %s", url.c_str(), token.c_str());
112122
if (!websocket_->Connect(url.c_str())) {
113123
ESP_LOGE(TAG, "Failed to connect to websocket server");
114124
SetError(Lang::Strings::SERVER_NOT_FOUND);

sdkconfig.defaults.esp32s3

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ CONFIG_SPIRAM=y
88
CONFIG_SPIRAM_MODE_OCT=y
99
CONFIG_SPIRAM_SPEED_80M=y
1010
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4096
11-
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y
1211
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=49152
1312
CONFIG_SPIRAM_MEMTEST=n
1413
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=y

0 commit comments

Comments
 (0)