WiFi.setCredentials problem using String

Hi,

I am new here and I have been trying to experiment with a core.

Running into this problem and could not find any answer on the web. I hope someone can help me.

I am trying to set WiFi network via a program.
All parameters SSID and KEY are stored in Strings called ssid and key

When compiling WiFi.setCredentials(ssid, key, WPA); per example
I will get this message: No matching function for call to “WiFiClass::setCredentials(String&, String&, int)”

However, if I were to do WiFi.setCredentials(“MY_SSID”, “MY_NETWORK_KEY”, WPA); then it will work fine.

What did I do wrong?

Thanks,

Kim

Welcome to the community!

If you could share your code, it’d be a whole lot easier to find where the problem may lie.

@Kiminator, setCredentials requires the following arguments:

setCredentials(const char *ssid, const char *password, unsigned long security)

So ssid and password have to be const char pointers, not strings. In other words, having:

const char mySSID[] = "ssidForMe";
const char password[] = "notgonnatell";
WiFi.setCredentials(mySSID, password, WPA);

will work because each item is an array of chars. So is putting constants in quotes:

WiFi.setCredentials("ssidForMe", "notgonnatell", WPA);

:smile:

1 Like

Or as an alternative you could use the c_str() methode of the String class.

1 Like

Thank-you all for your prompt reply.
My apology for not being as prompt.
Just tried out your suggestions, both were very valuable for me to solve my problem.
Now the function WiFi.setCredentials works for me.

If I may, can I ask this follow up question?

Is there a way to get the WiFi credentials (all, SSID (there is a function for this WiFi.SSID() - so that one is ok), but the KEY and the AUTH method) of the existing setup in the Core?
The reason why I am asking this is for reverting back to the old parameters in case the new setting won’t work. Example: if I set WiFi.credentials(“NewNetwork”, “NewKey”, “NewAuth”)… if not connected properly I would like to revert back to the old parameters (having of course saved the old ones somewhere). But I don’t know how to get the old KEY or the old AUTH type.

Regards,

Kim

The Core (actually the CC3000 chip) can store up to 7 credentials, and will try them all to find a working one. So if it turns out your new ones don’t work, it’ll try the next one.

Hi Moors,

Thanks.
As you saw in my other post for resetting to factory default. It does not seem to be so. As my Core was working until I tried to set to AUTH = WEP. It could not connect to the network - neither the WEP nor the previous one (WPA).

Kim

In case we reach the limit of 7 how the firmware manage to add a new configuration ?
Is the behavior the same from Core Code and with SmartConfig ?

The credentials are stored in a circular buffer. If you add an eighth set the first one will be replaced by it (at least in theory ;-))

And since this is part of the CC3000 WiFi chip it’s the same all over the system.

1 Like

I've tried to use c_str() method but does'nt work for me :frowning:
This is my test code:

    String ssds = "iPhone6";
    String psws = "mm2014mm" ;
    char mySSID[ssds.length()];
    char password[psws.length()];
    strcpy(mySSID, ssds.c_str());
    strcpy(password, psws.c_str());
    WiFi.setCredentials(mySSID, password, WPA2);

whats wrong ?

What exactly doesn’t work?

Have you confirmed that your String objects and the char-arrays actually contain your expected values (e.g. Serial.println())?

And you should allow for one extra byte (zero-terminator) in your char-arrays.

if i use credential directly:

WiFi.setCredentials("iPhone6", "mm2014mm", WPA2);

The wifi connection start, using previous code does’nt start…

try this and see if it works… i remember having to do something similar with coreID a while back

String ssds = "iPhone6";
String psws = "mm2014mm" ;
char mySSID[ssds.length()];
char password[psws.length()];
ssds.toCharArray(mySSID,ssds.length());
psws.toCharArray(password,psws.length());
WiFi.setCredentials(mySSID, password, WPA2);

@blondie63, have you actually read and understood my post???
Have you tried Serial.print()?
Have you understood what I meant with the extra byte?
Have you read the post from @peekay123, near the top of this thread?

I guess you had several people commenting on other threads to engage your own brain and not only rely on the crowd.

@Hootie81, while toCharArray() would help with copying, it would not actually help the main problem of possible memory corruption (where the effect, if it causes a crash or not, might depend on the length of the string literals due to the compiler placing variables on 2/4 byte boundaries - these errors will be a bitch to track down, if you happen to change the literals - first it works and next it doesn’t, and “no one” knows why!).
For your use I’d guess the easiest way would be to just forget about the char-array and use c_str() in the call to setCredentials() directly.
But to understand the length problematic, @blondie63 should stick with this aproach to get it working and take something home from it.

ok i understood how to use char & strings & extrabyte on C
I've done many test, this code is working fine:

    const char mySSID[] = "iPhone 6 plus Mauro";
    const char password[] = "mm2014mm";
    WiFi.setCredentials(mySSID, password, WPA2);

but i don't understand how to set mySSID using a string coming from a function like get_myssid().. :frowning:

To take it step by step.

I say your char arrays are too short. You know why?

2 Likes

yes you have to write:

String ssds = "iPhone6";
String psws = "mm2014mm" ;
char mySSID[ssds.length()+1];
char password[psws.length()+1]; 

char array need a space for \0

1 Like

Exactly, that was one of the reasons (possibly the main) why your code above didn’t work.

To see how this actually was the problem, you should try my suggestion to print out the contents of your ssds, psws, mySSID and password before you added the +1.

Could you post your print out results and an interpretation of it too?


It’s really interesting if you do and you might learn a lot from it - e.g. if you had your pwd just one char longer it would have worked right away, but still wouldn’t be safe.

1 Like

ok, but now how can i assign ssds value to mySSID ?
this code does’nt work:

String ssds = "iPhone6";
char mySSID[ssds.length() + 1];
mySSID[0] = ssds.c_str();

Try sticking with your first approach - using strcpy(), as you seem to know this way already - at first to understand what's going.

Don't move on before you actually got the hang of it.

And the way you tried to do this

suggests you're not there, yet.


BTW, I've already pointed out how to use c_str() in one of my previous posts :wink:

and

The compiler errors would help you to understand why things don't work some way and give you clues of how to change your code.

2 Likes