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 05:28, 4 June 2021 by Jeanrocco (Talk | contribs) (Additionnal Raspberry PI Zero W ack)

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. - deactivate the console ttyAMA0 by commenting it out in /etc/inittab
    2. - in /mnt/cmdline.txt remove console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
    3. - in /mnt/leaf.cfg add the following lrp modules to LRP=" ...iw.lrp, wireless.lrp, wpasupp.lrp..."
    4. - lrcfg > s) save, reboot
    5. - in /etc/default/wpasupplicant set ENABLE=1 and INTERFACE="wlan0"
    6. - in /etc/wpa_supplicant.conf set ssid="your_ssid" and psk="your_psk"
    7. - in /etc/network/interfaces add at the end:
            auto wlan0 
            iface wlan0 inet dhcp 
            wpa_supplicant_control 1
       6.-lrcfg > s) save, reboot

After this last reboot, the PI Zero W might still refuse to connect to your WIFI because it complains about missing some firmware or regulatory.db files. These files are not part of the Distribution yet, but can be copied from the Raspberry PI Zero OS or from here. If you are tempted by this last case, simply click the Download button in the web page and configdb.lrp will be copied to your Download directory.

Let's unpack this configdb.lrp:

sudo cd ~/Downloads
sudo mkdir configdb
cd configdb
sudo tar xzvf ../configdb.lrp

So now all your missing files are in ~/Downloads/configdb/

There are different ways of copying these files to your PI Zero W, but right now, you are connected to it with only the HDMI display and USB keyboard, and there are no easy way to copy these files... Let's look at some options:

  • Using the /dev/ttyAMA0 console: this implies you have some soldered pins on 3.3v, gpio 14 and 15, and ground...
  1. re-enable the console on /dev/ttyAMA0 by UN-commenting it in /etc/inittab
  2. lrcfg > s) save
  3. poweroff the PI and connect a ttl to USB converter from your PI's GPIO pins 14 and 15, to an external PC USB port
  4. on your external PC, put the missing files on a USB key: sudo cp -r ~/Downloads/configdb/lib/firmware/ /mnt/firmware/
  5. start a picocom session on your PC: picocom -b115200 /dev/ttyUSB0 and login to your PI
  6. unplug your USB keyboard from the PI Zero W, and plugin the USB key in it's place
  7. copy the USB key files to /lib/firmware : cp -r /mnt/firmware/* /lib/firmware/

Phew !!! that's a lot of steps ...

  • using configdb.lrp, no need to solder pins here...

mount your SD card on your external PC like:

sudo mount /dev/sdb1 /mnt
cd /mnt
sudo mkdir configdb
cd configdb
sudo tar xzvf ../configdb.lrp

Now you can copy the missing files to /mnt/configdb/lib/firmware/

sudo cp -r ~/Downloads/configdb/lib/firmware/* /mnt/configdb/lib/firmware/
cd /mnt/configdb ...just in case ...
sudo tar -c * | gzip -9 > configdb.lrp
sudo cp configdb.lrp ../

Put back the SD card in your PI and reboot (re-plug the keyboard) and test if you have WIFI connectivity:

ip addr

should give you wlan0: ip address... after a few seconds.


You just learned how to use configdb.lrp to preset some packages parameters.

Additionnal Raspberry PI Zero W ack

Here is a clean setup for the PI Zero W, everything fits in a 3D printed box, and the wiring to the flashy red hot APU2C2 serial port is a neat flat cable.

Erich's rendering ...
Erich's rendering

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.
  • In the previous case, you might also probably loose WIFI connectivity, so using a PI Zero W might not be as reliable... ymmv !
  • You can also remove or comment out DB_OPTIONS=" -B " in /etc/default/dropbear, to bring the security level back.


  • 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