Bering-uClibc 7.x - User Guide - Advanced Topics - Setting Up a Raspberry PI as a net to serial gateway

From bering-uClibc
Revision as of 20:30, 3 June 2021 by Jeanrocco (Talk | contribs) (Using the gateway)

Jump to: navigation, search
Setting Up a Raspberry PI as a net to serial gateway
Prev Bering-uClibc 7.x - User Guide Next


Goal

This setup shows how to use the LEAF tarball distribution for the raspberry PI, to access the serial communication port of a distant PC Engines APU2C2 using a simple SSH session. To access the serial port, we will first SSH to the Raspberry PI and then run a communication program like minicom or picocom.

This setup can be generalized to make any "device serial communication port" accessible through an SSH network session. Knowing that RS-232C communication distances are short, why not use a wired network instead that can reach a lot farther.

All of this was initially done using the standard Raspberry PI Raspbian OS distribution, but using the LEAF distribution OS instead, really transforms the somewhat flaky PI into a very stable and dependable production platform, since everything will then run in rams and no writings to the SD card will ever occur once in operation... It is a well known fact that, it is only a matter of time for the Raspberry PI to corrupt it's SD card, thus making it fail to boot or run. This is mainly caused by random power fails occurring at the same time the PI is writing to the SD card (further readings: https://hackaday.com/2016/08/03/single-board-revolution-preventing-flash-memory-corruption/).

You will need

  1. - one of the raspberry PIs: zero (I doubt it), zero_w, 1, 2, 3, 4 with matching power supply
  2. - either:
    1. rs232c to USB cable
    2. one db9 female to female null modem
      1. Rpi2 net serial gateway.jpg
  3. - or more simply:
    1. rs232c to 3.3 volts ttl converter, MAX3232 chip is best !
    2. some jumper cables
      1. Rs232 ttl rpi2 APU2C2.jpg
  4. - or the minimalist but cute PI Zero W
    1. same converter, but with a Paspberry PI Zero W
    2. jumper cables
      1. PIzero rs232.jpg
    3. you will need to setup the WIFI connection...

Setting up the SD

Prepare your SD card according to the previously given instructions...

Access your PI and do the following ajustments:

mount /dev/mmcblk0p1 /mnt
cd /mnt
  1. find leaf.cfg file and in LRP="...
    1. remove shorwall and dnsmasq; ... It is important to remove "shorewall" here, it's job is to block everything and "dnsmasq" is not needed !
    2. add local, picocom or minicom
    3. you should now have something like: LRP="license root nano local dhcpcd keyboard dropbear lighttpd webconf picocom patch"##
  2. Run:
    1. lrcfg > 3) Packages configuration > 6) dropbear > 1) dropbear configuration
    2. change line #DB_OPTIONS=" -s " to DB_OPTIONS=" -B "
    3. this will allow a first root login with no password
  1. And save everything with:
    1. lrcfg > s) Save configuration > Enough freespace? (y/N) y

Booting the Raspberry PI

  1. - Reboot your PI
  2. - After ~ 30 seconds, find out the PI's Ip address, and ssh in it: ssh root@raspberry_pi_IP_address. On Android, the "fing" app does marvels ... or simply run the command: ip addr
  3. - Set the new passwords for the OS and webconf, as you will be asked.
  4. - Open webconf from a browser http://raspberry_pi_IP_address to try it out
  5. - While you're there, it would be a good idea to comment-out eth1 in /etc/network/interfaces, look in Networking...
  6. - A good time also to do your "ssh-copy-id" to write ssh keys in /.ssh/authorized_keys (see https://bering-uclibc.zetam.org/wiki/Bering-uClibc_6.x_-_User_Guide_-_Advanced_Topics_-_Setting_Up_SSH_password-less_login_and_Port_Knocking )

Using the gateway

  1. If you are using the older and cumbersome RS-232C to USB cable + Null modem you are good to go:
    1. - connect the RS-232C to USB cable with the null modem between your PI and your device serial port
    2. - on your external PC, open an ssh session to the PI
    3. - within your ssh session, start picocom on the PI: picocom -b115200 /dev/ttyUSB0
    4. - hit return:... you should have the prompt to login in your router or whatever !
    5. - exit picocom with Cntl-a Cntl-x, help is Cntl-a Cntl-h
  2. If you are using the simpler RS-232C to 3.3 volts ttl converter you first need to deactivate a couple of things...
    1. - deactivate the console ttyAMA0 by commenting it out in /etc/inittab
    2. - in /mnt/cmdline.txt remove console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
    3. - lrcfg > s) save, reboot
    4. - connect the "RS-232C to 3.3 volts ttl converter" to your PI 3.3v(pin1) to ttl-VCC, TX(14) to ttl-TX, RX(15) to ttl-RX and ground(pin6) to ttl-GND, and plug the converter in your device serial port
    5. - on your external PC, open an ssh session to the PI
    6. - start picocom: picocom -b115200 /dev/ttyAMA0
    7. - hit return:... you should have the prompt to login in your router or whatever !
    8. - exit picocom with Cntl-a Cntl-x, help is Cntl-a Cntl-h
  3. Lastly, using the PI Zero W
    1. - in /mnt/leaf.cfg add the following lrp modules to LRP=" ...iw.lrp, wireless.lrp, wpasupp.lrp..."
    2. - reboot the PI
    3. - in /etc/default/wpasupplicant set ENABLE=1 and INTERFACE="wlan0"
    4. - in /etc/wpa_supplicant.conf set ssid="your_ssid" and psk="your_psk"
    5. - in /etc/network/interfaces add at the end:
            auto wlan0 
            iface wlan0 inet dhcp 
            wpa_supplicant_control 1
       6. reboot your PI

Correcting some bugs...

This last section is only for the first generation PI 1. I experienced some issues that needed fixing. With the newer versions these bugs might be gone now, but I didn't check that yet, as there are more important things to do, and besides it works now !!! and I've grown attached to my solutions :-) .

The other PIs 2 and 3, that I own, didn't show these problems.

From an already opened ssh session:

  • - fix xterm: go to /etc/terminfo/x and "cp xterm xterm-256color" ... # the "lrcfg" menu needs xterm-256color so we make one up in B&W !
reset jumper
reset jumper
reset jumper
reset jumper


  • - create a new "reboot" command: first, make the script /root/GPIO4reboot.sh and second, change the old reboot command in /sbin...

the script /root/GPIO4reboot.sh will set gpio4 to low (0) which will reset the PI, a hard reboot !

cd /root
nano GPIOreboot.sh

fill with this content:

#!/bin/sh
#
# reset avec gpio4 connecte sur reset du PI, P6 pin 1 square.
#
echo "4" > /sys/class/gpio/export             # we will talk to gpio4
echo "out" > /sys/class/gpio/gpio4/direction  # gpio4 on boot is an high-z input, but defaults to low (0) as an output

don't forget to make the script executable with:

chmod 0755 GPIO4reboot.sh

Let's replace the non-working "reboot" command with our own hardware reboot command ...

cd /sbin

mv reboot old_reboot               # keep old command in case someone repairs/fixes busybox...
ln -s /root/GPIO4reboot.sh reboot  # the "reboot" command link now points to /root/GPIO4reboot.sh


  • - set the local.local: everything we just did which is not part of the LEAF distribution has to be saved in /var/lib/lrpkg/local.local ...
lrcfg > 3) Packages configuration > 3) local > 1) list of files that should be saved > 

fill with this content:

var/lib/lrpkg/local.local
etc/terminfo/x/xterm-256color
root/.ssh/authorized_keys
root/GPIO4reboot.sh
sbin/reboot
sbin/old_reboot

***and a very important final save:

lrcfg > s) save configuration

Final thoughts

  • It would be a good idea to configure eth0 with a static IP address in /etc/network/interface. This would allow a network communication between your workstation and the PI Serial gateway even if your firewall and consequently DHCP server are down.
  • You can also remove or comment out DB_OPTIONS=" -B " in /etc/default/dropbear, to bring the security level back.
  • "picocom.lrp" is not part of the tarball yet, use "minicom.lrp" instead... or ask Kapeka !
  • It's always nice to have a console if you "ever" :-) have to debug the PI, you will need a 3.3v ttl to usb adapter connected on Ground, GPIO14 (TX) and GPIO15 (RX) respectively pins 6, 8 and 10 on the header.

To enable the console add to /etc/inittab:

ttyAMA0::respawn:/sbin/getty -L ttyAMA0 115200 vt100

and in /etc/securetty add:

ttyAMA0

and as usual: lrcfg >>> save !

  • Using the PI as a full blown firewall has not been tested here, the feeling is that it would be too slow... volunteers are welcome ...

Have fun

by: jrb with kapeka's idea to use leaf !


Prev Up Next


Rs232 ttl rpi2 APU2C2.jpg Rpi2 net serial gateway.jpg

Raspberry PI serial port

All Raspberry PIs have at least a serial comm. port available on GPIO pins 14 (tx) and 15 (rx) that can be connected to a ttl-rs232 converter, try using one with the MAX3232 chip as it works for 5 or 3.3 volts. You then only need to properly manage the jumper cables between the PI's GPIO pins and the rs232 converter. To talk to the serial port you will then have to use /dev/ttyAMA0.

An interesting and amusing setup is with the inexpensive and miniature RPI Zero W. In LEAF 7.0.1 you can find https://sourceforge.net/projects/leaf/files/Bering-uClibc/7.0.1/Bering-uClibc_PiZ.img/download image that you will copy on an SD card, using dd or Raspberry PI imager. .... to be continued ...

Erich's rendering ...
Erich's rendering