Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using PubSubClient with WiFiManager #15

Closed
lunanigra opened this issue Dec 1, 2015 · 11 comments
Closed

Using PubSubClient with WiFiManager #15

lunanigra opened this issue Dec 1, 2015 · 11 comments

Comments

@lunanigra
Copy link

Hello, can you give some hints how to use PubSubClient with your WiFiManager?

To init the PubSubClient a WiFiClient is given as parameter...
WiFiClient espClient;
PubSubClient client(espClient);

Works simple and fine. But how to retrieve a PubSubClient instance when using WiFiManager? If you recommend a different MQTT library it would be fine for me as well.

Thanks, Jan Christian

@tzapu
Copy link
Owner

tzapu commented Dec 3, 2015

Hi Jan,
Just use it as you mentioned above.

WiFiClient espClient;
PubSubClient client(espClient);

WiFiManager is active only during the startup phase. after it connects, you can build your app as normal.
WiFiClient does not have any direct connection or dependency on WiFiManager

@tzapu
Copy link
Owner

tzapu commented Dec 9, 2015

@lunanigra is it ok? did you get it running in the end?

@lunanigra
Copy link
Author

Hi! Thanks for your support.
Don't know what I did wrong last time; but it works :-)

Not sure if already fixed... When I tested the WifiManager few weeks ago some Wifi networks got listed twice. Nothing serious...

@tzapu
Copy link
Owner

tzapu commented Dec 10, 2015

hi, no problem,

have not seen that happening. if it becomes an issue please open another issue/

cheers

@tzapu tzapu closed this as completed Dec 10, 2015
@MyIoT01
Copy link

MyIoT01 commented May 16, 2016

Hi Tzapu,

First of all, thank you for your library that gave all of us. I also can't work out pubsubclient with wifi manager. Can you give me an example how to work out with it. Below code that i gave executed but doesn't work. Any help will be greatly appreciated.

`#include`` <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <PubSubClient.h>

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager

const char* mqtt_server = "myadress.com";

WiFiClient espClient;
PubSubClient espClient;

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}

}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();

//set custom ip for portal
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here  "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();


//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");

}

void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();

long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}

}

``

@muntakson
Copy link

Ho tzapu.
Thanks for your wifimanager.
I had the problem. The mqtt connection is not reliable with WifiManager.
My code is almost same abov except i added client.loop() at the end.

Bob

1 similar comment
@muntakson
Copy link

Ho tzapu.
Thanks for your wifimanager.
I had the problem. The mqtt connection is not reliable with WifiManager.
My code is almost same abov except i added client.loop() at the end.

Bob

@LeonVos
Copy link

LeonVos commented Jul 15, 2017

This should work: (ref to your MQTTserver)

WiFiClient wclient;
PubSubClient client(wclient, mqttserver);

@amrithmmh
Copy link

Hi! Thanks for your support.
Don't know what I did wrong last time; but it works :-)

Not sure if already fixed... When I tested the WifiManager few weeks ago some Wifi networks got listed twice. Nothing serious...

can you share your code ?...how to get it working?

@amrithmmh
Copy link

This should work: (ref to your MQTTserver)

WiFiClient wclient;
PubSubClient client(wclient, mqttserver);

does not work .It throws error

@mediaDS
Copy link

mediaDS commented Jan 25, 2021

Hi

Neither PubSubClient client(espClient); nor PubSubClient client(wclient, mqttserver); works. Both throw lenghty error messages. I am using PlatformIO and a ESP8266 board. If I use PubSubClient client; it compiles without erros but the code crashes as soon as I enter the reconnect loop. The ESP8266 just restarts. So, is there any way to use both libs together?

Edit (I forgot the logs):

PubSubClient client(wclient, mqttserver);

src/Werner.cpp:28:40: error: no matching function for call to 'PubSubClient::PubSubClient(WiFiManager&, const char*&)'
 PubSubClient client(Werner, mqtt_server);
                                        ^
src/Werner.cpp:28:40: note: candidates are:
In file included from src/Werner.cpp:5:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note:   candidate expects 5 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:128:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:128:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:127:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&, Stream&)
    PubSubClient(const char*, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:127:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:126:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&)
    PubSubClient(const char*, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:126:4: note:   candidate expects 3 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:125:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:125:4: note:   candidate expects 5 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:124:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:124:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:123:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:123:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:122:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&)
    PubSubClient(uint8_t *, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:122:4: note:   candidate expects 3 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:121:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:121:4: note:   candidate expects 5 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:120:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:120:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:119:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:119:4: note:   candidate expects 4 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:118:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)
    PubSubClient(IPAddress, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:118:4: note:   candidate expects 3 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:117:4: note: PubSubClient::PubSubClient(Client&)
    PubSubClient(Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:117:4: note:   candidate expects 1 argument, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:116:4: note: PubSubClient::PubSubClient()
    PubSubClient();
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:116:4: note:   candidate expects 0 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:88:7: note: PubSubClient::PubSubClient(const PubSubClient&)
 class PubSubClient : public Print {
       ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:88:7: note:   candidate expects 1 argument, 2 provided
*** [.pio/build/esp12e/src/Werner.cpp.o] Error 1

PubSubClient client(espClient);

src/Werner.cpp:28:30: error: no matching function for call to 'PubSubClient::PubSubClient(WiFiManager&)'
 PubSubClient client(espClient);
                              ^
src/Werner.cpp:28:30: note: candidates are:
In file included from src/Werner.cpp:5:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note:   candidate expects 5 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:128:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:128:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:127:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&, Stream&)
    PubSubClient(const char*, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:127:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:126:4: note: PubSubClient::PubSubClient(const char*, uint16_t, Client&)
    PubSubClient(const char*, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:126:4: note:   candidate expects 3 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:125:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:125:4: note:   candidate expects 5 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:124:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:124:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:123:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&, Stream&)
    PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:123:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:122:4: note: PubSubClient::PubSubClient(uint8_t*, uint16_t, Client&)
    PubSubClient(uint8_t *, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:122:4: note:   candidate expects 3 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:121:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:121:4: note:   candidate expects 5 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:120:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
    PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:120:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:119:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)
    PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:119:4: note:   candidate expects 4 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:118:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)
    PubSubClient(IPAddress, uint16_t, Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:118:4: note:   candidate expects 3 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:117:4: note: PubSubClient::PubSubClient(Client&)
    PubSubClient(Client& client);
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:117:4: note:   no known conversion for argument 1 from 'WiFiManager' to 'Client&'
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:116:4: note: PubSubClient::PubSubClient()
    PubSubClient();
    ^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:116:4: note:   candidate expects 0 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:88:7: note: PubSubClient::PubSubClient(const PubSubClient&)
 class PubSubClient : public Print {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants