Page 1 of 1

WiFi cannot change channel after unsuccessful wifi connection

Posted: Tue Mar 18, 2025 12:32 pm
by HelterSkelter
Hello,

I have an issue with changing WiFi channels if the ESP32 does not connect to the Wifi. I have made a code to illustrate my issue:

#include <WiFi.h>
#include <esp_wifi.h>

const char* ssid = "Test";
const char* password = "Test";
#define WIFI_CHANNEL 8

void ChangeChannel(){
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);

Serial.printf("Channel after ChangeChannel: %u\n", WiFi.channel());
}

void setup() {
// put your setup code here, to run once:

Serial.begin(115200);

WiFi.persistent(true);
WiFi.mode(WIFI_MODE_APSTA);

Serial.printf("Channel after Mode Set: %u\n", WiFi.channel());

ChangeChannel();

WiFi.begin(ssid,password,WIFI_CHANNEL); // Attempts to connect using stored credentials

int counter = 0;

Serial.println("********************CONNECTING********************");

Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED && counter<5) {
Serial.print('.');
delay(1000);
counter ++;
}

if (WiFi.status() == WL_CONNECTED) Serial.printf("\n Connected \n");
else Serial.printf("\n Not Connected \n");

Serial.printf("Channel after connection: %u\n", WiFi.channel());

ChangeChannel();

WiFi.disconnect();

Serial.println("********************DISCONNECTING********************");

Serial.printf("Channel after disconnection: %u\n", WiFi.channel());

ChangeChannel();
}

void loop() {

}

In the code, I tried to change the WiFi channel 3 times:

1. First time after WiFi.mode(WIFI_MODE_APSTA);
2. Second time after the WiFi connection attempt
3. Third time after disconnecting from WiFi.

When WiFi is available I get this on a serial monitor:

Channel after Mode Set: 5
Channel after ChangeChannel: 8
********************CONNECTING********************
Connecting to WiFi ....
Connected
Channel after connection: 1
Channel after ChangeChannel: 1
********************DISCONNECTING********************
Channel after disconnection: 1
Channel after ChangeChannel: 8

The channel changes normally after WiFi.mode(WIFI_MODE_APSTA) and after disconnect (as it should).

The issue arises when there is no Wifi available. Then I get this on the serial monitor:

Channel after Mode Set: 5
Channel after ChangeChannel: 8
********************CONNECTING********************
Connecting to WiFi ....
Connected
Channel after connection: 13
Channel after ChangeChannel: 13
********************DISCONNECTING********************
Channel after disconnection: 13
Channel after ChangeChannel: 13

As you can see from the serial monitor, the channel does not want to change after WiFi.begin(ssid,password,WIFI_CHANNEL) if the WiFi is not connected .

Are there any solutions available for this?

Thank you in advance,
Marin

Re: WiFi cannot change channel after unsuccessful wifi connection

Posted: Fri Mar 21, 2025 10:52 am
by ahsrabrifat
Try to stop the WiFi completely before changing the channel:

Code: Select all


WiFi.disconnect(true, true);  // Disconnect and erase credentials
esp_wifi_stop();              // Stop the WiFi driver
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
esp_wifi_start();             // Restart the WiFi driver



Re: WiFi cannot change channel after unsuccessful wifi connection

Posted: Mon Mar 24, 2025 12:26 pm
by HelterSkelter
Try to stop the WiFi completely before changing the channel:

Code: Select all


WiFi.disconnect(true, true);  // Disconnect and erase credentials
esp_wifi_stop();              // Stop the WiFi driver
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
esp_wifi_start();             // Restart the WiFi driver



That works,

Thank you very much :)

Re: WiFi cannot change channel after unsuccessful wifi connection

Posted: Tue Mar 25, 2025 1:40 pm
by ahsrabrifat
Try to stop the WiFi completely before changing the channel:

Code: Select all


WiFi.disconnect(true, true);  // Disconnect and erase credentials
esp_wifi_stop();              // Stop the WiFi driver
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
esp_wifi_start();             // Restart the WiFi driver



That works,

Thank you very much :)

Thanks for the update. You're welcome. :)