adafruit_requests setting timeout

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
mikek2
 
Posts: 1
Joined: Wed Mar 29, 2023 1:57 pm

adafruit_requests setting timeout

Post by mikek2 »

Hi,

I am working on a simple display with an LED screen to fetch from an http server within a raspberry pi on my local network and display the value it returns on the screen. It connects just fine several times and displays the values properly, but after a few minutes it hangs on a connection and the display halts while this occurs. The only solution I have seen so far is to unplug it and plug it back in, but within a few minutes it starts hanging on the connection again.

I saw reference of a settimeout() function on https://docs.circuitpython.org/projects ... t/api.html. I have not been able to figure out how to use it though.

Here is the relevant code

Code: Select all

def getRaspberryData():
    global display_text
    try:
        print("Fetching text from", raspberry_url)
        
        r = requests.get(raspberry_url)
        display_text = r.text
        r.close()
        print('received: ' + display_text)
        return display_text
    except Exception as e:
        print('error getting display text', e)
        return display_text


def initialSetup():
    global raspberry_url
    # Get wifi details and more from a secrets.py file
    try:
        from secrets import secrets
    except ImportError:
        print("WiFi secrets are kept in secrets.py, please add them there!")
        raise
    raspberry_url = secrets["raspberry"]
    # If you are using a board with pre-defined ESP32 Pins:
    esp32_cs = DigitalInOut(board.ESP_CS)
    esp32_ready = DigitalInOut(board.ESP_BUSY)
    esp32_reset = DigitalInOut(board.ESP_RESET)

    spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
    esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

    requests.set_socket(socket, esp)
    
    print("Connecting to AP...")
    while not esp.is_connected:
        try:
            esp.connect_AP(secrets["ssid"], secrets["password"])
        except OSError as e:
            print("could not connect to AP, retrying: ", e)
            continue

    getRaspberryData()
 
The final output before it hangs:

Code: Select all

received: Test message
Fetching text from http://[my-server-ip]:8000/output.txt
I don't particularly care if it fails every now and then, but since it is a single-threaded application it halts everything while it hangs. Ideally I would like some way to time out the get request.

(It seems to only occur when reaching out to the local raspberry pi, but I can still reach it from other devices on the network so I'm not sure what's going on with it)

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”