[SOLVED] Raspberry Pi retrieving data from Ubidots

Hi guys, as per title, Im trying to retrieve data from ubidots to Pi 2.
Im getting old values and its not updating real time on my Pi 2.
I am fine sending data from Android to Ubidots.

import time, sys
from ubidots import ApiClient                                                           #Library for Ubidots


#Connect to Ubidots

try:
    print "Requesting Ubidots token"
    api = ApiClient('###')
except:
    print "No internet connection"
    sys.exit(0)

print "Getting variables"
try:
    print "Connected to Ubidots"

except:
    print "No internet connection or no variables found"
    sys.exit(0)

while(True):
    # Read control variables
    api = ApiClient('###')
    output2_control = api.get_variable('###')
    lastValue2 = output2_control.get_values(1)
    print lastValue2
    time.sleep(5)

Result::

[{u'url': u'http://things.ubidots.com/api/v1.6/values/569c91247625424c39b4ed86', u'timestamp': 1453101359555L, u'created_at': u'2016-01-18T07:15:48.922', u'context': {}, u'value': 0.0}]
[{u'url': u'http://things.ubidots.com/api/v1.6/values/569c91247625424c39b4ed86', u'timestamp': 1453101359555L, u'created_at': u'2016-01-18T07:15:48.922', u'context': {}, u'value': 0.0}]

The url does not change, so does the value.
Help guys!

Hi there, it seems your script is correct; you get a response from the server containing data about the last value. If it’s not updating, might it be a problem on the sending side? A way to test this is to create a widget in your Ubidots dashboard called “Switch” and point it to this variable. Then manually toggle the switch to change the values between 0 and 1, then you should see this changes in the outcome of your Python script.

Hi hackmed,

Thanks for your input,tried the way u suggested, theres still a delay in between varies from 5 seconds to 20seconds?
Also my sending from Android is smooth and instantaneous.

Hey @RavePeace the delay might be due to network issues. If you see your data being updated in the Ubidots web interface, then it should be equally reflected when calling the variable through API. Is the delay between Switch Widget <> Ubidots, Android <> Ubidots, or Ubidots <> Raspi ?

Ubidots<>Raspi delayed, others was instant.

Ok, got it…can you try a ping request from the Raspi shell and see if the response time is below the 200 ms mark? also, you can reduce the polling time of your script to 1 second in the line

time.sleep(1)

(or even remove that line so it’s as fast as it gets).

Also, when declaring the Ubidots instance you can use a token instead of the API Key. This prevents the device from requesting a new token when it expires, saving one request (although in theory this only happens every 6 hours if the token is not being used).

api = ApiClient(token='###')

Ping request from Pi to? would appreciate if u can give the full command.

Tried using the token seems like more delayed.

Tried pinging api.ubidots.com , the speed is more than 200 ms like 250 ms

[{u’url’: u’http://things.ubidots.com/api/v1.6/values/56a76e6b7625421df9efa0b3’, u’timestamp’: 1453813355305L, u’created_at’: u’2016-01-26T13:02:35.305’, u’context’: {}, u’value’: 0.0}]
0.0
[{u’url’: u’http://things.ubidots.com/api/v1.6/values/56a76f987625422120f7e4c3’, u’timestamp’: 1453813656199L, u’created_at’: u’2016-01-26T13:07:36.199’, u’context’: {}, u’value’: 1.0}]
1.0
[{u’url’: u’http://things.ubidots.com/api/v1.6/values/56a76f987625422120f7e4c3’, u’timestamp’: 1453813656199L, u’created_at’: u’2016-01-26T13:07:36.199’, u’context’: {}, u’value’: 1.0}]
1.0
[{u’url’: u’http://things.ubidots.com/api/v1.6/values/56a76e6b7625421df9efa0b3’, u’timestamp’: 1453813355305L, u’created_at’: u’2016-01-26T13:02:35.305’, u’context’: {}, u’value’: 0.0}]
0.0

@hackmed am getting results like this. the value url keeps changing , this is just a small segment and its not the last value

Hi all, we double checked the Python library and can confirm the “get_values” Python function is working well:

_