chrispitzer
chrispitzer

Reputation: 1069

How do I change my wifi channel so I can send/receive from esp-now?

I have an ESP-IDF based project that talks over ESP-NOW.

I also want it to periodically hop on WiFi to check for updates.

When I hop on WiFi, the channel of the antenna changes, no surprises there. So my ESP-NOW mesh won't work while I'm on WiFi. (Maybe I'll address that someday, but not today)

However, when I try to send a message on ESP-NOW, I get the same error message whether I'm currently on WiFi, or whether I've already disconnected from WiFi using esp_wifi_disconnect().

The error message looks like this:

E (2375500) ESPNOW: Peer channel is not equal to the home channel, send fail!

Any idea how I can change the channel back to 1 so my ESP-NOW traffic can resume?

In case it's useful to see a larger trace of the console, here you go....

I (2365480) MESH: Sending successful!
I (2365480) MESH: ESP-NOW send callback. Status: Success

  ^ Note, that's what it looks like when mesh sends successfully...

I (2369590) WIRELESS: in connectToMyWiFi()
I (2369590) WIRELESS: Wifi connect response: ESP_OK

  ^ Here we're calling in to connect the wifi... We got back `ESP_OK` to our call to `esp_wifi_connect()`

I (2370490) MESH: Sending successful!
I (2370490) MESH: ESP-NOW send callback. Status: Success

  ^ The mesh squeaked out one last message before WiFi spun up...

I (2372430) wifi:ap channel adjust o:1,1 n:11,2
I (2372430) wifi:new:<11,0>, old:<1,1>, ap:<11,2>, sta:<11,0>, prof:1
I (2372430) wifi:state: init -> auth (b0)
I (2372440) wifi:state: auth -> assoc (0)
I (2372440) wifi:state: assoc -> run (10)
I (2372450) wifi:connected with The Lighthouse, aid = 7, channel 11, BW20, bssid = e0:63:da:58:96:1d
I (2372450) wifi:security: WPA2-PSK, phy: bgn, rssi: -53
I (2372460) wifi:pm start, type: 1
I (2372470) wifi:AP's beacon interval = 102400 us, DTIM period = 1

  ^ That's wifi doing stuff. Looks like we connected.

E (2375500) ESPNOW: Peer channel is not equal to the home channel, send fail!
E (2375500) MESH: esp_now send error

  ^ Yup - we now failed to send the ESP-NOW message. Makes sense, we're on WiFi on channel 11 right now. ESP-NOW wants to be on channel 1.

I (2379480) WIRELESS: in disconnectFromMyWiFi()
I (2379490) wifi:state: run -> init (0)
I (2379490) wifi:pm stop, total sleep time: 0 us / 7027038 us

I (2379500) wifi:new:<11,0>, old:<11,0>, ap:<11,2>, sta:<11,0>, prof:1
I (2379500) WIRELESS: Wifi disconnect response: ESP_OK

  ^ We just called `esp_wifi_disconnect()` and we can see the response, `ESP_OK`

E (2380510) ESPNOW: Peer channel is not equal to the home channel, send fail!
E (2380510) MESH: esp_now send error

  ^ We're still failing on our ESP-NOW messages.

... failure continues ...

Thank you!

Upvotes: -1

Views: 2625

Answers (2)

Son Goku
Son Goku

Reputation: 11

You can use this function:

void Set_Wifi_Channel(byte _primaryChan)
{
  ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true));
  wifi_second_chan_t secondChan = WIFI_SECOND_CHAN_NONE;
  ESP_ERROR_CHECK(esp_wifi_set_channel(_primaryChan, secondChan));
  ESP_ERROR_CHECK(esp_wifi_set_promiscuous(false));
}

Remember that there are 14 wifi channels in the 2.5Ghz band, so you can start sending at channel 1 and wait for (status == ESP_NOW_SEND_SUCCESS) on the OnDataSent event, if fail, try with channel 2 and so on...

Check this code sample, here is a full working code.

Tested on a ESP32-C3 (revision v0.4). Arduino library v2.0.17 ESP-IDF v4.4.7

Saludos Jose Luis

Upvotes: 0

Tam NhiNho
Tam NhiNho

Reputation: 1

ESP-Now requires a WiFI channel to work. you can select a custom channel by Starting the access point with a channel

WiFi.disconnect(); WiFi.mode(WIFI_AP_STA); WiFi.softAP("fakeSSID","fakePasw", channel, true); // hide true

Upvotes: 0

Related Questions