99 Python - geo-tp/ESP32-Bus-Pirate GitHub Wiki
Using Python with the ESP32 Bus Pirate
This page shows how to communicate with the ESP32 Bus Pirate using Python over a serial connection.
๐งฐ Requirements
- Python 3.x
pyserial
library (pip install pyserial
)- ESP32 Bus Pirate connected via USB
๐ Setup
Make sure you know the correct serial port:
- On Linux/Mac: typically
/dev/ttyUSB0
,/dev/tty.SLAB_USBtoUART
, etc. - On Windows: something like
COM3
,COM4
, etc.
You can list available ports using:
python -m serial.tools.list_ports
๐งช Basic Example
import serial
import time
# Replace with your actual port
PORT = "/dev/ttyUSB0"
BAUDRATE = 115200
with serial.Serial(PORT, BAUDRATE, timeout=1) as ser:
# Give device time to reset if needed
time.sleep(2)
# Wake up the Bus Pirate: send any random character
# This will trigger the welcome banner
ser.write(b"\n") # or any character
time.sleep(0.5)
# Read and print welcome message
while ser.in_waiting:
print(ser.readline().decode(errors="ignore").strip())
# Example command (e.g., enter WiFi mode)
ser.write(b"m wifi\n")
time.sleep(0.2)
# Read response
while ser.in_waiting:
print(ser.readline().decode(errors="ignore").strip())
๐ก Tips
- Always send
\n
to simulate Enter key. - Some commands require waiting before reading the full response.
- Use
ser.flushInput()
andser.flushOutput()
to clear buffers if needed. - For scripting instructions, you can send raw blocks like:
ser.write(b"[0x33 r:8]\n") # Send 1WIRE read ROM command
๐ ๏ธ Libraries
You can also create wrappers around modes or commands. Here's a simple helper:
class BusPirate:
def __init__(self, port="/dev/ttyUSB0", baudrate=115200):
self.ser = serial.Serial(port, baudrate, timeout=1)
time.sleep(2)
def send(self, command):
self.ser.write((command + "\n").encode())
def read_all(self):
output = []
while self.ser.in_waiting:
output.append(self.ser.readline().decode(errors="ignore").strip())
return output
def close(self):
self.ser.close()
โ Conclusion
Using pyserial
, you can fully automate your interactions with the ESP32 Bus Pirate, from scripting protocols to issuing mode commands.