MQTT read saved variables from storage (./config.json)

Hey guys,

I’m trying to get things done with my MQTT code snipped without having to use the hard coded credentials for the WiFi or MQTT stuff. WiFi seems to work fine and also the readout of my MQTT variables… but if I just want to use them for my MQTT connection I get this error:

mqtt_wifi_connect:151:42: error: invalid conversion from 'char*' to 'uint16_t {aka short unsigned int}' [-fpermissive]

   client.setServer(mqtt_server, mqtt_port);

or this

Arduino\libraries\PubSubClient\src/PubSubClient.h:130:18: error:   initializing argument 2 of 'PubSubClient& PubSubClient::setServer(const char*, uint16_t)' [-fpermissive]

    PubSubClient& setServer(const char * domain, uint16_t port)

Those variables are set via this:

strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_password, json["mqtt_password"]);
strcpy(mqtt_topic, json["mqtt_topic"]);

My variable declaration looks like this:

char mqtt_server[40];
char mqtt_port[6] = "1883";
char mqtt_user[20];
char mqtt_password[20];
char mqtt_topic[40];

What should I change to get this fixed?

I used mainly the code from this here: WiFiManager/AutoConnectWithFSParameters.ino at a5024562f815ac3072bd11318e63bdc7113ce31e · tzapu/WiFiManager · GitHub

problem solved :slight_smile:

mqtt_port = (uint16_t)strtol(mqtt_temp, NULL, 10);