[SOLVED] Getting device by name

Hi,

Playing around with some Onion Omega’s and python to create some sensor modules. I’m new to Python and have been using the Ubidots Python library from https://github.com/ubidots/ubidots-python

I’m trying to build my code as generic as possible. The Omega’s have a unique hostname that I use as the device name. The library only seems to support using the IDs to push and pull data.

Is there any way to use the library to pull the id by using the hostname. get_all_sources returns only the names it seems?

Or should I just write my own little library with generic python requests?

Bas

Greetings @basrijn, the Ubidots library right now does not support to retrieve or send values using device label and variable labels, but you may develop your own snippet using the python requests library, in this link you can find how to do that.

Also, attached I send you two example scripts for sending and retrieving data, simply adapt it to your needs.

Hope it helps you.

scripts.zip (1.1 KB)

Hi,

Hoping I can sneak in a generic Python question (I suspect). My datalogger has two code path:

  • If access to ubidots is available, it submits the data straight to the website
  • If no access is available, the same data goes into a local Sqlite3 DB

Once the connection is back the data that was cached is pushed out.

Very, very close to having it all working. I generate the data to submit as per the example you attached. It works great in the online mode. But I get SQL errors if I try to insert it in the DB, so I do ‘str(data)’ before I submit into the DB.

When uploading to Ubidots the effective code has become:
json=str(data)
instead off:
json=data

And the data does not get uploaded to Ubidots

Debug output looks fine (as in, the string representation seems identical):

[20171122 05:40:08] INFO … We have locally cached data to push out to Ubidots (5 records)
{‘pressure’: [{‘timestamp’: 1511328725, ‘value’: 1009}], ‘temperature’: [{‘timestamp’: 1511328725, ‘value’: ‘21.1’}], ‘humidity’: [{‘timestamp’: 1511328725, ‘value’: ‘47.2’}]}
[20171122 05:40:13] INFO Reading sensor data
[20171122 05:40:13] INFO … Reading data from sensor BME280
[20171122 05:40:13] INFO … Sending data to Ubidots
{‘pressure’: [{‘timestamp’: 1511329214, ‘value’: 1009}], ‘temperature’: [{‘timestamp’: 1511329214, ‘value’: ‘21.1’}], ‘humidity’: [{‘timestamp’: 1511329214, ‘value’: ‘47.3’}]}

So close, I hope I can store the whole result set in the DB, instead of individual meter reading that I then have to built up like what I do when reading the actual meter.

I suspect I somehow need to turn my string again into whatever datatype it was before. No clue where to start Googling for that. All hints/tips welcome, my attempt at Python for this project can be found at https://pastebin.com/kecfMPHj

TX!
Bas

And some more Google found me the answer: ast.literal_eval()

One reamining issue: my epoch timestamp lands in 1970 (1970-01-18 03:48:50 -08:00), the values however are correct for today?

Bas

Hi there!, please send your values in milliseconds and not in seconds, i.e you should send this timestamp 1511329214 as 1511329214000.

Regards

Hi,

Awesome, I guess I should have actually read what it said about timestamps :slight_smile:

One more question, are you able to share what rate limits are in place. I will sometimes have tens of thousands of points to push in (if the unit ha sbeen offline for a good amount of time).

I’m guessing I will have to add some chunking on my end?

Tx
Bas

Greetings @basrijn, the rate limit for free educational users is of dot per second, if you need to put thousands of dots in a single request I advise you to take a business plan that fits your post sample needs.

Regards