[GH-ISSUE #988] Can't use stored mqtt variables for PubSubclient #841

Closed
opened 2026-02-28 01:27:18 +03:00 by kerem · 1 comment
Owner

Originally created by @nicedevil007 on GitHub (Jan 2, 2020).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/988

Hey guys,

I'm took some code from the examples out here and I always got stuck at this point where the MQTT Server and Port should be used to connect to my MQTT Server.

The error messages are always those two:

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

   client.setServer(mqtt_server, mqtt_port);

and this:

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);

My sketch looks like this:

#include <FS.h>

#include <ESP8266WiFi.h>

#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>

// MQTT Stuff
#include <PubSubClient.h>
#include <millisDelay.h>
WiFiClient espClient;
PubSubClient client(espClient);
millisDelay mqttDelay;

// LED Blink for Setup
#include <Ticker.h>
Ticker ticker;

int LED = LED_BUILTIN;

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

bool shouldSaveConfig = false;

void saveConfigCallback () {
  Serial.println("[SETUP] Should save config");
  shouldSaveConfig = true;
}

void tick() {
  digitalWrite(LED, !digitalRead(LED));
}

void setup() {

  pinMode(LED, OUTPUT);
  ticker.attach(0.6, tick);

  Serial.begin(115200);
  Serial.println();

  //clean FS, for testing
  //SPIFFS.format();

  Serial.println("[SETUP] Mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("[SETUP] Mounted file system");
    if (SPIFFS.exists("/config.json")) {
      Serial.println("[SETUP] Reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("[SETUP] Opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonDocument json(1024);
        auto deserializeError = deserializeJson(json, buf.get());
        serializeJson(json, Serial);
        if ( ! deserializeError ) {
          ticker.attach(0.2, tick);
          Serial.println("\n[SETUP] Parsed json");
          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"]);
        } else {
          Serial.println("[SETUP] Failed to load json config");
        }
        configFile.close();
      }
    }
  } else {
    Serial.println("[SETUP] Failed to mount FS");
  }

  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 20);
  WiFiManagerParameter custom_mqtt_password("pass", "mqtt password", mqtt_password, 20);
  WiFiManagerParameter custom_mqtt_topic("pass", "mqtt topic", mqtt_topic, 40);

  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //add all your parameters here
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_mqtt_user);
  wifiManager.addParameter(&custom_mqtt_password);
  wifiManager.addParameter(&custom_mqtt_topic);

  //wifiManager.resetSettings();

  wifiManager.setMinimumSignalQuality(25);
  wifiManager.setRemoveDuplicateAPs(true);
  //wifiManager.setTimeout(120);

  if (!wifiManager.autoConnect()) {
    ticker.attach(1.2, tick);
    Serial.println("[SETUP] Failed to connect and hit timeout");
    delay(3000);
    ESP.reset();
    ticker.detach();
    digitalWrite(LED, LOW);
    delay(5000);
  }

  Serial.println("[SETUP] Connected");

  //read updated parameters
  strcpy(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(mqtt_user, custom_mqtt_user.getValue());
  strcpy(mqtt_password, custom_mqtt_password.getValue());
  strcpy(mqtt_topic, custom_mqtt_topic.getValue());

  if (shouldSaveConfig) {
    Serial.println("[SETUP] Saving config");
    DynamicJsonDocument json(1024);
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["mqtt_user"] = mqtt_user;
    json["mqtt_password"] = mqtt_password;
    json["mqtt_topic"] = mqtt_topic;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("[SETUP] Failed to open config file for writing");
    }

    serializeJson(json, Serial);
    serializeJson(json, configFile);
    configFile.close();
  }

  Serial.print("[SETUP] Local ip: ");
  Serial.println(WiFi.localIP());


  client.setServer(mqtt_server, mqtt_port);
  //client.setCallback(callback);

  delay(2000);

  while (!client.connected()) {
    Serial.println("[SETUP] Connecting to MQTT-Server");
    Serial.println((String)"[SETUP] IP:    " + mqtt_server + ":" + mqtt_port);
    Serial.println((String)"[SETUP] State: " + client.state());
    if (client.connect("ESP-LEDTEST", mqtt_user, mqtt_password )) {
      Serial.println("[SETUP] Succeeded.");
      ticker.detach();
      digitalWrite(LED, HIGH);
    } else {
      Serial.println("[SETUP] Failed.");
      ticker.detach();
      digitalWrite(LED, LOW);
      delay(2000);
    }
  }

  DynamicJsonDocument json(1024);
  //JsonObject json = doc.to<JsonObject>();
  json["status"] = "connected";
  json["switch"] = false;
  /*json["effect"] = EFFECT;
    json["speed"] = DELAY_TIME;*/
  JsonObject object = json.createNestedObject("color");
  object["h"] = 255;
  object["s"] = 255;
  object["v"] = 255;

  char buffer[512];
  serializeJson(json, buffer);

  client.publish(mqtt_topic, buffer);
  client.subscribe(mqtt_topic);
}

void loop() {

}

I'm using a Wemos D1 Mini R2.

Can anyone help me out?

Originally created by @nicedevil007 on GitHub (Jan 2, 2020). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/988 Hey guys, I'm took some code from the examples out here and I always got stuck at this point where the MQTT Server and Port should be used to connect to my MQTT Server. The error messages are always those two: ``` error: invalid conversion from 'char*' to 'uint16_t {aka short unsigned int}' [-fpermissive] client.setServer(mqtt_server, mqtt_port); ``` and this: ``` 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); ``` My sketch looks like this: ``` #include <FS.h> #include <ESP8266WiFi.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> #include <ArduinoJson.h> // MQTT Stuff #include <PubSubClient.h> #include <millisDelay.h> WiFiClient espClient; PubSubClient client(espClient); millisDelay mqttDelay; // LED Blink for Setup #include <Ticker.h> Ticker ticker; int LED = LED_BUILTIN; char mqtt_server[40]; char mqtt_port[6] = "1883"; char mqtt_user[20]; char mqtt_password[20]; char mqtt_topic[40]; bool shouldSaveConfig = false; void saveConfigCallback () { Serial.println("[SETUP] Should save config"); shouldSaveConfig = true; } void tick() { digitalWrite(LED, !digitalRead(LED)); } void setup() { pinMode(LED, OUTPUT); ticker.attach(0.6, tick); Serial.begin(115200); Serial.println(); //clean FS, for testing //SPIFFS.format(); Serial.println("[SETUP] Mounting FS..."); if (SPIFFS.begin()) { Serial.println("[SETUP] Mounted file system"); if (SPIFFS.exists("/config.json")) { Serial.println("[SETUP] Reading config file"); File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { Serial.println("[SETUP] Opened config file"); size_t size = configFile.size(); std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonDocument json(1024); auto deserializeError = deserializeJson(json, buf.get()); serializeJson(json, Serial); if ( ! deserializeError ) { ticker.attach(0.2, tick); Serial.println("\n[SETUP] Parsed json"); 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"]); } else { Serial.println("[SETUP] Failed to load json config"); } configFile.close(); } } } else { Serial.println("[SETUP] Failed to mount FS"); } WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6); WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 20); WiFiManagerParameter custom_mqtt_password("pass", "mqtt password", mqtt_password, 20); WiFiManagerParameter custom_mqtt_topic("pass", "mqtt topic", mqtt_topic, 40); WiFiManager wifiManager; //set config save notify callback wifiManager.setSaveConfigCallback(saveConfigCallback); //add all your parameters here wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); wifiManager.addParameter(&custom_mqtt_user); wifiManager.addParameter(&custom_mqtt_password); wifiManager.addParameter(&custom_mqtt_topic); //wifiManager.resetSettings(); wifiManager.setMinimumSignalQuality(25); wifiManager.setRemoveDuplicateAPs(true); //wifiManager.setTimeout(120); if (!wifiManager.autoConnect()) { ticker.attach(1.2, tick); Serial.println("[SETUP] Failed to connect and hit timeout"); delay(3000); ESP.reset(); ticker.detach(); digitalWrite(LED, LOW); delay(5000); } Serial.println("[SETUP] Connected"); //read updated parameters strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_user, custom_mqtt_user.getValue()); strcpy(mqtt_password, custom_mqtt_password.getValue()); strcpy(mqtt_topic, custom_mqtt_topic.getValue()); if (shouldSaveConfig) { Serial.println("[SETUP] Saving config"); DynamicJsonDocument json(1024); json["mqtt_server"] = mqtt_server; json["mqtt_port"] = mqtt_port; json["mqtt_user"] = mqtt_user; json["mqtt_password"] = mqtt_password; json["mqtt_topic"] = mqtt_topic; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("[SETUP] Failed to open config file for writing"); } serializeJson(json, Serial); serializeJson(json, configFile); configFile.close(); } Serial.print("[SETUP] Local ip: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, mqtt_port); //client.setCallback(callback); delay(2000); while (!client.connected()) { Serial.println("[SETUP] Connecting to MQTT-Server"); Serial.println((String)"[SETUP] IP: " + mqtt_server + ":" + mqtt_port); Serial.println((String)"[SETUP] State: " + client.state()); if (client.connect("ESP-LEDTEST", mqtt_user, mqtt_password )) { Serial.println("[SETUP] Succeeded."); ticker.detach(); digitalWrite(LED, HIGH); } else { Serial.println("[SETUP] Failed."); ticker.detach(); digitalWrite(LED, LOW); delay(2000); } } DynamicJsonDocument json(1024); //JsonObject json = doc.to<JsonObject>(); json["status"] = "connected"; json["switch"] = false; /*json["effect"] = EFFECT; json["speed"] = DELAY_TIME;*/ JsonObject object = json.createNestedObject("color"); object["h"] = 255; object["s"] = 255; object["v"] = 255; char buffer[512]; serializeJson(json, buffer); client.publish(mqtt_topic, buffer); client.subscribe(mqtt_topic); } void loop() { } ``` I'm using a Wemos D1 Mini R2. Can anyone help me out?
kerem closed this issue 2026-02-28 01:27:18 +03:00
Author
Owner

@nicedevil007 commented on GitHub (Jan 3, 2020):

ok solved it with this

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

<!-- gh-comment-id:570507240 --> @nicedevil007 commented on GitHub (Jan 3, 2020): ok solved it with this `mqtt_port = (uint16_t)strtol(mqtt_temp, NULL, 10);`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#841
No description provided.