I know this is a recurring question ... but I have read many documents, I have found some provided solutions but they are not working for me. I am sure is my fault when implementing them. So I need some help.
I am using an MKR1000 (similar to an Arduino Zero).
PubSubClient library works great. I have been able so subscribe and set a callback. MQTT messages arrive. The problem is I have been able to get it working using an static method. see below.
class Homie {
public:
...
static void callback(char* topic, uint8_t* payload, unsigned int length);
...
}
Homie::Homie(PubSubClient *client) {
...
client->setCallback(&Homie::callback);
...
}
But I need (I have been thinking about other alternatives which did not work for different reasons) to be able to reference the instance of my class.
I have tried a couple of alternatives which have not worked for me.
class Homie {
public:
...
void callback(char* topic, uint8_t* payload, unsigned int length);
...
}
Homie::Homie(PubSubClient *client) {
...
client->setCallback([this](char* topic, byte* payload, unsigned int length) { this->callback(topic,payload,length); });
...
}
But I receive an error.
Arduino: 1.8.10 (Linux), Board: "Arduino/Genuino MKR1000"
sketch/Homie.cpp: In constructor 'Homie::Homie(PubSubClient*)':
Homie.cpp:125:122: error: no matching function for call to 'PubSubClient::setCallback(Homie::Homie(PubSubClient*)::<lambda(char*, byte*, unsigned int)>)'
client->setCallback([this](char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });
^
In file included from sketch/Homie.h:36:0,
from sketch/Homie.cpp:1:
/home/genaro/Arduino/libraries/PubSubClient/src/PubSubClient.h:145:18: note: candidate: PubSubClient& PubSubClient::setCallback(void (*)(char*, uint8_t*, unsigned int))
PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
^~~~~~~~~~~
...
no matching function for call to 'PubSubClient::setCallback(Homie::Homie(PubSubClient*)::<lambda(char*, byte*, unsigned int)>)'
I know this is related with a pointer to my class being passed as first parameter. But I do not know how to solve that.
I also have tried the other solution I have found using std:bind. I have added #include and this changes in the code:
Homie::Homie(PubSubClient *client) : Device(client, NULL,(char *)"Homie") {
...
client->setCallback(std::bind(&Homie:callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
...
}
The errors here are referring to macros which I have not modified. No idea on this one.
Arduino: 1.8.10 (Linux), Board: "Arduino/Genuino MKR1000"
In file included from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/char_traits.h:39:0,
from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/string:40,
from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/stdexcept:39,
from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/array:39,
from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/tuple:39,
from /home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/functional:54,
from sketch/Homie.cpp:4:
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:265:56: error: macro "max" passed 3 arguments, but takes just 2
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
In file included from sketch/Homie.h:34:0,
from sketch/Homie.cpp:1:
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected unqualified-id before 'const'
min(const _Tp& __a, const _Tp& __b)
^
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected ')' before 'const'
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected ')' before 'const'
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:195:5: error: expected initializer before 'const'
/home/genaro/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/bits/stl_algobase.h:219:5: error: expected unqualified-id before 'const'
max(const _Tp& __a, const _Tp& __b)
^
...
Any ideas on what I might be doing wrong? Thanks in advance.