API Reference

Complete documentation for all classes, methods, and functions in the ESP32 GPIO Bridge Python library v0.1.8-beta. Now with production-ready stability!

ESP32GPIO Class

Main controller class providing all ESP32 GPIO operations. This is the primary interface for communicating with your ESP32.

ESP32GPIO(port, baudrate=115200, timeout=1.0, auto_connect=True)
Parameter Type Default Description
port str Serial port device path (e.g., '/dev/ttyUSB0', 'COM5')
baudrate int 115200 Serial communication speed
timeout float 1.0 Serial timeout in seconds
auto_connect bool True Whether to automatically connect on initialization
Basic Usage
from esp32_gpio_bridge import ESP32GPIO, find_esp32_port

# Auto-detect ESP32 port
port = find_esp32_port()

# Create ESP32GPIO instance
esp = ESP32GPIO(port)

# Or use context manager (recommended)
with ESP32GPIO(port) as esp:
    # Your code here
    pass

Connection Management

connect()

connect() → None

Establish connection to the ESP32 GPIO Bridge.

esp = ESP32GPIO(port, auto_connect=False)
esp.connect()

close()

close() → None

Close connection to ESP32 and cleanup resources.

esp.close()

get_version()

get_version() → str

Get ESP32 firmware version.

version = esp.get_version()
print(f"Firmware version: {version}")

get_status()

get_status() → Dict[str, Any]

Get ESP32 status including failsafe state.

status = esp.get_status()
print(f"State: {status['state']}")
print(f"Failsafe: {status['failsafe_engaged']}")

GPIO Operations

set_pin_mode()

set_pin_mode(pin: int, mode: str) → None

Set GPIO pin mode.

Parameters

  • pin: GPIO pin number (0-39)
  • mode: 'IN', 'OUT', or 'IN_PULLUP'
esp.set_pin_mode(2, "OUT")      # LED pin
esp.set_pin_mode(0, "IN_PULLUP") # Button pin

digital_write()

digital_write(pin: int, value: Union[int, bool]) → None

Write digital value to GPIO pin.

Parameters

  • pin: GPIO pin number (0-39)
  • value: 0, 1, False, or True
esp.digital_write(2, 1)    # Turn LED on
esp.digital_write(2, True)  # Same as above
esp.digital_write(2, 0)     # Turn LED off

digital_read()

digital_read(pin: int) → int

Read digital value from GPIO pin.

Returns

  • Digital value (0 or 1)
button_state = esp.digital_read(0)
if button_state == 0:
    print("Button pressed")

batch_digital_write()

batch_digital_write(pin_values: Dict[int, Union[int, bool]]) → None

Write multiple digital pins in a single command for efficiency.

esp.batch_digital_write({
    2: 1,   # Pin 2 HIGH
    4: 0,   # Pin 4 LOW
    5: 1    # Pin 5 HIGH
})

PWM Operations

pwm_init()

pwm_init(pin: int, frequency: int = 5000, resolution: int = 8) → int

Initialize PWM on a pin.

Parameters

  • pin: GPIO pin number
  • frequency: PWM frequency in Hz (1-40000)
  • resolution: PWM resolution in bits (1-16)
channel = esp.pwm_init(2, frequency=1000, resolution=8)
print(f"PWM channel: {channel}")

pwm_write()

pwm_write(pin: int, duty_cycle: int) → None

Set PWM duty cycle on a pin.

esp.pwm_write(2, 128)  # 50% duty cycle (8-bit)

pwm_set_duty_percent()

pwm_set_duty_percent(pin: int, percent: float) → None

Set PWM duty cycle as a percentage.

esp.pwm_set_duty_percent(2, 75.0)  # 75% duty cycle

pwm_stop()

pwm_stop(pin: int) → None

Stop PWM on a pin and release the channel.

esp.pwm_stop(2)

Analog Operations

analog_read()

analog_read(pin: int) → int

Read analog value from ADC pin.

Valid Pins

  • GPIO 32-39 (ADC1 channels)
  • Returns: 12-bit value (0-4095)
raw_value = esp.analog_read(34)
voltage = (raw_value / 4095.0) * 3.3
print(f"Voltage: {voltage:.2f}V")

analog_write()

analog_write(pin: int, value: int) → None

Write analog value to DAC pin.

Valid Pins

  • GPIO 25, 26 (DAC1, DAC2)
  • Value: 8-bit (0-255)
esp.analog_write(25, 128)  # 1.65V on GPIO 25
esp.analog_write(26, 255)  # 3.3V on GPIO 26

I2C Operations

i2c_init()

i2c_init(sda: int = 21, scl: int = 22, frequency: int = 100000) → None

Initialize I2C bus.

esp.i2c_init(sda=21, scl=22, frequency=100000)

i2c_scan()

i2c_scan() → List[int]

Scan I2C bus for devices.

devices = esp.i2c_scan()
print(f"Found devices: {[hex(addr) for addr in devices]}")

i2c_write()

i2c_write(address: int, data: Union[int, List[int], bytes]) → None

Write data to I2C device.

# Write single byte
esp.i2c_write(0x3C, 0x00)

# Write multiple bytes
esp.i2c_write(0x3C, [0x00, 0xFF, 0x80])

i2c_read()

i2c_read(address: int, num_bytes: int) → List[int]

Read data from I2C device.

data = esp.i2c_read(0x48, 4)
print(f"Read data: {data}")

EEPROM Operations

eeprom_read()

eeprom_read(address: int) → int

Read a single byte from EEPROM.

value = esp.eeprom_read(0)
print(f"Byte at address 0: {value}")

eeprom_write()

eeprom_write(address: int, value: int) → None

Write a single byte to EEPROM (requires commit).

esp.eeprom_write(0, 42)
esp.eeprom_commit()  # Save to flash

eeprom_read_block()

eeprom_read_block(address: int, length: int) → List[int]

Read a block of bytes from EEPROM.

data = esp.eeprom_read_block(0, 16)
print(f"Read 16 bytes: {data}")

eeprom_write_block()

eeprom_write_block(address: int, data: List[int]) → None

Write a block of bytes to EEPROM (requires commit).

data = [10, 20, 30, 40, 50]
esp.eeprom_write_block(0, data)
esp.eeprom_commit()

eeprom_write_string()

eeprom_write_string(address: int, text: str) → None

Write a string to EEPROM.

esp.eeprom_write_string(100, "Hello ESP32!")
esp.eeprom_commit()

eeprom_read_string()

eeprom_read_string(address: int, length: int) → str

Read a string from EEPROM.

text = esp.eeprom_read_string(100, 12)
print(f"Stored text: {text}")

eeprom_commit()

eeprom_commit() → None

Commit EEPROM changes to flash memory.

Must be called after write operations to persist changes.

eeprom_clear()

eeprom_clear() → None

Clear all EEPROM data (set all bytes to 0) and commit.

esp.eeprom_clear()  # Sets all bytes to 0 and commits

Utility Functions

find_esp32_port()

find_esp32_port(timeout: float = 2.0) → Optional[str]

Auto-detect ESP32 GPIO Bridge by actively probing serial ports.

from esp32_gpio_bridge import find_esp32_port

port = find_esp32_port()
if port:
    print(f"Found ESP32 on: {port}")
else:
    print("ESP32 not found")

list_serial_ports()

list_serial_ports() → List[Tuple[str, str]]

List all available serial ports.

from esp32_gpio_bridge import list_serial_ports

ports = list_serial_ports()
for device, description in ports:
    print(f"{device}: {description}")

select_port()

select_port() → Optional[str]

Interactive port selection menu.

from esp32_gpio_bridge import select_port

port = select_port()
if port:
    print(f"Selected: {port}")
else:
    print("No port selected")

reset_failsafe()

reset_failsafe() → bool

Manually reset/disable failsafe mode.

success = esp.reset_failsafe()
if success:
    print("Failsafe was engaged and reset")
else:
    print("Failsafe was not engaged")

Error Handling

Common Exceptions

ValueError

Raised for invalid parameters (pin numbers, modes, etc.)

try:
    esp.analog_read(99)  # Invalid pin
except ValueError as e:
    print(f"Invalid pin: {e}")

IOError

Raised for connection issues and communication errors

try:
    esp.digital_write(2, 1)
except IOError as e:
    print(f"Connection error: {e}")

TimeoutError

Raised when no response is received from ESP32

try:
    version = esp.get_version()
except TimeoutError as e:
    print(f"Timeout: {e}")