Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multi WiFi saving feature #3705

Closed
wants to merge 13 commits into from
Closed

Added multi WiFi saving feature #3705

wants to merge 13 commits into from

Conversation

JPZV
Copy link
Contributor

@JPZV JPZV commented Jan 18, 2024

(Note: This is a rebase of #3703 with the 0_15 branch)

TL;DR:

This PR adds the Multi-Wifi saving feature, which was requested and discussed in #2845, #2974, #852, #1228, and other issues that I may not found.

How it works:

User's side

For the user I tried to make it as simple as possible: Now the WiFi setting page will have three "new" sections:

  • Connect to a new network: Here the user can scan for a nearby network or add it manually with the option "Other network..."
  • Saved networks: Here the user can manage its saved network by changing the SSID, the Password, or deleting it.
  • Addresses settings: Here is the old DHCP/IP and mDNS settings. Sadly, this will affect to all networks, so, for example, setting a static IP to 192.168.0.64/24 while on networkA, this will be used for every other network, even if the subnet of networkB starts from 192.168.1.0/24. So, DHCP is fully recommended from now on if using multiple networks.

In back-end

Nowadays WLED saves the WiFi setting in two global vars: clientSSID and clientPass. Both are strings (well... Char array). Now, I removed these vars and created three new ones: clientNetsSSID, clientNetsPass and clientSavedNets. The first two are almost the same as the old vars, but now they're Strings Array (or Char Matrix, or Char Array Array, or whatever you want to call it), which can save up to 5 Networks in ESP32, or 3 in ESP8266 (those limits were selected arbitrarily). While clientSavedNets is a Byte that stores how many networks are saved and loaded into memory.

When WLED wants to connect to a network, it first scan for every nearby networks and start looking for the nearest saved network. Then, tries to connect to it, and if it can't, then try with the next near one. Each attempt has like 5 seconds before failing. This amount of time was selected because "Last reconnect too old" message was being fired immediately with anything greater than 5 seconds and with just one network.

Backward compatibility

Now WLED will check if the config file has a network saved with the last scheme, and if there's, then it will add it as the first network and then it'll save the config file with the new scheme

Drawbacks

Because I'm not familiar with Improv and EEPROM saving in WLED, and because of backward compatibility concerns, in both cases it'll set/load the first network. For example:

-  readStringFromEEPROM(  0, clientSSID, 32);
-  readStringFromEEPROM( 32, clientPass, 64);
-  readStringFromEEPROM( 96,      cmDNS, 32);
+  readStringFromEEPROM(  0, clientNetsSSID[0], 32);
+  readStringFromEEPROM( 32, clientNetsPass[0], 64);
+  readStringFromEEPROM( 96,             cmDNS, 32);

This shouldn't be a big issue for most users, except for those who uses Improv/EEPROM and wanted to save multiple WiFi networks.

Also, as mentioned before, now the IP Settings will affect to every network, even if the subnets differs com each others.

Files Changed:

  • cfg.cpp: Now the WiFi settings will be saved as ssid%d and psk%d, where %d goes from 0 to 4, instead of ssid and psk respectively
  • const.h: Added WLED_MAX_SAVED_NETWORKS, SUBPAGE_MAIN_SETTINGS, and SUBPAGE_WIFI_NETWORKS constant
  • data/settings_wifi.htm: Added three new sections: Connect to a new network, Saved networks, and Addresses settings. Also, changed the loadJS() function to letting load more than one JS.
  • data/style.css: Added input[type="password"] to the same input[type="text"] style.
  • fcn_declare.h: Changed the declaration of getSettingsJS to void getSettingsJS(byte subPage, char* dest, byte subSettings);, so it can load sub-settings from the same page.
  • improv.cpp, set.cpp, wled.cpp and wled_eeprom.cpp: Added support for multiple WiFi settings.
  • wled.h:
    • Replaced clientSSID and clientPass global vars with clientNetsSSID and clientNetsPass respectively.
    • Added clientSavedNets global var.
    • Now WLED_WIFI_CONFIGURED will be true if the first network is configured, or if clientSavedNets is greater than zero.
  • wled_server.cpp: Now the function serveSettingsJS will accept the s argument for loading sub-settings from the same page.
    • In case s is zero (default behavior). It'll return the GetV JS function. Otherwise, it'll return the GetW one.
  • xml.cpp: Added support for SUBPAGE_WIFI_NETWORKS sub-setting in SUBPAGE_WIFI page. In this case, it'll return a tuple of three where the first item is the amount of saved networks, while the second item is the maximum amount of networks that can be saved, and the last item is an array of every saved network.

Up to 5 for ESP32 and Up to 3 for ESP8266 based devices
@JPZV JPZV mentioned this pull request Jan 18, 2024
@blazoncek
Copy link
Collaborator

Thank you. Will have a look ASAP.

Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is nice but I find implementation a bit inefficient.
There are multiple areas that need addressing.

I particularly do not like static memory allocation and handling of JSON. Unfortunately I did not delve in JavaScript and settings page due to lack of time. I will comment on those at some later time.

JPZV added 11 commits January 19, 2024 23:17
The only way to change the SSID is by deleting it and then adding it again
Also, improved the way of saving the WiFi into the json config.

Note: Lost backward compatibility with IP settings
Now we don't use Delay() for waiting a connection before trying the next network
Bug 1: Deleting a middle WiFi could lead to delete the wrong WiFi

Bug 2: Deleting a middle WiFi could leave an empty space between the remaining WiFis
@JPZV
Copy link
Contributor Author

JPZV commented Jan 20, 2024

@blazoncek I've implemented all the changes that you requested until now.

Feel free to add more feedback to it.

@blazoncek
Copy link
Collaborator

I am sorry if this looks sublime, but it took me less time to implement than to describe what I wanted.

This is how it should look in UI.
Screenshot 2024-01-20 at 22 58 09
I've implemented vectors in the background so the memory is allocated dynamically for each configuration.

I've also added custom DNS which was long overdue.

I'll push a new branch so that we can compare and only take the best of both versions.

blazoncek added a commit that referenced this pull request Jan 20, 2024
@blazoncek
Copy link
Collaborator

Closed in favour of #3709

@blazoncek blazoncek closed this Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants