Connection error with REST API
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Connection error with REST API

by kallileo on Wed Dec 27, 2017 2:56 pm

I'm using the Rest API on Raspberry Pi to upload temperature and humidity to coresponding feeds of the Adafruit IO but I get connections errors too often.
It seems that when I use the PC only for browsing the data is uploaded normally but I could still get a connection error after couple of hours.
If qBittorent is running then there is almost immediately a connection error.
Is it possible to make the connection stable?
Should I switch to MQTT API for better performance?

Below is my code and the error message.

Code: Select all | TOGGLE FULL SIZE
from Adafruit_IO import Client
aio = Client('xxxx')

while (True):
   humidity, temperature = Adafruit_DHT.read_retry(11, 4)
   if humidity is not None and temperature is not None:
      print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))

      aio.send('Temperature', temperature)
      aio.send('Humidity', humidity)
      print('Failed to get reading. Try again!')

Error message in the terminal:
Temp=22.0*C Humidity=80.0%
2017-12-27 20:37:25
Traceback (most recent call last):
File "DHT11_adafruitIO.py", line 41, in <module>
aio.send('Temperature', temperature)
File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 90, in send
return Data.from_dict(self._post(path, {'value': value}))
File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 71, in _post
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))

Posts: 1
Joined: Wed Dec 27, 2017 2:26 pm

Re: Connection error with REST API

by abachman on Fri Dec 29, 2017 2:55 pm

Hi kallileo!

Strange that a bittorent client would interfere, that sounds like a problem the Pi is having rather than Adafruit IO. May be unrelated. May be memory :(

What kind of interval / delay are you using in between attempts to publish data to IO? If you're only publishing once every minute or 5 minutes or something, the MQTT API isn't going to get you any improvement in performance. It may even make it trickier to handle errors since it requires an always-on TCP connection to function rather than setting up the connection as needed for each publish event.

Probably the easiest troubleshooting thing to try first is to wrap your `aio.send()` code in a try: ... except: block. Something like this might work:
Code: Select all | TOGGLE FULL SIZE
import sys
    aio.send('Temperature', temperature)
    aio.send('Humidity', humidity)
    print("Failed to publish, unexpected error:", sys.exc_info()[0])

The `import sys` and `sys.exc_info` bits just provide some information about the error, but you can leave them out if it's always the same.

In general, with any code that relies on a remote system, exceptions and errors are always possible, even if all the systems in between are reliable 99.999% of the time. Any piece of the chain: the Pi, your router, your ISP, the Internet, Adafruit IO's service provider, Adafruit IO itself, could be at fault. With programs like this, ignoring the error and retrying after a few seconds is safe and reasonable. Especially when the errors are occasional ("after couple of hours") or transient ("if qBittorent is running").

To answer your question, "Is it possible to make the connection stable?" -> no, because we don't control every piece of the system. But, we can make the script robust in the face of common failure modes.

Another thing you could try is cutting your script down to the bare minimum required to read the data and publish it once and then trigger that from a cron task. Since both of your failure scenarios sound like they might be related to memory usage (which can be hard to diagnose), running the task from cron prevents memory leaks in Python or the Adafruit IO library from accumulating over a long run. I'm using this tactic to power a raspberry-pi 3 webcam that takes a picture once a minute and publishes it.

- adam

Posts: 168
Joined: Mon Feb 01, 2010 12:48 pm

Please be positive and constructive with your questions and comments.