Go Down

Topic: new data to a *char variable (Read 74 times) previous topic - next topic

g889ln

Hi guys,

could you please help me to solve the following problem?

In my project, I store the wifi SSID and pass:

Code: [Select]
char* homeWifiSSID = "DefaultSSID";
char* homeWifiPassword = "DafaultPassword";


Later, we can send parameters (for example new wifi data) to the ESP, the program will write them to ESP's memory.
Next time, after "boot", in a function, it tries to read its memory: 


Code: [Select]
// SSID
String ssidString = readFromMemory(1, 30); // gives back a String, for example "newSSID"
char homeWifiSSID[ssidString.length()+1]; 
ssidString.toCharArray(homeWifiSSID, ssidString.length()+1);
Serial.print("homeWifiSSID = "); Serial.println(homeWifiSSID);


The main purpose is to connect to the network with the new data but in the "connect" function the ESP tries to connect to the "defaultSSID" network. So, my question is: How can I send the new SSID to the memory where the *homeWifiSSID "points"?

Thanks for your answers!

Mihaly


ballscrewbob

@g889ln

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.



It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

gfvalvo

You don't want to "put new data in" as string pointer that's defined:
Code: [Select]
char* homeWifiSSID = "DefaultSSID";
Because you'll overwrite the string literal which can cause bad things to happen. In fact, that definition is wrong. It should be:
Code: [Select]
const char* homeWifiSSID = "DefaultSSID";
Or, even better:
Code: [Select]
const char * const homeWifiSSID = "DefaultSSID";
If you want to be able to change the string on the fly, use an char array instead:
Code: [Select]
char homeWifiSSID[20] = "DefaultSSID";
Where the size of the array (20 in my example above) needs to be large enough to hold the largest string you'll ever try to put in there (plus the terminating null). To change the contents of the string, use strncpy().
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

vaj4088

Yes, but because of the snippet of code, it appears that homeWifiSSID is declared twice, which could also cause problems.




Go Up