Hi guys,
I need help. I want to wrap this mqtt library in a class, and I’m getting this compilation issue:
running command$ particle compile photon /home/user/particle/mqttInClass --saveTo /home/user/particle/mqttInClass/firmware.bin
Compiling code for photon
Including:
/home/user/particle/mqttInClass/MQTT.h
/home/user/particle/mqttInClass/mqttClient.h
/home/user/particle/mqttInClass/MQTT.cpp
/home/user/particle/mqttInClass/mqttClient.cpp
attempting to compile firmware
Compile failed. Exiting.
mqttClient.cpp: In constructor 'c_mqttClient::c_mqttClient()':
mqttClient.cpp:9:56: error: no match for call to '(MQTT) (const char [15], uint16_t, <unresolved overloaded function type>)'
mqttCli("www.sample.com", uint16_t(1883), mqttCallback);
^
make[1]: *** [../build/target/user/platform-6mqttClient.o] Error 1
make: *** [user] Error 2
This is the code that gets that issue:
file mqttClient.cpp:
#include "application.h"
#include "mqttClient.h"
void c_mqttClient::mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
Particle.publish("mqtt message received", "", 60, PRIVATE);
}
c_mqttClient::c_mqttClient() {
mqttCli("www.sample.com", uint16_t(1883), mqttCallback);
}
file mqttClient.h:
#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H
#include "application.h"
#include "MQTT.h"
class c_mqttClient {
MQTT mqttCli;
public:
c_mqttClient();
void mqttCallback(char* topic, byte* payload, unsigned int length);
};
#endif
I searched google, this community, c forums and I believe the issue is due to the following fact:
Pointers to non-static members are different to ordinary C function pointers since they need the this-pointer of a class object to be passed. Thus ordinary function pointers and non-static member functions have different and incompatible signatures!
Source is here.
So I went ahead and tried the wrapper approach described here (second answer) and here. I ended up with this issue:
running command$ particle compile photon /home/user/particle/mqttInClass --saveTo /home/user/particle/mqttInClass/firmware.bin
Compiling code for photon
Including:
/home/user/particle/mqttInClass/MQTT.h
/home/user/particle/mqttInClass/mqttClient.h
/home/user/particle/mqttInClass/MQTT.cpp
/home/user/particle/mqttInClass/mqttClient.cpp
attempting to compile firmware
Compile failed. Exiting.
mqttClient.cpp: In constructor 'c_mqttClient::c_mqttClient()':
mqttClient.cpp:19:63: error: no match for call to '(MQTT) (const char [15], uint16_t, void (&)(char*, uint8_t*, unsigned int))'
mqttCli("www.sample.com", uint16_t(1883), mqttCallback_wrapper);
^
make[1]: *** [../build/target/user/platform-6mqttClient.o] Error 1
make: *** [user] Error 2
File mqttClient.cpp:
#include "application.h"
#include "mqttClient.h"
void* pt2Object;
void c_mqttClient::mqttCallback_wrapper (char* topic, uint8_t* payload, unsigned int length){
// explicitly cast to a pointer to c_mqttClient
c_mqttClient* mySelf = (c_mqttClient*) pt2Object;
// call member
mySelf->mqttCallback(topic, payload, length);
}
void c_mqttClient::mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
Particle.publish("mqtt message received", "", 60, PRIVATE);
}
c_mqttClient::c_mqttClient() {
mqttCli("www.sample.com", uint16_t(1883), mqttCallback_wrapper);
}
File mqttClient.h:
#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H
#include "application.h"
#include "MQTT.h"
class c_mqttClient {
MQTT mqttCli;
public:
c_mqttClient();
void mqttCallback(char* topic, uint8_t* payload, unsigned int length);
static void mqttCallback_wrapper(char* topic, uint8_t* payload, unsigned int length);
};
#endif
I’ve been trying variations of this code for a couple of weeks now without success.
Can someone spot what I’m doing wrong here by any chance?
Thank you!
Gustavo.
PS: I do not expect “www.sample.com” to work, I just wanted to simplify my code in this post (so I don’t need to add a byte array to define the mqtt broker ip)