ubidots / ubidots-python

A python API client for Ubidots
ubidots.com/docs/libraries/python.html
MIT License
30 stars 13 forks source link

Error pushing values #15

Open sibero80 opened 6 years ago

sibero80 commented 6 years ago

I have this very simple test script:

from ubidots import ApiClient
import subprocess, re, math, time

# Create an ApiClient object

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

# Get a Ubidots Variable

variable = api.get_variable('####################')

# Here is where you usually put the code to capture the data, either through yo$

count = 1

while(1):

    # Write the value to your variable in Ubidots
    count += 1
    aqi = 30
    print (count)
    print (aqi)
    response = variable.save_value({"value": aqi})
    #print(response)
    time.sleep(4)

But after 40 - 60 seconds of pushing data, I get this exception:

Traceback (most recent call last):
  File "ubi3.py", line 26, in <module>
    response = variable.save_value({"value": aqi})
  File "/usr/local/lib/python3.4/dist-packages/ubidots/apiclient.py", line 148, in wrapped_f
    return fn(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/ubidots/apiclient.py", line 306, in save_value
    return self.bridge.post('variables/' + self.id + '/values', data).json()
  File "/usr/local/lib/python3.4/dist-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
jotathebest commented 6 years ago

This error is related with a downtime server, I believe that you should capture it inside a try - except