adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 36 forks source link

Requests + IO generates RuntimeError: Sending request failed #139

Closed mwweinberg closed 11 months ago

mwweinberg commented 11 months ago

I'm having problems using requests while also pushing data to adafruit io.

The code below works when it is run as is. However, if I try to upload sensor data by uncommenting the lines:

    # try:
    #     funhouse.push_to_io(FEED_2_5, aqdata["pm25 env"])
    #     funhouse.push_to_io(TEMP_FEED, funhouse.peripherals.temperature - TEMPERATURE_OFFSET)
    #     funhouse.push_to_io(HUM_FEED, funhouse.peripherals.relative_humidity)
    #     print("data pushed")
    # except:
    #     print("error uploading data, moving on")
    # #print io counter
    # print("data point number " + str(io_counter))
    # io_counter += 1

I get an error:

Traceback (most recent call last): File "code.py", line 89, in File "adafruit_requests.py", line 728, in get File "adafruit_requests.py", line 668, in request File "adafruit_requests.py", line 515, in _get_socket RuntimeError: Sending request failed

I'm running all of this on a funhouse attached to a PMSA003I Air Quality Breakout (https://www.adafruit.com/product/4632) with the 20230718 library bundle. The target URL returns a 129 byte payload. Does this look like a known problem? Thanks!

import time
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_pm25.i2c import PM25_I2C
from adafruit_funhouse import FunHouse

#for the external API
import adafruit_requests as requests
import keys
import socketpool
import ssl
import wifi

reset_pin = None

funhouse = FunHouse(default_bg=None)

# Create library object, use 'slow' 100KHz frequency!
i2c = board.I2C()
# Connect to a PM2.5 sensor over I2C
pm25 = PM25_I2C(i2c, reset_pin)

print("Found PM2.5 sensor, reading data...")

# Turn on WiFi
funhouse.network.enabled = True
print("wifi on")
# Connect to WiFi
funhouse.network.connect()
print("wifi connected")

#these variables sets up the requests
pool = socketpool.SocketPool(wifi.radio)
requests = requests.Session(pool, ssl.create_default_context())

#IO Stuff
FEED_2_5 = "2pointfive"
TEMP_FEED = "temp"
HUM_FEED = "humidity"
TEMPERATURE_OFFSET = (
    3  # Degrees C to adjust the temperature to compensate for board produced heat
)
io_counter = 1

#Colors
BLACK = (0,0,0)
GREEN = (0,228,0)
YELLOW = (255, 255, 0)
ORANGE = (255,126,0)
RED = (255,0,0)
PURPLE = (143,63,151)
MAROON = (126,0,35)

while True:

    try:
        aqdata = pm25.read()
        # print(aqdata)
    except RuntimeError:
        print("Unable to read from sensor, retrying...")
        continue

    #IO Stuff

    # Push to IO using REST
    # try:
    #     funhouse.push_to_io(FEED_2_5, aqdata["pm25 env"])
    #     funhouse.push_to_io(TEMP_FEED, funhouse.peripherals.temperature - TEMPERATURE_OFFSET)
    #     funhouse.push_to_io(HUM_FEED, funhouse.peripherals.relative_humidity)
    #     print("data pushed")
    # except:
    #     print("error uploading data, moving on")
    # #print io counter
    # print("data point number " + str(io_counter))
    # io_counter += 1

    # get remote AQI data
    # #https://learn.adafruit.com/adafruit-funhouse/getting-the-date-time   
    target_URL = keys.AQI_URL
    # print(target_URL)
    # pool = socketpool.SocketPool(wifi.radio)

    response = requests.get(target_URL)
    jsonResponse = response.json()
    print(jsonResponse)
    # currentAQI = jsonResponse[0]["AQI"]

    time.sleep(120)
mwweinberg commented 11 months ago

It looks like the problem was that I was unintentionally opening a second set of sockets as part of creating the requests object. Replacing this line:

requests = requests.Session(pool, ssl.create_default_context())

with this line:

requests = funhouse.network._wifi.requests

allows requests to use the socket that was already created for the IO stuff.