Console and network over serial connection

From JMK Wiki
Jump to navigation Jump to search

Setting up serial connection to the Raspberry Pi

Serial cable

Some of the working serial cables that can be found on ebay are the following:

  • USB To RS232 TTL UART PL2303HX Auto Converter USB to COM Cable Adapter Module (works with Linux)
  • USB 2.0 to TTL UART Module Serial Converter CP2102 STC Replace Ft232 Module (works with Windows 10)

Connecting the cable

The serial converter has four cables, of which 3 must be connected to the Raspberry Pi:

  • GND (ground, black): to one of the ground pins on the RPI (e.g.: pin 6)
  • RXD (receiver, white): to the UART TXD pin on the RPI (pin 8)
  • TXD (transmitter, green): to the UART RXD pin on the RPI (pin 10)
  • VCC (5V power, red): either unconnected (when the RPI is powered via microUSB) or to one of the 5V pins of the RPI to power from the serial cable (e.g.: pin 2)

For more information about the pins see https://pinout.xyz/

Before the first boot

After installing the image to the SD card, make sure that the /boot/cmdline.txt has the serial console set up:

console=serial0,921600

Serial terminal client

Source: https://elinux.org/RPi_Serial_Connection

Linux

Screen

user@localhost ~ $ screen /dev/ttyUSB0 921600

Exit with CTRL+A : then type exit

Minicom

user@localhost ~ $ minicom -b 921600 -o -D /dev/ttyUSB0

Exit with CTRL+A X

Windows

PuTTY

  • connection type: serial
  • serial line: the COM port of the serial cable (e.g.: COM3)
  • speed: 921600

Booting to the Raspberry Pi

Open the serial connection on the client (either Linux or Windows) and power up the Raspberry. The boot messages should be already visible in the terminal emulator. When the login prompt is visible enter the username and the password:

Raspbian GNU/Linux 9 raspberrypi ttyAMA0
raspberrypi login: pi
Password:

Setting the serial connection speed

Set the speed of the serial connection to 112.5kBps (900kbps)

root@localhost ~ # stty -F /dev/ttyAMA0 921600 raw

To set this on every boot edit the /etc/rc.local file:

root@localhost ~ # nano -w /etc/rc.local

Add the following right before the exit 0 line:

if [ -c /dev/ttyAMA0 ]; then
    stty -F /dev/ttyAMA0 921600 raw
fi

Serial network connection

Sources: https://elinux.org/RPi_Serial_Connection, http://www.instructables.com/id/Connect-the-Raspberry-Pi-to-network-using-UART/

Host setup

In this example the host will be a Gentoo Linux.

Prerequisites

Install ppp:

root@localhost ~ # emerge -av ppp

Seting up ppp provider

Create a ppp provider configuration called serial0 (192.168.0.20 is IP address of the host, and 192.168.0.45 is the IP address of the RPI):

root@localhost ~ # nano -w /etc/ppp/peers/serial0
/dev/ttyUSB0 921600
192.168.1.20:192.168.1.40
noauth
local
debug
nocrtscts
persist
maxfail 0
holdoff 1
proxyarp
passive

Configuring IP forwarding

For the client to be able to reach the network beyond the host IP forwarding needs to be enabled:

root@localhost ~ # sysctl -w net.ipv4.ip_forward=1

To make this permanent edit the sysctl configuration file:

root@localhost ~ # nano -w /etc/sysctl.d/99-sysctl.conf

And add the following line (or uncomment it if it's already present):

net.ipv4.ip_forward = 1

Listening for ppp connections

Start the ppp interface and let it listen to any incoming connections:

root@localhost ~ # pon serial0

Client setup

In this example the client will be a Raspbian installation which unfortunately lacks the pppd by default. Power down the RPI remove the SD card and load it into another machine.

Download ppp and its dependencies

Go to http://raspbian.raspberrypi.org/raspbian/pool/main/p/ppp/ and find the ppp package and its dependency libpcap at http://raspbian.raspberrypi.org/raspbian/pool/main/libp/libpcap/. Download the armhf archives to the SD card (replace /mnt/mmc/p2 with the path where the root filesystem of the SD card is mounted):

root@localhost ~ # cd /mnt/mmc/p2/var/cache/apt/archives/

Umount and remove the SD card, put it back into the RPI and boot up.

Installing ppp and its dependencies

First try installing with apt:

root@localhost ~ # apt install ppp


This will fail if the RPI installation is out of date and apt is looking for older versions of the packages. In this case install using dpkg:

root@localhost ~ # dpkg -i /var/cache/apt/archives/libpcap0.8_1.8.1-3_armhf.deb
root@localhost ~ # dpkg -i /var/cache/apt/archives/ppp_2.4.7-1+4_armhf.deb

Setting up a ppp interface

Create a ppp provider configuration called serial0 (192.168.0.45 is the desired IP address, and 192.168.0.20 is the IP address of the host):

root@localhost ~ # nano -w /etc/ppp/peers/serial0
/dev/ttyAMA0 921600
192.168.1.40:192.168.1.20
noauth
local
debug
nocrtscts
persist
maxfail 0
holdoff 1
defaultroute
lcp-echo-interval 5
lcp-echo-failure 3

Create a ppp0 interface and set its provider to the above one:

root@localhost ~ # nano -w /etc/network/interfaces
auto ppp0
iface ppp0 inet ppp
    provider serial0

Disable serial console

In order to be able to use the serial connection for networking purposes the serial console needs to be disabled by removing the console=serial0,921600 kernel argument.

root@localhost ~ # nano -w /boot/cmdline.txt

Enable SSH

Since the serial console will no longer be accessible we need to enable SSH access to the RPI:

root@localhost ~ # raspi-config

Under the Interfacing options select SSH and enable it.

Reboot and connect

Double-check everything (as the serial console will not function anymore) and restart the RPI.

After the RPI has finished booting run the following on the host machine to initiate the connection:

root@localhost ~ # pon serial0