0

redux: simple python example?
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.
Please be positive and constructive with your questions and comments.

redux: simple python example?

by bbulkow on Fri Jan 15, 2021 4:01 pm

Thank you for the response in my post from yesterday. However, it seems like you've locked the thread, so I can't respond. Not sure why you did that, something about forking to a a python API forum, but I don't know where that is, so I guess I'll create a new thread? My apologies if I'm not doing the right thing.

You asked what documentation I'm reading. The Adafruit.io site points to the Python github repo, which states the documentation is here:
https://adafruit-io-python-client.readt ... en/latest/

The quickstart is here:
https://adafruit-io-python-client.readt ... start.html

I've also read the example code in the repos, which has a different way of addressing feeds, creating the feed objects. The simple one that I've tried is:
https://github.com/adafruit/Adafruit_IO ... publish.py

In all cases, I created the topic through the web UI, which I think is the right pattern. Creating topics from an IOT device seems unusual - topics are a very heavyweight construct consuming server resources and I don't want a bug to cause creating a bunch. Just for fun, I did try the code which attempts to get the "feed handle" and if there's an exception create it, and it says the feed already exists, so something's working. From the documentation, it's a bit unclear in the python interface where one creates a client-side feed object (for posting and reading) vs creating a server-side feed object.

However, I don't think this is the issue, because of the following documentation sentence: "Note that you can use the send function to create a feed and send it a new value in a single call. It’s recommended that you use send instead of manually constructing feed instances." https://adafruit-io-python-client.readt ... feeds.html . This implies both should work. If I hadn't checked the name of my username and key over and over I would think I've got the wrong username and key - or maybe I do have some problem of UTF8 conversion. I'm running a fairly generic python 3.9.1 with very little added modules, though.

"What am I trying to do" - I am trying to run the world's simplest example that will post a data point to adafruit.io (thus, a topic). I thought I'd start simple. I have a variety of projects in mind, but I wanted to get a handle on the simple case first.

You'd like my full code. Sure! Here's the code for posting a point, with an init routine then the actual post. Once this works, I'll wrap it up in a class and do some fancier things, but I wanted the simplest thing working first. Here's the basic part of my code at the moment:

Code: Select all | TOGGLE FULL SIZE
import Adafruit_IO

ADAFRUIT_IO_USERNAME = "XXX"
ADAFRUIT_IO_KEY = "YYY"
ADAFRUIT_IO_feed_upload = "ttt1"
ADAFRUIT_IO_feed_download = "ttt3"

g_feed_upload = None
g_feed_download = None
g_aio = None

def log_configure():
   global g_aio, g_feed_upload, g_feed_download

   g_aio = Adafruit_IO.Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

   try:
      g_feed_upload = g_aio.feeds(ADAFRUIT_IO_feed_upload)
   except Adafruit_IO.RequestError as e:
      print(' request error: couldnt get the upload feed, aborting ',e)
      return(-1)
   except Exception as e:
      print(' couldnt get the upload feed, aborting, error ',e)
      return(-1)

   try:
      g_feed_download = g_aio.feeds(ADAFRUIT_IO_feed_download)
   except Adafruit_IO.RequestError as e:
      print(' coulnt get the download feed, aborting ',e)
      return(-1)
   except Exception as e:
      print(' couldnt get the download feed, aborting, error ',e)
      return(-1)

   return(0)


def log_datapoint(upload,download,timestamp):
   global g_aio, g_feed_upload, g_feed_download

   g_aio.send_data(g_feed_upload.key, upload)
   g_aio.send_data(g_feed_download.key, download)

   # g_aio.send(ADAFRUIT_IO_feed_upload, upload)
   # g_aio.send(ADAFRUIT_IO_feed_download, download)   

You can see I've tried using the string for the name of the topic (like some examples), and using the topic's key, used in other examples.

I asked about how I see the URL through the Python API. If I've got the wrong URL, I need to fix that? And how might I see the "base URI" or maybe endpoint constructed by the topic?

Thanks for the information on freeze. It appears my version is 2.5.0. Looking at the github repo, that's the latest, so I'm good.

The following page, I imagine, has an error that might have thrown me:
https://adafruit-io-python-client.readt ... er-methods

The "send batch data" section says you can send a list of data elements using "send_batch_data", but the code uses "create_data". Does one create the elements, then create a larger object which implicitly sends, or is this a typo and the code should be calling "send_batch_data"? I was looking at "send batch data" because it would seem the best API if I have one-feed-per-value instead of using the JSON-object-per-feed - but it looks like the batch can only be per-feed? So I'd only gain efficiency by buffering multiple samples, that's fine.

The github repo points to "readthedocs" frequently. I've now scanned the recent releases in Github here: https://github.com/adafruit/Adafruit_IO_Python/releases and I see there is an API for time, and it points to "readthedocs" for how to use it (2.3.2 release has a pointer). I've scanned all the pages on "readthedocs" and I don't see anything related to time at all, and given the release specifically points there, I must be going senile. Can you point to the docs page for this feature?

Re: creation time. You've pointed me to the REST HTTP documentation. Are those variables exposed in the Python API? Would you suggest I abandon the Python API and use the HTTP API? I'll certainly see my errors better, and it looks like the HTTP documentation has far more detail - is that API recommended?

"Feeds contain one value" - ok, so it's not recommended for that value to be a composite value, ie, using the feature of having the value be a JSON object? Instead, the "group" feature is used? According to this page, I can have my value be a JSON object, thus contain all the coordinated "values" and avoid having to correlate the portions of a sample myself by reading multiple feeds: https://io.adafruit.com/api/docs/cookbo ... oring-json This must be a normal case: I am measuring network information, and a "sample" is basically upload, download, and ping time(latency), and I always have the three "values" as part of a "sample". I have another project where I am measuring temperature and humidity - even more important to correlate, because humidity has to be adjusted based on temperature. Looking to understand the recommended best practice.

If there's an overview of these basic topics and I'm missed them, I apologize.

bbulkow
 
Posts: 8
Joined: Sat Oct 19, 2013 8:18 pm

Re: redux: simple python example?

by bbulkow on Fri Jan 15, 2021 4:44 pm

Oh, another point. I see there's an issue raised on the python API that says you'll get a 404 reading from an empty topic. I'm not reading per se, I'm writing, could this same issue be causing my problem? I guess the work-around would be inserting a dummy data point from the website just to get things started?

bbulkow
 
Posts: 8
Joined: Sat Oct 19, 2013 8:18 pm

Re: redux: simple python example?

by brubell on Wed Jan 20, 2021 11:38 am

Which thread did I lock? It was likely an accident on my end...

brubell
 
Posts: 1256
Joined: Fri Jul 17, 2015 10:33 pm

Re: redux: simple python example?

by bbulkow on Wed Jan 20, 2021 3:28 pm

It is called "Simple Python API Problems" and it was by me :-)

Feel free to merge back if you'd like, or not, I'm answering the questions you raised there

In any case, still looking to break the jam here and get my data points inserted. Looking forward to some advice.

( I haven't tried adding a dummy datapoint from the UI, if you think that'll help, nor have I tried just coding the HTTP interface, what do you advise)

bbulkow
 
Posts: 8
Joined: Sat Oct 19, 2013 8:18 pm

Re: redux: simple python example?

by brubell on Fri Jan 22, 2021 10:38 am

I unlocked that thread but I'll continue it here..


You asked what documentation I'm reading. The Adafruit.io site points to the Python github repo, which states the documentation is here:
https://adafruit-io-python-client.readt ... en/latest/

The quickstart is here:
https://adafruit-io-python-client.readt ... start.html

I've also read the example code in the repos, which has a different way of addressing feeds, creating the feed objects. The simple one that I've tried is:
https://github.com/adafruit/Adafruit_IO ... publish.py


I am confused - do some of these examples not work properly? If so, which?

"What am I trying to do" - I am trying to run the world's simplest example that will post a data point to adafruit.io (thus, a topic). I thought I'd start simple. I have a variety of projects in mind, but I wanted to get a handle on the simple case first.

You'd like my full code. Sure! Here's the code for posting a point, with an init routine then the actual post. Once this works, I'll wrap it up in a class and do some fancier things, but I wanted the simplest thing working first. Here's the basic part of my code at the moment:

Before we dive deeper into technical API discussions, I'm most interested in getting your code running and helping you post to IO.. Does the sample code you included above work as intended? If not, what errors does it throw?

brubell
 
Posts: 1256
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.