Serial Bridge over WLAN with an ESP-01

From JoBaPedia
Jump to navigation Jump to search

Serial Bridge over WLAN

Sometimes you have a device that communicates over a serial port, but it is not in range of a server or notebook to connect it to. Then you can use an ESP8266 with the [ESP-Link] firmware.

  • Dl the firmware as described [here] (I used 2.2.3)
  • Connect to the new ESP* AP with the usual IP 192.168.4.1 and configure your wlan, hostname (e.g. esp-link1) and pin assignments
  • Configure optional services (syslog, ntp, mDNS) as needed
  • Connect Rx/Tx of the ESP8266 to the Tx/Rx of the device and optionally its reset line to the configured pin
  • Check the µC Console for expected serial output

On the receiving side, run socat like this (maybe use -t or -T for longer inactivity timeouts and make sure socat is restarted in a loop (see systemd service below)

$ sudo socat pty,link=/dev/ttyTCP0,ignoreeof,user=joachim,group=dialout,mode=770,raw,echo=0 tcp:esp-link1:23 &

This creates a virtual serial port that can be used like normal, e.g.

$ pio device monitor -p /dev/ttyTCP0 -b 115200 --raw

The --raw parameter is useful for showing ansi colored output (as used e.g. by esp32 idf logs).

To automate this, a systemd service can be handy: /etc/systemd/system/esp-link1.service

[Unit]
Description=Socat Virtual Serial Port of my esp-link1
After=network.target

[Service]
Type=simple
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=esp-link1-0

ExecStart=/usr/bin/socat -d pty,link=/dev/ttyTCP0,ignoreeof,user=root,group=dialout,mode=770,raw,echo=0 tcp:esp-link1:23
Restart=always

[Install]
WantedBy=multi-user.target

Activated as usual like this

sudo systemctl daemon-reload
sudo systemctl enable esp-link1
sudo systemctl start esp-link1
sudo systemctl status esp-link1

All done, now /dev/ttyTCP0 is permanently available. Enjoy the new networking device :)

As an alternative to socat (and independent from OS), a second esp-link device connected to an USB-Serial adapter could be used at the other end (not tested)