Serial console to my Pi gateway shows escape codes instead of colours and htop is a mess
Me again (chicken coop ammonia person). The coop Pi now runs as a small gateway: reads the NH3 sensor and two temperature probes and publishes to Hermetry. The Wi-Fi out in the shed is terrible, so when it drops I plug a CP2102 USB-TTL cable into the Pi’s GPIO UART and use PuTTY on my laptop at 115200.
Logging in works, but my gateway script prints coloured log lines and over serial I get stuff like:
^[[32mINFO^[[0m published nh3_ppm=9.1
^[[33mWARN^[[0m wifi reconnect (3)
And htop draws garbage and wraps at 80 columns even with a maximised PuTTY window. Over SSH both look perfect. Is serial just limited like that?
Try picocom instead of PuTTY? I always found PuTTY weird on serial.
Same thing in picocom from my Linux box, I tried.
It’s the Pi side, not the terminal program. Two things are missing on a serial login that SSH gives you for free.
The terminal type: SSH passes your TERM along (xterm-256color from PuTTY). The serial getty sets TERM to vt220, which has no colour, so programs that check TERM fall back or print raw codes. Check with echo $TERM.
The window size: there’s no way for the serial line to tell the Pi how big your window is, so it assumes 80×24 and never changes.
I put this in ~/.bashrc on every box I console into:
case "$(tty)" in
/dev/ttyS0|/dev/ttyAMA0|/dev/serial0)
export TERM=xterm-256color
stty rows 50 cols 160 # match your PuTTY window
;;
esac
If you install the xterm package you can type resize instead of hardcoding the size, it asks the terminal and sets it.
Your script’s colours will then work, assuming the logging library checks TERM.
Side note: the same script also logs to journald when it runs as a service, and colour codes in the journal are annoying. Most logging libs let you turn colour off when stdout isn’t a tty.
+1 to the bashrc snippet, same on our gateways. I’d add export LANG=C.UTF-8 there too if you see box-drawing characters break in htop.
TERM was vt220, exactly. With the bashrc block it’s colours and a full-width htop over serial now. I also added Eric’s tip, the service runs with colour off so the journal is clean. Thanks all!
- You must be logged in to reply to this topic.
Join the conversation
Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

