Skip to content

Instantly share code, notes, and snippets.

@oldmud0
Last active June 19, 2024 17:13
Show Gist options
  • Save oldmud0/4946228fc2e279932d06b32e0e23968d to your computer and use it in GitHub Desktop.
Save oldmud0/4946228fc2e279932d06b32e0e23968d to your computer and use it in GitHub Desktop.
Maximize Wi-Fi performance on ESP32
constexpr auto WIFI_CHANNEL = 9; // Find a relatively quiet channel
constexpr auto WIFI_DATA_RATE = WIFI_PHY_RATE_24M;
void initFastWifi() {
// Set up Wi-Fi with a faster data rate by disabling AMPDU
// and setting a very fast fixed rate.
// https://www.esp32.com/viewtopic.php?t=9965
// We also need delays to work around races between each step:
// https://esp32.com/viewtopic.php?t=2512
WiFi.disconnect();
vTaskDelay(100 / portTICK_RATE_MS);
WiFi.mode(WIFI_STA);
vTaskDelay(100 / portTICK_RATE_MS);
ESP_ERROR_CHECK(esp_wifi_stop());
vTaskDelay(100 / portTICK_RATE_MS);
ESP_ERROR_CHECK(esp_wifi_deinit());
vTaskDelay(100 / portTICK_RATE_MS);
wifi_init_config_t initConfig = WIFI_INIT_CONFIG_DEFAULT();
initConfig.ampdu_tx_enable = false;
ESP_ERROR_CHECK(esp_wifi_init(&initConfig));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE));
ESP_ERROR_CHECK(esp_wifi_internal_set_fix_rate(WIFI_IF_STA, true, WIFI_DATA_RATE));
ESP_ERROR_CHECK(esp_now_init());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment