weirdly can't set server to variable with pubsubclient on ESP8266

Hello peeps

I’m clueless what to do.

I tinkered with a wifi portal a while ago where you could tell the ESP a ssid and password, it would save them on the EEPROM and on the next boot log onto said network. Now I wanted to add the functionality to also change the mqtt server through the web portal.

I’m reading the the values from the EEPROM and returning them as a String, then converting them to the correct type of variable.

for user and password the documentation says it’s const char, for server it should also be possible to use uint8_t or the IP.

Now the weird part

for the first two I converted with toCharArray and it’s all good, tried the same with the server and it didn’t, so I tried declaring a char and directly referencing it, which didn’t work.

    char cMSID[] = "m21.cloudmqtt.com";
    Serial.println("");
    Serial.print("cMSID = ");
    Serial.println(cMSID);
    Serial.println("");
    
    client.setServer(cMSID, readCred(findAddress(mqttPortPosition)).toInt());

If I try the same with a char* it does

    char* cMSID = "m21.cloudmqtt.com";
    Serial.println("");
    Serial.print("cMSID = ");
    Serial.println(cMSID);
    Serial.println("");
    
    client.setServer(cMSID, readCred(findAddress(mqttPortPosition)).toInt());

but then when try to use the values I read out of the EEPROM it again doesn’t, even though on the Serial monitor it prints out the correct address

    String sMSID = readCred(findAddress(mqttServerPosition));
    const int sMSIDLength = sMSID.length() + 1;
    byte bMSID[sMSIDLength];
    sMSID.getBytes(bMSID, sMSIDLength);
    char* cMSID = (char*) bMSID;
    Serial.println("");
    Serial.print("(char*) bMSID = ");
    Serial.println((char*) bMSID);
    Serial.println("");
    Serial.print("cMSID = ");
    Serial.println(cMSID);
    Serial.println("");
    
    client.setServer(cMSID, readCred(findAddress(mqttPortPosition)).toInt());

here are the functions I use to read out the EEPROM

void saveCred(String what, int where){
  EEPROM.begin(512);
  int charLength = what.length();
  Serial.print("writing eeprom length: ");
  Serial.println(charLength);
  EEPROM.write(where, charLength);
  Serial.println("writing eeprom data:");
  for (unsigned int i = where + 1; i < where + 1 + what.length(); ++i){
    EEPROM.write(i, what[i - 1 - where]);
    Serial.print("Wrote: ");
    Serial.println(what[i - 1 - where]);
  }
  EEPROM.end();
}

String readCred(int where){
  EEPROM.begin(512);
  String what = "";
  int charLength = EEPROM.read(where);
  Serial.print("reading eeprom length: ");
  Serial.println(charLength);
  for (unsigned int i = where + 1; i < where + 1 + charLength; ++i){
    what += char(EEPROM.read(i));
    //Serial.println(i);
  }
  //ssidOld.trim();
  Serial.print("length: ");
  Serial.println(what.length());
  Serial.print("what: ");
  Serial.println(what);
  EEPROM.end();
  return what;
}

int findAddress(int number){
  int address = 0;
  for(int j = 0; j < number; j++){
    address += maxLengthRom[j] + 1;
  }
  return address;
}

I have absolutely no idea what I could try next T_T

In the attachment you’ll find my whole project, the parts I mentioned are in the ba_pubsub and zy_setup file

zz_loop.ino (182 Bytes)

zy_setup.ino (2.37 KB)

fa_EEPROM.ino (1.05 KB)

ea_print.ino (705 Bytes)

da_wifi.ino (617 Bytes)

ca_AP.ino (4.75 KB)

ba_pubsub.ino (1.91 KB)

a_global_mqtt_esp8266_4.0.ino (2.9 KB)

aa_shizzle.ino (329 Bytes)

    String sMSID = readCred(findAddress(mqttServerPosition));
    const int sMSIDLength = sMSID.length() + 1;
    byte bMSID[sMSIDLength];
    sMSID.getBytes(bMSID, sMSIDLength);

After this code executes, bMSID is NOT a string (a NULL terminated array of chars).

    char* cMSID = (char*) bMSID;

cMSID does not point to a string.

    client.setServer(cMSID, readCred(findAddress(mqttPortPosition)).toInt());

This function expects a string as the first argument. You are not supplying a string.

Thanks for your reply! I still don't quite get it though :sweat_smile:

I'm guessing you don't mean the String object since that returns "no matching function for call to 'PubSubClient::setServer(String&, long int)'"?

I tried with .c_str() and before that I had tried it with a char array but that didn't work either, similar to this code:

    String sMUID = readCred(findAddress(mqttUserPosition));
    const int sMUIDLength = sMUID.length() + 1;
    char cMUID[sMUIDLength];
    sMUID.toCharArray(cMUID, sMUIDLength);
    Serial.println("");
    Serial.print("cMUID = ");
    Serial.println(cMUID);

What am I missing? again it prints out what I'd like it to just doesn't work within the function....

just doesn't work within the function

You'd need to edit the library, to print what you are passing in, to see what the problem is. That last snippet certainly should work. Assuming, of course, that you call it at the right time.