Adding location metadata in MQTT using FunHouse libraries.

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
mrrmay
 
Posts: 42
Joined: Sun Mar 28, 2021 11:51 am

Adding location metadata in MQTT using FunHouse libraries.

Post by mrrmay »

Ideally, what I'd like to do is grab my location data from secrets.py, format it appropriately (preferably JSON), and send it via MQTT using the FunHouse library.

My code is an elaboration of the Temperature Logger Example on Read the Docs. I stole the Heat Index function from the DHT library and want to use secrets.py to store location data as described in IoT Air Quality Sensor with Adafruit IO.

I am entirely vexed about how to add the location metadata. I didn't find anything in adafruit_funhouse library that would do this. So I dropped to the PortalBase library but didn't find any methods of adding the metadata to the data being sent.

Is there a way to attach the location metadata to the 'data' block of push_to_io in PortalBase?

The other option could be to use url_encode and manually format the url.

Either way, I'm not deft enough (yet) to make it work. If it's not possible, then I'll import the adafruit_io and adafruit_minimqtt and work that way.

Any ideas?

User avatar
eherrada
 
Posts: 161
Joined: Thu Jan 04, 2018 4:59 pm

Re: Adding location metadata in MQTT using FunHouse librarie

Post by eherrada »

Hey, all you have to do is pass a third argument to io.send_data with your metadata. I'm not entirely sure of the formatting requirements, but in the example you reference, they format it as a tuple so I'd go with that.
Image

Try this:

Code: Select all

location_metadata = (secrets['latitude'], secrets['longitude'], secrets['elevation'])
funhouse.network.mqtt_publish("FEED_KEY", VALUE, metadata=location_metadata)
replacing FEED_KEY with the name of your feed and value with the value to publish.

User avatar
eherrada
 
Posts: 161
Joined: Thu Jan 04, 2018 4:59 pm

Re: Adding location metadata in MQTT using FunHouse librarie

Post by eherrada »

Not sure if the image is working, so I'll try it one or two different ways
<blockquote class="imgur-embed-pub" lang="en" data-id="a/l46RYQ3" data-context="false" ><a href="//imgur.com/a/l46RYQ3"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

https://imgur.com/a/l46RYQ3

User avatar
mrrmay
 
Posts: 42
Joined: Sun Mar 28, 2021 11:51 am

Re: Adding location metadata in MQTT using FunHouse librarie

Post by mrrmay »

Thank you, @dherrera!

It was the first thing I tried... threw an error about too many arguments.

I ended up loading adafruit_io and working with that. I was hoping to use the FunHouse libraries as much as possible... purely for vanity, it puts fewer lines in my code. LOL.

Both FunHouse and PortalBase, which FunHouse is built on, lack the option for a location argument. Maybe, if I poke at the code long enough in GitHub I can add the functionality.

Here's the PortalBase code on GitHub starting at line 365. It's in Python so I think I can wrap my novice brain around it ...

Code: Select all

def push_to_io(self, feed_key, data):
        """Push data to an adafruit.io feed
        :param str feed_key: Name of feed key to push data to.
        :param data: data to send to feed
        """

        io_client = self._get_io_client()

        while True:
            try:
                feed_id = io_client.get_feed(feed_key)
            except AdafruitIO_RequestError:
                # If no feed exists, create one
                feed_id = io_client.create_new_feed(feed_key)
            except RuntimeError as exception:
                print("An error occured, retrying! 1 -", exception)
                continue
            break

        while True:
            try:
                io_client.send_data(feed_id["key"], data)
            except RuntimeError as exception:
                print("An error occured, retrying! 2 -", exception)
                continue
            except NameError as exception:
                print(feed_id["key"], data, exception)
                continue
            break
I see two options. First, to add an argument to the push_to_feed function. Second, create a data object containing the metadata and give that to push_to_feed. I wonder if the underlying code would accept and parse either option.

User avatar
eherrada
 
Posts: 161
Joined: Thu Jan 04, 2018 4:59 pm

Re: Adding location metadata in MQTT using FunHouse librarie

Post by eherrada »

Oh yeah, you're definitely right. I'll see about adding that to the library

User avatar
mrrmay
 
Posts: 42
Joined: Sun Mar 28, 2021 11:51 am

Re: Adding location metadata in MQTT using FunHouse librarie

Post by mrrmay »

Whoa! I was just looking at the GitHub code and noticed two more parameters in the function for metadata and precision. That is awesome. :)

It's pretty cool to see, and learn a trick or two.

Thank you.

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”