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
ESP32to the serial console of the target:TARGET ESP32 ( UART2)RXTX2(GPIO17)TXRX2(GPIO16)GNDGND3.3V3.3V(optional)Remember that the
ESP32is using3.3Vlogic levels. Connecting it to5Vlogic level serial consoles might damage yourESP32.
You can also connect the3.3Vpins to power theESP32from the target. -
Create the file
src/credentials.hpp:#define WIFISSID "WIFI SSID" #define WIFIPASS "WIFI Password" #define SSHUSER "user" #define SSHPASS "user"
-
Generate
RSAhostkeys for theSSHserver: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.
Ifesp32sshserialdoesn'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 theESP32during boot, or in your routers webinterface. -
Your
SSHsession is now directly connected withSerial2of theESP32. -
Use
Ctrl-G(Bell) to terminate theSSHconnection.
Context: SinceCtrl-Cis directly forwarded to the serial console, and there is no equivalent for it on serial consoles, the connection effectively cannot terminate ever.Ctrl-Gis captured by theESP32before 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 theSSHsession 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-256colorto 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' DEBUGThen in .bashrc directly after the interactive terminal check insert
[ -f .serialrc ] && source .serialrcIt needs to be in the beginning, for the color settings to apply properly.
Huge thanks to
LibSSH-ESP32
for making this possible.