Data Send Failure and Feed Reconnect Issues

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
cajeck
 
Posts: 7
Joined: Thu Jun 17, 2021 1:28 pm

Data Send Failure and Feed Reconnect Issues

Post by cajeck »

So I figured out how to repurpose the python code for the Environmental Monitor guide to use my sensors. It ran good for a few hours, but then in the middle of the night when I was asleep, it received this error:

Code: Select all

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 0xb5715250>: 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/cajeck/feeds/pressure/data (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb5715250>: 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 "environmental_monitor.py", line 111, in <module>
    aio.send(pressure_feed.key, int(pressure_data))
  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/cajeck/feeds/pressure/data (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xb5715250>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
From what I understood, it just timed out. I tried to reconnect and got this error next:

Code: Select all

Traceback (most recent call last):
  File "environmental_monitor.py", line 62, in <module>
    gas_feed = aio.feeds('Gas')
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 274, in feeds
    return Feed.from_dict(self._get(path))
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 118, in _get
    self._handle_error(response)
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 108, in _handle_error
    raise RequestError(response)
Adafruit_IO.errors.RequestError: Adafruit IO request failed: 404 Not Found - not found - There is no feed with the key 'Gas'. You can create one using the feed creation API https://io.adafruit.com/api/docs/#create-feed or at https://io.adafruit.com/cajeck/feeds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "environmental_monitor.py", line 68, in <module>
    gas_feed = aio.create_feed(Feed(name='Gas'))
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 285, in create_feed
    return Feed.from_dict(self._post(path, {"feed": feed._asdict()}))
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 127, in _post
    self._handle_error(response)
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_IO/client.py", line 105, in _handle_error
    raise RequestError(response)
Adafruit_IO.errors.RequestError: Adafruit IO request failed: 400 Bad Request - ['Name must be unique within the selected group', 'Key must be unique within the selected group', 'Group feeds is invalid']
I'm not sure why it keeps trying to make a new feed? How do I need to write this for it to connect to the feeds that have already been made?

Here's the code for the environmental monitor:

Code: Select all

"""
'environmental_monitor.py'
===============================================================================
Example of sending I2C sensor data
from multiple sensors to Adafruit IO.

Tutorial Link: https://learn.adafruit.com/adafruit-io-air-quality-monitor

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!

Author(s): Brent Rubell for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.

Dependencies:
    - Adafruit_Blinka (CircuitPython, on Pi.)
        (https://github.com/adafruit/Adafruit_Blinka)
    - Adafruit_CircuitPython_SGP30.
        (https://github.com/adafruit/Adafruit_CircuitPython_SGP30)
    - Adafruit_CircuitPython_VEML6070.
        (https://github.com/adafruit/Adafruit_CircuitPython_VEML6070)
    - Adafruit_CircuitPython_BME280.
        (https://github.com/adafruit/Adafruit_CircuitPython_BME280)

CAJECK NOTE:
	I'm altering the original code to use the ltr390 for uv, and bme680 for everything else!
"""

# Import standard python modules
import time

# import Adafruit Blinka
import board
import busio

# import CircuitPython sensor libraries
import adafruit_ltr390
import adafruit_bme680

# import Adafruit IO REST client
from Adafruit_IO import Client, Feed, RequestError

# loop timeout, in seconds.
LOOP_DELAY = 10

# 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 = [key]

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

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

try: # if we already have the feeds, assign them.
    gas_feed = aio.feeds('Gas')
    uv_feed = aio.feeds('UV')
    temperature_feed = aio.feeds('Temperature')
    humidity_feed = aio.feeds('Humidity')
    pressure_feed = aio.feeds('Pressure')
except RequestError: # if we don't, create and assign them.
    gas_feed = aio.create_feed(Feed(name='Gas'))
    uv_feed = aio.create_feed(Feed(name='UV'))
    temperature_feed = aio.create_feed(Feed(name='Temperature'))
    humidity_feed = aio.create_feed(Feed(name='Humidity'))
    pressure_feed = aio.create_feed(Feed(name='Pressure'))
# Create busio I2C
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
# Create LTR390 object.
ltr = adafruit_ltr390.LTR390(i2c)
# Create BME680 object.
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)
bme680.sea_level_pressure = 1013.25

while True:
    print('Reading sensors...')

    # Read LTR390.
    uv_data = ltr.uvi

    # Read BME680.
    gas_data = bme680.gas
    temp_data = bme680.temperature
    # convert temperature (C->F)
    temp_data = int(temp_data) * 1.8 + 32
    humid_data = bme680.humidity
    pressure_data = bme680.pressure

    print('sending data to adafruit io...')
    # Send BME680 Data to Adafruit IO.
    print("Gas: %d ohm" % bme680.gas)
    aio.send(gas_feed.key, gas_data)
    time.sleep(2)
    # Send ltr390 Data to Adafruit IO.
    print('UV Index: ', uv_data)
    aio.send(uv_feed.key, uv_data)
    time.sleep(2)
    # Send BME680 Data to Adafruit IO.
    print('Temperature: %0.1f C' % temp_data)
    aio.send(temperature_feed.key, temp_data)
    print("Humidity: %0.1f %%" % humid_data)
    aio.send(humidity_feed.key, int(humid_data))
    time.sleep(2)
    print("Pressure: %0.1f hPa" % pressure_data)
    aio.send(pressure_feed.key, int(pressure_data))
    # avoid timeout from adafruit io
    time.sleep(LOOP_DELAY * 60)
I remembered to take out the IO key!

So with this I was able to get it working for a while, as you can see here:

Image

But after that first error, I haven't been able to reconnect. At least, not without losing my feed data. I'd like to see some of this longterm data on a graph, so I want to avoid this. Any help would be appreciated...

User avatar
cajeck
 
Posts: 7
Joined: Thu Jun 17, 2021 1:28 pm

Re: Data Send Failure and Feed Reconnect Issues

Post by cajeck »

Okay! I looked at my feed info and saw that my keys were capitalized when they should be lowercase. This seems to have resolved the reconnect issue.

But I'm still wondering about the data send failure. I extended the loop delay from 60 to 120. Maybe my little pi zero was struggling to keep up at that pace? I don't know. Just trying it.

Still would love some input, if anyone has any.

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

Re: Data Send Failure and Feed Reconnect Issues

Post by brubell »

But I'm still wondering about the data send failure. I extended the loop delay from 60 to 120. Maybe my little pi zero was struggling to keep up at that pace? I don't know. Just trying it.
Which error, specifically, was the "data send failure"?

User avatar
cajeck
 
Posts: 7
Joined: Thu Jun 17, 2021 1:28 pm

Re: Data Send Failure and Feed Reconnect Issues

Post by cajeck »

brubell wrote:
But I'm still wondering about the data send failure. I extended the loop delay from 60 to 120. Maybe my little pi zero was struggling to keep up at that pace? I don't know. Just trying it.
Which error, specifically, was the "data send failure"?
I don't know precisely, that was just a guess. In the first code block I shared in my original post I kept seeing something about connection errors and timeouts.

It could be my internet connection I suppose. I'm simply uncertain. From what I've observed internet appears stable enough, but I'm not watching this all the time.

It's since happened a few more times but I was able to reconnect without issue

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

Re: Data Send Failure and Feed Reconnect Issues

Post by brubell »

Ok, if it occurs again, please copy and paste the error here. I'll take a look!

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”