Skip to content

Instantly share code, notes, and snippets.

@RoganDawes
Created March 11, 2024 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoganDawes/3c339644c1fcf80c2d598aa29838593a to your computer and use it in GitHub Desktop.
Save RoganDawes/3c339644c1fcf80c2d598aa29838593a to your computer and use it in GitHub Desktop.
ESPHome YAML to configure an ESP32 for UART PitM duties, both with local relay and external relay using socat.
esphome:
name: serial-pitm
friendly_name: Serial Person in the Middle
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 0s
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Serial Pitm Hotspot"
captive_portal:
web_server:
external_components:
- source: github://oxan/esphome-stream-server
uart:
- id: uart_bus1
rx_pin: GPIO25
tx_pin: GPIO26
baud_rate: 9600
debug:
direction: BOTH
dummy_receiver: false
- id: uart_bus2
rx_pin: GPIO14
tx_pin: GPIO27
baud_rate: 9600
# debug:
# direction: RX
# dummy_receiver: false
# Provides a mechanism on the ESP32 to change the baud rate without
# having to recompile and flash new firmware. Can be accessed via the
# web interface
select:
- id: change_baud_rate
name: Baud rate
platform: template
options:
- "2400"
- "9600"
- "38400"
- "57600"
- "115200"
- "256000"
- "512000"
- "921600"
initial_option: "9600"
optimistic: true
restore_value: True
internal: false
entity_category: config
icon: mdi:swap-horizontal
set_action:
- lambda: |-
auto u1 = id(uart_bus1);
auto u2 = id(uart_bus2);
u1->flush();
u2->flush();
uint32_t new_baud_rate = stoi(x);
ESP_LOGD("change_baud_rate", "Changing baud rate from %i to %i", u1->get_baud_rate(), new_baud_rate);
if (u1->get_baud_rate() != new_baud_rate ||
u2->get_baud_rate() != new_baud_rate) {
u1->set_baud_rate(new_baud_rate);
u2->set_baud_rate(new_baud_rate);
u1->load_settings();
u2->load_settings();
}
stream_server:
- uart_id: uart_bus1
port: 2001
- uart_id: uart_bus2
port: 2002
esphome:
name: serial-pitm
friendly_name: Serial Person in the Middle
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 0s
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Serial Pitm Hotspot"
captive_portal:
web_server:
external_components:
- source: github://ssieb/esphome_components
components: [ uart_mitm ]
uart:
- id: uart_bus1
rx_pin: GPIO25
tx_pin: GPIO26
baud_rate: 9600
debug:
direction: BOTH
dummy_receiver: false
- id: uart_bus2
rx_pin: GPIO14
tx_pin: GPIO27
baud_rate: 9600
# debug:
# direction: RX
# dummy_receiver: false
# Provides a mechanism on the ESP32 to change the baud rate without
# having to recompile and flash new firmware. Can be accessed via the
# web interface
select:
- id: change_baud_rate
name: Baud rate
platform: template
options:
- "2400"
- "9600"
- "38400"
- "57600"
- "115200"
- "256000"
- "512000"
- "921600"
initial_option: "9600"
optimistic: true
restore_value: True
internal: false
entity_category: config
icon: mdi:swap-horizontal
set_action:
- lambda: |-
auto u1 = id(uart_bus1);
auto u2 = id(uart_bus2);
u1->flush();
u2->flush();
uint32_t new_baud_rate = stoi(x);
ESP_LOGD("change_baud_rate", "Changing baud rate from %i to %i", u1->get_baud_rate(), new_baud_rate);
if (u1->get_baud_rate() != new_baud_rate ||
u2->get_baud_rate() != new_baud_rate) {
u1->set_baud_rate(new_baud_rate);
u2->set_baud_rate(new_baud_rate);
u1->load_settings();
u2->load_settings();
}
uart_mitm:
uart1: uart_bus1
uart2: uart_bus2
#!/bin/sh
# Relays traffic from on UART stream server on the ESP32 through
# Mallet listening as a SOCKS server on localhost:1080, relayed
# from Mallet to the other stream server on the ESP32
socat TCP:serial-pitm:2001 SOCKS:127.0.0.1:serial-pitm:2002,socksport=1080
#!/bin/sh
# Relays traffic from one UART stream server on the ESP32 to
# the other one, dumping a hex trace of the traffic in the process
socat -x TCP:serial-pitm:2001 TCP:serial-pitm:2002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment