Good Evening Everyone,
I'm working on a project that scans for nearby networks and allows the user to choose which network they want to connect to. Because of the size of my screen & need to print instructions, I can only show the user 5 network names at a time. I have conditionals set up to check for errors, and if the scan only finds 5 networks (0-4 index) then great, I have just enough space to print them all. If I find 5 networks or less, my code works great!
My problem is when I scan for networks and the WiFi chip finds more than 5. In that case I need to loop through my list and show just 5 network names at a time. I have tried so many different variations to make this work, I think I'm just tired and missing something obvious. Also, this example is tricky enough that I'm not certain what to Google. All attempts to help are sincerely appreciated!
The part of this code that works well is comprised of two functions that work together. They are below.
// this function scans for networks and if there are more than 5 allows the user to press 'p' to refresh the page showing any remaining unseen networks.
int selectNetwork()
{
bool validSelection = false;
String networkName = "";
// scan for networks:
int availableNetworks = WiFi.scanNetworks();
if (availableNetworks < 1)
{
print("No Wifi connections available");
}
displayNetworkNames(networkName, availableNetworks);
while (!validSelection)
{
char selection = readKeyOrSerial();
switch (selection)
{
case 'p': // user presses 'p' to iterate through 5 networks at a time until they have //seen all network names scanned.
{
validSelection = true;
// not sure if I'll end up calling displayNetworkNames(networkName, availableNetworks); here or another function that operates differently.
break;
}
}
void displayNetworkNames(String networkName, int availableNetworks)
{
setupScreen(); // this function sets up my cursor, font etc...
if (availableNetworks < 1)
{
print("No networks found");
}
if (availableNetworks <= 4) // Exactly 5 networks are found.
{
print("Available Networks: ");
// print the network number and name for each network found:
for (int net = 0; net < availableNetworks; net++)
{
print(String(thisNet + 1));
print(") ");
println(WiFi.SSID(net));
}
println(networkName);
}
// This is my broken code that I'm not sure how to fix...
else // 6 or more networks are found. Loop through our found networks as many times as necessary to print them all 5 at a time.
{
int rowNumber = 5;
int totalIterations = (availableNetworkCount/rowNumber);
do
{
for (int net = 0, totalIterations; net < rowNumber; net++, totalIterations--)
{
print(String(thisNet + 1));
print(") ");
println(WiFi.SSID(net));
wait(1000);
}
println(networkName);
}
while (totalIterations > 0);
}Preformatted text