Raspberry Pi 4B

Serial Setup, Ubuntu Core

Posted by Rico's Nerd Cluster on October 10, 2023

Set Up Rpi For Serial Communication

  1. Enable rpi serial:

    1
    
     sudo raspi-config
    
    1. Rpi has an option to allow a user to login to the system via serial. Choose no
    2. Answer YES to the question about serial hardware port.
  2. To access UART without sudo, do sudo usermod -a -G dialout $USER
  3. /boot/config.txt

    1
    2
    
     enable_uart=1
     dtoverlay=disable-bt
    
  4. /boot/cmdline.txt: remove this: console=serial0,115200
  5. sudo reboot
  6. ls -l /dev/serial* should see:

    1
    
     lrwxrwxrwx 1 root root 7 Jan  3 00:00 /dev/serial0 -> ttyAMA0
    
  7. do a loop back test with Tx and Rx connected together

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- Tools like `screen` and `minicom` may hang. So just test with python should be fine

- Test with `serial`

```python
import serial

# Open the serial port
ser = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)

# Write data
ser.write(b'Hello, UART!\n')

# Read data (loopback test)
data = ser.readline()
print(f"Received: {data}")

ser.close()
```

UART is not GPIOs, even though on rpi4b, UART Tx, Rx are on GPIO 14, 15. This is because the UART protocol requires active toggling from the hardware UART controller.

A typical UART output looks like:

1
2
/dev/serial0 -> ttyS0
/dev/serial1 -> ttyAMA0

Accessing GPIO Without Root

/dev/gpiomem allows users in the gpio group to access GPIO without needing sudo.

  • /dev/mem allows access to all physical memory

Running Docker on Rpi 4b+

  • Issue: My docker container exited immediately after launching. There is a warning: The requested image's platform (linux/arm64) does not match the detected host platform (linux/arm/v8)
    • Solution: “You’ve got a mixed kernel/userspace install as a result of Raspbian forcibly upgrading your kernel to 64-bit and as a result you can’t run most images, even forcing the architecture via the tag.”
    • Reference
    • One can confirm that the raspbian is truly 32 bits:
      • file /bin/bash, If it says 32-bit executable, the system is 32-bit. If it says 64-bit executable, the system is 64-bit.
      • dpkg --print-architecture. armhf indicates a 32-bit system, arm64 indicates a 64-bit system.

Ubuntu Core

Ubuntu core is a light-weight Ubuntu system for IoT.

IMPORTANT: Enable Password Login (at least temporarily)

  • Add SSH Key: Ubuntu One account just adds a trustable device to the device. But Ubuntu core device doesn’t sync with it.. This means if we don’t enable password login, the device on Ubuntu One will be the only machine that can log onto the IoT Device.
  • To enable password login:
    • Go to /etc/ssh/sshd_config, add PasswordAuthentication yes
    • Restart ssh
      1
      2
      
        sudo systemctl start ssh
        sudo systemctl enable ssh
      
    • set a password:
      1
      
        sudo passwd <USERNAME>
      

Git and Docker

First, let’s download docker: sudo snap install docker. Then, we need to create a group docker

  • create new group
  • sudo groupadd --system --extrausers test. Ubuntu core manages group membership in /var/lib/extrausers
  • sudo usermod -aG docker $USER
  • getent group|grep docker can verify that we have been truly added to the docker group.
  • This is significant, because otherwise, all dockers need to be launched with sudo!!.

On Ubuntu Core, it’s common to use the alpine/git docker image to do git (because git is not in the snapstore)

- without USER being in the docker group, all git commands actually go through sudo docker run ...., which will set up repos in root

Enable Serial

  • sudo usermod -a -G dialout $(whoami)
  • config.txt and cmdline.txt files moved to ` /run/mnt/ubuntu-seed/`. Open the config.txt file with a text editor:

    1
    2
    3
    4
    5
    6
    
      sudo nano /run/mnt/ubuntu-seed/config.txt
      # Add or update the following lines to enable the UART:
    
      enable_uart=1
      dtoverlay=disable-bt
    
    
    • enable_uart=1: Enables UART on the Pi.
    • dtoverlay=disable-bt: Disables the Bluetooth module to free up the serial port.
  • Edit cmdline.txt: sudo nano /run/mnt/ubuntu-seed/cmdline.txt

    • Find the line that starts with console= and make sure it includes console=serial0,115200. If it’s missing, add it. The final line might look something like this: console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 ro fsck.repair=yes net.ifnames=0
  • Reboot the Pi
  • Verify Serial Connection ls -l /dev/serial*. You should see entries like /dev/serial0 or /dev/ttyAMA0.
  • If there’s a serial permission denied issue, it seems to be resolved by running sudo...
    1
    2
    
      raise SerialException("Could not configure port: {}".format(msg))
    serial.serialutil.SerialException: Could not configure port: (5, 'Input/output error')