Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/puara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,18 @@ bool Puara::get_StaIsConnected()
{
return g_puara.wifi.get_StaIsConnected();
}

bool Puara::set_wifi_tx_power(int8_t max_tx_power)
{
return g_puara.wifi.set_wifi_tx_power(max_tx_power);
}

bool Puara::set_wifi_channels(int8_t primary, wifi_second_chan_t second)
{
return g_puara.wifi.set_wifi_channels(primary, second);
}

bool Puara::get_wifi_channels(int8_t& primary, wifi_second_chan_t& second)
{
return g_puara.wifi.get_wifi_channels(primary, second);
}
4 changes: 4 additions & 0 deletions src/puara.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class Puara

double getVarNumber(std::string varName);
std::string getVarText(std::string varName);

bool set_wifi_tx_power(int8_t max_tx_power);
bool set_wifi_channels(int8_t primary, wifi_second_chan_t second);
bool get_wifi_channels(int8_t& primary, wifi_second_chan_t& second);
};

#endif
69 changes: 69 additions & 0 deletions src/puara_wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void WiFi::wifi_init()
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20);
esp_wifi_set_bandwidth(WIFI_IF_AP, WIFI_BW_HT20);

// Set device hostname
esp_err_t setname = esp_netif_set_hostname(ap_netif, config.dmiName.c_str());
if(setname != ESP_OK)
Expand Down Expand Up @@ -274,4 +277,70 @@ bool WiFi::get_StaIsConnected()
{
return StaIsConnected;
}
/*
Set maximum transmitting power after WiFi start.
Value will be mapped to the max_tx_power of the struct wifi_country_t.
Param power unit is 0.25dBm
Range is [8, 84] corresponding to 2dBm - 20dBm.
Mapping Table {Power, max_tx_power} = {
{8, 2}, {20, 5}, {28, 7}, {34, 8}, {44, 11}, {52, 13},
{56, 14}, {60, 15}, {66, 16}, {72, 18}, {80, 20} }

Relationship between set value and actual value as follows:
{set value range, actual value} = {
{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, {[34, 43],34}, {[44, 51],44},
{[52, 55],52}, {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, {[72, 79],72},
{[80, 84],80}
}.
*/
bool WiFi::set_wifi_tx_power(int8_t max_tx_power) {
esp_err_t result = esp_wifi_set_max_tx_power(max_tx_power);
if(result == ESP_OK) {
return true;
} else {
ESP_LOGE(PUARA_TAG,"set_wifi_tx_power: Failed to set WiFi TX power. Error code: %d", result);
return false;
}
}

/*
Set primary/secondary channel of device.
(Puara now uses HT20 as default)
primary --
for HT20, primary is the channel number
for HT40, primary is the primary channel
second --
for HT20, second is ignored,
for HT40, second is the second channel

The channel info set by this API will not be stored in NVS.
If you want to remember the channel used before WiFi stop,
you need to call this API again after WiFi start, or you can call
esp_wifi_set_config() to store the channel info in NVS.
*/
bool WiFi::set_wifi_channels(int8_t primary, wifi_second_chan_t second) {
esp_err_t result = esp_wifi_set_channel(primary, second);
if(result == ESP_OK) {
return true;
} else {
ESP_LOGE(PUARA_TAG,"set_wifi_channels: Failed to set WiFi channels. Error code: %d", result);
return false;
}
}

/*
Get the primary/secondary channel of device.
primary -- store current primary channel
second -- [out] store current second channel
*/
bool WiFi::get_wifi_channels(int8_t& primary, wifi_second_chan_t& second) {
esp_err_t result = esp_wifi_get_channel((uint8_t*)&primary, &second);
if(result == ESP_OK) {
return true;
} else {
ESP_LOGE(PUARA_TAG,"get_wifi_channels: Failed to get WiFi channels. Error code: %d", result);
return false;
}
}

} // namespace PuaraAPI
6 changes: 5 additions & 1 deletion src/puara_wifi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ struct WiFi
static void sta_event_handler(
void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
void wifi_init();

bool set_wifi_tx_power(int8_t max_tx_power);
bool set_wifi_channels(int8_t primary, wifi_second_chan_t second);
bool get_wifi_channels(int8_t& primary, wifi_second_chan_t& second);

const int wifi_connected_bit = BIT0;
const int wifi_fail_bit = BIT1;
EventGroupHandle_t s_wifi_event_group;

wifi_config_t wifi_config_sta;
wifi_config_t wifi_config_ap;
short int connect_counter;

};
}
Loading