A basic wrapper around the Ubidots API to share between projects.
This is an internal package to organize our code better accross projects. It will probably not released to PyPi. There is probably a similar project already. However that seems to be simple enough in order to not take on an extra external dependency.
Please install from Github and specify commit since this might be in constant change.
pip install git+https://github.com/tnc-ca-geo/ubidots-aws@124f2a14e12a7f85dbf8d35414a89526ab36148a
The package uses setuptools as build tool. Make sure it is installed in your environment.
Install the package in your local environment with
pip install . --upgradeimport requests
from ubidots.connectors import UbidotsConnector
token = `BBUS-0aA0aA0aA0aA0aA0aA0aA0aA0aA0aA
device = 'a_device'
connector = UbidotsConnector(device, token)
try:
response = connector.add_variable(
'temperature', 22, timestamp=1771617885).commit()
except requests.exceptions.HTTPError
print('HTTP error')
else:
print(response.content)It is essential to call .commit() to send the data to the Ubidots API. If it does not raise an HTTP error, the stored data will be cleared. If you need to clear the data manually by calling the .clear() method.
Several variables can be added to the same commit. However, for several instances of the same variable only the last one will be acknowledged. This is particular useful if all the variables should have the same timestamp.
response = UbidotsConnector(device, token
).add_variable('temperature', 22, timestamp=1771617885
).add_variable('humidity', 88.8, timestamp=1771617885
).commit()You can also add context to the variable, e.g.:
response = UbidotsConnector(device, token
).add_variable('position', 1, context={'lat': 10, 'lon': 10}
).commit()If the timestamp is ommitted Ubidots will use current time.
response = UbidotsConnector(device, token
).add_variable('water_level', 10, context={'unit': 'feet'}
).commit()