ESP8266 + WiFiManager + pubsubclient

SirSiggi picture SirSiggi · Jan 27, 2016 · Viewed 7.6k times · Source

I'm using an ESP8266 and want to control it using MQTT with the MQTT server being my Synology DS415+. I want the ESP to sit in a place where I cannot access it using serial once it is installed, so I need to be able to configure it's Wifi-Credentials and the MQTT-Server IP, Port and credentials using WiFi.

Thus I decided that the WiFiManager-Library and the PubSubClient-Library can do this for me. The problem is: I cannot get PubSubClient to work using WiFiManager, because I haven't yet found out how I can tell PubSubClient the right "client" to use.

The following example works on my ESP, however it does not allow for dynamic configuration of the ESPs Wifi: https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino

I came up with the following: http://pastebin.com/t5evEy1i

This however does not work, its output via serial is the following:

mounting FS...
mounted file system
reading config file
opened config file
{"mqtt_server":"192.168.1.250","mqtt_port":"9001","switch_token":"BackupSwitch"}
parsed json
*WM: Adding parameter
*WM: server
*WM: Adding parameter
*WM: port
*WM: Adding parameter
*WM: blynk
*WM: 
*WM: AutoConnect
*WM: Reading SSID
*WM: SSID: 
*WM: XXX
*WM: Reading Password
*WM: Password: XXX
*WM: Connecting as wifi client...
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.1.74
connected...yeey :)
local ip
192.168.1.74
Attempting MQTT connection...192.168.1.250:9001
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...192.168.1.250:9001
failed, rc=-2 try again in 5 seconds

I'm pretty sure the problem lies in the definition of the PubSubClient in line 17 and 18:

WiFiClient espClient;
PubSubClient client(espClient);

But I don't know how to extract the client from WiFiManager to give it to the PubSubClient-Library.

What I need is how to get an object that is equal to what WiFiClient or EthernetClient creates, that WiFiManager probably creates and that I can give as an argument to PubSubClient client(espClient);

Does anyone have any idea how to achieve this? Thanks in advance.

Answer

seamus picture seamus · May 16, 2016

You don't need to pull anything out of WiFiManager since it is using WiFiClient. All you need to do is:

#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);

void mqttCallback(char* topic, byte* payload, unsigned int length) {
    // message received
}

void mqttReconnect() {
    // reconnect code from PubSubClient example
}

void setup() {
  WiFiManager wifiManager;
  wifiManager.setTimeout(180);

  if(!wifiManager.autoConnect("AutoConnectAP")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    ESP.reset();
    delay(5000);
  } 

  Serial.println("connected...yeey :)");

  client.setServer(mqtt_server, 1883);
  client.setCallback(mqttCallback);
}

void loop() {
  if (!client.connected()) {
    mqttReconnect();
  }
  client.loop();
  yield();
}