maintaining connections

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

maintaining connections

Post by Rcayot »

hello!

I have a dashboard that I am trying to get working, and so far it works great except that about once a day or so it loses its connection. Now my internet is via satellite so performance and latency can be an issue at times.

it appears that the name resolution is timing out. I am wondering if just delaying the reconnect will allow the program to continue to run, or does the 'next' data point cause the timeout?

I can delay the time between data sends, right now it is about 2 minutes, I could easily set it to 5 or even 10 minutes. I have not seen much in the way the underlying libraries work.

Anyway, a suggestion,. or to point me in a direction where I can find out exactly the issue is and correct it. IF it is jus tmy internet connection, then there is not much I can do.

I DID read that after so many connection requests in a short time the IO server will stop reconnect requests for a period of time,. that is why I trhink building in a short delay (somewhere?) would possibly help.

Thanks in advance.

Roger

So, here is the errors that dump when the script (running headless) is disconnected:

Temp=21.0*C Humidity=26.0% Pressure=1009.9hPa
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 57, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 841, in _validate_conn
conn.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 301, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0xb5abced0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='io.adafruit.com', port=443): Max retries exceeded with url: /api/v2/Rcayot/feeds/pressure/data (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb5abced0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "temp_humidity.py", line 56, in <module>
aio.send(pressure_feed.key, str(pressure))
File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 154, in send_data
return self.create_data(feed, payload)
File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 254, in create_data
return Data.from_dict(self._post(path, data._asdict()))
File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 126, in _post
data=json.dumps(data))
File "/usr/lib/python3/dist-packages/requests/api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='io.adafruit.com', port=443): Max retries exceeded with url: /api/v2/Rcayot/feeds/pressure/data (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb5abced0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: maintaining connections

Post by brubell »

Are you using the Adafruit IO Python MQTT client or HTTP client?

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: maintaining connections

Post by Rcayot »

thanks Brubell.

I believe it is HTTP, I based it on your example temp_humity.py in the 'basics' folder.

Here is the code

Code: Select all

"""
'temp_humidity.py'
==================================
Example of sending analog sensor
values to an Adafruit IO feed.

Author(s): Roger Ayotte bassed on: Brent Rubell


Dependencies:
    - Adafruit IO Python Client
    - adafruit_bme280
"""
import time

import board
import busio
import adafruit_bme280

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

from Adafruit_IO import Client, Feed, Data, RequestError
import datetime

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'mykey'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'my username'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
pressure_feed = aio.feeds('pressure')

while True:
    humidity = bme280.relative_humidity
    temperature = bme280.temperature
    pressure = bme280.pressure
    if humidity is not None and temperature is not None and pressure is not None:
        print('Temp={0:0.1f}*C Humidity={1:0.1f}% Pressure={2:0.1f}hPa'.format(temperature, humidity, pressure))
        # Send humidity and temperature feeds to Adafruit IO
        temperature = '%.2f'%(temperature)
        humidity = '%.2f'%(humidity)
        pressure = '%.2f'%(pressure)
        aio.send(temperature_feed.key, str(temperature))
        aio.send(humidity_feed.key, str(humidity))
        aio.send(pressure_feed.key, str(pressure))
        
    else:
        print('Failed to get DHT22 Reading, trying again in ', 20, 'seconds')
    # Timeout to avoid flooding Adafruit IO
    time.sleep(120)

User avatar
petespee
 
Posts: 12
Joined: Sun Jan 13, 2019 11:13 pm

Re: maintaining connections

Post by petespee »

This is interesting - I came here with similar & other problems.

I get a very similar error to you pulling data from OpenWeatherMap. I've had to create a Try/Except routine that basically just waits 10 seconds if an exception is thrown and then tries again. But this can go on for a while. (Running in Python on a RasPi)

One problem I haven't resolved is updating my weather dashboard from my M4 Feather and Ethernet Featherwing. At some point the dashboard just stops updating, but nothing changes on my M4 Feather. It still keeps working as nothing is wrong. I've had to implement a microcontroller.reset() function that executes every couple hours to restart the connection.

Please post here if you have any luck.

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: maintaining connections

Post by Rcayot »

Pete,

Can youi post your try/exception code?

I am a real NOOB.

Roger

User avatar
petespee
 
Posts: 12
Joined: Sun Jan 13, 2019 11:13 pm

Re: maintaining connections

Post by petespee »

Absolutely! I'm still learning Python as well so I cant claim that this is great form, but it seems to work!

Code: Select all

#Main Loop
while True:
    try:
        time_stamp = int(time.monotonic() % 300)
        if time_stamp == 0:
            now = time.strftime("%H:%M:%S")
            nowd = "Updated " + time.strftime("%H:%M on %A")
            owmtemp = get_owm(data_source)
            print("OWM Reported Outdoor Temperature is", owmtemp, "C at", now)
            post_aio("owm-temp", str(owmtemp))

            #Get Data from Adafruit_IO
            ftemp = float(get_aio("temperature"))
            fhumid = float(get_aio("humidity"))
            fpress = float(get_aio("pressure"))

            print("AIO Measured Outdoor Temperature is", ftemp, "C")
            print("AIO Measured Outdoor Humidity is", fhumid, "%")
            print("AIO Measured Outdoor Pressure is", fpress, "hpa")

            #Work out Dewpoint on Pi (Rather than on M4 Airlift)
            gamma = (17.62 * ftemp /(243.12 + ftemp)) + math.log(fhumid / 100.0)
            dewpoint = round((243.12 * gamma) / (17.62 - gamma), 2)
            print("Calculated Outdoor Dewpoint is", dewpoint)
            post_aio("dewpoint", str(dewpoint))

            update_display()

    except Exception as ex:
        print("Exception ", str(ex), " at ", now)
        with open(errorlog, "a") as errorfile:
            errorfile.write("Exception " + str(ex) + " at " + now)
            errorfile.write("\n")
        time.sleep(10)
        continue

User avatar
Rcayot
 
Posts: 321
Joined: Sat Feb 08, 2020 6:48 pm

Re: maintaining connections

Post by Rcayot »

I changed my update time to 300 seconds, and have not been dropped since.

Thanks all for the help.

Roger

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”