[SOLVED] Raspi 2 and Ubidots

Dear all,
I have encountered a problem with variable from Ubidots.
A variable name Control is created. Raspberry PI is configured to obtain the data from the Ubitdots variable and trigger LED on/off accordingly. Suppose to be very straight forward, I can control everything fine at begining. But after the python code run for a while, the data seems corrupted and I am no more able to get any data from the variable…
Below is the coding used for ubidots. I have no problem sending data to Ubidots. Only has problem getting data from Ubidots. Please advise if I have done anything wrong.

api = ApiClient(‘XXX’)

output_control = api.get_variable(‘5649fc3c76254218a00e0960’) #Put here your valve ID’s

val = output_control.get_values(1) #Get the valve value from Ubidots

for a in val:
print a[‘value’]
if(a[‘value’]==1.0 ):
print “Valve ON”
#GPIO.output(13,False)
#GPIO.output(6,True)
test_value=1.0
var=1
writeNumber(var)
print"RPI:Hi Arduino, Sprinkler ON ",var
else:
print “Valve OFF”
#GPIO.output(13,True)
#GPIO.output(6,False)
var=2
writeNumber(var)
print"RPI:Hi Arduino, Sprinkler OFF ",var

Hello @ryudolow your code seems fine. Probably if there’s an occasional connection problem, your code will fall into an exception and break. For example, if Ubidots replies that the variable has no values, it will break in the line “print a[‘value’]”, or if your Raspi has no internet connection for a second, then it will break in the “get_value(1)” line.

To avoid this, put it into a try:except, for example If i wanted to read a variable continuously:

from ubidots import ApiClient

api = ApiClient(token="xxx")

control = api.get_variable("565f48fb7625424c867b2e1b")

while(1):
    try:
        val = control.get_values(1) #Get the valve value from Ubidots
        for a in val:
            print a['value']
            if(a['value']==1.0 ):
                print "Valve ON"
            else:
                print "Valve OFF"
    except:
        continue

Hi dijana,

al = control.get_values(1) #Get the valve value from Ubidots

Thanks for reply. May I know also why the above command will give me unstable value also?
For example if I set my variable to ‘1’ at the ubidots dashboard. I am supposed to receive ‘1’ every time. But when I run the python program, I am getting mixture of ‘1’ and ‘0’ for the variable value. Appreciated your advise.

Try printing the whole response to see what’s changing, for example:

al = control.get_values(1) #Get the valve value from Ubidots
print al

You can paste it here to see what is being different than the average response.

[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb5176254230c9e88f53’, u’timestamp’: 1449585489881L, u’created_at’: u’2015-12-08T14:38:09.881’, u’context’: {}, u’value’: 1.0}]
1.0
Valve ON
Result: 1.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666ec4952fdbb019ccd65a2’, u’timestamp’: 1449585737261L, u’created_at’: u’2015-12-08T14:42:17.261’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666ec5276254235fc3f4a38’, u’timestamp’: 1449585746412L, u’created_at’: u’2015-12-08T14:42:26.412’, u’context’: {}, u’value’: 1.0}]
1.0
Valve ON
Result: 1.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666ec6352fdbb019dcd5bf1’, u’timestamp’: 1449585763265L, u’created_at’: u’2015-12-08T14:42:43.265’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666ec6a762542346d95a54e’, u’timestamp’: 1449585770331L, u’created_at’: u’2015-12-08T14:42:50.331’, u’context’: {}, u’value’: 1.0}]
1.0
Valve ON
Result: 1.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF
Result: 0.0
[{u’url’: u’http://app.ubidots.com/api/v1.6/values/5666eb3b52fdbb0194cd5bf2’, u’timestamp’: 1449585467358L, u’created_at’: u’2015-12-08T14:37:47.358’, u’context’: {}, u’value’: 0.0}]
0.0
Valve OFF