ESP-NOW specifics [WIFI-2245]

daneast
Posts: 9
Joined: Wed Apr 22, 2020 11:26 am

Re: ESP-NOW specifics [WIFI-2245]

Postby daneast » Fri Apr 24, 2020 7:29 pm

FINALLY got this sorted out. I'm not sure if this is a bug in ESP8266 libraries, or a lack of documentation or proper example code, but the problem is that the ESP8266 was operating in higher channels that put them outside the frequency of the channel the ESP32 was operating on. In code for both ESP32 and ESP8266 I explicitly set the peers to use channel 1.

Code: Select all

    uint8_t broadCastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    esp_now_peer_info_t *peer = (esp_now_peer_info_t *)malloc(sizeof(esp_now_peer_info_t));
    memset(peer, 0, sizeof(esp_now_peer_info_t));
    peer->channel = 1;
    peer->ifidx = ESP_IF_WIFI_STA;
    peer->encrypt = false;
    memcpy(peer->peer_addr, broadCastMac, ESP_NOW_ETH_ALEN);
    esp_now_add_peer(peer);
That works on the ESP32 and it runs on channel 1. However the ESP8266 was somewhere up in channels 8-12. So apparently the peer channel setting either doesn't actually do anything on ESP8266, or it does not set the channel in broadcast mode, or it is buggy. If I set the wifi channel in general then that solves the problem and the ESP8266 runs on channel 1.

Code: Select all

	esp_wifi_start();
	esp_wifi_set_channel(1,1);
Neither the ESP32 nor ESP8266 ESP-NOW demo code sets the channel in that way, so it is somewhat random whether or not they happen to all end up on the same channel or not.

Kaisha
Posts: 42
Joined: Thu Mar 05, 2020 8:59 pm

Re: ESP-NOW specifics [WIFI-2245]

Postby Kaisha » Fri Apr 24, 2020 10:14 pm

My understanding is that the WiFi channel is switched to whatever the current AP you are connected to is using. This can cause a problem if your ESP-NOW code is working on, say channel 1, while the AP wants channel 11. I'm not sure if there's any way around this atm... Maybe someone with more knowledge on this could comment.

It'd be nice if the two (ESP-NOW and WiFi) could work on separate channels, but I don't know if that's possible.

daneast
Posts: 9
Joined: Wed Apr 22, 2020 11:26 am

Re: ESP-NOW specifics [WIFI-2245]

Postby daneast » Fri Apr 24, 2020 10:43 pm

The three ESP8266s I tested with had never been connected to an AP or anything else. I took them out of the package and flashed my ESP-NOW code on them and they all worked and could send and receive to one another, apparently using higher channels. This was ESP-NOW only communication. On one of them today I flashed a test client code that connected to my local AP on channel 6, because I was curious if they were indeed working correctly with standard Wifi. Then I flashed my ESP-NOW code back, and it still could communicate with the other 2 ESP8266s.

Now, again, on the ESP32, it used channel 1 right from the beginning as specified in the peer configuration. So maybe this is a bug that was fixed, or something not shown in the examples where you need to set the wifi channel generally as well.

Kaisha
Posts: 42
Joined: Thu Mar 05, 2020 8:59 pm

Re: ESP-NOW specifics [WIFI-2245]

Postby Kaisha » Sat Apr 25, 2020 12:09 am

I wish this was documented more clearly, and I'd like to know if they intend for WiFi and ESP-NOW to work together, and if so what we need to do pre and post WiFi connection.

zhangyanjiao
Posts: 34
Joined: Mon Aug 28, 2017 3:27 am

Re: ESP-NOW specifics [WIFI-2245]

Postby zhangyanjiao » Mon Apr 27, 2020 12:02 pm

hi Kaisha,
what do you mean by saying
I'd like to know if they intend for WiFi and ESP-NOW to work together
?
Can you explain more ?

Kaisha
Posts: 42
Joined: Thu Mar 05, 2020 8:59 pm

Re: ESP-NOW specifics [WIFI-2245]

Postby Kaisha » Tue Apr 28, 2020 3:54 pm

Its not quite clear in the documentation how ESP-NOW and WiFi access are supposed to work, when they are both being used at the same time. The biggest issue is WiFi channel selection.

When setting up ESP-NOW you are required to supply a WiFi channel. So if I wanted to setup a simple ESP-NOW sensor network I might choose channel 1. This will work fine until I connect to a WiFi AP. Since I don't necessarily have access to the AP settings, the WiFi channel could be different than the ESP-NOW channel (in this example channel 1). If the AP used channel 3 or 4, you'd get some, but not all, of the ESP-NOW packets (some would be lost). Should the AP choose channel 11, pretty much every ESP-NOW packet would be lost.

Of course the initial solution is to pick channel 0 for ESP-NOW, except this time ESP-NOW would only work when all devices are connected to the same AP, at the same time. If you want to use ESP-NOW when a WiFi AP isn't available, you're SOL, since all the devices will not necessarily be on the same channel.

It gets even more complicated if you plan to use ESP-NOW both before a WiFi connection has been established, and after. Since channels have been switched half way through the application.

And there are even more scenarios (multiple APs, ESP-NOW peers using different channels, etc...) where things get even worse when using ESP-NOW with WiFi.

The fundamental problem is, from my understanding, that the ESP-NOW channel is directly tied to the WiFi channel. I don't know if there is some hardware requirement for this, but if there isn't the best solution for everyone is that ESP-NOW operate on a channel that is independent from the current WiFi channel (if one even exists).

Having a connectionless, simple, low-level, broadcast orientated protocol, where one can send out packets and receive them without any significant overhead, enables a whole host of awesome applications. ESP-NOW (particularly with the broadcast address peer) would fit that to a 'T', provided you can work around the channel mess.

Also the broadcast peer (mac address: 0xFFFFFFFFFFFF) should be documented, or perhaps an esp_now_broadcast(const uint8_t *peer_addr, const uint8_t *data, size_t len) function added that simply broadcasts ignoring peer data.

zhangyanjiao
Posts: 34
Joined: Mon Aug 28, 2017 3:27 am

Re: ESP-NOW specifics [WIFI-2245]

Postby zhangyanjiao » Thu Apr 30, 2020 6:52 am

yes, you are right, the ESP-NOW channel is directly tied to the WiFi channel. So we don't suggest you use ESP-NOW and WiFi connection at the same time.

If you want to send ESP-NOW data to all peers, you can set the peer_mac to NULL in `esp_now_send()`.
Refering https://docs.espressif.com/projects/esp ... p_now.html
If the peer_mac is ff:ff:ff:ff:ff:ff, all peers can receive the data also.
Last edited by zhangyanjiao on Tue Jun 02, 2020 6:25 am, edited 2 times in total.

Kaisha
Posts: 42
Joined: Thu Mar 05, 2020 8:59 pm

Re: ESP-NOW specifics [WIFI-2245]

Postby Kaisha » Thu Apr 30, 2020 12:57 pm

I appreciate the response. Now correct me if I'm wrong, but in STA+AP mode (WIFI_MODE_APSTA) the station and AP can be on different channels correct? If so, perhaps have ESP-NOW use the AP channel (which we have direct control over)?

zhangyanjiao
Posts: 34
Joined: Mon Aug 28, 2017 3:27 am

Re: ESP-NOW specifics [WIFI-2245]

Postby zhangyanjiao » Wed May 06, 2020 8:16 am

In STA+AP mode, the STA and AP are in the same mode. If you set softAP to channel 1, then STA connects to an AP on channel 6, the softAP will change to channel 6.

gmag11
Posts: 4
Joined: Fri Nov 20, 2015 5:42 am

Re: ESP-NOW specifics [WIFI-2245]

Postby gmag11 » Sun Jun 19, 2022 5:50 pm

Hi, I've developed a library to use ESP-NOW with Arduino core in both ESP8266 and ESP32. It adds some features as RSSI level information for every packet.

Have a look to it in https://github.com/gmag11/QuickESPNow

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 112 guests