forked from xiaozhi/xiaozhi-esp32
move wifi/ml307 to boards folder
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "ml307_board.h"
|
||||
#include "boards/ml307_board.h"
|
||||
#include "system_reset.h"
|
||||
#include "audio_device.h"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wifi_board.h"
|
||||
#include "boards/wifi_board.h"
|
||||
#include "system_reset.h"
|
||||
#include "audio_device.h"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wifi_board.h"
|
||||
#include "boards/wifi_board.h"
|
||||
#include "box_audio_device.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ml307_board.h"
|
||||
#include "boards/ml307_board.h"
|
||||
#include "box_audio_device.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ml307_board.h"
|
||||
#include "boards/ml307_board.h"
|
||||
#include "box_audio_device.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wifi_board.h"
|
||||
#include "boards/wifi_board.h"
|
||||
#include "box_audio_device.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
110
main/boards/ml307_board.cc
Normal file
110
main/boards/ml307_board.cc
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "ml307_board.h"
|
||||
#include "application.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
#include <ml307_http.h>
|
||||
#include <ml307_ssl_transport.h>
|
||||
#include <web_socket.h>
|
||||
|
||||
static const char *TAG = "Ml307Board";
|
||||
|
||||
static std::string csq_to_string(int csq) {
|
||||
if (csq == -1) {
|
||||
return "No network";
|
||||
} else if (csq >= 0 && csq <= 9) {
|
||||
return "Very bad";
|
||||
} else if (csq >= 10 && csq <= 14) {
|
||||
return "Bad";
|
||||
} else if (csq >= 15 && csq <= 19) {
|
||||
return "Fair";
|
||||
} else if (csq >= 20 && csq <= 24) {
|
||||
return "Good";
|
||||
} else if (csq >= 25 && csq <= 31) {
|
||||
return "Very good";
|
||||
}
|
||||
return "Invalid";
|
||||
}
|
||||
|
||||
|
||||
Ml307Board::Ml307Board() : modem_(ML307_TX_PIN, ML307_RX_PIN, 4096) {
|
||||
}
|
||||
|
||||
void Ml307Board::StartNetwork() {
|
||||
auto& application = Application::GetInstance();
|
||||
auto& display = application.GetDisplay();
|
||||
display.SetText(std::string("Wait for network\n"));
|
||||
int result = modem_.WaitForNetworkReady();
|
||||
if (result == -1) {
|
||||
application.Alert("Error", "PIN is not ready");
|
||||
return;
|
||||
} else if (result == -2) {
|
||||
application.Alert("Error", "Registration denied");
|
||||
return;
|
||||
}
|
||||
|
||||
// Print the ML307 modem information
|
||||
std::string module_name = modem_.GetModuleName();
|
||||
std::string imei = modem_.GetImei();
|
||||
std::string iccid = modem_.GetIccid();
|
||||
ESP_LOGI(TAG, "ML307 Module: %s", module_name.c_str());
|
||||
ESP_LOGI(TAG, "ML307 IMEI: %s", imei.c_str());
|
||||
ESP_LOGI(TAG, "ML307 ICCID: %s", iccid.c_str());
|
||||
}
|
||||
|
||||
void Ml307Board::StartModem() {
|
||||
auto& display = Application::GetInstance().GetDisplay();
|
||||
display.SetText(std::string("Starting modem"));
|
||||
modem_.SetDebug(false);
|
||||
modem_.SetBaudRate(921600);
|
||||
|
||||
auto& application = Application::GetInstance();
|
||||
// If low power, the material ready event will be triggered by the modem because of a reset
|
||||
modem_.OnMaterialReady([this, &application]() {
|
||||
ESP_LOGI(TAG, "ML307 material ready");
|
||||
application.Schedule([this, &application]() {
|
||||
application.SetChatState(kChatStateIdle);
|
||||
StartNetwork();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void Ml307Board::Initialize() {
|
||||
ESP_LOGI(TAG, "Initializing Ml307Board");
|
||||
StartModem();
|
||||
}
|
||||
|
||||
Http* Ml307Board::CreateHttp() {
|
||||
return new Ml307Http(modem_);
|
||||
}
|
||||
|
||||
WebSocket* Ml307Board::CreateWebSocket() {
|
||||
return new WebSocket(new Ml307SslTransport(modem_, 0));
|
||||
}
|
||||
|
||||
bool Ml307Board::GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) {
|
||||
if (!modem_.network_ready()) {
|
||||
return false;
|
||||
}
|
||||
network_name = modem_.GetCarrierName();
|
||||
signal_quality = modem_.GetCsq();
|
||||
signal_quality_text = csq_to_string(signal_quality);
|
||||
return signal_quality != -1;
|
||||
}
|
||||
|
||||
std::string Ml307Board::GetBoardJson() {
|
||||
// Set the board type for OTA
|
||||
std::string board_type = BOARD_TYPE;
|
||||
std::string module_name = modem_.GetModuleName();
|
||||
std::string carrier_name = modem_.GetCarrierName();
|
||||
std::string imei = modem_.GetImei();
|
||||
std::string iccid = modem_.GetIccid();
|
||||
int csq = modem_.GetCsq();
|
||||
std::string board_json = std::string("{\"type\":\"" + board_type + "\",");
|
||||
board_json += "\"revision\":\"" + module_name + "\",";
|
||||
board_json += "\"carrier\":\"" + carrier_name + "\",";
|
||||
board_json += "\"csq\":\"" + std::to_string(csq) + "\",";
|
||||
board_json += "\"imei\":\"" + imei + "\",";
|
||||
board_json += "\"iccid\":\"" + iccid + "\"}";
|
||||
return board_json;
|
||||
}
|
||||
23
main/boards/ml307_board.h
Normal file
23
main/boards/ml307_board.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef ML307_BOARD_H
|
||||
#define ML307_BOARD_H
|
||||
|
||||
#include "board.h"
|
||||
#include <ml307_at_modem.h>
|
||||
|
||||
class Ml307Board : public Board {
|
||||
protected:
|
||||
Ml307AtModem modem_;
|
||||
|
||||
virtual std::string GetBoardJson() override;
|
||||
void StartModem();
|
||||
|
||||
public:
|
||||
Ml307Board();
|
||||
virtual void Initialize() override;
|
||||
virtual void StartNetwork() override;
|
||||
virtual Http* CreateHttp() override;
|
||||
virtual WebSocket* CreateWebSocket() override;
|
||||
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
|
||||
};
|
||||
|
||||
#endif // ML307_BOARD_H
|
||||
104
main/boards/wifi_board.cc
Normal file
104
main/boards/wifi_board.cc
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "wifi_board.h"
|
||||
#include "application.h"
|
||||
#include "system_info.h"
|
||||
#include "builtin_led.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_http.h>
|
||||
#include <tcp_transport.h>
|
||||
#include <tls_transport.h>
|
||||
#include <web_socket.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
#include <wifi_station.h>
|
||||
#include <wifi_configuration_ap.h>
|
||||
|
||||
static const char *TAG = "WifiBoard";
|
||||
|
||||
static std::string rssi_to_string(int rssi) {
|
||||
if (rssi >= -55) {
|
||||
return "Very good";
|
||||
} else if (rssi >= -65) {
|
||||
return "Good";
|
||||
} else if (rssi >= -75) {
|
||||
return "Fair";
|
||||
} else if (rssi >= -85) {
|
||||
return "Poor";
|
||||
} else {
|
||||
return "No network";
|
||||
}
|
||||
}
|
||||
|
||||
void WifiBoard::StartNetwork() {
|
||||
auto& application = Application::GetInstance();
|
||||
auto& display = application.GetDisplay();
|
||||
auto& builtin_led = BuiltinLed::GetInstance();
|
||||
|
||||
// Try to connect to WiFi, if failed, launch the WiFi configuration AP
|
||||
auto& wifi_station = WifiStation::GetInstance();
|
||||
display.SetText(std::string("Connect to WiFi\n") + wifi_station.GetSsid());
|
||||
wifi_station.Start();
|
||||
if (!wifi_station.IsConnected()) {
|
||||
application.Alert("Info", "Configuring WiFi");
|
||||
builtin_led.SetBlue();
|
||||
builtin_led.Blink(1000, 500);
|
||||
auto& wifi_ap = WifiConfigurationAp::GetInstance();
|
||||
wifi_ap.SetSsidPrefix("Xiaozhi");
|
||||
wifi_ap.Start();
|
||||
// Wait forever until reset after configuration
|
||||
while (true) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WifiBoard::Initialize() {
|
||||
ESP_LOGI(TAG, "Initializing WifiBoard");
|
||||
}
|
||||
|
||||
Http* WifiBoard::CreateHttp() {
|
||||
return new EspHttp();
|
||||
}
|
||||
|
||||
WebSocket* WifiBoard::CreateWebSocket() {
|
||||
std::string url = CONFIG_WEBSOCKET_URL;
|
||||
if (url.find("wss://") == 0) {
|
||||
return new WebSocket(new TlsTransport());
|
||||
} else {
|
||||
return new WebSocket(new TcpTransport());
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiBoard::GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) {
|
||||
if (wifi_config_mode_) {
|
||||
auto& wifi_ap = WifiConfigurationAp::GetInstance();
|
||||
network_name = wifi_ap.GetSsid();
|
||||
signal_quality = -99;
|
||||
signal_quality_text = wifi_ap.GetWebServerUrl();
|
||||
return true;
|
||||
}
|
||||
auto& wifi_station = WifiStation::GetInstance();
|
||||
if (!wifi_station.IsConnected()) {
|
||||
return false;
|
||||
}
|
||||
network_name = wifi_station.GetSsid();
|
||||
signal_quality = wifi_station.GetRssi();
|
||||
signal_quality_text = rssi_to_string(signal_quality);
|
||||
return signal_quality != -1;
|
||||
}
|
||||
|
||||
std::string WifiBoard::GetBoardJson() {
|
||||
// Set the board type for OTA
|
||||
auto& wifi_station = WifiStation::GetInstance();
|
||||
std::string board_type = BOARD_TYPE;
|
||||
std::string board_json = std::string("{\"type\":\"" + board_type + "\",");
|
||||
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() + "\",";
|
||||
}
|
||||
board_json += "\"mac\":\"" + SystemInfo::GetMacAddress() + "\"}";
|
||||
return board_json;
|
||||
}
|
||||
20
main/boards/wifi_board.h
Normal file
20
main/boards/wifi_board.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef WIFI_BOARD_H
|
||||
#define WIFI_BOARD_H
|
||||
|
||||
#include "board.h"
|
||||
|
||||
class WifiBoard : public Board {
|
||||
protected:
|
||||
bool wifi_config_mode_ = false;
|
||||
|
||||
virtual std::string GetBoardJson() override;
|
||||
|
||||
public:
|
||||
virtual void Initialize() override;
|
||||
virtual void StartNetwork() override;
|
||||
virtual Http* CreateHttp() override;
|
||||
virtual WebSocket* CreateWebSocket() override;
|
||||
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
|
||||
};
|
||||
|
||||
#endif // WIFI_BOARD_H
|
||||
Reference in New Issue
Block a user