libretiny-eu / libretiny

PlatformIO development platform for IoT modules
http://docs.libretiny.eu/
MIT License
382 stars 55 forks source link

Added ability to specify WPA2 PSK in place of WiFi password #276

Open devgs opened 2 months ago

devgs commented 2 months ago

This is a feature that's already supported by ESP devices. However, in case of Tuya devices (at least for BK7231N) it's invaluable as it can reduce the connect time by 4 seconds or more. It's all because in order to start negotiation, each client is required to derive the PSK that is a function of SSID and Password. And it's a deterministic function: it can be calculated once, stored somewhere and used many times for the same pair of SSID and Password. Actually, looking at BDK it's obvious that native firmware does exactly that. It even has a caching system that from looks of it is capable of storing multiple records.

Saving and restoring connection parameters is a more complex feature that is not implemented by this PR. However, it can now be easily done with the fixes made in bk_wlan_start_sta_adv_fix. This PR allows user to manually pre-calculate PSK for each of hist networks and use them instead of plain passwords in YAML. Which already works with ESP32/ESP8266, but raises Passphrase too long error in LibreTiny: all due to BDK limitations that are now circumvented.

On my cb3s based door sensor, using fast_connect: true with pre-calculated PSK I have managed to achieve a boot-to-mqtt-and-sleep time that is faster than the original firmware (probably because of higher latency): around 1.25-1.40s. Using a fixed BSSID and channel can push it down 1s or less.

Additionally, this PR removes the need for two supplemental config structs in WiFiClass, STA_CFG and STA_ADV_CFG, in favor of the latter.

More info on PSK can be found here: https://jorisvr.nl/wpapsk.html