HarringayMakerSpace / ESP-Now

ESP-Now Examples
300 stars 48 forks source link

Gateway does not receive data in WIFI_STA #5

Closed cmorda closed 6 years ago

cmorda commented 6 years ago

https://github.com/HarringayMakerSpace/ESP-Now/blob/1db590d1bd6d99b8ae69e905ea5beb4b63695dde/EspNowWatsonRestartingGateway/EspNowWatsonRestartingGateway.ino#L70

Line 70 has the ESP in WIFI_STA mode, however, I cannot get it to receive data unless I change it to WIFI_AP mode.

torntrousers commented 6 years ago

Thats interesting. I wonder if something has changed in the ESP8266 SDK. What version of the Arduino ESP8266 core are you using and I'll give it a try?

cmorda commented 6 years ago

It is the latest, 2.4.1.

I'm not beyond accepting responsibility for it not working but running the unmodified sketches on two ESP did not work for me.

However, thinking about it now, I found an option to erase the WiFi settings flash memory during programming. That option is not available on my current workstation (not the one I was using when I encountered the problem). I wonder if old data from prior sketches might affect things.

cmorda commented 6 years ago

I am also having trouble setting the MAC address on the gateway. It seems to work on the first round but after connecting to WiFi and restarting, it reverts to the default MAC. This is in my code, based heavily on yours. I haven't tested that functionality on yours.

torntrousers commented 6 years ago

I have to admit I also had trouble with the mac address after restart, and raised an issue about it - https://github.com/esp8266/Arduino/issues/3024. But that took so long I never progressed it. However, it seems intermittent and sometimes that just goes away and works. So now I went back and had a look at that and wrote a minimal sketch to demonstrate the problem and with the latest SDK it worked. Then I went back trying different older SDK versions and it worked on them too, then trying the latest again and it stopped working and now doesn't work on any version I try. Mystifying. It seems sometimes flashing the sketch does something that breaks it and its very hard to get it working again. I think its a bug somewhere in the way wifi settings get persisted in flash. The way that seems to fix it is to completely erase the entire flash with the esptool - esptool --port COM4 erase_flash and then update the skecth to add WiFi.persistent(false); before doing any Wifi stuff.

torntrousers commented 6 years ago

See the update in #3024 - to fix the setting the mac address in initVariant the Wifi mode must be either WIFI_AP or WIFI_AP_STA before the restart. So a simple work around is to just add WiFi.mode(WIFI_STA) right before doing the ESP.restart().

I think you're right about line 70 should be setting the mode to WIFI_AP. All the other sketches I have with esp-now use mode WIFI_AP for the receiver. I wonder if I didn't test this example properly before uploading it to Github.

torntrousers commented 6 years ago

Actually, it also works putting the WiFi.mode(WIFI_STA) just before the wifi_set_macaddr in initVariant

cmorda commented 6 years ago

That works perfectly. Thanks!

In order to hide the network, and secure it against some possible vulnerability discovered in the future, I added the following below the WiFi.mode line, on line 35:

WiFi.softAP("someSSID", "someKey", 1, true);