mycustom library and pubsubclient call back

935 views
Skip to first unread message

Waqas Ahmed

unread,
Jan 23, 2016, 1:04:55 PM1/23/16
to MQTT
Hi. I am trying to make my own library for my Esp8266 Arduino using ESP8266 library and PubSubClient library. Problem is pubsubclient call back function, It is giving error. May someone please help me?

If I delete this line "_client.setCallback(callback);" from my cpp file (Highlighted in red in cpp file contents), it compiles successfully but when I add this line, It gives following error.


C:\Users\Ahmed\Documents\Arduino\libraries\EspMQTT\EspMQTT.cpp: In member function 'void EspMQTT::begin()':
C:\Users\Ahmed\Documents\Arduino\libraries\EspMQTT\EspMQTT.cpp:33:30: error: no matching function for call to 'PubSubClient::setCallback(<unresolved overloaded function type>)'
  _client.setCallback(callback);
                              ^
C:\Users\Ahmed\Documents\Arduino\libraries\EspMQTT\EspMQTT.cpp:33:30: note: candidate is:
In file included from C:\Users\Ahmed\Documents\Arduino\libraries\EspMQTT\EspMQTT.h:13:0,
                 from C:\Users\Ahmed\Documents\Arduino\libraries\EspMQTT\EspMQTT.cpp:8:
C:\Users\Ahmed\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:108:18: note: PubSubClient& PubSubClient::setCallback(void (*)(char*, uint8_t*, unsigned int))
    PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
                  ^
C:\Users\Ahmed\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:108:18: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'void (*)(char*, uint8_t*, unsigned int) {aka void (*)(char*, unsigned char*, unsigned int)}'
Error compiling.



Here is my library header contents;
#ifndef ESPMQTT_H_
#define ESPMQTT_H_

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define RESET_PIN 16

class EspMQTT {
public:
EspMQTT();
void begin();
void wifi();
void mqtt();
void loop();
void callback(char* topic, byte* payload, unsigned int length);
private:
int _wifi_reconnect_tries = 0;
int _mqtt_reconnect_tries = 0;
long _wifi_reconnect_time = 0L;
long _mqtt_reconnect_time = 0L;
long _wifi_check_time = 15000L;
long _last_reported = 0;
int _counter = 0;
};
#endif

And here is cpp file contents;
#include "EspMQTT.h"

extern char* SKETCH_ID;
extern char* WIFI_SSID;
extern char* WIFI_PASSWORD;
extern char* MQTT_SERVER;
extern char* MQTT_USER;
extern char* MQTT_PASSWORD;
extern char* MQTT_PUBLISH_TOPIC;
extern char* MQTT_SUBSCRIBE_TOPIC;


WiFiClient _espClient;
PubSubClient _client(_espClient);

EspMQTT::EspMQTT() {

}

void EspMQTT::begin()
{
Serial.begin(115200);
Serial.println("System started");
_client.setServer(MQTT_SERVER, 1883);
_client.setCallback(callback);
wifi();
}

void EspMQTT::callback(char* topic, byte* payload, unsigned int length)
{
String message = String();
 for (int i = 0; i < length; i++) {
   char input_char = (char)payload[i];
   message += input_char;
 }
}

void EspMQTT::wifi()
{

++ _wifi_reconnect_tries;
if (_mqtt_reconnect_tries != 0) {
_mqtt_reconnect_tries = 0;
}
boolean networkScan = false;
int n = WiFi.scanNetworks();
delay(300);
for (int i = 0; i < n; ++i) {

if (WiFi.SSID(i) == WIFI_SSID) {
Serial.print(WIFI_SSID);
Serial.print(" is available");
Serial.println("");
networkScan = true;
break;
}
}
if(networkScan) {
if (_wifi_reconnect_tries > 1) {
Serial.print("Retrying:: ");
}
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
Serial.println("");
long wifi_initiate = millis();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
if (WiFi.status() == WL_CONNECTED) {
break;
}
if ((millis() - wifi_initiate) > 15000L) {
break;
}
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.print(" Connected!!");
Serial.println("");
_wifi_reconnect_tries = 0;
} else if ((WiFi.status() != WL_CONNECTED) && (_wifi_reconnect_tries > 3)) {
Serial.print(" Failed to connect to ");
Serial.print(WIFI_SSID);
Serial.println("");
Serial.println("Rebooting...");
delay(1000);
digitalWrite(RESET_PIN, LOW);
}
} else {
Serial.print(WIFI_SSID);
Serial.print(" is offline");
Serial.println("");
if (_wifi_reconnect_tries > 3) {
_wifi_check_time = 300000L;
_wifi_reconnect_tries = 0;
Serial.println("System will try again after 5 minutes");
}
}
}

void EspMQTT::mqtt()
{
if (_mqtt_reconnect_tries > 1) {
Serial.print("Retrying:: ");
}
Serial.print("Connecting to mqtt server: ");
Serial.println(MQTT_SERVER);
_client.connect(SKETCH_ID, MQTT_USER, MQTT_PASSWORD);
delay(500);
if (_client.connected()) {
_client.publish(MQTT_PUBLISH_TOPIC, "system online"); // Initial system status publish to server
_client.subscribe(MQTT_SUBSCRIBE_TOPIC); // Subscribe to your MQTT topic
Serial.println(".. Connected!!");
_mqtt_reconnect_tries = 0;
} else {

Serial.print("Failed to connect to mqtt server, rc=");
Serial.print(_client.state());
Serial.println("");
}
}

void EspMQTT::loop()
{
_client.loop();
if((WiFi.status() != WL_CONNECTED) && ((millis() - _wifi_reconnect_time) > _wifi_check_time)) {
   _wifi_check_time = 15000L;
   _wifi_reconnect_time = millis();
   wifi();
 }
if ((WiFi.status() == WL_CONNECTED) && (!_client.connected()) && ((millis() - _mqtt_reconnect_time) > 5000L)) {
   _mqtt_reconnect_time = millis();
   ++ _mqtt_reconnect_tries;
   mqtt();
   if ((_mqtt_reconnect_tries > 5) && (!_client.connected())) {
     Serial.println("Rebooting");
     delay(1000);
     digitalWrite(16, LOW);
   }
 }
}


And Here is ino file contents
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "EspMQTT.h"

const char* SKETCH_ID = "MSUnit";
const char* WIFI_SSID = "Ahmed";
const char* WIFI_PASSWORD = "myPassword";
const char* MQTT_SERVER = "192.168.0.30";
const char* MQTT_USER = "pi";
const char* MQTT_PASSWORD = "myPassword";
const char* MQTT_PUBLISH_TOPIC = "MSUnit-out";
const char* MQTT_SUBSCRIBE_TOPIC = "MSUnit-in";

EspMQTT client;
void setup()
{
client.begin();
}


void loop()
{
client.loop();
delay(1);
}



ma...@gcmanager.de

unread,
Nov 12, 2016, 6:27:31 AM11/12/16
to MQTT
I have exactly the same problem!!! Do you have a solution?

Frédéric Clement

unread,
Apr 19, 2017, 7:42:17 AM4/19/17
to MQTT
Hi All,

I have exactly the same problem, I tried :

in my .h :

class MyClass    {
private :
      MQTT_CALLBACK_SIGNATURE
;
....
}



and my .cpp :

void MyClass::AutoRegister()
{
   
if (this->now - this->last_register_attempt < 1000*REGISTER_DELAY) return;
   
if (WiFi.status() != WL_CONNECTED || this->is_registered) return;

   
if (!this->mqtt)    {
       
this->mqtt = new PubSubClient(this->wifiClient);
       
this->mqtt->setServer(this->srv, 1883);
       
this->mqtt->setCallback(this->callback);
   
}
}



This is OK, but when I try to declare the callback as a method of my class :

void MyClass::callback(char *,  uint8_t*, uint32_t)
{
....
}



I get :

no 'void MyClass::callback(char*, uint8_t*, uint32_t)' member function declared in class

Frédéric Clement

unread,
Apr 19, 2017, 10:41:12 AM4/19/17
to MQTT
Answering to myself :

the working solution is :

this->mqtt->setCallback([this](char *topic, byte *payload, unsigned int length) {   this->callback(topic, payload, length); });
Message has been deleted

iot.mie...@gmail.com

unread,
Jul 24, 2017, 3:27:25 PM7/24/17
to MQTT
Frédéric Clement : Can you share/public your solution for class ?

Waqas Ahmed

unread,
Jul 25, 2017, 2:51:23 AM7/25/17
to MQTT
Hi,

I have written a post on this specific issue. Here is the link of my post;


You also find my library for reference on the following link;


If still have a question, just contact me and I'll try my best to help you.
Reply all
Reply to author
Forward
0 new messages