0

Can't connect to AIO when script runs from local.rc
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Can't connect to AIO when script runs from local.rc

by exploder on Wed Mar 27, 2019 5:19 pm

Ahoy,

Pardon my newness to this. I've got a RasPi and a very simple Python script intended to keep watch for brownouts in some funky building wiring; ultimately I want the pi to send my feed a 1 every time it boots up, and a 0 every minute after that. The script works great when I run it manually. However, when I try to make it run from /etc/rc.local, it fails -- most of the time, but not always -- and in ways I have so far failed to understand.

Note that rc.local is set to run the script as the standard pi user, not as root, so I would think that would rule out environment variables, but maybe I'm missing something.

Initially, this was the error shown:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "/home/pi/i_am_alive.py", line 13, in <module>
    IsUp_feed = aio.feeds('grumbletron.is-up')
  File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 241, in feeds
    return Feed.from_dict(self._get(path))
  File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 90, in _get
    proxies=self.proxies)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 487, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='io.adafruit.com', port=443): Max retries exceeded with url: /api/v2/exploder/feeds/grumbletron.is-up (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x75e7ae50>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

It looked like maybe it was glitching out because the network somehow hadn't had time to come up after boot -- so I added a 60-second delay to my script, before it even loads the Adafruit IO module.

Code: Select all | TOGGLE FULL SIZE
import time
time.sleep(60)
from Adafruit_IO import Client, Feed
ADAFRUIT_IO_KEY = '[redacted]'
ADAFRUIT_IO_USERNAME = '[redacted]'
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
IsUp_feed = aio.feeds('[insert feed here]')
JustBooted = 1
while True:
    aio.send(IsUp_feed.key, JustBooted)
    JustBooted = 0
    time.sleep(60)

After that it worked perfectly some of the time, but the rest of the time, for no clear reason, it would fail at boot with a completely different error:

Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "/home/pi/i_am_alive.py", line 13, in <module>
    IsUp_feed = aio.feeds('grumbletron.is-up')
  File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 241, in feeds
    return Feed.from_dict(self._get(path))
  File "/usr/local/lib/python2.7/dist-packages/Adafruit_IO/client.py", line 90, in _get
    proxies=self.proxies)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 497, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('', 'osrandom_rand_bytes', 'getrandom() initialization failed.')],)",)

For a while I thought the success rate was good enough for jazz, so I set it up with a read-only file system, and now so far it seems like it's back to not working at all, even when the pi is jumpered into read-write mode.

Is there a different way I should be doing any of this?

exploder
 
Posts: 3
Joined: Sat Dec 11, 2010 4:10 am

Please be positive and constructive with your questions and comments.