Generic onMessage callback

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
jpasqua
 
Posts: 7
Joined: Thu Aug 20, 2020 8:35 pm

Generic onMessage callback

Post by jpasqua »

I am considering moving from Blynk to AdafruitIO and would value some advice. I will be using the C++ library.

As I understand it, if I want to subscribe to a feed within a group, I use the onMessage function to specify a callback that will be invoked when the subscribed data is updated. The callback receives a AdafruitIO_Data object from which I can identify the feed, but not the group. This means I can't have a completely generic callback that decides what to do based on the group/feed info.

Think of my use case as a dynamic dashboard on a small standalone display. Group/feed names are not bound at compile time. The feed/group names are read from a JSON file at run time and may be updated through a web interface. I don't see a way to write a single generic onMessage handler since I don't know which groups/feeds I'm being supplied. I also can't set up callbacks statically in the code since I don't know how many group/feeds there will be.

I do have an option that I believe will work which is to create std::function instances for each group/feed that encode the group name, but that's kind of ugly and wasteful.

I may be missing something obvious or am thinking about the setup incorrectly. I would appreciate any advice.

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Generic onMessage callback

Post by brubell »

I don't see a way to write a single generic onMessage handler since I don't know which groups/feeds I'm being supplied. I also can't set up callbacks statically in the code since I don't know how many group/feeds there will be.
Is this possible within Blynk? How are you structuring your code with their client library's callback methods - I know they also use Arduino/C++ ?

User avatar
jpasqua
 
Posts: 7
Joined: Thu Aug 20, 2020 8:35 pm

Re: Generic onMessage callback

Post by jpasqua »

With Blynk you can issue an explicit read on an ID/virtual pin combo, as opposed to a notification callback. It wasn't a callback in that case. At least that's the way their API used to work before they upended everything and went a new direction. I prefer the callback notification model rather than polling periodically.

I could just forget about groups and fake them by making every feed name be GROUP_FEED. For example, if I was going to have a group named "IndoorWeatherStation" with feeds named "Temp", "Humidity", and "Baro", I could just flatten the name space and have three feeds named "IndoorWeatherStation_Temp", "IndoorWeatherStation_Humidity", and "IndoorWeatherStation_Baro". Certainly not ideal, but it would work I think.

User avatar
jpasqua
 
Posts: 7
Joined: Thu Aug 20, 2020 8:35 pm

Re: Generic onMessage callback

Post by jpasqua »

For the time being I just forked the library and made the callback type a std::function. That way I can pass in class members or lambdas with captures to handle additional state. As I learn more about the library and the best way to use it, I may find a better way.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”