0

I'll try to explain my need:

  1. I have a generic library I created, called myIOT.h.
  2. This class have a pre-configured MQTT and wifi setup.
  3. Inside this library there is a mqtt_callback function, for all generic commands, such as boot_time and connection_status.
  4. BUT, this library serves in several of my iot device's code, that each one has a few more mqtt commands, that is tailored just for s specific device, and I looking for a way that iot.mqtt_callback(ext_mqtt_func) will get as parameter a local ( which belong to the sketch and not to the library ), that will have the additional code needed.

I'll try to explain more clearly:

void myIOT::mqtt_callback(ext_mqtt_func){
if (a == true ) {
do something

};

else {

ext_mqtt_func()); // calling another fucntion that belongs to sketch and not library

}
2
  • Note that it is better to use if (a) than comparing a boolean with true or false. If you name a better so it is clear it is a boolean it is better maintainable. Jan 11, 2019 at 22:51
  • @MichelKeijzers it is a string compare function to set a MQTT comman
    – guyd
    Jan 12, 2019 at 6:00

1 Answer 1

1

Yes, you can do it.

You pass function pointer as function parameter.

See this stackoverflow question:

https://stackoverflow.com/questions/2582161/c-function-pointer-as-parameter

1
  • thank you, it works great. Since it have to a string using a MQTT, this ext_fuct needs as well to pass this string - and the explanation above is for a no-arg function, so I did upgrade it to be typedef void (*cb_func)(char msg1[50]) that passes its string ( and it works great ), but I would rather pass it as a pointer to that string ( which fails )
    – guyd
    Jan 12, 2019 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.