HarringayMakerSpace / ESP-Now

ESP-Now Examples
301 stars 48 forks source link

ESP-Now does work with WiFi #11

Open WindyYam opened 5 years ago

WindyYam commented 5 years ago

First good code for ESP-now functions

Recently I have tested with a 1-master 1-slave setup, master connected to WiFi ap. as long as ESP-Now and WiFi operate at the same wifi channel, they both do work.

since ap channel is determined by ap gateway, the only thing we can change is ESP-Now channel. just connect to ap, then set the slave the same channel as the master's wifi_get_channel().

this could be done with a default init channel, after master connected ap, do the following on master:

wifi_channel = wifi_get_channel(); wifi_set_channel(default_channel); for (int i = 0; i < 5; i++) { //tell slave to switch wifi_channel } wifi_set_channel(wifi_channel);

on the slave, do: in the recv callback: wifi_set_channel(wifi_channel); //the wifi_channel is from master

Edit: after some experiments wifi_set_channel only last a few seconds at slave side, the only stable way to change channel on slave is by setting AP config wifi_softap_set_config_current(conf)

torntrousers commented 5 years ago

How reliably does this actually work for you? I've been trying it but although sometimes messages do get through but many others are not received, whereas with Wifi off messages get received quite reliably. I'll keep trying it out but I'd be interested if you or others have had good success with this or also just get some messages received when Wifi is on.

WindyYam commented 5 years ago

I've had some try'n'error and the system work without any problem. but the anonying part is that i have to set both master and slave to station+ap in order to set the channel by wifi_softap_set_config_current() in my new setup the master do the espnow mac registration and connect to the AP then broadcast it's wifi channel by ssid. slave scan for it and set it's channel by wifi_softap_set_config_current(). after these step the espnow system could work properly. In my most failure tries they do not work just because their channels are not the same. You could print their channel at run time constantly with wifi_get_channel to see if they are right. the wifi_set_channel is not a reliable way to set channel.