Open lilalukas opened 4 years ago
Have you tested this? I think it's is a very nice and necessary solution indeed, if it works. I am wondering if the channel can be changed on the fly.
We have to keep in mind that ESP-Now is invented to cover longer distances so the chance that the sender (master) is not within reach of the router is not imaginary. In those cases a routine that scans available networks does not work. I am also worried that the sender trying to connect to a Wi-Fi station will be a no-no for battery operated solutions, but I am not sure about the power requirements of ESP-Now, to be honest. Would WiFi.mode(WIFI_MODE_STA); still be necessary?
It works!
bool channelFound = false;
uint8_t channel = 1;
void onDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) {
if (!channelFound && status != ESP_NOW_SEND_SUCCESS) {
tryNextChannel();
}
else {
channelFound = true;
}
}
void tryNextChannel() {
channel = channel % 13 + 1;
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
Serial.println(channel);
}
It needs more refinement as there can be other reasons that a send fails and then you don't want the channel to be changed again, but it's a start.
EDIT: To answer my own question, yes, WiFi.mode(WIFI_MODE_STA) is still necessary. And I should have mentioned that setup() and initWiFi() remain the same, but without the
// int32_t channel = getWiFiChannel(WIFI_SSID);
I am not sure about the power requirements of ESP-Now
you can check in my repository the deep dive into the power requirements of ESPnow together with the time to wake up, gather data, send to receiver and go to sleep
@Leonos
First of all, a thousand apologies for not having replied to you all this time. I have too many projects on the go. :grin:
You are right to point this out. I used this technique on all my sensors spread around the house for almost two years, and it works great. The router can change the channel anytime, and everyone is tuned in automatically.
I should have updated the code on GitHub a while ago, but I completely forgot to. I'll update it as soon as I have time.
You should know that with WIFI_MODE_STA
, the ESP32 periodically enters a power-saving mode and puts its radio communication circuit on standby. In other words, if your sender modules try to transmit their data with ESP-NOW while the receiver’s modem is on standby, then transmissions will inevitably fail. To avoid this, you can force the receiver to keep its radio circuit permanently powered:
WiFi.setSleep(WIFI_PS_NONE);
@homonto
I just looked at your GitHub repo. A great project and very well documented. Congratulations, and thank you!
Hey all,
I can confirm that you have to use the same WiFi-Channel for ESPNow-Sender and ESPNow-to-WiFi-Gateways. If you have multiple routers with the same SSID (e.g. in university or company) and you always want to use the best WiFi (so no hardcoded channel) you have to try to send the message on each channel until you get a success reply.
`
`