This project aims to make a serial console
connected to the ESP32
accessible using SSH
.
This way the ESP32
will act like an USB-UART-Converter
,
but over the network.
-
Wire up your
ESP32
to the serial console of the target:TARGET ESP32 ( UART2
)RX
TX2
(GPIO17
)TX
RX2
(GPIO16
)GND
GND
3.3V
3.3V
(optional)Remember that the
ESP32
is using3.3V
logic levels. Connecting it to5V
logic level serial consoles might damage yourESP32
.
You can also connect the3.3V
pins to power theESP32
from the target. -
Create the file
src/credentials.hpp
:#define WIFISSID "WIFI SSID" #define WIFIPASS "WIFI Password" #define SSHUSER "user" #define SSHPASS "user"
-
Generate
RSA
hostkeys for theSSH
server:mkdir data ssh-keygen -t rsa -f data/hostkey_rsa
-
Upload the filesystem image to the
ESP32
-
Compile and upload the project to the
ESP32
-
Access the device using:
ssh -t user@esp32sshserial
and log in using the credentials.
Ifesp32sshserial
doesn't work, you can try appending your networks suffix, or just use the IP-Address of theESP32
, which you may find in the USB Serial Console of theESP32
during boot, or in your routers webinterface. -
Your
SSH
session is now directly connected withSerial2
of theESP32
. -
Use
Ctrl-G
(Bell) to terminate theSSH
connection.
Context: SinceCtrl-C
is directly forwarded to the serial console, and there is no equivalent for it on serial consoles, the connection effectively cannot terminate ever.Ctrl-G
is captured by theESP32
before relaying to Serial and will do nothing else than terminate the connection. You can then anytime reconnect and continue where you left of.
Remember: Disconnecting from theSSH
session will not log you out of the connected serial console.
If you have a working connection with a linux machine,
you can do these additional steps
to enhance the serial experience
almost to a native SSH
-like level.
Import the resizeserial
function:
resizeserial() {
local old=$(stty -g)
stty raw -echo min 0 time 5
printf '\033[18t' > /dev/tty
local rows
local cols
IFS=';t' read -r _ rows cols _ < /dev/tty
stty "$old"
stty cols "$cols" rows "$rows"
}
Then run resizeserial
anytime to resize the terminal.
Run
export TERM=xterm-256color
to enable colored output.
Both above mentioned mechanisms can be automated very easily.
Create a file named .serialrc
:
#!/bin/bash
# if not accessing from serial console, stop futher execution
[ "$TERM" != "vt220" ] && return 0
resizeserial() {
local old=$(stty -g)
stty raw -echo min 0 time 5
printf '\033[18t' > /dev/tty
local rows
local cols
IFS=';t' read -r _ rows cols _ < /dev/tty
stty "$old"
stty cols "$cols" rows "$rows"
}
# force enable color
export TERM=xterm-256color
# run resizeserial everytime before command execution
#trap resizeserial DEBUG
PROMPT_COMMAND='_trace=yes'
trap '[[ "$_trace" == "yes" ]] && resizeserial; _trace=no' DEBUG
Then in .bashrc
directly after the interactive terminal check insert
[ -f .serialrc ] && source .serialrc
It needs to be in the beginning, for the color settings to apply properly.
Huge thanks to
LibSSH-ESP32
for making this possible.