Declaring multiple wifi credentials with structs

Hi everyone, i have been coding multiple proyects with 3 friends in multiple locations. a regular issue was changing wifi credentials every time someone wants to connect the esp32 to his local network.

As a solution i am trying to create a wifi header that contains a different wifi connection function for every given situation. a different header stores the wifi credentials, both header would then be used in all the projects we work together.

my idea was to create a structure structured to store the wifi variables required as shown below, then only pass the correct structure depending where you are. for each place we would have variables already hardcoded. this header only stores the wifi credentials, is intended to be changed is necessary.

More in details, the whole concept here is when we call the wifi initialization function, is already prepared to receive a struct containing everything, as all the struct have the same format, the function will then substract the ssid, password, ip etc and call the wifi.config and wifi.begin functions and start the wifi connection with the credentials desired. this avoids us to change multiple code lines and just change the struct sent as a parameter. i have yet not programmed this as i encountered this issue in the process.

#include "WiFi.h"

void credentials_settings();

struct wifiCredentialsFixedIP {
    char *ssid;
    char *password;
    IPAddress local_IP();
    IPAddress gateway();
    IPAddress subnetMask();
    IPAddress primaryDNS();
    IPAddress secondaryDNS();
};

wifiCredentialsFixedIP wifiCredentialsWIP_Oficina;

void credentials_settings(){
    wifiCredentialsWIP_Oficina.ssid = "";
    wifiCredentialsWIP_Oficina.password = "";
    //-----------problem here----------
    wifiCredentialsWIP_Oficina.local_IP (192,168,0,99);
    wifiCredentialsWIP_Oficina.gateway (192,168,0,1);
    wifiCredentialsWIP_Oficina.subnetMask (255,255,255,0);
    wifiCredentialsWIP_Oficina.primaryDNS (8,8,8,8);
    wifiCredentialsWIP_Oficina.secondaryDNS (8,8,4,4);
    //-----------problem here----------
}

the problem is in all the IPAdress type functions from the WiFi.h library. when i try to set the functions intellisense tells me that there is "too many arguments in function call". im not sure if the problem is on how i declared the struct or on how i am passing the parameters to the IPAddress type functions. maybe i am taking the wrong aproach on how to solve this issue and this functions are not intended to be stored or something.

you can create a table of wifi parameters.
there ia a wifi scan routine, scanNetworks() that returns an array of networks that can searched for on matching an ssid value in the table and then use those credentials to connect

#include "WiFi.h"

struct wifiCredentialsFixedIP {
    char *ssid;
    char *password;
}

creentialTbl [] = {
    { "ssid1", "password1" },
    { "ssid2", "password2" },
    { "ssid3", "password3" },
};

corrected above

A struct is very similar to a class, except that all members are public by default (instead of private in a class)

struct wifiCredentialsFixedIP {
    char *ssid;
    char *password;

Those first two are pointers

    IPAddress local_IP();

Because of the parentheses, this is a method, not a field. It declares that the function takes no parameters and returns an IPAddress. There is no function/method body, which is apparently allowed as long as you don't actually try to call it. Then the linker will complain. So it should be

    IPAddress local_IP;

When you actually try to call it -- not your intention, but that's what it is

    wifiCredentialsWIP_Oficina.local_IP (192,168,0,99);

The compiler tries to match the number of arguments and so you get that "too many" error. If you remove all the arguments there, then the compiler finds its match and is OK with it. But again, the linker will fail because there is no function body.

To assign it, you can avoid repeating the class name by using brace initialization. But you also need an equal sign for that assignment

    wifiCredentialsWIP_Oficina.local_IP = {192,168,0,99};

The compiler knows the type of the thing you are trying to assign to, so it finds a matching constructor that takes four integers (uint8_t in this case)

So in a way, this case turned out to be about punctuation.

You might also consider creating some values to reuse

IPAddress subnetMask24{255,255,255,0};
IPAddress googleDNS1{8,8,8,8};
IPAddress googleDNS2{8,8,4,4};
wifiCredentialsFixedIP wifiCredentialsWIP_Oficina;

and then

    wifiCredentialsWIP_Oficina.subnetMask = subnetMask24;
    wifiCredentialsWIP_Oficina.primaryDNS = googleDNS1;
    wifiCredentialsWIP_Oficina.secondaryDNS = googleDNS2;

Because the fields are not pointers, you don't save any memory -- you might actually use a little -- but it's arguably clearer.

Seems like the intent is to have five IPAddress fields; so removing the parentheses from the struct definition, you could have the table of parameters, using the nested brace initialization, as you had it before.

Oh, and since the fields of IPAddress total only four bytes, there is no memory savings when using a pointer (on esp32). It may or may not be appropriate to use shared instances. A pointer is certainly less safe.

If you want static IP addresses set them in the router not on your board. Most (some? all?) routers allow you to lock a particular IP address to a MAC address.

I have no comments about the other things you are trying to do.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.