Skip to content

espcamera blocks network requests after capture until deinit called #10678

@brentru

Description

@brentru

CircuitPython version and board name

Adafruit CircuitPython 10.0.1 on 2025-10-09; Adafruit Camera with ESP32S3

Code/REPL

import os
import time
import ssl
import binascii
import digitalio
import adafruit_pycamera
import board
import wifi
import socketpool
import adafruit_requests
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

aio_username = os.getenv("ADAFRUIT_AIO_USERNAME")
aio_key = os.getenv("ADAFRUIT_AIO_KEY")

print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(
    os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
)
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")

# Create initial connection to get/create the feed
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
io = IO_HTTP(aio_username, aio_key, requests)

# Adafruit IO feed configuration
try:
    # Get the 'camera' feed from Adafruit IO
    feed_camera = io.get_feed("camera")
except AdafruitIO_RequestError:
    # If no 'camera' feed exists, create one
    feed_camera = io.create_new_feed("camera")

print("Feed ready:", feed_camera["key"])

# Initialize memento camera
pycam = adafruit_pycamera.PyCamera()
# Turn off TFT backlight
pycam.display.brightness = 0.0


# Capture an image and send it to Adafruit IO."""
print("Focusing camera...")
pycam.autofocus()
print("Capturing image...")
jpeg = pycam.capture_into_jpeg()
print("Captured image!")

if jpeg is not None:
    # Encode JPEG data into base64 for sending to Adafruit IO
    print("Encoding image...")
    encoded_data = binascii.b2a_base64(jpeg).strip().decode('utf-8')
    print("JPEG sz:", len(jpeg), "bytes")
    print("Encoded sz:", len(encoded_data), "characters")

    # Attempt to send encoded_data to Adafruit IO camera feed
    print("Sending image to Adafruit IO...")
    io.send_data(feed_camera["key"], encoded_data)
    print("Sent image to IO!")
else:
    print("ERROR: JPEG frame capture failed!")

Behavior

code.py output:
Connecting to Transit
Connected to Transit!
Feed ready: camera
Found AW9523
Initializing camera
Found camera OV5640 (240 x 176) at I2C address 3c
colors 16777215
colors 16777215
/lib/adafruit_pycamera/ov5640_autofocus.bin
init done @ 2.681
No SD card inserted
Focusing camera...
Capturing image...
Captured 2687 bytes of jpeg data
Resolution 240 x 240
Captured image!
Encoding image...
JPEG sz: 2687 bytes
Encoded sz: 3584 characters
Sending image to Adafruit IO...
Traceback (most recent call last):
  File "code.py", line 59, in <module>
  File "adafruit_io/adafruit_io.py", line 604, in send_data
  File "adafruit_io/adafruit_io.py", line 548, in _post
  File "adafruit_requests.py", line 725, in post
  File "adafruit_requests.py", line 649, in request
  File "adafruit_connection_manager.py", line 331, in get_socket
  File "adafruit_connection_manager.py", line 248, in _get_connected_socket
KeyboardInterrupt:

Description

Network requests after pycam object initialization seem to fail. This previously worked on CircuitPython 9.x projects like the IoT Doorbell

I ruled out simple memory issues via gc analysis and deleting large objects (like the jpeg object)

Workaround (below) is to call pycam.camera.deinit(), then pycamera.camera.init() between captures

Additional information

Adding a call to pycam.camera.deinit() causes the code to succeed.

    # Attempt to send encoded_data to Adafruit IO camera feed
    print("Sending image to Adafruit IO...")
    pycam.camera.deinit()
    io.send_data(feed_camera["key"], encoded_data)
    print("Sent image to IO!")

Output:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Connecting to Transit
Connected to Transit!
Feed ready: camera
Found AW9523
Initializing camera
Found camera OV5640 (240 x 176) at I2C address 3c
colors 16777215
colors 16777215
/lib/adafruit_pycamera/ov5640_autofocus.bin
init done @ 2.67798
No SD card inserted
Focusing camera...
Capturing image...
Captured 2527 bytes of jpeg data
Resolution 240 x 240
Captured image!
Encoding image...
JPEG sz: 2527 bytes
Encoded sz: 3372 characters
Sending image to Adafruit IO...
Sent image to IO!

Code done running.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions