Skip to main content

Framed binary USB bridge protocol for talking to an ESP32 from Termux (or any Linux host) — no root required.

Project description

ESP-Bridge

Framed binary USB bridge protocol for talking to an ESP32 from Termux — or any Linux host — no root required.

This is the transport layer extracted from NRSuite. It doesn't know anything about Wi-Fi, packet capture, or any specific use case — it just gets JSON commands and binary chunks reliably between an ESP32 and a host process over USB CDC, with automatic root/no-root backend detection. Use it to build your own ESP32-to-Termux tools.

Pairs with the BridgeProtocol PlatformIO library on the firmware side — install both and the two speak the same frames out of the box.

Install

pip install espbridge          # once published
# or, for local development:
git clone https://github.com/7wp81x/ESP-Bridge
cd ESP-Bridge
pip install -e .

On stock Termux you'll also need:

pkg install python termux-api libusb

plus the Termux:API app from F-Droid (not the Play Store version).

Quick start

See examples/echo_cmd.py for a full runnable example. The short version:

import os, espbridge as eb

backend = eb.detect_backend()          # "termux" or "root"
device  = eb.wrap_fd(int(os.environ["TERMUX_USB_FD"])) if backend == "termux" \
          else eb.wrap_direct()

ep_in, ep_out, iface = eb.get_cdc_endpoints(device)
eb.claim_device(device, iface, fd_wrapped=(backend == "termux"))
eb.reset_endpoint_toggles(device, ep_in, ep_out)

sender   = eb.Sender(device, ep_out)
receiver = eb.ReceiverThread(device, ep_in); receiver.start()
proto    = eb.Protocol(sender, receiver);    proto.start()

resp = proto.send_cmd("PING")          # blocks until RESP or timeout
print(resp)

Frame format

[MAGIC 2B: 0xAD 0xDE][TYPE 1B][ID 1B][LENGTH 4B LE][PAYLOAD NB]
Type Hex Direction Payload
CMD 0x01 Host → ESP32 JSON {"cmd": "...", "args": {...}}
RESP 0x02 ESP32 → Host JSON response, matched by frame ID
EVENT 0x03 ESP32 → Host Async JSON, id=0
PCAP 0x04 ESP32 → Host Raw binary chunk (id = chunk index) — despite the name, use this for any binary stream
ACK 0x05 Host → ESP32 JSON {"chunk": N} — flow control
HTML 0x06 Host → ESP32 Chunked raw payload upload

Commands (CMD) are entirely up to you — define whatever cmd strings and args your firmware understands. This library only handles framing, transport, and request/response matching.

What's included

  • protocol.py — frame builder/parser, Protocol class (send_cmd/on_event/on_pcap)
  • sender.py — thread-safe bulk OUT writer with retry
  • receiver.py — background bulk IN reader thread
  • usb_device.py — root vs. no-root backend detection, permission flow, endpoint discovery

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

espbridge-1.0.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

espbridge-1.0.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file espbridge-1.0.1.tar.gz.

File metadata

  • Download URL: espbridge-1.0.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for espbridge-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f3923ba3075bbec5159b698593b85b8a7e1543223a845df07ea3ecc1ba113f52
MD5 9085a254ae0d55d76b43fe1dd8d08cb2
BLAKE2b-256 96e8fc567dfff15efa2fda197c758f23a703d11e288126f571a042b5cffa7d43

See more details on using hashes here.

Provenance

The following attestation bundles were made for espbridge-1.0.1.tar.gz:

Publisher: python-publish.yml on 7wp81x/ESP-Bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file espbridge-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: espbridge-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for espbridge-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5beba0d1a43bb6971838f9e7ec2bc1dbe498e19226c97d78aca88baa0c64a06
MD5 b5f3f4d70aad06824afc9b2ef924aa29
BLAKE2b-256 ecd270e13d9d39184697762b4fd8f313deeb23bfe0b8894d2fa1c21df1fce444

See more details on using hashes here.

Provenance

The following attestation bundles were made for espbridge-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on 7wp81x/ESP-Bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page