0

I have a boolean function which contains a string. In my main project this string is extracted from a website using an ESP8266 which is compatible with the Arduino IDE. I need to convert this string to a char* type of which is to be used in the loop() section of the code. The issue which I have faced is related to the setting of the mPass global variable, which does not change when it is done in the restoreConfig() Boolean function, meaning it remains blank when called for in the loop.

I do understand that this problem is related to the fact that I am defining the mPass within the Boolean function, however I am unsure as to the process in which I can change the global variable without errors.

I have tried resolving this problem through moving the mqttpass, mqttpasslen and mPass variables out of the restoreConfig() function, however it creates significantly more problems than that which I have at the moment. As a result I have only presented the code without any adjustment below:

char* mPass = "";

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (restoreConfig()) {
    Serial.println(mPass);
    Serial.println("ABOVE IS mPass IN LOOP");
  }
  delay(1000);
}

boolean restoreConfig() {
  String mqttpass = "DOGS";
  int mqttpasslen = mqttpass.length() + 1;
  char mPass[mqttpasslen];
  mqttpass.toCharArray(mPass, mqttpasslen);
  Serial.println(mPass);
  Serial.println("ABOVE IS mPass IN BOOLEAN");
  return true;
}

My current output from this is:

DOGS
ABOVE IS mPass IN BOOLEAN

ABOVE IS mPass IN LOOP
1

You have local variable mPass in the function restoreConfig. It 'shadows' the global variable.

  • Hi @Juraj, I do understand that this is the case and have since uncommented the local variable mPass, however now my output does not show anything but blank. If I attempt to move char mPass[mqttpasslen], then I have the issue that then mqttpasslen is undefined. Do you have any suggestions as to the way I can resolve this? – Lachlan Etherton Nov 5 '18 at 7:53
  • the global variable is only a pointer. change it to array to allocate space. – Juraj Nov 5 '18 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.